mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Remove dead code from snippet_generator (#174440)
Randomly stumbled on code that seems to no longer have clients.
This commit is contained in:
parent
e5d5c01850
commit
511c4e0bfc
@ -92,104 +92,6 @@ class SnippetGenerator {
|
||||
});
|
||||
}
|
||||
|
||||
/// Consolidates all of the snippets and the assumptions into one snippet, in
|
||||
/// order to create a compilable result.
|
||||
Iterable<SourceLine> consolidateSnippets(List<CodeSample> samples, {bool addMarkers = false}) {
|
||||
if (samples.isEmpty) {
|
||||
return <SourceLine>[];
|
||||
}
|
||||
final Iterable<SnippetSample> snippets = samples.whereType<SnippetSample>();
|
||||
final List<SourceLine> snippetLines = <SourceLine>[...snippets.first.assumptions];
|
||||
for (final SnippetSample sample in snippets) {
|
||||
parseInput(sample);
|
||||
snippetLines.addAll(_processBlocks(sample));
|
||||
}
|
||||
return snippetLines;
|
||||
}
|
||||
|
||||
/// A RegExp that matches a Dart constructor.
|
||||
static final RegExp _constructorRegExp = RegExp(r'(const\s+)?_*[A-Z][a-zA-Z0-9<>._]*\(');
|
||||
|
||||
/// A serial number so that we can create unique expression names when we
|
||||
/// generate them.
|
||||
int _expressionId = 0;
|
||||
|
||||
List<SourceLine> _surround(String prefix, Iterable<SourceLine> body, String suffix) {
|
||||
return <SourceLine>[
|
||||
if (prefix.isNotEmpty) SourceLine(prefix),
|
||||
...body,
|
||||
if (suffix.isNotEmpty) SourceLine(suffix),
|
||||
];
|
||||
}
|
||||
|
||||
/// Process one block of sample code (the part inside of "```" markers).
|
||||
/// Splits any sections denoted by "// ..." into separate blocks to be
|
||||
/// processed separately. Uses a primitive heuristic to make sample blocks
|
||||
/// into valid Dart code.
|
||||
List<SourceLine> _processBlocks(CodeSample sample) {
|
||||
final List<SourceLine> block = sample.parts
|
||||
.expand<SourceLine>((SkeletonInjection injection) => injection.contents)
|
||||
.toList();
|
||||
if (block.isEmpty) {
|
||||
return <SourceLine>[];
|
||||
}
|
||||
return _processBlock(block);
|
||||
}
|
||||
|
||||
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
|
||||
// so that it doesn't start with "// " and get caught in this again
|
||||
buffer.add(SourceLine('/${block[index].text}'));
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses the input for the various code and description segments, and
|
||||
/// returns a set of skeleton injections in the order found.
|
||||
List<SkeletonInjection> parseInput(CodeSample sample) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user