Skip to content

Commit de1662c

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate lib/src/services/search/search_engine.dart and related.
Change-Id: I7c0501eb6e84313d3eb179f41e8bdaf6190004ce Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/400860 Reviewed-by: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent c8bf291 commit de1662c

File tree

13 files changed

+210
-199
lines changed

13 files changed

+210
-199
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@ lib/src/services/refactoring/framework/formal_parameter.dart
1717
lib/src/services/refactoring/legacy/extract_method.dart
1818
lib/src/services/refactoring/legacy/move_file.dart
1919
lib/src/services/refactoring/legacy/refactoring.dart
20-
lib/src/services/refactoring/legacy/refactoring_internal.dart
2120
lib/src/services/refactoring/legacy/refactoring_manager.dart
2221
lib/src/services/refactoring/legacy/rename.dart
2322
lib/src/services/refactoring/legacy/rename_import.dart
2423
lib/src/services/search/element_visitors.dart
25-
lib/src/services/search/search_engine.dart
26-
lib/src/services/search/search_engine_internal.dart
2724
test/abstract_single_unit.dart
2825
test/services/refactoring/legacy/abstract_rename.dart
2926
test/services/search/element_visitors_test.dart

pkg/analysis_server/lib/src/protocol_server.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ Location? newLocation_fromElement2(engine.Element2? element) {
326326

327327
/// Create a Location based on an [engine.SearchMatch].
328328
Location newLocation_fromMatch(engine.SearchMatch match) {
329-
var unitElement = _getUnitElement(match.element);
329+
var unitElement = _getUnitElement(match.element2.asElement!);
330330
return _locationForArgs(unitElement, match.sourceRange);
331331
}
332332

@@ -366,7 +366,7 @@ OverriddenMember newOverriddenMember_fromEngine(engine.Element2 member) {
366366
SearchResult newSearchResult_fromMatch(engine.SearchMatch match) {
367367
var kind = newSearchResultKind_fromEngine(match.kind);
368368
var location = newLocation_fromMatch(match);
369-
var path = _computePath(match.element);
369+
var path = _computePath(match.element2.asElement!);
370370
return SearchResult(location, kind, !match.isResolved, path);
371371
}
372372

pkg/analysis_server/lib/src/services/refactoring/agnostic/change_method_signature.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ class _SignatureUpdater {
690690

691691
/// Returns the resolved unit with [reference].
692692
Future<ResolvedUnitResult?> referenceUnitResult(SearchMatch reference) async {
693-
var element = reference.element;
693+
var element = reference.element2.asElement!;
694694
return await sessionHelper.getResolvedUnitByElement(element);
695695
}
696696

@@ -970,7 +970,7 @@ class _SignatureUpdater {
970970
required ExecutableElement element,
971971
required ChangeBuilder builder,
972972
}) async {
973-
var references = await searchEngine.searchReferences(element);
973+
var references = await searchEngine.searchReferences2(element.asElement2);
974974
for (var reference in references) {
975975
var unitResult = await referenceUnitResult(reference);
976976
if (unitResult == null) {

pkg/analysis_server/lib/src/services/refactoring/legacy/move_file.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import 'package:analyzer/dart/ast/ast.dart';
1414
import 'package:analyzer/file_system/file_system.dart';
1515
import 'package:analyzer/source/source_range.dart';
1616
import 'package:analyzer/src/dart/analysis/driver.dart';
17+
import 'package:analyzer/src/dart/analysis/search.dart';
1718
import 'package:analyzer/src/dart/ast/ast.dart';
19+
import 'package:analyzer/src/utilities/extensions/element.dart';
1820
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
1921
import 'package:analyzer_plugin/utilities/range_factory.dart';
2022
import 'package:path/path.dart' as path show posix, Context;
@@ -280,14 +282,12 @@ class MoveFileRefactoringImpl extends RefactoringImpl
280282
}
281283

282284
// Finally, locate all other incoming references to this file.
283-
var matches = await refactoringWorkspace.searchEngine.searchReferences(
284-
element,
285-
);
286-
var references = getSourceReferences(matches);
285+
var references = await refactoringWorkspace.searchEngine
286+
.searchLibraryFragmentReferences(element.asElement2);
287287
for (var reference in references) {
288288
recordReference(
289289
range: reference.range,
290-
sourceFile: reference.file,
290+
sourceFile: reference.libraryFragment.source.fullName,
291291
targetFile: oldPath,
292292
quotedUriValue: _extractUriString(reference),
293293
);
@@ -341,9 +341,9 @@ class MoveFileRefactoringImpl extends RefactoringImpl
341341
);
342342
}
343343

344-
/// Extracts the existing URI string from a [SourceReference].
345-
String _extractUriString(SourceReference reference) {
346-
var source = reference.element.source!;
344+
/// Extracts the existing URI string from a [LibraryFragmentSearchMatch].
345+
String _extractUriString(LibraryFragmentSearchMatch reference) {
346+
var source = reference.libraryFragment.source;
347347
return source.contents.data.substring(
348348
reference.range.offset,
349349
reference.range.end,

pkg/analysis_server/lib/src/services/refactoring/legacy/refactoring_internal.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import 'package:analysis_server/src/protocol_server.dart' hide Element;
88
import 'package:analysis_server/src/services/correction/status.dart';
99
import 'package:analysis_server/src/services/refactoring/legacy/refactoring.dart';
1010
import 'package:analysis_server/src/services/search/search_engine.dart';
11-
import 'package:analyzer/dart/element/element.dart';
1211
import 'package:analyzer/dart/element/element2.dart';
1312
import 'package:analyzer/source/source.dart';
1413
import 'package:analyzer/source/source_range.dart';
@@ -68,8 +67,6 @@ class SourceReference {
6867

6968
SourceReference(this._match);
7069

71-
Element get element => _match.element;
72-
7370
Element2 get element2 => _match.element2;
7471

7572
/// The full path of the file containing the match.
@@ -90,9 +87,6 @@ class SourceReference {
9087

9188
bool get isResolved => _match.isResolved;
9289

93-
/// Return the [LibraryElement] for the [file].
94-
LibraryElement get libraryElement => _match.libraryElement;
95-
9690
/// Return the [LibraryElement2] for the [file].
9791
LibraryElement2 get libraryElement2 => _match.libraryElement2;
9892

pkg/analysis_server/lib/src/services/refactoring/legacy/rename.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class RenameProcessor {
6565
void addReferenceEdits(List<SearchMatch> matches) {
6666
var references = getSourceReferences(matches);
6767
for (var reference in references) {
68-
if (!workspace.containsElement(reference.element)) {
68+
if (!workspace.containsElement2(reference.element2)) {
6969
continue;
7070
}
7171
reference.addEdit(change, newName);
@@ -76,7 +76,7 @@ class RenameProcessor {
7676
Future<void> renameElement(Element element) {
7777
addDeclarationEdit(element);
7878
return workspace.searchEngine
79-
.searchReferences(element)
79+
.searchReferences2(element.asElement2!)
8080
.then(addReferenceEdits);
8181
}
8282

pkg/analysis_server/lib/src/services/refactoring/legacy/rename_import.dart

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import 'package:analysis_server/src/protocol_server.dart';
66
import 'package:analysis_server/src/services/correction/status.dart';
77
import 'package:analysis_server/src/services/refactoring/legacy/naming_conventions.dart';
88
import 'package:analysis_server/src/services/refactoring/legacy/refactoring.dart';
9-
import 'package:analysis_server/src/services/refactoring/legacy/refactoring_internal.dart';
109
import 'package:analysis_server/src/services/refactoring/legacy/rename.dart';
1110
import 'package:analyzer/dart/analysis/results.dart';
1211
import 'package:analyzer/dart/ast/ast.dart';
1312
import 'package:analyzer/dart/element/element.dart';
1413
import 'package:analyzer/source/source_range.dart';
14+
import 'package:analyzer/src/dart/analysis/search.dart';
1515
import 'package:analyzer/src/dart/ast/utilities.dart';
16+
import 'package:analyzer/src/utilities/extensions/element.dart';
1617
import 'package:analyzer_plugin/utilities/range_factory.dart';
1718

1819
/// A [Refactoring] for renaming [LibraryImportElement]s.
@@ -79,25 +80,34 @@ class RenameImportRefactoringImpl extends RenameRefactoringImpl {
7980
}
8081
}
8182
// update references
82-
var matches = await searchEngine.searchReferences(element);
83-
var references = getSourceReferences(matches);
83+
var references = await searchEngine.searchLibraryImportReferences(
84+
element.asElement2,
85+
);
8486
for (var reference in references) {
8587
if (newName.isEmpty) {
86-
reference.addEdit(change, '');
88+
doSourceChange_addSourceEdit(
89+
change,
90+
reference.libraryFragment.source,
91+
newSourceEdit_range(reference.range, ''),
92+
);
8793
} else {
8894
var identifier = await _getInterpolationIdentifier(reference);
8995
if (identifier != null) {
90-
doSourceChange_addElementEdit(
96+
doSourceChange_addFragmentEdit(
9197
change,
92-
reference.element,
98+
reference.libraryFragment,
9399
SourceEdit(
94100
identifier.offset,
95101
identifier.length,
96102
'{$newName.${identifier.name}}',
97103
),
98104
);
99105
} else {
100-
reference.addEdit(change, '$newName.');
106+
doSourceChange_addSourceEdit(
107+
change,
108+
reference.libraryFragment.source,
109+
newSourceEdit_range(reference.range, '$newName.'),
110+
);
101111
}
102112
}
103113
}
@@ -120,9 +130,9 @@ class RenameImportRefactoringImpl extends RenameRefactoringImpl {
120130
/// an [InterpolationExpression] without surrounding curly brackets, return
121131
/// it. Otherwise return `null`.
122132
Future<SimpleIdentifier?> _getInterpolationIdentifier(
123-
SourceReference reference,
133+
LibraryFragmentSearchMatch reference,
124134
) async {
125-
var source = reference.element.source!;
135+
var source = reference.libraryFragment.source;
126136
var unitResult = sessionHelper.session.getParsedUnit(source.fullName);
127137
if (unitResult is! ParsedUnitResult) {
128138
return null;

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

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/dart/element/element.dart';
65
import 'package:analyzer/dart/element/element2.dart';
76
import 'package:analyzer/source/source.dart';
87
import 'package:analyzer/source/source_range.dart';
@@ -76,18 +75,6 @@ class MatchKind {
7675
/// The interface [SearchEngine] defines the behavior of objects that can be
7776
/// used to search for various pieces of information.
7877
abstract class SearchEngine {
79-
/// Adds all subtypes of the given [type] into [allSubtypes].
80-
///
81-
/// If [allSubtypes] already contains an element it is assumed that it
82-
/// contains the entire subtree and the element won't be search on further.
83-
///
84-
/// [type] - the [InterfaceElement] being subtyped by the found matches.
85-
Future<void> appendAllSubtypes(
86-
InterfaceElement type,
87-
Set<InterfaceElement> allSubtypes,
88-
OperationPerformanceImpl performance,
89-
);
90-
9178
/// Adds all subtypes of the given [type] into [allSubtypes].
9279
///
9380
/// If [allSubtypes] already contains an element it is assumed that it
@@ -98,16 +85,21 @@ abstract class SearchEngine {
9885
OperationPerformanceImpl performance,
9986
);
10087

101-
/// If the [type] has subtypes, return the set of names of members which these
102-
/// subtypes declare, possibly empty. If the [type] does not have subtypes,
103-
/// return `null`.
104-
Future<Set<String>?> membersOfSubtypes(InterfaceElement type);
105-
10688
/// If the [type] has subtypes, return the set of names of members which these
10789
/// subtypes declare, possibly empty. If the [type] does not have subtypes,
10890
/// return `null`.
10991
Future<Set<String>?> membersOfSubtypes2(InterfaceElement2 type);
11092

93+
/// Returns references to the given [fragment].
94+
Future<List<LibraryFragmentSearchMatch>> searchLibraryFragmentReferences(
95+
LibraryFragment fragment,
96+
);
97+
98+
/// Returns references to the given [import].
99+
Future<List<LibraryFragmentSearchMatch>> searchLibraryImportReferences(
100+
LibraryImport import,
101+
);
102+
111103
/// Returns declarations of class members with the given name.
112104
///
113105
/// [name] - the name being declared by the found matches.
@@ -119,14 +111,6 @@ abstract class SearchEngine {
119111
/// [name] - the name being referenced by the found matches.
120112
Future<List<SearchMatch>> searchMemberReferences(String name);
121113

122-
/// Return the prefixes used to reference the [element] in any of the
123-
/// compilation units in the [library]. The returned set will include an empty
124-
/// string if the element is referenced without a prefix.
125-
Future<Set<String>> searchPrefixesUsedInLibrary(
126-
LibraryElement library,
127-
Element element,
128-
);
129-
130114
/// Return the prefixes used to reference the [element] in any of the
131115
/// compilation units in the [library]. The returned set will include an empty
132116
/// string if the element is referenced without a prefix.
@@ -135,28 +119,12 @@ abstract class SearchEngine {
135119
Element2 element,
136120
);
137121

138-
/// Returns references to the given [Element].
139-
///
140-
/// [element] - the [Element] being referenced by the found matches.
141-
Future<List<SearchMatch>> searchReferences(Element element);
142-
143122
/// Returns references to the given [element].
144123
Future<List<SearchMatch>> searchReferences2(Element2 element);
145124

146125
/// Returns direct subtypes of the given [type].
147126
///
148-
/// [type] - the [ClassElement] being subtyped by the found matches.
149-
/// [cache] - the [SearchEngineCache] used to speeding up the computation. If
150-
/// empty it will be filled out and can be used on any subsequent query.
151-
Future<List<SearchMatch>> searchSubtypes(
152-
InterfaceElement type,
153-
SearchEngineCache cache, {
154-
OperationPerformanceImpl? performance,
155-
});
156-
157-
/// Returns direct subtypes of the given [type].
158-
///
159-
/// [type] - the [ClassElement] being subtyped by the found matches.
127+
/// [type] - the [InterfaceElement2] being subtyped by the found matches.
160128
/// [cache] - the [SearchEngineCache] used to speeding up the computation. If
161129
/// empty it will be filled out and can be used on any subsequent query.
162130
Future<List<SearchMatch>> searchSubtypes2(
@@ -182,9 +150,6 @@ class SearchEngineCache {
182150
/// Instances of the class [SearchMatch] represent a match found by
183151
/// [SearchEngine].
184152
abstract class SearchMatch {
185-
/// Return the [Element] containing the match.
186-
Element get element;
187-
188153
/// Return the element containing the match.
189154
Element2 get element2;
190155

@@ -194,15 +159,12 @@ abstract class SearchMatch {
194159
/// Is `true` if field or method access is done using qualifier.
195160
bool get isQualified;
196161

197-
/// Is `true` if the match is a resolved reference to some [Element].
162+
/// Is `true` if the match is a resolved reference to some [Element2].
198163
bool get isResolved;
199164

200165
/// The kind of the match.
201166
MatchKind get kind;
202167

203-
/// Return the [LibraryElement] for the [file].
204-
LibraryElement get libraryElement;
205-
206168
/// Return the library element for the [file].
207169
LibraryElement2 get libraryElement2;
208170

0 commit comments

Comments
 (0)