Skip to content

Commit 2d7bb5d

Browse files
bwilkersonCommit Queue
authored andcommitted
Migrate 5 more rename pieces and a utility class
Change-Id: I54a9ac1590c262ca82d6ba62b8cac248cbfd9ee0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/399703 Reviewed-by: Phil Quitslund <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent ed8e22f commit 2d7bb5d

File tree

14 files changed

+217
-225
lines changed

14 files changed

+217
-225
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ lib/src/services/refactoring/legacy/refactoring_internal.dart
2525
lib/src/services/refactoring/legacy/refactoring_manager.dart
2626
lib/src/services/refactoring/legacy/rename.dart
2727
lib/src/services/refactoring/legacy/rename_import.dart
28-
lib/src/services/refactoring/legacy/rename_library.dart
29-
lib/src/services/refactoring/legacy/rename_local.dart
30-
lib/src/services/refactoring/legacy/rename_parameter.dart
31-
lib/src/services/refactoring/legacy/rename_type_parameter.dart
32-
lib/src/services/refactoring/legacy/rename_unit_member.dart
33-
lib/src/services/refactoring/legacy/visible_ranges_computer.dart
3428
lib/src/services/refactoring/move_top_level_to_file.dart
3529
lib/src/services/search/element_visitors.dart
3630
lib/src/services/search/search_engine.dart

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ final class ExtractMethodRefactoringImpl extends RefactoringImpl
278278
final List<int> lengths = <int>[];
279279

280280
/// The map of local elements to their visibility ranges.
281-
late Map<LocalElement, SourceRange> _visibleRangeMap;
281+
late Map<LocalElement2, SourceRange> _visibleRangeMap;
282282

283283
/// The map of local names to their visibility ranges.
284284
final Map<String, List<SourceRange>> _localNames =
@@ -642,7 +642,7 @@ final class ExtractMethodRefactoringImpl extends RefactoringImpl
642642
var parent = _parentMember!.parent;
643643
// top-level function
644644
if (parent is CompilationUnit) {
645-
var libraryElement = parent.declaredElement!.library;
645+
var libraryElement = parent.declaredFragment!.element;
646646
return validateCreateFunction(_searchEngine, libraryElement, name);
647647
}
648648
// method of class
@@ -1684,7 +1684,7 @@ class _InitializeParametersVisitor extends GeneralizingAstVisitor<void> {
16841684
if (element is LocalElement) {
16851685
// declared local elements
16861686
if (node.inDeclarationContext()) {
1687-
var range = ref._visibleRangeMap[element];
1687+
var range = ref._visibleRangeMap[element.asElement2];
16881688
if (range != null) {
16891689
var ranges = ref._localNames.putIfAbsent(name, () => <SourceRange>[]);
16901690
ranges.add(range);
@@ -1714,7 +1714,7 @@ class _InitializeParametersVisitor extends GeneralizingAstVisitor<void> {
17141714
// remember information for conflicts checking
17151715
if (element is LocalElement) {
17161716
// declared local elements
1717-
var range = ref._visibleRangeMap[element as LocalElement];
1717+
var range = ref._visibleRangeMap[element.asElement2 as LocalElement2];
17181718
if (range != null) {
17191719
var name = node.name.lexeme;
17201720
var ranges = ref._localNames.putIfAbsent(name, () => <SourceRange>[]);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ Set<String> _getNamesConflictingAt(AstNode node) {
165165
var localsRange = _getLocalsConflictingRange(node);
166166
var enclosingExecutable = getEnclosingExecutableNode(node);
167167
if (enclosingExecutable != null) {
168-
var visibleRangeMap = VisibleRangesComputer.forNode2(enclosingExecutable);
168+
var visibleRangeMap = VisibleRangesComputer.forNode(enclosingExecutable);
169169
visibleRangeMap.forEach((element, elementRange) {
170170
if (elementRange.intersects(localsRange)) {
171171
result.add(element.displayName);

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ abstract class RenameRefactoring implements Refactoring {
471471
workspace,
472472
sessionHelper,
473473
resolvedUnit,
474-
element,
474+
element.asElement2!,
475475
);
476476
}
477477
if (element is ConstructorElement) {
@@ -489,19 +489,31 @@ abstract class RenameRefactoring implements Refactoring {
489489
);
490490
}
491491
if (element is LibraryElement) {
492-
return RenameLibraryRefactoringImpl(workspace, sessionHelper, element);
492+
return RenameLibraryRefactoringImpl(
493+
workspace,
494+
sessionHelper,
495+
element.asElement2,
496+
);
493497
}
494498
if (element is ParameterElement) {
495-
return RenameParameterRefactoringImpl(workspace, sessionHelper, element);
499+
return RenameParameterRefactoringImpl(
500+
workspace,
501+
sessionHelper,
502+
element.asElement2,
503+
);
496504
}
497505
if (element is LocalElement) {
498-
return RenameLocalRefactoringImpl(workspace, sessionHelper, element);
506+
return RenameLocalRefactoringImpl(
507+
workspace,
508+
sessionHelper,
509+
element.asElement2 as LocalElement2,
510+
);
499511
}
500512
if (element is TypeParameterElement) {
501513
return RenameTypeParameterRefactoringImpl(
502514
workspace,
503515
sessionHelper,
504-
element,
516+
element.asElement2 as TypeParameterElement2,
505517
);
506518
}
507519
if (enclosingElement is InterfaceElement) {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:analysis_server/src/services/search/search_engine.dart';
1111
import 'package:analyzer/dart/element/element.dart';
1212
import 'package:analyzer/dart/element/element2.dart';
1313
import 'package:analyzer/src/dart/analysis/session_helper.dart';
14+
import 'package:analyzer/src/dart/element/element.dart';
1415
import 'package:analyzer/src/generated/java_core.dart';
1516
import 'package:analyzer/src/utilities/extensions/element.dart';
1617
import 'package:analyzer_plugin/utilities/range_factory.dart';
@@ -39,7 +40,18 @@ class RenameProcessor {
3940

4041
/// Add the edit that updates the [element] declaration.
4142
void addDeclarationEdit2(Element2? element) {
42-
if (element != null && workspace.containsElement2(element)) {
43+
if (element == null) {
44+
return;
45+
} else if (element is LibraryElementImpl) {
46+
// TODO(brianwilkerson): Consider adding public API to get the offset and
47+
// length of the library's name.
48+
var nameRange = range.startOffsetLength(
49+
element.nameOffset,
50+
element.nameLength,
51+
);
52+
var edit = newSourceEdit_range(nameRange, newName);
53+
doSourceChange_addFragmentEdit(change, element.firstFragment, edit);
54+
} else if (workspace.containsElement2(element)) {
4355
Fragment? fragment = element.firstFragment;
4456
while (fragment != null) {
4557
var edit = newSourceEdit_range(range.fragmentName(fragment)!, newName);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ class _RenameClassMemberValidator extends _BaseClassMemberValidator {
451451
localElements = collector.elements;
452452
localElementMap[unitFragment] = localElements;
453453

454-
visibleRangeMap.addAll(VisibleRangesComputer.forNode2(unit));
454+
visibleRangeMap.addAll(VisibleRangesComputer.forNode(unit));
455455
}
456456

457457
return localElements;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class _ExtensionMemberValidator {
178178
localElements = collector.elements;
179179
localElementMap[unitFragment] = localElements;
180180

181-
visibleRangeMap.addAll(VisibleRangesComputer.forNode2(unit));
181+
visibleRangeMap.addAll(VisibleRangesComputer.forNode(unit));
182182
}
183183

184184
return localElements;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ import 'package:analysis_server/src/services/correction/status.dart';
66
import 'package:analysis_server/src/services/refactoring/legacy/naming_conventions.dart';
77
import 'package:analysis_server/src/services/refactoring/legacy/refactoring.dart';
88
import 'package:analysis_server/src/services/refactoring/legacy/rename.dart';
9-
import 'package:analyzer/dart/element/element.dart';
9+
import 'package:analyzer/dart/element/element2.dart';
1010

1111
/// A [Refactoring] for renaming [LibraryElement]s.
1212
class RenameLibraryRefactoringImpl extends RenameRefactoringImpl {
1313
RenameLibraryRefactoringImpl(
1414
super.workspace,
1515
super.sessionHelper,
16-
LibraryElement super.element,
17-
);
16+
LibraryElement2 super.element2,
17+
) : super.c2();
1818

1919
@override
20-
LibraryElement get element => super.element as LibraryElement;
20+
LibraryElement2 get element2 => super.element2 as LibraryElement2;
2121

2222
@override
2323
String get refactoringName {
@@ -40,6 +40,6 @@ class RenameLibraryRefactoringImpl extends RenameRefactoringImpl {
4040
@override
4141
Future<void> fillChange() async {
4242
var processor = RenameProcessor(workspace, sessionHelper, change, newName);
43-
await processor.renameElement(element);
43+
await processor.renameElement2(element2);
4444
}
4545
}

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

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@ import 'package:analysis_server/src/services/search/hierarchy.dart';
1414
import 'package:analyzer/dart/ast/ast.dart';
1515
import 'package:analyzer/dart/ast/token.dart';
1616
import 'package:analyzer/dart/ast/visitor.dart';
17-
import 'package:analyzer/dart/element/element.dart';
17+
import 'package:analyzer/dart/element/element2.dart';
1818
import 'package:analyzer/source/source_range.dart';
1919
import 'package:analyzer/src/dart/element/element.dart';
20-
import 'package:analyzer/src/utilities/extensions/element.dart';
2120

2221
class ConflictValidatorVisitor extends RecursiveAstVisitor<void> {
2322
final RefactoringStatus result;
2423
final String newName;
25-
final LocalElement target;
26-
final Map<Element, SourceRange> visibleRangeMap;
27-
final Set<Element> conflictingLocals = <Element>{};
24+
final LocalElement2 target;
25+
final Map<Element2, SourceRange> visibleRangeMap;
26+
final Set<Element2> conflictingLocals = <Element2>{};
2827

2928
ConflictValidatorVisitor(
3029
this.result,
@@ -36,7 +35,7 @@ class ConflictValidatorVisitor extends RecursiveAstVisitor<void> {
3635
@override
3736
void visitFunctionDeclaration(FunctionDeclaration node) {
3837
_checkDeclaration(
39-
declaredElement: node.declaredElement!,
38+
declaredElement: node.declaredFragment!.element,
4039
nameToken: node.name,
4140
);
4241

@@ -45,8 +44,8 @@ class ConflictValidatorVisitor extends RecursiveAstVisitor<void> {
4544

4645
@override
4746
void visitSimpleIdentifier(SimpleIdentifier node) {
48-
var nodeElement = node.staticElement;
49-
if (nodeElement != null && nodeElement.name == newName) {
47+
var nodeElement = node.element;
48+
if (nodeElement != null && nodeElement.name3 == newName) {
5049
if (conflictingLocals.contains(nodeElement)) {
5150
return;
5251
}
@@ -56,11 +55,11 @@ class ConflictValidatorVisitor extends RecursiveAstVisitor<void> {
5655
targetRange.contains(node.offset) &&
5756
!node.isQualified &&
5857
!_isNamedExpressionName(node)) {
59-
nodeElement =
60-
getSyntheticAccessorVariable(nodeElement.asElement2!).asElement!;
58+
nodeElement = getSyntheticAccessorVariable(nodeElement);
6159
var nodeKind = nodeElement.kind.displayName;
62-
var nodeName = getElementQualifiedName(nodeElement.asElement2!);
63-
var nameElementSourceName = nodeElement.source!.shortName;
60+
var nodeName = getElementQualifiedName(nodeElement);
61+
var nameElementSourceName =
62+
nodeElement.firstFragment.libraryFragment!.source.shortName;
6463
var refKind = target.kind.displayName;
6564
var message =
6665
'Usage of $nodeKind "$nodeName" declared in '
@@ -73,15 +72,15 @@ class ConflictValidatorVisitor extends RecursiveAstVisitor<void> {
7372
@override
7473
void visitVariableDeclaration(VariableDeclaration node) {
7574
_checkDeclaration(
76-
declaredElement: node.declaredElement!,
75+
declaredElement: node.declaredFragment!.element,
7776
nameToken: node.name,
7877
);
7978

8079
super.visitVariableDeclaration(node);
8180
}
8281

8382
void _checkDeclaration({
84-
required Element? declaredElement,
83+
required Element2? declaredElement,
8584
required Token nameToken,
8685
}) {
8786
if (declaredElement != null && nameToken.lexeme == newName) {
@@ -90,19 +89,19 @@ class ConflictValidatorVisitor extends RecursiveAstVisitor<void> {
9089
conflictingLocals.add(declaredElement);
9190
var nodeKind = declaredElement.kind.displayName;
9291
var message = "Duplicate $nodeKind '$newName'.";
93-
result.addError(message, newLocation_fromElement(declaredElement));
92+
result.addError(message, newLocation_fromElement2(declaredElement));
9493
return;
9594
}
9695
}
9796
}
9897

99-
SourceRange? _getVisibleRange(LocalElement element) {
98+
SourceRange? _getVisibleRange(LocalElement2 element) {
10099
return visibleRangeMap[element];
101100
}
102101

103102
/// Returns whether [element] and [target] are visible together.
104-
bool _isVisibleWithTarget(Element element) {
105-
if (element is LocalElement) {
103+
bool _isVisibleWithTarget(Element2 element) {
104+
if (element is LocalElement2) {
106105
var targetRange = _getVisibleRange(target);
107106
var elementRange = _getVisibleRange(element);
108107
return elementRange != null && elementRange.intersects(targetRange);
@@ -121,15 +120,15 @@ class RenameLocalRefactoringImpl extends RenameRefactoringImpl {
121120
RenameLocalRefactoringImpl(
122121
super.workspace,
123122
super.sessionHelper,
124-
LocalElement super.element,
125-
);
123+
LocalElement2 super.element,
124+
) : super.c2();
126125

127126
@override
128-
LocalElement get element => super.element as LocalElement;
127+
LocalElement2 get element2 => super.element2 as LocalElement2;
129128

130129
@override
131130
String get refactoringName {
132-
if (element is FunctionElement) {
131+
if (element2 is LocalFunctionElement) {
133132
return 'Rename Local Function';
134133
}
135134
return 'Rename Local Variable';
@@ -138,13 +137,13 @@ class RenameLocalRefactoringImpl extends RenameRefactoringImpl {
138137
@override
139138
Future<RefactoringStatus> checkFinalConditions() async {
140139
var result = RefactoringStatus();
141-
var resolvedUnit = await sessionHelper.getResolvedUnitByElement(element);
140+
var resolvedUnit = await sessionHelper.getResolvedUnitByElement2(element2);
142141
var unit = resolvedUnit?.unit;
143142
unit?.accept(
144143
ConflictValidatorVisitor(
145144
result,
146145
newName,
147-
element,
146+
element2,
148147
VisibleRangesComputer.forNode(unit),
149148
),
150149
);
@@ -154,9 +153,9 @@ class RenameLocalRefactoringImpl extends RenameRefactoringImpl {
154153
@override
155154
RefactoringStatus checkNewName() {
156155
var result = super.checkNewName();
157-
if (element is LocalVariableElement) {
156+
if (element2 is LocalVariableElement2) {
158157
result.addStatus(validateVariableName(newName));
159-
} else if (element is FunctionElement) {
158+
} else if (element2 is LocalFunctionElement) {
160159
result.addStatus(validateFunctionName(newName));
161160
}
162161
return result;
@@ -166,36 +165,37 @@ class RenameLocalRefactoringImpl extends RenameRefactoringImpl {
166165
Future<void> fillChange() async {
167166
var processor = RenameProcessor(workspace, sessionHelper, change, newName);
168167

169-
var element = this.element;
170-
if (element is PatternVariableElementImpl) {
171-
var rootVariable = element.rootVariable;
172-
var declaredElements =
168+
var element = element2;
169+
if (element is PatternVariableElement2) {
170+
var rootVariable =
171+
(element.firstFragment as PatternVariableElementImpl).rootVariable;
172+
var declaredFragments =
173173
rootVariable is JoinPatternVariableElementImpl
174174
? rootVariable.transitiveVariables
175175
.whereType<BindPatternVariableElementImpl>()
176176
.toList()
177-
: [element];
178-
for (var declaredElement in declaredElements) {
179-
processor.addDeclarationEdit(declaredElement);
180-
if (declaredElement is BindPatternVariableElementImpl) {
177+
: [element.firstFragment];
178+
for (var declaredFragment in declaredFragments) {
179+
processor.addDeclarationEdit2(declaredFragment.element);
180+
if (declaredFragment is BindPatternVariableElementImpl) {
181181
// If a variable is used to resolve a named field with an implicit
182182
// name, we need to make the field name explicit.
183-
var fieldName = declaredElement.node.fieldNameWithImplicitName;
183+
var fieldName = declaredFragment.node.fieldNameWithImplicitName;
184184
if (fieldName != null) {
185-
processor.replace(
185+
processor.replace2(
186186
referenceElement: element,
187187
offset: fieldName.colon.offset,
188188
length: 0,
189-
code: element.name,
189+
code: element.name3!,
190190
);
191191
}
192192
}
193193
}
194194
} else {
195-
processor.addDeclarationEdit(element);
195+
processor.addDeclarationEdit2(element);
196196
}
197197

198-
var references = await searchEngine.searchReferences(element);
198+
var references = await searchEngine.searchReferences2(element);
199199
processor.addReferenceEdits(references);
200200
}
201201
}

0 commit comments

Comments
 (0)