shaderSourceWindow function Debugging and profiling

String shaderSourceWindow(
  1. String source,
  2. int line, {
  3. int context = 3,
})

source lines around 1-based line, numbered, with the offending line marked by >. context lines are shown on each side.

Implementation

String shaderSourceWindow(String source, int line, {int context = 3}) {
  final lines = source.split('\n');
  final first = (line - 1 - context).clamp(0, lines.length);
  final last = (line - 1 + context + 1).clamp(0, lines.length);
  final width = '$last'.length;
  final buffer = StringBuffer();
  for (var i = first; i < last; i++) {
    final number = '${i + 1}'.padLeft(width);
    buffer.writeln('${i + 1 == line ? '>' : ' '} $number | ${lines[i]}');
  }
  return buffer.toString();
}