toLogicalLines static method

List<String> toLogicalLines(
  1. List<String> physicalLines
)

Merges physical source lines into logical statements so that line-based scanning is immune to how dart format wraps code. dart format may split one statement across several physical lines (e.g. a long field = json['key']; becomes field = + json['key'];). This joins the continuation lines back together — accumulating until the buffered text ends with ;, { or } — so callers see each statement whole, with its tokens on a single logical line, regardless of formatting.

Implementation

static List<String> toLogicalLines(List<String> physicalLines) {
  final List<String> result = [];
  final StringBuffer buffer = StringBuffer();
  for (final line in physicalLines) {
    if (buffer.isEmpty) {
      buffer.write(line);
    } else {
      buffer.write(' ${line.trim()}');
    }
    final String trimmed = buffer.toString().trimRight();
    if (trimmed.endsWith(';') ||
        trimmed.endsWith('{') ||
        trimmed.endsWith('}')) {
      result.add(buffer.toString());
      buffer.clear();
    }
  }
  if (buffer.isNotEmpty) {
    result.add(buffer.toString());
  }
  return result;
}