removeDartFormatCommentsBetweenParts static method

  1. @visibleForTesting
String removeDartFormatCommentsBetweenParts(
  1. List<String> lines
)

Removes // dart format on and // dart format off lines that appear between part directives, along with any surrounding blank lines.

Implementation

@visibleForTesting
static String removeDartFormatCommentsBetweenParts(List<String> lines) {
  final result = <String>[];
  var i = 0;
  while (i < lines.length) {
    final line = lines[i];
    final trimmed = line.trim();

    final isPart = trimmed.startsWith('part ') &&
        (trimmed.contains("'") || trimmed.contains('"'));

    if (isPart) {
      result.add(line);
      i++;
      // Look ahead: skip blanks and dart format comment(s); if we reach a
      // part directive, drop the skipped content (comment + line breaks).
      if (i < lines.length) {
        var j = i;
        while (j < lines.length) {
          final t = lines[j].trim();
          if (t.isEmpty) {
            j++;
            continue;
          }
          if (t == '// dart format on' || t == '// dart format off') {
            j++;
            continue;
          }
          if (t.startsWith('part ') &&
              (t.contains("'") || t.contains('"'))) {
            i = j;
            break;
          }
          break;
        }
      }
      continue;
    }

    result.add(line);
    i++;
  }
  return result.join('\n');
}