Skip to content

Commit 3bbcf1b

Browse files
bwilkersonCommit Queue
authored andcommitted
Migrate move to file refactoring
Change-Id: I006ac6d11ef712f51ce97c3b310634949bbc1918 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/400160 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent ec7cb0c commit 3bbcf1b

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ lib/src/services/refactoring/legacy/refactoring_internal.dart
2222
lib/src/services/refactoring/legacy/refactoring_manager.dart
2323
lib/src/services/refactoring/legacy/rename.dart
2424
lib/src/services/refactoring/legacy/rename_import.dart
25-
lib/src/services/refactoring/move_top_level_to_file.dart
2625
lib/src/services/search/element_visitors.dart
2726
lib/src/services/search/search_engine.dart
2827
lib/src/services/search/search_engine_internal.dart

pkg/analysis_server/lib/src/services/refactoring/move_top_level_to_file.dart

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import 'package:analysis_server/src/utilities/extensions/string.dart';
99
import 'package:analysis_server/src/utilities/import_analyzer.dart';
1010
import 'package:analyzer/dart/ast/ast.dart';
1111
import 'package:analyzer/dart/ast/token.dart';
12-
import 'package:analyzer/dart/element/element.dart';
12+
import 'package:analyzer/dart/element/element2.dart';
1313
import 'package:analyzer/source/line_info.dart';
1414
import 'package:analyzer/source/source_range.dart';
15-
import 'package:analyzer/src/utilities/extensions/element.dart';
1615
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
1716
import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart';
1817
import 'package:analyzer_plugin/utilities/range_factory.dart';
@@ -151,31 +150,33 @@ class MoveTopLevelToFile extends RefactoringProducer {
151150
builder.addDeletion(sourceRange);
152151
}
153152
});
154-
var libraries = <LibraryElement, Set<Element>>{};
153+
var libraries = <LibraryElement2, Set<Element2>>{};
155154
for (var element2 in analyzer.movingDeclarations) {
156-
var element = element2.asElement!;
157-
var matches = await searchEngine.searchReferences(element);
155+
var element = element2;
156+
var matches = await searchEngine.searchReferences2(element);
158157
for (var match in matches) {
159158
if (match.isResolved) {
160-
libraries.putIfAbsent(match.libraryElement, () => {}).add(element);
159+
libraries.putIfAbsent(match.libraryElement2, () => {}).add(element2);
161160
}
162161
}
163162
}
164163

165164
/// Don't update the library from which the code is being moved because
166165
/// that's already been done.
167-
libraries.remove(libraryResult.element);
166+
libraries.remove(libraryResult.element2);
168167
for (var entry in libraries.entries) {
169168
var library = entry.key;
170169
var prefixes = <String>{};
171170
for (var element in entry.value) {
172-
var prefixList = await searchEngine.searchPrefixesUsedInLibrary(
171+
var prefixList = await searchEngine.searchPrefixesUsedInLibrary2(
173172
library,
174173
element,
175174
);
176175
prefixes.addAll(prefixList);
177176
}
178-
await builder.addDartFileEdit(library.source.fullName, (builder) {
177+
await builder.addDartFileEdit(library.firstFragment.source.fullName, (
178+
builder,
179+
) {
179180
for (var prefix in prefixes) {
180181
builder.importLibrary(destinationImportUri, prefix: prefix);
181182
}
@@ -284,7 +285,7 @@ class MoveTopLevelToFile extends RefactoringProducer {
284285
unitResult.unit,
285286
candidateElements:
286287
candidateMembers.keys
287-
.map((member) => member.declaredElement)
288+
.map((member) => member.declaredFragment?.element)
288289
.nonNulls
289290
.toSet(),
290291
);
@@ -333,8 +334,8 @@ class MoveTopLevelToFile extends RefactoringProducer {
333334
.expand((unit) => unit.unit.declarations)
334335
.expand((declaration) => declaration.sealedSuperclassElements)
335336
// Check if any of them are in the source file.
336-
.map((element) => element.enclosingElement3)
337-
.contains(unitResult.unit.declaredElement);
337+
.map((element) => element.enclosingElement2)
338+
.contains(unitResult.unit.declaredFragment?.element);
338339
}
339340

340341
/// Return a list containing the top-level declarations that are selected, or
@@ -486,10 +487,10 @@ class _SealedSubclassIndex {
486487
final CompilationUnit unit;
487488

488489
/// The set of initial candidate elements.
489-
final Set<Element> candidateElements;
490+
final Set<Element2> candidateElements;
490491

491492
/// A map of sealed named classes/mixin elements to a set of their subclasses.
492-
final Map<Element, Set<CompilationUnitMember>> sealedTypeSubclasses = {};
493+
final Map<Element2, Set<CompilationUnitMember>> sealedTypeSubclasses = {};
493494

494495
/// Whether or not the candidate set is invalid.
495496
///
@@ -515,7 +516,7 @@ class _SealedSubclassIndex {
515516

516517
// If this declaration is a candidate but it's sealed super is not,
517518
// we have an invalid selection.
518-
if (isCandidate(declaration.declaredElement) &&
519+
if (isCandidate(declaration.declaredFragment?.element) &&
519520
!isCandidate(superElement)) {
520521
hasInvalidCandidateSet = true;
521522
return;
@@ -536,7 +537,7 @@ class _SealedSubclassIndex {
536537
...members,
537538
...members.whereType<NamedCompilationUnitMember>().expand(
538539
(member) => findSubclassesOfSealedRecursively(
539-
sealedTypeSubclasses[member.declaredElement] ?? const {},
540+
sealedTypeSubclasses[member.declaredFragment?.element] ?? const {},
540541
),
541542
),
542543
};
@@ -545,10 +546,10 @@ class _SealedSubclassIndex {
545546

546547
extension on CompilationUnitMember {
547548
/// Gets all sealed [ClassElement]s that are superclasses of this member.
548-
Iterable<ClassElement> get sealedSuperclassElements {
549+
Iterable<ClassElement2> get sealedSuperclassElements {
549550
return superclasses
550-
.map((type) => type?.element)
551-
.whereType<ClassElement>()
551+
.map((type) => type?.element2)
552+
.whereType<ClassElement2>()
552553
.where((element) => element.isSealed);
553554
}
554555

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ abstract class SearchEngine {
127127
Element element,
128128
);
129129

130+
/// Return the prefixes used to reference the [element] in any of the
131+
/// compilation units in the [library]. The returned set will include an empty
132+
/// string if the element is referenced without a prefix.
133+
Future<Set<String>> searchPrefixesUsedInLibrary2(
134+
LibraryElement2 library,
135+
Element2 element,
136+
);
137+
130138
/// Returns references to the given [Element].
131139
///
132140
/// [element] - the [Element] being referenced by the found matches.

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
@@ -159,6 +159,19 @@ class SearchEngineImpl implements SearchEngine {
159159
return await driver.search.prefixesUsedInLibrary(library, element);
160160
}
161161

162+
@override
163+
Future<Set<String>> searchPrefixesUsedInLibrary2(
164+
covariant LibraryElementImpl library,
165+
Element2 element,
166+
) async {
167+
var driver =
168+
(library.session.analysisContext as DriverBasedAnalysisContext).driver;
169+
return await driver.search.prefixesUsedInLibrary(
170+
library,
171+
element.asElement!,
172+
);
173+
}
174+
162175
@override
163176
Future<List<SearchMatch>> searchReferences(Element element) async {
164177
var allResults = <SearchResult>[];

0 commit comments

Comments
 (0)