Skip to content

Commit ad8c44f

Browse files
keertipCommit Queue
authored andcommitted
[Elements.migrate] Migrate LibraryElementSuggestionBuilder
Change-Id: I56dde963754fbaf5012f7229fa5a37441c210c4c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396140 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Keerti Parthasarathy <[email protected]>
1 parent 478aba8 commit ad8c44f

File tree

3 files changed

+80
-120
lines changed

3 files changed

+80
-120
lines changed

pkg/analysis_server/lib/src/cider/completion.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class CiderCompletionComputer {
226226
);
227227
var exportMap = element.exportNamespace.definedNames;
228228
for (var definedElement in exportMap.values) {
229-
definedElement.accept(visitor);
229+
definedElement.asElement2?.accept2(visitor);
230230
}
231231
return suggestionBuilder.suggestions.toList();
232232
}

pkg/analysis_server/lib/src/cider/local_library_contributor.dart

Lines changed: 73 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@ import 'package:analysis_server/src/services/completion/dart/completion_manager.
88
import 'package:analysis_server/src/services/completion/dart/suggestion_builder.dart'
99
show SuggestionBuilder;
1010
import 'package:analyzer/dart/ast/ast.dart';
11-
import 'package:analyzer/dart/element/element.dart';
1211
import 'package:analyzer/dart/element/element2.dart';
1312
import 'package:analyzer/dart/element/type.dart';
14-
import 'package:analyzer/dart/element/visitor.dart';
15-
import 'package:analyzer/src/utilities/extensions/element.dart';
13+
import 'package:analyzer/dart/element/visitor2.dart';
1614
import 'package:analyzer_plugin/src/utilities/completion/optype.dart';
1715

1816
/// A visitor for building suggestions based upon the elements defined by
1917
/// a source file contained in the same library but not the same as
2018
/// the source in which the completions are being requested.
21-
class LibraryElementSuggestionBuilder extends GeneralizingElementVisitor<void> {
19+
class LibraryElementSuggestionBuilder
20+
extends GeneralizingElementVisitor2<void> {
2221
final DartCompletionRequest request;
2322

2423
final SuggestionBuilder builder;
@@ -30,7 +29,7 @@ class LibraryElementSuggestionBuilder extends GeneralizingElementVisitor<void> {
3029
final String? prefix;
3130

3231
/// The set of libraries that have been, or are currently being, visited.
33-
final Set<LibraryElement> visitedLibraries = <LibraryElement>{};
32+
final Set<LibraryElement2> visitedLibraries = <LibraryElement2>{};
3433

3534
factory LibraryElementSuggestionBuilder(
3635
DartCompletionRequest request,
@@ -60,179 +59,141 @@ class LibraryElementSuggestionBuilder extends GeneralizingElementVisitor<void> {
6059
);
6160

6261
@override
63-
void visitClassElement(ClassElement element) {
62+
void visitClassElement(ClassElement2 element) {
6463
AstNode node = request.target.containingNode;
65-
if (node is ExtendsClause &&
66-
!element.isExtendableIn(request.libraryElement)) {
64+
var libraryElement = request.libraryElement2;
65+
if (node is ExtendsClause && !element.isExtendableIn2(libraryElement)) {
6766
return;
6867
} else if (node is ImplementsClause &&
69-
!element.isImplementableIn(request.libraryElement)) {
68+
!element.isImplementableIn2(libraryElement)) {
7069
return;
71-
} else if (node is WithClause &&
72-
!element.isMixableIn(request.libraryElement)) {
70+
} else if (node is WithClause && !element.isMixableIn2(libraryElement)) {
7371
return;
7472
}
7573
_visitInterfaceElement(element);
7674
}
7775

7876
@override
79-
void visitCompilationUnitElement(CompilationUnitElement element) {
80-
element.visitChildren(this);
81-
}
82-
83-
@override
84-
void visitElement(Element element) {
77+
void visitElement(Element2 element) {
8578
// ignored
8679
}
8780

8881
@override
89-
visitEnumElement(EnumElement element) {
82+
visitEnumElement(EnumElement2 element) {
9083
_visitInterfaceElement(element);
9184
}
9285

9386
@override
94-
void visitExtensionElement(ExtensionElement element) {
87+
void visitExtensionElement(ExtensionElement2 element) {
9588
if (opType.includeReturnValueSuggestions) {
96-
if (element.name != null) {
97-
builder.suggestExtension(
98-
element.asElement2 as ExtensionElement2,
99-
kind: kind,
100-
prefix: prefix,
101-
);
89+
if (element.name3 != null) {
90+
builder.suggestExtension(element, kind: kind, prefix: prefix);
10291
}
10392
}
10493
}
10594

10695
@override
107-
void visitExtensionTypeElement(ExtensionTypeElement element) {
96+
void visitExtensionTypeElement(ExtensionTypeElement2 element) {
10897
_visitInterfaceElement(element);
10998
}
11099

111100
@override
112-
void visitFunctionElement(FunctionElement element) {
113-
// Do not suggest operators or local functions
114-
if (element.isOperator) {
115-
return;
116-
}
117-
if (element.enclosingElement3 is! CompilationUnitElement) {
118-
return;
119-
}
120-
var returnType = element.returnType;
121-
if (returnType is VoidType) {
122-
if (opType.includeVoidReturnSuggestions) {
123-
builder.suggestTopLevelFunction(
124-
element.asElement2 as TopLevelFunctionElement,
125-
kind: kind,
126-
prefix: prefix,
127-
);
128-
}
129-
} else {
130-
if (opType.includeReturnValueSuggestions) {
131-
builder.suggestTopLevelFunction(
132-
element.asElement2 as TopLevelFunctionElement,
133-
kind: kind,
134-
prefix: prefix,
135-
);
101+
void visitGetterElement(GetterElement element) {
102+
var variable = element.variable3;
103+
if (opType.includeReturnValueSuggestions ||
104+
(opType.includeAnnotationSuggestions &&
105+
variable != null &&
106+
variable.isConst)) {
107+
var parent = element.enclosingElement2;
108+
if (parent is InterfaceElement2 || parent is ExtensionElement2) {
109+
if (element.isSynthetic) {
110+
if (variable is FieldElement2) {
111+
builder.suggestField(variable, inheritanceDistance: 0.0);
112+
}
113+
} else {
114+
builder.suggestGetter(element, inheritanceDistance: 0.0);
115+
}
116+
} else {
117+
builder.suggestTopLevelGetter(element, prefix: prefix);
136118
}
137119
}
138120
}
139121

140122
@override
141-
void visitLibraryElement(LibraryElement element) {
123+
void visitLibraryElement(LibraryElement2 element) {
142124
if (visitedLibraries.add(element)) {
143-
element.visitChildren(this);
125+
element.visitChildren2(this);
144126
}
145127
}
146128

147129
@override
148-
visitMixinElement(MixinElement element) {
130+
visitMixinElement(MixinElement2 element) {
149131
AstNode node = request.target.containingNode;
150132
if (node is ImplementsClause &&
151-
!element.isImplementableIn(request.libraryElement)) {
133+
!element.isImplementableIn2(request.libraryElement2)) {
152134
return;
153135
}
154136
_visitInterfaceElement(element);
155137
}
156138

157139
@override
158-
void visitPropertyAccessorElement(PropertyAccessorElement element) {
159-
var variable = element.variable2;
140+
void visitSetterElement(SetterElement element) {
141+
var variable = element.variable3;
160142
if (opType.includeReturnValueSuggestions ||
161143
(opType.includeAnnotationSuggestions &&
162144
variable != null &&
163145
variable.isConst)) {
164-
var parent = element.enclosingElement3;
165-
if (parent is InterfaceElement || parent is ExtensionElement) {
166-
if (element.isSynthetic) {
167-
if (element.isGetter) {
168-
if (variable is FieldElement) {
169-
builder.suggestField(
170-
variable.asElement2,
171-
inheritanceDistance: 0.0,
172-
);
173-
}
174-
}
175-
} else {
176-
if (element.isGetter) {
177-
builder.suggestGetter(
178-
element.asElement2 as GetterElement,
179-
inheritanceDistance: 0.0,
180-
);
181-
} else {
182-
builder.suggestSetter(
183-
element.asElement2 as SetterElement,
184-
inheritanceDistance: 0.0,
185-
);
186-
}
146+
var parent = element.enclosingElement2;
147+
if (parent is InterfaceElement2 || parent is ExtensionElement2) {
148+
if (!element.isSynthetic) {
149+
builder.suggestSetter(element, inheritanceDistance: 0.0);
187150
}
188151
} else {
189-
if (element.isGetter) {
190-
builder.suggestTopLevelGetter(
191-
element.asElement2 as GetterElement,
192-
prefix: prefix,
193-
);
194-
} else {
195-
builder.suggestTopLevelSetter(
196-
element.asElement2 as SetterElement,
197-
prefix: prefix,
198-
);
199-
}
152+
builder.suggestTopLevelSetter(element, prefix: prefix);
153+
}
154+
}
155+
}
156+
157+
@override
158+
void visitTopLevelFunctionElement(TopLevelFunctionElement element) {
159+
var returnType = element.returnType;
160+
if (returnType is VoidType) {
161+
if (opType.includeVoidReturnSuggestions) {
162+
builder.suggestTopLevelFunction(element, kind: kind, prefix: prefix);
163+
}
164+
} else {
165+
if (opType.includeReturnValueSuggestions) {
166+
builder.suggestTopLevelFunction(element, kind: kind, prefix: prefix);
200167
}
201168
}
202169
}
203170

204171
@override
205-
void visitTopLevelVariableElement(TopLevelVariableElement element) {
172+
void visitTopLevelVariableElement(TopLevelVariableElement2 element) {
206173
if (opType.includeReturnValueSuggestions && !element.isSynthetic) {
207-
builder.suggestTopLevelVariable(
208-
element.asElement2 as TopLevelVariableElement2,
209-
prefix: prefix,
210-
);
174+
builder.suggestTopLevelVariable(element, prefix: prefix);
211175
}
212176
}
213177

214178
@override
215-
void visitTypeAliasElement(TypeAliasElement element) {
179+
void visitTypeAliasElement(TypeAliasElement2 element) {
216180
if (opType.includeTypeNameSuggestions) {
217-
builder.suggestTypeAlias(
218-
element.asElement2 as TypeAliasElement2,
219-
prefix: prefix,
220-
);
181+
builder.suggestTypeAlias(element, prefix: prefix);
221182
}
222183
}
223184

224185
/// Add constructor suggestions for the given class.
225186
///
226187
/// If [onlyConst] is `true`, only `const` constructors will be suggested.
227188
void _addConstructorSuggestions(
228-
ClassElement element, {
189+
ClassElement2 element, {
229190
bool onlyConst = false,
230191
}) {
231-
if (element is EnumElement) {
192+
if (element is EnumElement2) {
232193
return;
233194
}
234195

235-
for (var constructor in element.constructors) {
196+
for (var constructor in element.constructors2) {
236197
if (constructor.isPrivate) {
237198
continue;
238199
}
@@ -242,19 +203,15 @@ class LibraryElementSuggestionBuilder extends GeneralizingElementVisitor<void> {
242203
if (onlyConst && !constructor.isConst) {
243204
continue;
244205
}
245-
builder.suggestConstructor(
246-
constructor.asElement2 as ConstructorElement2,
247-
kind: kind,
248-
prefix: prefix,
249-
);
206+
builder.suggestConstructor(constructor, kind: kind, prefix: prefix);
250207
}
251208
}
252209

253-
void _visitInterfaceElement(InterfaceElement element) {
210+
void _visitInterfaceElement(InterfaceElement2 element) {
254211
if (opType.includeTypeNameSuggestions) {
255-
builder.suggestInterface(element.asElement2, prefix: prefix);
212+
builder.suggestInterface(element, prefix: prefix);
256213
}
257-
if (element is ClassElement) {
214+
if (element is ClassElement2) {
258215
if (opType.includeConstructorSuggestions) {
259216
_addConstructorSuggestions(element);
260217
} else if (opType.includeAnnotationSuggestions) {
@@ -266,24 +223,21 @@ class LibraryElementSuggestionBuilder extends GeneralizingElementVisitor<void> {
266223
var contextType = request.contextType;
267224
if (contextType is InterfaceType) {
268225
// TODO(scheglov): This looks not ideal - we should suggest getters.
269-
for (var field in element.fields) {
226+
for (var field in element.fields2) {
270227
if (field.isStatic &&
271-
field.isAccessibleIn(request.libraryElement) &&
228+
field.isAccessibleIn2(request.libraryElement2) &&
272229
typeSystem.isSubtypeOf(field.type, contextType)) {
273230
if (field.isSynthetic) {
274-
var getter = field.getter;
231+
var getter = field.getter2;
275232
if (getter != null) {
276233
builder.suggestGetter(
277-
getter.asElement2 as GetterElement,
234+
getter,
278235
inheritanceDistance: 0.0,
279236
withEnclosingName: true,
280237
);
281238
}
282239
} else {
283-
builder.suggestStaticField(
284-
field.asElement2,
285-
prefix: prefix,
286-
);
240+
builder.suggestStaticField(field, prefix: prefix);
287241
}
288242
}
289243
}

pkg/analysis_server/lib/src/services/completion/dart/completion_manager.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import 'package:analyzer/dart/analysis/session.dart';
2020
import 'package:analyzer/dart/ast/ast.dart';
2121
import 'package:analyzer/dart/ast/token.dart';
2222
import 'package:analyzer/dart/element/element.dart';
23+
import 'package:analyzer/dart/element/element2.dart';
2324
import 'package:analyzer/dart/element/type.dart';
2425
import 'package:analyzer/file_system/file_system.dart';
2526
import 'package:analyzer/source/source.dart';
@@ -34,6 +35,7 @@ import 'package:analyzer/src/generated/source.dart' show SourceFactory;
3435
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
3536
import 'package:analyzer/src/util/performance/operation_performance.dart';
3637
import 'package:analyzer/src/utilities/completion_matcher.dart';
38+
import 'package:analyzer/src/utilities/extensions/element.dart';
3739
import 'package:analyzer/src/utilities/fuzzy_matcher.dart';
3840
import 'package:analyzer_plugin/src/utilities/completion/completion_target.dart';
3941
import 'package:analyzer_plugin/src/utilities/completion/optype.dart';
@@ -401,6 +403,10 @@ class DartCompletionRequest {
401403
return analysisSession.inheritanceManager;
402404
}
403405

406+
/// Getter for the [Element2] repersentation of [libraryElement].
407+
LibraryElement2 get libraryElement2 =>
408+
libraryElement.asElement2 as LibraryElement2;
409+
404410
/// Answer the [DartType] for Object in dart:core
405411
InterfaceType get objectType => libraryElement.typeProvider.objectType;
406412

0 commit comments

Comments
 (0)