Skip to content

Commit 7cac23a

Browse files
bwilkersonCommit Queue
authored andcommitted
Migrate ElementReferencesComputer
Change-Id: I23ee2bef1c2dc1925cf7a730d0b46583f499067c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/400142 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 00478a7 commit 7cac23a

File tree

6 files changed

+42
-32
lines changed

6 files changed

+42
-32
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ lib/src/lsp/handlers/handler_definition.dart
1212
lib/src/lsp/handlers/handler_references.dart
1313
lib/src/lsp/handlers/handler_rename.dart
1414
lib/src/protocol_server.dart
15-
lib/src/search/element_references.dart
1615
lib/src/services/correction/namespace.dart
1716
lib/src/services/kythe/kythe_visitors.dart
1817
lib/src/services/refactoring/agnostic/change_method_signature.dart

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:analyzer/source/source_range.dart';
1212
import 'package:analyzer/src/dart/ast/element_locator.dart';
1313
import 'package:analyzer/src/dart/ast/utilities.dart';
1414
import 'package:analyzer/src/dart/element/element.dart';
15+
import 'package:analyzer/src/utilities/extensions/element.dart';
1516

1617
/// Returns the container for [element] that should be used in Call Hierarchy.
1718
///
@@ -221,7 +222,7 @@ class DartCallHierarchyComputer {
221222
}
222223

223224
var computer = ElementReferencesComputer(searchEngine);
224-
var references = await computer.compute(element, false);
225+
var references = await computer.compute(element.asElement2, false);
225226

226227
// Group results by their container, since we only want to return a single
227228
// entry for a body, with a set of ranges within.

pkg/analysis_server/lib/src/handler/legacy/search_find_element_references.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:analysis_server/src/handler/legacy/legacy_handler.dart';
1010
import 'package:analysis_server/src/protocol_server.dart';
1111
import 'package:analysis_server/src/search/element_references.dart';
1212
import 'package:analyzer/dart/element/element.dart';
13+
import 'package:analyzer/src/utilities/extensions/element.dart';
1314

1415
/// The handler for the `search.findElementReferences` request.
1516
class SearchFindElementReferencesHandler extends LegacyHandler {
@@ -52,7 +53,10 @@ class SearchFindElementReferencesHandler extends LegacyHandler {
5253
// search elements
5354
if (element != null) {
5455
var computer = ElementReferencesComputer(searchEngine);
55-
var results = await computer.compute(element, params.includePotential);
56+
var results = await computer.compute(
57+
element.asElement2!,
58+
params.includePotential,
59+
);
5660
sendSearchResults(
5761
protocol.SearchResultsParams(
5862
searchId,

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'package:analyzer/dart/ast/ast.dart';
1515
import 'package:analyzer/dart/element/element.dart';
1616
import 'package:analyzer/src/dart/ast/utilities.dart';
1717
import 'package:analyzer/src/util/performance/operation_performance.dart';
18+
import 'package:analyzer/src/utilities/extensions/element.dart';
1819

1920
typedef StaticOptions = Either2<bool, ReferenceOptions>;
2021

@@ -90,10 +91,17 @@ class ReferencesHandler
9091

9192
var computer = ElementReferencesComputer(server.searchEngine);
9293
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+
}
93101
var results = await performance.runAsync(
94102
'computer.compute',
95103
(childPerformance) =>
96-
computer.compute(element, false, performance: childPerformance),
104+
computer.compute(element2, false, performance: childPerformance),
97105
);
98106

99107
Location? toLocation(SearchMatch result) {

pkg/analysis_server/lib/src/search/element_references.dart

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import 'package:analysis_server/src/protocol_server.dart'
66
show SearchResult, newSearchResult_fromMatch;
77
import 'package:analysis_server/src/services/search/hierarchy.dart';
88
import 'package:analysis_server/src/services/search/search_engine.dart';
9-
import 'package:analyzer/dart/element/element.dart';
9+
import 'package:analyzer/dart/element/element2.dart';
1010
import 'package:analyzer/src/util/performance/operation_performance.dart';
11-
import 'package:analyzer/src/utilities/extensions/element.dart';
1211

1312
/// A computer for `search.findElementReferences` request results.
1413
class ElementReferencesComputer {
@@ -18,7 +17,7 @@ class ElementReferencesComputer {
1817

1918
/// Computes [SearchMatch]es for [element] references.
2019
Future<List<SearchMatch>> compute(
21-
Element element,
20+
Element2 element,
2221
bool withPotential, {
2322
OperationPerformanceImpl? performance,
2423
}) async {
@@ -50,7 +49,7 @@ class ElementReferencesComputer {
5049
/// Returns a [Future] completing with a [List] of references to [element] or
5150
/// to the corresponding hierarchy [Element]s.
5251
Future<List<SearchMatch>> _findElementsReferences(
53-
Element element,
52+
Element2 element,
5453
OperationPerformanceImpl performance,
5554
) async {
5655
var allResults = <SearchMatch>[];
@@ -70,9 +69,9 @@ class ElementReferencesComputer {
7069

7170
/// Returns a [Future] completing with a [List] of references to [element].
7271
Future<List<SearchMatch>> _findSingleElementReferences(
73-
Element element,
72+
Element2 element,
7473
) async {
75-
return searchEngine.searchReferences(element);
74+
return searchEngine.searchReferences2(element);
7675
}
7776

7877
/// Returns a [Future] completing with [Element]s to search references to.
@@ -81,31 +80,30 @@ class ElementReferencesComputer {
8180
/// corresponding [Element] in the hierarchy is returned.
8281
///
8382
/// Otherwise, only references to [element] should be searched.
84-
Future<Iterable<Element>> _getRefElements(
85-
Element element,
83+
Future<Iterable<Element2>> _getRefElements(
84+
Element2 element,
8685
OperationPerformanceImpl performance,
8786
) async {
88-
if (element is ParameterElement && element.isNamed) {
89-
return (await performance.runAsync(
87+
if (element is FormalParameterElement && element.isNamed) {
88+
return await performance.runAsync(
9089
'getHierarchyNamedParameters',
91-
(_) => getHierarchyNamedParameters(searchEngine, element.asElement2),
92-
)).map((e) => e.asElement);
90+
(_) => getHierarchyNamedParameters(searchEngine, element),
91+
);
9392
}
94-
if (element is ClassMemberElement) {
93+
if (element is MethodElement2 ||
94+
element is FieldElement2 ||
95+
element is ConstructorElement2) {
9596
var (members, parameters) = await performance.runAsync(
9697
'getHierarchyMembers',
9798
(performance) => getHierarchyMembersAndParameters(
9899
searchEngine,
99-
element.asElement2!,
100+
element,
100101
performance: performance,
101102
includeParametersForFields: true,
102103
),
103104
);
104105

105-
return {
106-
...members.map((e) => e.asElement!),
107-
...parameters.map((e) => e.asElement),
108-
};
106+
return {...members.map((e) => e), ...parameters.map((e) => e)};
109107
}
110108
return [element];
111109
}
@@ -114,10 +112,10 @@ class ElementReferencesComputer {
114112
return newSearchResult_fromMatch(match);
115113
}
116114

117-
static bool _isMemberElement(Element element) {
118-
if (element is ConstructorElement) {
115+
static bool _isMemberElement(Element2 element) {
116+
if (element is ConstructorElement2) {
119117
return false;
120118
}
121-
return element.enclosingElement3 is InterfaceElement;
119+
return element.enclosingElement2 is InterfaceElement2;
122120
}
123121
}

pkg/analysis_server/test/lsp/references_test.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class MyClass {
8181
void f() {
8282
MyClass()./*[0*/field/*0]*/ = '';
8383
print(MyClass()./*[1*/field/*1]*/);
84-
84+
8585
var myInstance = MyClass();
8686
myInstance./*[2*/field/*2]*/ = '';
8787
print(myInstance./*[3*/field/*3]*/);
@@ -167,7 +167,7 @@ class MyClass {
167167
void f() {
168168
MyClass()./*[0*/field/*0]*/ = '';
169169
print(MyClass()./*[1*/field/*1]*/);
170-
170+
171171
var myInstance = MyClass();
172172
myInstance./*[2*/field/*2]*/ = '';
173173
print(myInstance./*[3*/field/*3]*/);
@@ -188,7 +188,7 @@ class MyClass {
188188
void f() {
189189
MyClass()./*[0*/field/*0]*/ = '';
190190
print(MyClass()./*[1*/fi^eld/*1]*/);
191-
191+
192192
var myInstance = MyClass();
193193
myInstance./*[2*/field/*2]*/ = '';
194194
print(myInstance./*[3*/field/*3]*/);
@@ -203,8 +203,8 @@ void f() {
203203
var content = '''
204204
imp^ort 'dart:async' as async;
205205
206-
/*[0*/async./*0]*/Future<String>? f() {}
207-
/*[1*/async./*1]*/Future<String>? g() {}
206+
/*[0*/async/*0]*/.Future<String>? f() {}
207+
/*[1*/async/*1]*/.Future<String>? g() {}
208208
''';
209209

210210
await _checkRanges(content);
@@ -370,7 +370,7 @@ class MyClass {
370370
void f() {
371371
MyClass()./*[0*/field/*0]*/ = '';
372372
print(MyClass()./*[1*/field/*1]*/);
373-
373+
374374
var myInstance = MyClass();
375375
myInstance./*[2*/field/*2]*/ = '';
376376
print(myInstance./*[3*/field/*3]*/);
@@ -391,7 +391,7 @@ class MyClass {
391391
void f() {
392392
MyClass()./*[0*/fie^ld/*0]*/ = '';
393393
print(MyClass()./*[1*/field/*1]*/);
394-
394+
395395
var myInstance = MyClass();
396396
myInstance./*[2*/field/*2]*/ = '';
397397
print(myInstance./*[3*/field/*3]*/);

0 commit comments

Comments
 (0)