4
4
5
5
import 'dart:async' ;
6
6
7
+ import 'package:analysis_server/src/protocol_server.dart'
8
+ show CompletionSuggestion, CompletionSuggestionKind;
7
9
import 'package:analysis_server/src/provisional/completion/dart/completion_dart.dart' ;
8
- import 'package:analysis_server/src/services/completion/dart/feature_computer.dart' ;
9
10
import 'package:analysis_server/src/services/completion/dart/suggestion_builder.dart'
10
- show createSuggestion, ElementSuggestionBuilder, SuggestionBuilder;
11
- import 'package:analyzer/dart/analysis/features.dart' ;
11
+ show ElementSuggestionBuilder, SuggestionBuilder;
12
12
import 'package:analyzer/dart/element/element.dart' ;
13
- import 'package:analyzer/dart/element/nullability_suffix.dart' ;
14
13
import 'package:analyzer/dart/element/type.dart' ;
15
14
import 'package:analyzer/dart/element/visitor.dart' ;
16
15
import 'package:analyzer_plugin/src/utilities/completion/optype.dart' ;
17
16
18
- import '../../../protocol_server.dart'
19
- show CompletionSuggestion, CompletionSuggestionKind;
20
-
21
17
/// A visitor for building suggestions based upon the elements defined by
22
18
/// a source file contained in the same library but not the same as
23
19
/// the source in which the completions are being requested.
@@ -26,7 +22,9 @@ class LibraryElementSuggestionBuilder extends GeneralizingElementVisitor
26
22
@override
27
23
final DartCompletionRequest request;
28
24
29
- final OpType optype;
25
+ final SuggestionBuilder builder;
26
+
27
+ final OpType opType;
30
28
31
29
DartType contextType;
32
30
@@ -38,65 +36,31 @@ class LibraryElementSuggestionBuilder extends GeneralizingElementVisitor
38
36
/// The set of libraries that have been, or are currently being, visited.
39
37
final Set <LibraryElement > visitedLibraries = < LibraryElement > {};
40
38
41
- LibraryElementSuggestionBuilder (this .request, [this .prefix])
42
- : optype = request.opType {
39
+ LibraryElementSuggestionBuilder (this .request, this .builder, [this .prefix])
40
+ : opType = request.opType {
43
41
contextType = request.featureComputer
44
42
.computeContextType (request.target.containingNode);
45
43
kind = request.target.isFunctionalArgument ()
46
44
? CompletionSuggestionKind .IDENTIFIER
47
- : optype .suggestKind;
45
+ : opType .suggestKind;
48
46
}
49
47
50
48
@override
51
49
LibraryElement get containingLibrary => request.libraryElement;
52
50
53
51
@override
54
52
void visitClassElement (ClassElement element) {
55
- if (optype.includeTypeNameSuggestions) {
56
- // if includeTypeNameSuggestions, then use the filter
57
- int relevance;
58
- if (request.useNewRelevance) {
59
- relevance = _relevanceForType (element.thisType);
60
- } else if (element.hasDeprecated) {
61
- relevance = DART_RELEVANCE_LOW ;
62
- } else {
63
- relevance = optype.typeNameSuggestionsFilter (
64
- _instantiateClassElement (element), DART_RELEVANCE_DEFAULT );
65
- }
66
- if (relevance != null ) {
67
- addSuggestion (element, prefix: prefix, relevance: relevance);
68
- }
53
+ if (opType.includeTypeNameSuggestions) {
54
+ builder.suggestClass (element, kind: kind, prefix: prefix);
69
55
}
70
- if (optype.includeConstructorSuggestions) {
71
- int relevance;
72
- if (request.useNewRelevance) {
73
- relevance = _relevanceForType (element.thisType);
74
- } else if (element.hasDeprecated) {
75
- relevance = DART_RELEVANCE_LOW ;
76
- } else {
77
- relevance = optype.returnValueSuggestionsFilter (
78
- _instantiateClassElement (element), DART_RELEVANCE_DEFAULT );
79
- }
80
- _addConstructorSuggestions (element, relevance);
56
+ if (opType.includeConstructorSuggestions) {
57
+ _addConstructorSuggestions (element);
81
58
}
82
- if (optype .includeReturnValueSuggestions) {
59
+ if (opType .includeReturnValueSuggestions) {
83
60
if (element.isEnum) {
84
- var enumName = element.displayName;
85
- int relevance;
86
- if (request.useNewRelevance) {
87
- relevance = _relevanceForType (element.thisType);
88
- } else if (element.hasDeprecated) {
89
- relevance = DART_RELEVANCE_LOW ;
90
- } else {
91
- relevance = optype.returnValueSuggestionsFilter (
92
- _instantiateClassElement (element), DART_RELEVANCE_DEFAULT );
93
- }
94
61
for (var field in element.fields) {
95
62
if (field.isEnumConstant) {
96
- addSuggestion (field,
97
- prefix: prefix,
98
- relevance: relevance,
99
- elementCompletion: '$enumName .${field .name }' );
63
+ builder.suggestEnumConstant (field, prefix: prefix);
100
64
}
101
65
}
102
66
}
@@ -115,15 +79,8 @@ class LibraryElementSuggestionBuilder extends GeneralizingElementVisitor
115
79
116
80
@override
117
81
void visitExtensionElement (ExtensionElement element) {
118
- if (optype.includeReturnValueSuggestions) {
119
- int relevance;
120
- if (request.useNewRelevance) {
121
- relevance = _relevanceForType (element.extendedType);
122
- } else {
123
- relevance =
124
- element.hasDeprecated ? DART_RELEVANCE_LOW : DART_RELEVANCE_DEFAULT ;
125
- }
126
- addSuggestion (element, prefix: prefix, relevance: relevance);
82
+ if (opType.includeReturnValueSuggestions) {
83
+ builder.suggestExtension (element, kind: kind, prefix: prefix);
127
84
}
128
85
element.visitChildren (this );
129
86
}
@@ -138,43 +95,21 @@ class LibraryElementSuggestionBuilder extends GeneralizingElementVisitor
138
95
return ;
139
96
}
140
97
var returnType = element.returnType;
141
- int relevance;
142
- if (request.useNewRelevance) {
143
- relevance = _relevanceForType (returnType);
144
- } else {
145
- relevance = element.hasDeprecated
146
- ? DART_RELEVANCE_LOW
147
- : (element.library == containingLibrary
148
- ? DART_RELEVANCE_LOCAL_FUNCTION
149
- : DART_RELEVANCE_DEFAULT );
150
- }
151
98
if (returnType != null && returnType.isVoid) {
152
- if (optype .includeVoidReturnSuggestions) {
153
- addSuggestion (element, prefix : prefix, relevance : relevance );
99
+ if (opType .includeVoidReturnSuggestions) {
100
+ builder. suggestTopLevelFunction (element, kind : kind, prefix : prefix );
154
101
}
155
102
} else {
156
- if (optype .includeReturnValueSuggestions) {
157
- addSuggestion (element, prefix : prefix, relevance : relevance );
103
+ if (opType .includeReturnValueSuggestions) {
104
+ builder. suggestTopLevelFunction (element, kind : kind, prefix : prefix );
158
105
}
159
106
}
160
107
}
161
108
162
109
@override
163
110
void visitFunctionTypeAliasElement (FunctionTypeAliasElement element) {
164
- if (optype.includeTypeNameSuggestions) {
165
- int relevance;
166
- if (request.useNewRelevance) {
167
- // TODO(brianwilkerson) Figure out whether there are any features that
168
- // ought to be used here and what the right default value is.
169
- relevance = 400 ;
170
- } else {
171
- relevance = element.hasDeprecated
172
- ? DART_RELEVANCE_LOW
173
- : (element.library == containingLibrary
174
- ? DART_RELEVANCE_LOCAL_FUNCTION
175
- : DART_RELEVANCE_DEFAULT );
176
- }
177
- addSuggestion (element, prefix: prefix, relevance: relevance);
111
+ if (opType.includeTypeNameSuggestions) {
112
+ builder.suggestFunctionTypeAlias (element, prefix: prefix);
178
113
}
179
114
}
180
115
@@ -187,96 +122,36 @@ class LibraryElementSuggestionBuilder extends GeneralizingElementVisitor
187
122
188
123
@override
189
124
void visitPropertyAccessorElement (PropertyAccessorElement element) {
190
- if (optype.includeReturnValueSuggestions) {
191
- int relevance;
192
- if (request.useNewRelevance) {
193
- relevance = _relevanceForType (element.returnType);
194
- } else if (element.hasDeprecated) {
195
- relevance = DART_RELEVANCE_LOW ;
125
+ if (opType.includeReturnValueSuggestions) {
126
+ if (element.enclosingElement is ClassElement ) {
127
+ // TODO(brianwilkerson) Verify that we cannot reach this point and
128
+ // remove the dead code.
129
+ builder.suggestAccessor (element, inheritanceDistance: - 1.0 );
196
130
} else {
197
- if (element.library == containingLibrary) {
198
- if (element.enclosingElement is ClassElement ) {
199
- relevance = DART_RELEVANCE_LOCAL_FIELD ;
200
- } else {
201
- relevance = DART_RELEVANCE_LOCAL_TOP_LEVEL_VARIABLE ;
202
- }
203
- } else {
204
- relevance = DART_RELEVANCE_DEFAULT ;
205
- }
131
+ builder.suggestTopLevelPropertyAccessor (element, prefix: prefix);
206
132
}
207
- addSuggestion (element, prefix: prefix, relevance: relevance);
208
133
}
209
134
}
210
135
211
136
@override
212
137
void visitTopLevelVariableElement (TopLevelVariableElement element) {
213
- if (optype.includeReturnValueSuggestions) {
214
- int relevance;
215
- if (request.useNewRelevance) {
216
- relevance = _relevanceForType (element.type);
217
- } else {
218
- relevance = element.hasDeprecated
219
- ? DART_RELEVANCE_LOW
220
- : (element.library == containingLibrary
221
- ? DART_RELEVANCE_LOCAL_TOP_LEVEL_VARIABLE
222
- : DART_RELEVANCE_DEFAULT );
223
- }
224
- addSuggestion (element, prefix: prefix, relevance: relevance);
138
+ if (opType.includeReturnValueSuggestions && ! element.isSynthetic) {
139
+ builder.suggestTopLevelVariable (element, prefix: prefix);
225
140
}
226
141
}
227
142
228
143
/// Add constructor suggestions for the given class.
229
- void _addConstructorSuggestions (ClassElement classElem, int relevance) {
230
- var className = classElem.name;
144
+ void _addConstructorSuggestions (ClassElement classElem) {
231
145
for (var constructor in classElem.constructors) {
232
146
if (constructor.isPrivate) {
233
147
continue ;
234
148
}
235
149
if (classElem.isAbstract && ! constructor.isFactory) {
236
150
continue ;
237
151
}
238
-
239
- var completion = constructor.displayName;
240
- completion = completion.isNotEmpty ? '$className .$completion ' : className;
241
- if (prefix != null && prefix.isNotEmpty) {
242
- completion = '$prefix .$completion ' ;
243
- }
244
- var suggestion = createSuggestion (request, constructor,
245
- completion: completion, relevance: relevance);
246
- if (suggestion != null ) {
247
- suggestion.selectionOffset = suggestion.completion.length;
248
- suggestions.add (suggestion);
249
- }
152
+ builder.suggestConstructor (constructor, kind: kind, prefix: prefix);
250
153
}
251
154
}
252
-
253
- InterfaceType _instantiateClassElement (ClassElement element) {
254
- var typeParameters = element.typeParameters;
255
- var typeArguments = const < DartType > [];
256
- if (typeParameters.isNotEmpty) {
257
- var typeProvider = request.libraryElement.typeProvider;
258
- typeArguments = typeParameters.map ((t) {
259
- return typeProvider.dynamicType;
260
- }).toList ();
261
- }
262
-
263
- var nullabilitySuffix = request.featureSet.isEnabled (Feature .non_nullable)
264
- ? NullabilitySuffix .none
265
- : NullabilitySuffix .star;
266
-
267
- return element.instantiate (
268
- typeArguments: typeArguments,
269
- nullabilitySuffix: nullabilitySuffix,
270
- );
271
- }
272
-
273
- int _relevanceForType (DartType elementType) {
274
- var contextTypeFeature =
275
- request.featureComputer.contextTypeFeature (contextType, elementType);
276
- // TODO(brianwilkerson) Figure out whether there are other features that
277
- // ought to be used here and what the right default value is.
278
- return toRelevance (contextTypeFeature, 800 );
279
- }
280
155
}
281
156
282
157
/// A contributor that produces suggestions based on the top level members in
@@ -295,7 +170,7 @@ class LocalLibraryContributor extends DartCompletionContributor {
295
170
return const < CompletionSuggestion > [];
296
171
}
297
172
298
- var visitor = LibraryElementSuggestionBuilder (request);
173
+ var visitor = LibraryElementSuggestionBuilder (request, builder );
299
174
for (var unit in libraryUnits) {
300
175
if (unit != null && unit.source != request.source) {
301
176
unit.accept (visitor);
0 commit comments