4
4
5
5
import 'dart:async' ;
6
6
7
- // ignore: implementation_imports
8
- import 'package:_fe_analyzer_shared/src/base/syntactic_entity.dart' ;
9
7
import 'package:analyzer/dart/analysis/analysis_context.dart' ;
10
8
import 'package:analyzer/dart/analysis/analysis_context_collection.dart' ;
11
9
import 'package:analyzer/dart/analysis/results.dart' ;
12
10
import 'package:analyzer/dart/ast/ast.dart' ;
11
+ import 'package:analyzer/dart/ast/token.dart' ;
13
12
import 'package:analyzer/file_system/physical_file_system.dart' ;
14
13
import 'package:watcher/watcher.dart' ;
15
14
@@ -28,6 +27,27 @@ typedef PreviewPath = ({String path, Uri uri});
28
27
/// Represents a set of previews for a given file.
29
28
typedef PreviewMapping = Map <PreviewPath , List <String >>;
30
29
30
+ extension on Token {
31
+ /// Convenience getter to identify tokens for private fields and functions.
32
+ bool get isPrivate => toString ().startsWith ('_' );
33
+ }
34
+
35
+ extension on Annotation {
36
+ /// Convenience getter to identify `@Preview` annotations
37
+ bool get isPreview => name.name == 'Preview' ;
38
+ }
39
+
40
+ /// Convenience getters for examining [String] paths.
41
+ extension on String {
42
+ bool get isDartFile => endsWith ('.dart' );
43
+ bool get isGeneratedPreviewFile => endsWith (PreviewCodeGenerator .generatedPreviewFilePath);
44
+ }
45
+
46
+ extension on ParsedUnitResult {
47
+ /// Convenience method to package [path] and [uri] into a [PreviewPath]
48
+ PreviewPath toPreviewPath () => (path: path, uri: uri);
49
+ }
50
+
31
51
class PreviewDetector {
32
52
PreviewDetector ({required this .fs, required this .logger, required this .onChangeDetected});
33
53
@@ -49,8 +69,7 @@ class PreviewDetector {
49
69
final String eventPath = event.path;
50
70
// Only trigger a reload when changes to Dart sources are detected. We
51
71
// ignore the generated preview file to avoid getting stuck in a loop.
52
- if (! eventPath.endsWith ('.dart' ) ||
53
- eventPath.endsWith (PreviewCodeGenerator .generatedPreviewFilePath)) {
72
+ if (! eventPath.isDartFile || eventPath.isGeneratedPreviewFile) {
54
73
return ;
55
74
}
56
75
logger.printStatus ('Detected change in $eventPath .' );
@@ -110,36 +129,35 @@ class PreviewDetector {
110
129
111
130
for (final String filePath in context.contextRoot.analyzedFiles ()) {
112
131
logger.printTrace ('Checking file: $filePath ' );
113
- if (! filePath.endsWith ( '.dart' ) ) {
132
+ if (! filePath.isDartFile ) {
114
133
continue ;
115
134
}
116
135
117
136
final SomeParsedLibraryResult lib = context.currentSession.getParsedLibrary (filePath);
118
137
if (lib is ParsedLibraryResult ) {
119
- for (final ParsedUnitResult unit in lib.units) {
120
- final List <String > previewEntries =
121
- previews[(path: unit.path, uri: unit.uri)] ?? < String > [];
122
- for (final SyntacticEntity entity in unit.unit.childEntities) {
123
- if (entity is FunctionDeclaration && ! entity.name.toString ().startsWith ('_' )) {
138
+ for (final ParsedUnitResult libUnit in lib.units) {
139
+ final List <String > previewEntries = previews[libUnit.toPreviewPath ()] ?? < String > [];
140
+ for (final CompilationUnitMember entity in libUnit.unit.declarations) {
141
+ if (entity is FunctionDeclaration && ! entity.name.isPrivate) {
124
142
bool foundPreview = false ;
125
143
for (final Annotation annotation in entity.metadata) {
126
- if (annotation.name.name == 'Preview' ) {
144
+ if (annotation.isPreview ) {
127
145
// What happens if the annotation is applied multiple times?
128
146
foundPreview = true ;
129
147
break ;
130
148
}
131
149
}
132
150
if (foundPreview) {
133
151
logger.printStatus ('Found preview at:' );
134
- logger.printStatus ('File path: ${unit .uri }' );
152
+ logger.printStatus ('File path: ${libUnit .uri }' );
135
153
logger.printStatus ('Preview function: ${entity .name }' );
136
154
logger.printStatus ('' );
137
155
previewEntries.add (entity.name.toString ());
138
156
}
139
157
}
140
158
}
141
159
if (previewEntries.isNotEmpty) {
142
- previews[(path : unit.path, uri : unit.uri )] = previewEntries;
160
+ previews[libUnit. toPreviewPath ( )] = previewEntries;
143
161
}
144
162
}
145
163
} else {
0 commit comments