guidesComputeViewport function

List<GuideBlock> guidesComputeViewport({
  1. required Rope rope,
  2. required int firstVisible,
  3. required int lastVisible,
  4. required int tabSize,
})

Indent info for visible lines. Blank (whitespace-only) lines are skipped. Tabs advance to the next tab stop, so tab-indented files get correct levels.

Implementation

List<GuideBlock> guidesComputeViewport({
  required Rope rope,
  required int firstVisible,
  required int lastVisible,
  required int tabSize,
}) {
  final first = math.max(0, firstVisible);
  final last = math.min(lastVisible, rope.lineCount - 1);
  if (first > last) return const [];

  final tab = math.max(1, tabSize);
  final lines = rope.lineRange(first, last + 1);
  final blocks = <GuideBlock>[];

  for (var i = 0; i < lines.length; i++) {
    final text = lines[i];
    var column = 0;
    var j = 0;
    for (; j < text.length; j++) {
      final unit = text.codeUnitAt(j);
      if (unit == 0x20) {
        column++;
      } else if (unit == 0x09) {
        column += tab - (column % tab);
      } else {
        break;
      }
    }
    if (j == text.length || column == 0) continue; // blank or unindented

    final line = first + i;
    blocks.add(
      GuideBlock(
        startLine: line,
        endLine: line,
        indentLevel: column ~/ tab,
        leadingSpaces: column,
      ),
    );
  }
  return blocks;
}