Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
59b406c7
Unverified
Commit
59b406c7
authored
May 07, 2021
by
Greg Spencer
Committed by
GitHub
May 07, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Sort imports when interpolating sample templates (#81873)
parent
9bca38c5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
74 additions
and
9 deletions
+74
-9
analyze_sample_code.dart
dev/bots/analyze_sample_code.dart
+2
-8
snippets.dart
dev/snippets/lib/snippets.dart
+57
-1
snippets_test.dart
dev/snippets/test/snippets_test.dart
+15
-0
No files found.
dev/bots/analyze_sample_code.dart
View file @
59b406c7
...
...
@@ -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.
...
...
dev/snippets/lib/snippets.dart
View file @
59b406c7
...
...
@@ -62,7 +62,7 @@ class SnippetGenerator {
/// [SnippetType.sample] snippets.
String
interpolateTemplate
(
List
<
_ComponentTuple
>
injections
,
String
template
,
Map
<
String
,
Object
?>
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
<
String
>
description
=
injections
...
...
@@ -93,6 +93,62 @@ class SnippetGenerator {
return
injections
[
componentIndex
].
mergedContent
;
}
}).
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.
...
...
dev/snippets/test/snippets_test.dart
View file @
59b406c7
...
...
@@ -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
<
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
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment