in packages/snippets/lib/snippet_generator.dart [181:236]
List<SourceLine> _processBlock(List<SourceLine> block) {
final String firstLine = block.first.text;
if (firstLine.startsWith('new ') ||
firstLine.startsWith(_constructorRegExp)) {
_expressionId += 1;
return _surround('dynamic expression$_expressionId = ', block, ';');
} else if (firstLine.startsWith('await ')) {
_expressionId += 1;
return _surround(
'Future<void> expression$_expressionId() async { ', block, ' }');
} else if (block.first.text.startsWith('class ') ||
block.first.text.startsWith('enum ')) {
return block;
} else if ((block.first.text.startsWith('_') ||
block.first.text.startsWith('final ')) &&
block.first.text.contains(' = ')) {
_expressionId += 1;
return _surround(
'void expression$_expressionId() { ', block.toList(), ' }');
} else {
final List<SourceLine> buffer = <SourceLine>[];
int blocks = 0;
SourceLine? subLine;
final List<SourceLine> subsections = <SourceLine>[];
for (int index = 0; index < block.length; index += 1) {
// Each section of the dart code that is either split by a blank line, or with
// '// ...' is treated as a separate code block.
if (block[index].text.trim().isEmpty || block[index].text == '// ...') {
if (subLine == null) {
continue;
}
blocks += 1;
subsections.addAll(_processBlock(buffer));
buffer.clear();
assert(buffer.isEmpty);
subLine = null;
} else if (block[index].text.startsWith('// ')) {
if (buffer.length > 1) // don't include leading comments
buffer.add(SourceLine(
'/${block[index].text}')); // so that it doesn't start with "// " and get caught in this again
} else {
subLine ??= block[index];
buffer.add(block[index]);
}
}
if (blocks > 0) {
if (subLine != null) {
subsections.addAll(_processBlock(buffer));
}
// Combine all of the subsections into one section, now that they've been processed.
return subsections;
} else {
return block;
}
}
}