Skip to content

Commit a9c3991

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Migrate LSP Find References to new element model
Change-Id: I07c78af854c573a759971ee16c21fdc7de376952 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/401060 Commit-Queue: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent 11904ed commit a9c3991

File tree

2 files changed

+24
-31
lines changed

2 files changed

+24
-31
lines changed

pkg/analysis_server/lib/src/lsp/handlers/handler_references.dart

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import 'package:analysis_server/src/services/search/search_engine.dart'
1212
show SearchMatch;
1313
import 'package:analyzer/dart/analysis/results.dart';
1414
import 'package:analyzer/dart/ast/ast.dart';
15-
import 'package:analyzer/dart/element/element.dart';
15+
import 'package:analyzer/dart/element/element2.dart';
1616
import 'package:analyzer/src/dart/ast/utilities.dart';
1717
import 'package:analyzer/src/util/performance/operation_performance.dart';
1818
import 'package:analyzer/src/utilities/extensions/element.dart';
@@ -51,23 +51,11 @@ class ReferencesHandler
5151
);
5252
}
5353

54-
List<Location> _getDeclarations(Element element) {
55-
element = element.nonSynthetic;
56-
var unitElement = element.thisOrAncestorOfType<CompilationUnitElement>();
57-
if (unitElement == null) {
58-
return [];
59-
}
60-
61-
return [
62-
Location(
63-
uri: uriConverter.toClientUri(unitElement.source.fullName),
64-
range: toRange(
65-
unitElement.lineInfo,
66-
element.nameOffset,
67-
element.nameLength,
68-
),
69-
),
70-
];
54+
List<Location> _getDeclarations(Element2 element) {
55+
return element.nonSynthetic2.fragments
56+
.map((fragment) => fragmentToLocation(uriConverter, fragment))
57+
.nonNulls
58+
.toList();
7159
}
7260

7361
Future<ErrorOr<List<Location>?>> _getReferences(
@@ -79,9 +67,9 @@ class ReferencesHandler
7967
var node = NodeLocator(offset).searchWithin(result.unit);
8068
node = _getReferenceTargetNode(node);
8169

82-
var element = switch (server.getElementOfNode(node)) {
83-
FieldFormalParameterElement(:var field?) => field,
84-
PropertyAccessorElement(:var variable2?) => variable2,
70+
var element = switch (server.getElementOfNode2(node)) {
71+
FieldFormalParameterElement2(:var field2?) => field2,
72+
PropertyAccessorElement2(:var variable3?) => variable3,
8573
(var element) => element,
8674
};
8775

@@ -91,17 +79,11 @@ class ReferencesHandler
9179

9280
var computer = ElementReferencesComputer(server.searchEngine);
9381
var session = element.session ?? result.session;
94-
var element2 =
95-
element is LibraryImportElement
96-
? element.prefix?.element.asElement2
97-
: element.asElement2;
98-
if (element2 == null) {
99-
return success(null);
100-
}
82+
10183
var results = await performance.runAsync(
10284
'computer.compute',
10385
(childPerformance) =>
104-
computer.compute(element2, false, performance: childPerformance),
86+
computer.compute(element, false, performance: childPerformance),
10587
);
10688

10789
Location? toLocation(SearchMatch result) {

pkg/analysis_server/test/lsp/references_test.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ void f() {
199199
await _checkRanges(content);
200200
}
201201

202-
Future<void> test_import_prefix() async {
202+
Future<void> test_import_prefix_declaration() async {
203203
var content = '''
204-
imp^ort 'dart:async' as async;
204+
import 'dart:async' as asy^nc;
205205
206206
/*[0*/async/*0]*/.Future<String>? f() {}
207207
/*[1*/async/*1]*/.Future<String>? g() {}
@@ -210,6 +210,17 @@ imp^ort 'dart:async' as async;
210210
await _checkRanges(content);
211211
}
212212

213+
Future<void> test_import_prefix_reference() async {
214+
var content = '''
215+
import 'dart:async' as async;
216+
217+
/*[0*/^async/*0]*/.Future<String>? f() {}
218+
/*[1*/async/*1]*/.Future<String>? g() {}
219+
''';
220+
221+
await _checkRanges(content);
222+
}
223+
213224
Future<void> test_initializingFormal_argument_withDeclaration() async {
214225
// Find references on an initializing formal argument should include
215226
// all references to the field too.

0 commit comments

Comments
 (0)