From 59b406c76098a9df213af7aee35cb36b7f107941 Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Fri, 7 May 2021 15:34:04 -0700 Subject: [PATCH] Sort imports when interpolating sample templates (#81873) --- dev/bots/analyze_sample_code.dart | 10 +---- dev/snippets/lib/snippets.dart | 58 +++++++++++++++++++++++++++- dev/snippets/test/snippets_test.dart | 15 +++++++ 3 files changed, 74 insertions(+), 9 deletions(-) diff --git a/dev/bots/analyze_sample_code.dart b/dev/bots/analyze_sample_code.dart index eb86843320..239d5314e7 100644 --- a/dev/bots/analyze_sample_code.dart +++ b/dev/bots/analyze_sample_code.dart @@ -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 analysisOptions = File(path.join(directory.path, 'analysis_options.yaml')); - analysisOptions.writeAsStringSync(''' -include: ${rootAnalysisOptions.absolute.path} - -analyzer: - errors: - directives_ordering: ignore -'''); + analysisOptions.writeAsStringSync('include: ${rootAnalysisOptions.absolute.path}'); } /// Writes out a sample section to the disk and returns the file. diff --git a/dev/snippets/lib/snippets.dart b/dev/snippets/lib/snippets.dart index 0b12ce125b..91b615f550 100644 --- a/dev/snippets/lib/snippets.dart +++ b/dev/snippets/lib/snippets.dart @@ -62,7 +62,7 @@ class SnippetGenerator { /// [SnippetType.sample] snippets. String interpolateTemplate(List<_ComponentTuple> injections, String template, Map metadata) { final RegExp moustacheRegExp = RegExp('{{([^}]+)}}'); - return template.replaceAllMapped(moustacheRegExp, (Match match) { + final String interpolated = template.replaceAllMapped(moustacheRegExp, (Match match) { if (match[1] == 'description') { // Place the description into a comment. final List description = injections @@ -93,6 +93,62 @@ class SnippetGenerator { return injections[componentIndex].mergedContent; } }).trim(); + return _sortImports(interpolated); + } + + String _sortImports(String code) { + final List result = []; + final List lines = code.split('\n'); + final List imports = []; + 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 dartImports = []; + final List packageImports = []; + final List otherImports = []; + final RegExp typeRegExp = RegExp(r'''import\s+['"](?\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, [ + ...dartImports, + if (dartImports.isNotEmpty) '', + ...packageImports, + if (packageImports.isNotEmpty) '', + ...otherImports, + ]); + } + return result.join('\n'); } /// Interpolates the [injections] into an HTML skeleton file. diff --git a/dev/snippets/test/snippets_test.dart b/dev/snippets/test/snippets_test.dart index e5590b8a96..88c88debae 100644 --- a/dev/snippets/test/snippets_test.dart +++ b/dev/snippets/test/snippets_test.dart @@ -30,6 +30,11 @@ void main() { {{description}} +import 'package:flutter/material.dart'; +import '../foo.dart'; + +{{code-imports}} + {{code-my-preamble}} main() { @@ -68,6 +73,10 @@ A description of the snippet. On several lines. +```dart imports +import 'dart:ui'; +``` + ```my-dart_language my-preamble const String name = 'snippet'; ``` @@ -108,6 +117,12 @@ void main() { expect(outputContents, contains('A description of the snippet.')); expect(outputContents, contains('void main() {')); expect(outputContents, contains("const String name = 'snippet';")); + final List 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 {