Sort imports when interpolating sample templates (#81873)
This commit is contained in:
parent
9bca38c5e5
commit
59b406c760
@ -613,16 +613,10 @@ dependencies:
|
|||||||
''');
|
''');
|
||||||
|
|
||||||
|
|
||||||
// Copy in the analysis options from the Flutter root.
|
// Import the analysis options from the Flutter root.
|
||||||
final File rootAnalysisOptions = File(path.join(_flutterRoot,'analysis_options.yaml'));
|
final File rootAnalysisOptions = File(path.join(_flutterRoot,'analysis_options.yaml'));
|
||||||
final File analysisOptions = File(path.join(directory.path, 'analysis_options.yaml'));
|
final File analysisOptions = File(path.join(directory.path, 'analysis_options.yaml'));
|
||||||
analysisOptions.writeAsStringSync('''
|
analysisOptions.writeAsStringSync('include: ${rootAnalysisOptions.absolute.path}');
|
||||||
include: ${rootAnalysisOptions.absolute.path}
|
|
||||||
|
|
||||||
analyzer:
|
|
||||||
errors:
|
|
||||||
directives_ordering: ignore
|
|
||||||
''');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Writes out a sample section to the disk and returns the file.
|
/// Writes out a sample section to the disk and returns the file.
|
||||||
|
@ -62,7 +62,7 @@ class SnippetGenerator {
|
|||||||
/// [SnippetType.sample] snippets.
|
/// [SnippetType.sample] snippets.
|
||||||
String interpolateTemplate(List<_ComponentTuple> injections, String template, Map<String, Object?> metadata) {
|
String interpolateTemplate(List<_ComponentTuple> injections, String template, Map<String, Object?> metadata) {
|
||||||
final RegExp moustacheRegExp = RegExp('{{([^}]+)}}');
|
final RegExp moustacheRegExp = RegExp('{{([^}]+)}}');
|
||||||
return template.replaceAllMapped(moustacheRegExp, (Match match) {
|
final String interpolated = template.replaceAllMapped(moustacheRegExp, (Match match) {
|
||||||
if (match[1] == 'description') {
|
if (match[1] == 'description') {
|
||||||
// Place the description into a comment.
|
// Place the description into a comment.
|
||||||
final List<String> description = injections
|
final List<String> description = injections
|
||||||
@ -93,6 +93,62 @@ class SnippetGenerator {
|
|||||||
return injections[componentIndex].mergedContent;
|
return injections[componentIndex].mergedContent;
|
||||||
}
|
}
|
||||||
}).trim();
|
}).trim();
|
||||||
|
return _sortImports(interpolated);
|
||||||
|
}
|
||||||
|
|
||||||
|
String _sortImports(String code) {
|
||||||
|
final List<String> result = <String>[];
|
||||||
|
final List<String> lines = code.split('\n');
|
||||||
|
final List<String> imports = <String>[];
|
||||||
|
int firstImport = -1;
|
||||||
|
int lineNumber =0;
|
||||||
|
for (final String line in lines) {
|
||||||
|
if (RegExp(r'^\s*import').matchAsPrefix(line) != null) {
|
||||||
|
if (firstImport < 0) {
|
||||||
|
firstImport = lineNumber;
|
||||||
|
}
|
||||||
|
imports.add(line);
|
||||||
|
} else {
|
||||||
|
result.add(line);
|
||||||
|
}
|
||||||
|
lineNumber++;
|
||||||
|
}
|
||||||
|
if (firstImport > 0) {
|
||||||
|
final List<String> dartImports = <String>[];
|
||||||
|
final List<String> packageImports = <String>[];
|
||||||
|
final List<String> otherImports = <String>[];
|
||||||
|
final RegExp typeRegExp = RegExp(r'''import\s+['"](?<type>\w+)''');
|
||||||
|
imports.sort();
|
||||||
|
for (final String importLine in imports) {
|
||||||
|
final RegExpMatch? match = typeRegExp.firstMatch(importLine);
|
||||||
|
if (match != null) {
|
||||||
|
switch (match.namedGroup('type')) {
|
||||||
|
case 'dart':
|
||||||
|
dartImports.add(importLine);
|
||||||
|
break;
|
||||||
|
case 'package':
|
||||||
|
packageImports.add(importLine);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
otherImports.add(importLine);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
otherImports.add(importLine);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert the sorted sections in the proper order, with a blank line in between
|
||||||
|
// sections.
|
||||||
|
result.insertAll(firstImport, <String>[
|
||||||
|
...dartImports,
|
||||||
|
if (dartImports.isNotEmpty) '',
|
||||||
|
...packageImports,
|
||||||
|
if (packageImports.isNotEmpty) '',
|
||||||
|
...otherImports,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
return result.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Interpolates the [injections] into an HTML skeleton file.
|
/// Interpolates the [injections] into an HTML skeleton file.
|
||||||
|
@ -30,6 +30,11 @@ void main() {
|
|||||||
|
|
||||||
{{description}}
|
{{description}}
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import '../foo.dart';
|
||||||
|
|
||||||
|
{{code-imports}}
|
||||||
|
|
||||||
{{code-my-preamble}}
|
{{code-my-preamble}}
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
@ -68,6 +73,10 @@ A description of the snippet.
|
|||||||
|
|
||||||
On several lines.
|
On several lines.
|
||||||
|
|
||||||
|
```dart imports
|
||||||
|
import 'dart:ui';
|
||||||
|
```
|
||||||
|
|
||||||
```my-dart_language my-preamble
|
```my-dart_language my-preamble
|
||||||
const String name = 'snippet';
|
const String name = 'snippet';
|
||||||
```
|
```
|
||||||
@ -108,6 +117,12 @@ void main() {
|
|||||||
expect(outputContents, contains('A description of the snippet.'));
|
expect(outputContents, contains('A description of the snippet.'));
|
||||||
expect(outputContents, contains('void main() {'));
|
expect(outputContents, contains('void main() {'));
|
||||||
expect(outputContents, contains("const String name = 'snippet';"));
|
expect(outputContents, contains("const String name = 'snippet';"));
|
||||||
|
final List<String> lines = outputContents.split('\n');
|
||||||
|
final int dartUiLine = lines.indexOf("import 'dart:ui';");
|
||||||
|
final int materialLine = lines.indexOf("import 'package:flutter/material.dart';");
|
||||||
|
final int otherLine = lines.indexOf("import '../foo.dart';");
|
||||||
|
expect(dartUiLine, lessThan(materialLine));
|
||||||
|
expect(materialLine, lessThan(otherLine));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('generates snippets', () async {
|
test('generates snippets', () async {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user