computeIndentFolds function

List<FoldRange> computeIndentFolds(
  1. List<String> lineTexts, {
  2. int tabWidth = 4,
})

Computes indent folds over lineTexts.

A fold starts at a non-blank line followed by a deeper-indented non-blank line, and ends at the last consecutively deeper line (trailing blank lines belong to the enclosing scope, not the fold). Tabs count tabWidth columns.

Implementation

List<FoldRange> computeIndentFolds(List<String> lineTexts, {int tabWidth = 4}) {
  final width = tabWidth < 1 ? 1 : tabWidth;
  final indents = [for (final line in lineTexts) _indentWidth(line, width)];
  final folds = <FoldRange>[];
  for (var i = 0; i < lineTexts.length; i++) {
    final indent = indents[i];
    if (indent < 0) continue;
    final next = _nextNonBlank(indents, i + 1);
    if (next < 0 || indents[next] <= indent) continue;
    var end = next;
    for (var j = next + 1; j < lineTexts.length; j++) {
      final current = indents[j];
      if (current < 0) continue;
      if (current <= indent) break;
      end = j;
    }
    folds.add(FoldRange(startLine: i, endLine: end));
  }
  return List<FoldRange>.unmodifiable(folds);
}