Skip to content

Commit 779a433

Browse files
bwilkersonCommit Queue
authored andcommitted
Migrate four computer classes
Change-Id: I9d5a9cd8c85ef703938aecb4e233467917f73aac Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396101 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent d7ed156 commit 779a433

File tree

9 files changed

+101
-63
lines changed

9 files changed

+101
-63
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ lib/src/cider/local_library_contributor.dart
66
lib/src/cider/rename.dart
77
lib/src/computer/computer_call_hierarchy.dart
88
lib/src/computer/computer_documentation.dart
9-
lib/src/computer/computer_hover.dart
10-
lib/src/computer/computer_inlay_hint.dart
11-
lib/src/computer/computer_lazy_type_hierarchy.dart
129
lib/src/computer/import_elements_computer.dart
13-
lib/src/computer/imported_elements_computer.dart
1410
lib/src/domains/analysis/implemented_dart.dart
1511
lib/src/domains/analysis/occurrences_dart.dart
1612
lib/src/handler/legacy/completion_get_suggestions2.dart

pkg/analysis_server/lib/src/computer/computer_hover.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class DartUnitHoverComputer {
154154

155155
var definingSource = library.firstFragment.source;
156156
var uri = definingSource.uri;
157-
var analysisSession = _unit.declaredElement?.session;
157+
var analysisSession = _unit.declaredFragment?.element.session;
158158

159159
String? libraryName, libraryPath;
160160
if (uri.isScheme('file') && analysisSession != null) {

pkg/analysis_server/lib/src/computer/computer_inlay_hint.dart

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'package:analyzer/dart/analysis/results.dart';
99
import 'package:analyzer/dart/ast/ast.dart';
1010
import 'package:analyzer/dart/ast/syntactic_entity.dart';
1111
import 'package:analyzer/dart/ast/visitor.dart';
12-
import 'package:analyzer/dart/element/element.dart';
12+
import 'package:analyzer/dart/element/element2.dart';
1313
import 'package:analyzer/dart/element/nullability_suffix.dart';
1414
import 'package:analyzer/dart/element/type.dart';
1515
import 'package:analyzer/source/line_info.dart';
@@ -44,10 +44,10 @@ class DartInlayHintComputer {
4444
/// automatically.
4545
void _addParameterNamePrefix(
4646
SyntacticEntity nodeOrToken,
47-
ParameterElement parameter,
47+
FormalParameterElement parameter,
4848
) {
49-
var name = parameter.name;
50-
if (name.isEmpty) {
49+
var name = parameter.name3;
50+
if (name == null || name.isEmpty) {
5151
return;
5252
}
5353
var offset = nodeOrToken.offset;
@@ -185,8 +185,8 @@ class DartInlayHintComputer {
185185
InlayHintLabelPart(
186186
// Write type without type args or nullability suffix. Type args need
187187
// adding as their own parts, and the nullability suffix does after them.
188-
value: type.element?.name ?? type.getDisplayString(),
189-
location: _locationForElement(type.element),
188+
value: type.element3?.name3 ?? type.getDisplayString(),
189+
location: _locationForElement(type.element3),
190190
),
191191
);
192192
// Call recursively for any nested type arguments.
@@ -202,20 +202,21 @@ class DartInlayHintComputer {
202202
}
203203
}
204204

205-
Location? _locationForElement(Element? element) {
205+
Location? _locationForElement(Element2? element) {
206206
if (element == null) {
207207
return null;
208208
}
209-
var compilationUnit =
210-
element.thisOrAncestorOfType<CompilationUnitElement>();
211-
var path = compilationUnit?.source.fullName;
212-
var lineInfo = compilationUnit?.lineInfo;
213-
if (path == null || lineInfo == null || element.nameOffset == -1) {
209+
var firstFragment = element.firstFragment;
210+
var nameOffset = firstFragment.nameOffset2;
211+
var libraryFragment = firstFragment.libraryFragment;
212+
var path = libraryFragment?.source.fullName;
213+
var lineInfo = libraryFragment?.lineInfo;
214+
if (path == null || lineInfo == null || nameOffset == null) {
214215
return null;
215216
}
216217
return Location(
217218
uri: pathContext.toUri(path),
218-
range: toRange(lineInfo, element.nameOffset, element.nameLength),
219+
range: toRange(lineInfo, nameOffset, firstFragment.name2?.length ?? 0),
219220
);
220221
}
221222

@@ -248,7 +249,7 @@ class _DartInlayHintComputerVisitor extends GeneralizingAstVisitor<void> {
248249
void visitArgumentList(ArgumentList node) {
249250
for (var argument in node.arguments) {
250251
if (argument is! NamedExpression) {
251-
var parameter = argument.staticParameterElement;
252+
var parameter = argument.correspondingParameter;
252253
if (parameter != null) {
253254
_computer._addParameterNamePrefix(argument, parameter);
254255
}
@@ -269,8 +270,8 @@ class _DartInlayHintComputerVisitor extends GeneralizingAstVisitor<void> {
269270
return;
270271
}
271272

272-
var declaration = node.declaredElement;
273-
if (declaration != null) {
273+
var declaration = node.declaredElement2;
274+
if (declaration is LocalVariableElement2) {
274275
_computer._addTypePrefix(node.name, declaration.type);
275276
}
276277
}
@@ -284,7 +285,7 @@ class _DartInlayHintComputerVisitor extends GeneralizingAstVisitor<void> {
284285
return;
285286
}
286287

287-
var declaration = node.declaredElement;
288+
var declaration = node.declaredElement2;
288289
if (declaration != null) {
289290
_computer._addTypePrefix(node.name, declaration.type);
290291
}
@@ -304,7 +305,7 @@ class _DartInlayHintComputerVisitor extends GeneralizingAstVisitor<void> {
304305
return;
305306
}
306307

307-
var declaration = node.declaredElement;
308+
var declaration = node.declaredFragment?.element;
308309
if (declaration != null) {
309310
// For getters/setters, the type must come before the property keyword,
310311
// not the name.
@@ -351,7 +352,7 @@ class _DartInlayHintComputerVisitor extends GeneralizingAstVisitor<void> {
351352
return;
352353
}
353354

354-
var declaration = node.declaredElement;
355+
var declaration = node.declaredFragment?.element;
355356
if (declaration != null) {
356357
_computer._addTypePrefix(node.name, declaration.returnType);
357358
}
@@ -394,7 +395,7 @@ class _DartInlayHintComputerVisitor extends GeneralizingAstVisitor<void> {
394395
return;
395396
}
396397

397-
var declaration = node.declaredElement;
398+
var declaration = node.declaredFragment?.element;
398399
if (declaration != null) {
399400
// Prefer to insert before `name` to avoid going before keywords like
400401
// `required`.
@@ -412,7 +413,7 @@ class _DartInlayHintComputerVisitor extends GeneralizingAstVisitor<void> {
412413
return;
413414
}
414415

415-
var declaration = node.declaredElement;
416+
var declaration = node.declaredFragment?.element;
416417
if (declaration != null) {
417418
_computer._addTypePrefix(node, declaration.type);
418419
}

pkg/analysis_server/lib/src/computer/computer_lazy_type_hierarchy.dart

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'package:analysis_server/src/services/search/search_engine.dart';
66
import 'package:analyzer/dart/analysis/results.dart';
77
import 'package:analyzer/dart/ast/ast.dart';
8-
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:analyzer/dart/element/element2.dart';
99
import 'package:analyzer/dart/element/type.dart';
1010
import 'package:analyzer/source/source_range.dart';
1111
import 'package:analyzer/src/dart/element/element.dart';
@@ -38,7 +38,7 @@ class DartLazyTypeHierarchyComputer {
3838
SearchEngine searchEngine,
3939
) async {
4040
var targetElement = await _findTargetElement(location);
41-
if (targetElement is! InterfaceElement) {
41+
if (targetElement is! InterfaceElement2) {
4242
return null;
4343
}
4444

@@ -92,8 +92,8 @@ class DartLazyTypeHierarchyComputer {
9292
Declaration? declaration = node?.thisOrAncestorMatching(
9393
(node) => _isValidTargetDeclaration(node),
9494
);
95-
var element = declaration?.declaredElement;
96-
if (element is InterfaceElement) {
95+
var element = declaration?.declaredFragment?.element;
96+
if (element is InterfaceElement2) {
9797
type = element.thisType;
9898
}
9999
}
@@ -102,19 +102,21 @@ class DartLazyTypeHierarchyComputer {
102102
}
103103

104104
/// Locate the [Element] referenced by [location].
105-
Future<InterfaceElement?> _findTargetElement(ElementLocation location) async {
106-
var element = await _result.session.locateElement(location);
107-
return element is InterfaceElement ? element : null;
105+
Future<InterfaceElement2?> _findTargetElement(
106+
ElementLocation location,
107+
) async {
108+
var element = await _result.session.locateElement2(location);
109+
return element is InterfaceElement2 ? element : null;
108110
}
109111

110112
/// Gets immediate sub types for the class/mixin [element].
111113
Future<List<TypeHierarchyRelatedItem>> _getSubtypes(
112-
InterfaceElement target,
114+
InterfaceElement2 target,
113115
SearchEngine searchEngine,
114116
) async {
115117
/// Helper to convert a [SearchMatch] to a [TypeHierarchyRelatedItem].
116118
TypeHierarchyRelatedItem toHierarchyItem(SearchMatch match) {
117-
var element = match.element as InterfaceElement;
119+
var element = match.element2 as InterfaceElement2;
118120
var type = element.thisType;
119121
switch (match.kind) {
120122
case MatchKind.REFERENCE_IN_EXTENDS_CLAUSE:
@@ -131,12 +133,13 @@ class DartLazyTypeHierarchyComputer {
131133
}
132134
}
133135

134-
var matches = await searchEngine.searchSubtypes(
136+
var matches = await searchEngine.searchSubtypes2(
135137
target,
136138
SearchEngineCache(),
137139
);
140+
var seenElements = <Element2>{};
138141
return matches
139-
.where((match) => !(match.element as InterfaceElement).isAugmentation)
142+
.where((match) => seenElements.add(match.element2))
140143
.map(toHierarchyItem)
141144
.toList();
142145
}
@@ -206,7 +209,7 @@ class DartLazyTypeHierarchyComputer {
206209

207210
// Verify the element we arrived at matches the targetElement to guard
208211
// against code changes that made the path from the anchor invalid.
209-
return type != null && type.element == target.element ? type : null;
212+
return type != null && type.element3 == target.element3 ? type : null;
210213
}
211214
}
212215

@@ -258,17 +261,17 @@ class TypeHierarchyItem {
258261
: this(
259262
type: type,
260263
displayName: _displayNameForType(type),
261-
location: type.element.location!,
262-
nameRange: _nameRangeForElement(type.element),
263-
codeRange: _codeRangeForElement(type.element),
264-
file: type.element.source.fullName,
264+
location: type.element3.location!,
265+
nameRange: _nameRangeForElement(type.element3),
266+
codeRange: _codeRangeForElement(type.element3),
267+
file: type.element3.firstFragment.libraryFragment.source.fullName,
265268
);
266269

267270
/// Returns the [SourceRange] of the code for [element].
268-
static SourceRange _codeRangeForElement(Element element) {
271+
static SourceRange _codeRangeForElement(Element2 element) {
269272
// Non-synthetic elements should always have code locations.
270-
var elementImpl = element.nonSynthetic as ElementImpl;
271-
return SourceRange(elementImpl.codeOffset!, elementImpl.codeLength!);
273+
var firstFragment = element.nonSynthetic2.firstFragment as ElementImpl;
274+
return SourceRange(firstFragment.codeOffset!, firstFragment.codeLength!);
272275
}
273276

274277
/// Returns a name to display in the hierarchy for [type].
@@ -277,15 +280,15 @@ class TypeHierarchyItem {
277280
}
278281

279282
/// Returns the [SourceRange] of the name for [element].
280-
static SourceRange _nameRangeForElement(Element element) {
281-
element = element.nonSynthetic;
283+
static SourceRange _nameRangeForElement(Element2 element) {
284+
var fragment = element.nonSynthetic2.firstFragment;
282285

283286
// Some non-synthetic items can still have invalid nameOffsets (for example
284287
// a compilation unit). This should never happen here, but guard against it.
285-
assert(element.nameOffset != -1);
286-
return element.nameOffset == -1
288+
assert(fragment.nameOffset2 != -1);
289+
return fragment.nameOffset2 == -1
287290
? SourceRange(0, 0)
288-
: SourceRange(element.nameOffset, element.nameLength);
291+
: SourceRange(fragment.nameOffset2 ?? 0, fragment.name2?.length ?? 0);
289292
}
290293
}
291294

pkg/analysis_server/lib/src/computer/imported_elements_computer.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'package:analysis_server/protocol/protocol_generated.dart';
66
import 'package:analyzer/dart/ast/ast.dart';
77
import 'package:analyzer/dart/ast/visitor.dart';
8-
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:analyzer/dart/element/element2.dart';
99
import 'package:analyzer/src/dart/ast/extensions.dart';
1010

1111
/// An object used to compute the list of elements referenced within a given
@@ -71,8 +71,8 @@ class _Visitor extends UnifyingAstVisitor<void> {
7171
void visitNamedType(NamedType node) {
7272
if (node.offset <= endOffset && node.end >= startOffset) {
7373
var importPrefix = node.importPrefix;
74-
var prefix = importPrefix?.element?.name ?? '';
75-
_addElement(prefix, node.element);
74+
var prefix = importPrefix?.element2?.name3 ?? '';
75+
_addElement(prefix, node.element2);
7676
}
7777

7878
super.visitNamedType(node);
@@ -91,7 +91,7 @@ class _Visitor extends UnifyingAstVisitor<void> {
9191
node.offset <= endOffset &&
9292
node.end >= startOffset &&
9393
!_isConstructorDeclarationReturnType(node)) {
94-
var nodeElement = node.writeOrReadElement;
94+
var nodeElement = node.writeOrReadElement2;
9595

9696
var prefix = '';
9797
var parent = node.parent;
@@ -108,18 +108,18 @@ class _Visitor extends UnifyingAstVisitor<void> {
108108
}
109109
}
110110

111-
void _addElement(String prefix, Element? element) {
111+
void _addElement(String prefix, Element2? element) {
112112
if (element == null) {
113113
return;
114114
}
115-
if (element is PrefixElement) {
115+
if (element is PrefixElement2) {
116116
return;
117117
}
118-
if (element.enclosingElement3 is! CompilationUnitElement) {
118+
if (element.enclosingElement2 is! LibraryElement2) {
119119
return;
120120
}
121121

122-
var path = element.library?.definingCompilationUnit.source.fullName;
122+
var path = element.library2?.firstFragment.source.fullName;
123123
if (path == null) {
124124
return;
125125
}
@@ -130,17 +130,17 @@ class _Visitor extends UnifyingAstVisitor<void> {
130130
() => ImportedElements(path, prefix, <String>[]),
131131
);
132132
var elementNames = elements.elements;
133-
var elementName = element.name;
133+
var elementName = element.name3;
134134
if (elementName != null && !elementNames.contains(elementName)) {
135135
elementNames.add(elementName);
136136
}
137137
}
138138

139139
String _getPrefixFrom(SimpleIdentifier identifier) {
140140
if (identifier.offset <= endOffset && identifier.end >= startOffset) {
141-
var prefixElement = identifier.staticElement;
142-
if (prefixElement is PrefixElement) {
143-
return prefixElement.name;
141+
var prefixElement = identifier.element;
142+
if (prefixElement is PrefixElement2) {
143+
return prefixElement.name3 ?? '';
144144
}
145145
}
146146
return '';

pkg/analysis_server/lib/src/services/search/search_engine.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ abstract class SearchEngine {
128128
OperationPerformanceImpl? performance,
129129
});
130130

131+
/// Returns direct subtypes of the given [type].
132+
///
133+
/// [type] - the [ClassElement] being subtyped by the found matches.
134+
/// [cache] - the [SearchEngineCache] used to speeding up the computation. If
135+
/// empty it will be filled out and can be used on any subsequent query.
136+
Future<List<SearchMatch>> searchSubtypes2(
137+
InterfaceElement2 type,
138+
SearchEngineCache cache, {
139+
OperationPerformanceImpl? performance,
140+
});
141+
131142
/// Returns all the top-level declarations matching the given pattern.
132143
///
133144
/// [pattern] the regular expression used to match the names of the

pkg/analysis_server/lib/src/services/search/search_engine_internal.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,19 @@ class SearchEngineImpl implements SearchEngine {
170170
return results.map(SearchMatchImpl.forSearchResult).toList();
171171
}
172172

173+
@override
174+
Future<List<SearchMatch>> searchSubtypes2(
175+
InterfaceElement2 type,
176+
SearchEngineCache searchEngineCache, {
177+
OperationPerformanceImpl? performance,
178+
}) async {
179+
return await searchSubtypes(
180+
type.asElement,
181+
searchEngineCache,
182+
performance: performance,
183+
);
184+
}
185+
173186
@override
174187
Future<List<SearchMatch>> searchTopLevelDeclarations(String pattern) async {
175188
var allElements = <Element>{};

pkg/analysis_server/test/src/computer/imported_elements_computer_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ void f() {
377377
''';
378378
await _computeElements(content, selection);
379379
assertElements([
380-
ImportedElements(fooPath, 'prefix', ['foo=']),
380+
ImportedElements(fooPath, 'prefix', ['foo']),
381381
]);
382382
}
383383

0 commit comments

Comments
 (0)