Skip to content

Commit

Permalink
Migrate 5 more rename pieces and a utility class
Browse files Browse the repository at this point in the history
Change-Id: I54a9ac1590c262ca82d6ba62b8cac248cbfd9ee0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/399703
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
bwilkerson authored and Commit Queue committed Dec 10, 2024
1 parent ed8e22f commit 2d7bb5d
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 225 deletions.
6 changes: 0 additions & 6 deletions pkg/analysis_server/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ lib/src/services/refactoring/legacy/refactoring_internal.dart
lib/src/services/refactoring/legacy/refactoring_manager.dart
lib/src/services/refactoring/legacy/rename.dart
lib/src/services/refactoring/legacy/rename_import.dart
lib/src/services/refactoring/legacy/rename_library.dart
lib/src/services/refactoring/legacy/rename_local.dart
lib/src/services/refactoring/legacy/rename_parameter.dart
lib/src/services/refactoring/legacy/rename_type_parameter.dart
lib/src/services/refactoring/legacy/rename_unit_member.dart
lib/src/services/refactoring/legacy/visible_ranges_computer.dart
lib/src/services/refactoring/move_top_level_to_file.dart
lib/src/services/search/element_visitors.dart
lib/src/services/search/search_engine.dart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ final class ExtractMethodRefactoringImpl extends RefactoringImpl
final List<int> lengths = <int>[];

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

/// The map of local names to their visibility ranges.
final Map<String, List<SourceRange>> _localNames =
Expand Down Expand Up @@ -642,7 +642,7 @@ final class ExtractMethodRefactoringImpl extends RefactoringImpl
var parent = _parentMember!.parent;
// top-level function
if (parent is CompilationUnit) {
var libraryElement = parent.declaredElement!.library;
var libraryElement = parent.declaredFragment!.element;
return validateCreateFunction(_searchEngine, libraryElement, name);
}
// method of class
Expand Down Expand Up @@ -1684,7 +1684,7 @@ class _InitializeParametersVisitor extends GeneralizingAstVisitor<void> {
if (element is LocalElement) {
// declared local elements
if (node.inDeclarationContext()) {
var range = ref._visibleRangeMap[element];
var range = ref._visibleRangeMap[element.asElement2];
if (range != null) {
var ranges = ref._localNames.putIfAbsent(name, () => <SourceRange>[]);
ranges.add(range);
Expand Down Expand Up @@ -1714,7 +1714,7 @@ class _InitializeParametersVisitor extends GeneralizingAstVisitor<void> {
// remember information for conflicts checking
if (element is LocalElement) {
// declared local elements
var range = ref._visibleRangeMap[element as LocalElement];
var range = ref._visibleRangeMap[element.asElement2 as LocalElement2];
if (range != null) {
var name = node.name.lexeme;
var ranges = ref._localNames.putIfAbsent(name, () => <SourceRange>[]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Set<String> _getNamesConflictingAt(AstNode node) {
var localsRange = _getLocalsConflictingRange(node);
var enclosingExecutable = getEnclosingExecutableNode(node);
if (enclosingExecutable != null) {
var visibleRangeMap = VisibleRangesComputer.forNode2(enclosingExecutable);
var visibleRangeMap = VisibleRangesComputer.forNode(enclosingExecutable);
visibleRangeMap.forEach((element, elementRange) {
if (elementRange.intersects(localsRange)) {
result.add(element.displayName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ abstract class RenameRefactoring implements Refactoring {
workspace,
sessionHelper,
resolvedUnit,
element,
element.asElement2!,
);
}
if (element is ConstructorElement) {
Expand All @@ -489,19 +489,31 @@ abstract class RenameRefactoring implements Refactoring {
);
}
if (element is LibraryElement) {
return RenameLibraryRefactoringImpl(workspace, sessionHelper, element);
return RenameLibraryRefactoringImpl(
workspace,
sessionHelper,
element.asElement2,
);
}
if (element is ParameterElement) {
return RenameParameterRefactoringImpl(workspace, sessionHelper, element);
return RenameParameterRefactoringImpl(
workspace,
sessionHelper,
element.asElement2,
);
}
if (element is LocalElement) {
return RenameLocalRefactoringImpl(workspace, sessionHelper, element);
return RenameLocalRefactoringImpl(
workspace,
sessionHelper,
element.asElement2 as LocalElement2,
);
}
if (element is TypeParameterElement) {
return RenameTypeParameterRefactoringImpl(
workspace,
sessionHelper,
element,
element.asElement2 as TypeParameterElement2,
);
}
if (enclosingElement is InterfaceElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:analysis_server/src/services/search/search_engine.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/src/dart/analysis/session_helper.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/generated/java_core.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
Expand Down Expand Up @@ -39,7 +40,18 @@ class RenameProcessor {

/// Add the edit that updates the [element] declaration.
void addDeclarationEdit2(Element2? element) {
if (element != null && workspace.containsElement2(element)) {
if (element == null) {
return;
} else if (element is LibraryElementImpl) {
// TODO(brianwilkerson): Consider adding public API to get the offset and
// length of the library's name.
var nameRange = range.startOffsetLength(
element.nameOffset,
element.nameLength,
);
var edit = newSourceEdit_range(nameRange, newName);
doSourceChange_addFragmentEdit(change, element.firstFragment, edit);
} else if (workspace.containsElement2(element)) {
Fragment? fragment = element.firstFragment;
while (fragment != null) {
var edit = newSourceEdit_range(range.fragmentName(fragment)!, newName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ class _RenameClassMemberValidator extends _BaseClassMemberValidator {
localElements = collector.elements;
localElementMap[unitFragment] = localElements;

visibleRangeMap.addAll(VisibleRangesComputer.forNode2(unit));
visibleRangeMap.addAll(VisibleRangesComputer.forNode(unit));
}

return localElements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class _ExtensionMemberValidator {
localElements = collector.elements;
localElementMap[unitFragment] = localElements;

visibleRangeMap.addAll(VisibleRangesComputer.forNode2(unit));
visibleRangeMap.addAll(VisibleRangesComputer.forNode(unit));
}

return localElements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import 'package:analysis_server/src/services/correction/status.dart';
import 'package:analysis_server/src/services/refactoring/legacy/naming_conventions.dart';
import 'package:analysis_server/src/services/refactoring/legacy/refactoring.dart';
import 'package:analysis_server/src/services/refactoring/legacy/rename.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';

/// A [Refactoring] for renaming [LibraryElement]s.
class RenameLibraryRefactoringImpl extends RenameRefactoringImpl {
RenameLibraryRefactoringImpl(
super.workspace,
super.sessionHelper,
LibraryElement super.element,
);
LibraryElement2 super.element2,
) : super.c2();

@override
LibraryElement get element => super.element as LibraryElement;
LibraryElement2 get element2 => super.element2 as LibraryElement2;

@override
String get refactoringName {
Expand All @@ -40,6 +40,6 @@ class RenameLibraryRefactoringImpl extends RenameRefactoringImpl {
@override
Future<void> fillChange() async {
var processor = RenameProcessor(workspace, sessionHelper, change, newName);
await processor.renameElement(element);
await processor.renameElement2(element2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ import 'package:analysis_server/src/services/search/hierarchy.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/source/source_range.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';

class ConflictValidatorVisitor extends RecursiveAstVisitor<void> {
final RefactoringStatus result;
final String newName;
final LocalElement target;
final Map<Element, SourceRange> visibleRangeMap;
final Set<Element> conflictingLocals = <Element>{};
final LocalElement2 target;
final Map<Element2, SourceRange> visibleRangeMap;
final Set<Element2> conflictingLocals = <Element2>{};

ConflictValidatorVisitor(
this.result,
Expand All @@ -36,7 +35,7 @@ class ConflictValidatorVisitor extends RecursiveAstVisitor<void> {
@override
void visitFunctionDeclaration(FunctionDeclaration node) {
_checkDeclaration(
declaredElement: node.declaredElement!,
declaredElement: node.declaredFragment!.element,
nameToken: node.name,
);

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

@override
void visitSimpleIdentifier(SimpleIdentifier node) {
var nodeElement = node.staticElement;
if (nodeElement != null && nodeElement.name == newName) {
var nodeElement = node.element;
if (nodeElement != null && nodeElement.name3 == newName) {
if (conflictingLocals.contains(nodeElement)) {
return;
}
Expand All @@ -56,11 +55,11 @@ class ConflictValidatorVisitor extends RecursiveAstVisitor<void> {
targetRange.contains(node.offset) &&
!node.isQualified &&
!_isNamedExpressionName(node)) {
nodeElement =
getSyntheticAccessorVariable(nodeElement.asElement2!).asElement!;
nodeElement = getSyntheticAccessorVariable(nodeElement);
var nodeKind = nodeElement.kind.displayName;
var nodeName = getElementQualifiedName(nodeElement.asElement2!);
var nameElementSourceName = nodeElement.source!.shortName;
var nodeName = getElementQualifiedName(nodeElement);
var nameElementSourceName =
nodeElement.firstFragment.libraryFragment!.source.shortName;
var refKind = target.kind.displayName;
var message =
'Usage of $nodeKind "$nodeName" declared in '
Expand All @@ -73,15 +72,15 @@ class ConflictValidatorVisitor extends RecursiveAstVisitor<void> {
@override
void visitVariableDeclaration(VariableDeclaration node) {
_checkDeclaration(
declaredElement: node.declaredElement!,
declaredElement: node.declaredFragment!.element,
nameToken: node.name,
);

super.visitVariableDeclaration(node);
}

void _checkDeclaration({
required Element? declaredElement,
required Element2? declaredElement,
required Token nameToken,
}) {
if (declaredElement != null && nameToken.lexeme == newName) {
Expand All @@ -90,19 +89,19 @@ class ConflictValidatorVisitor extends RecursiveAstVisitor<void> {
conflictingLocals.add(declaredElement);
var nodeKind = declaredElement.kind.displayName;
var message = "Duplicate $nodeKind '$newName'.";
result.addError(message, newLocation_fromElement(declaredElement));
result.addError(message, newLocation_fromElement2(declaredElement));
return;
}
}
}

SourceRange? _getVisibleRange(LocalElement element) {
SourceRange? _getVisibleRange(LocalElement2 element) {
return visibleRangeMap[element];
}

/// Returns whether [element] and [target] are visible together.
bool _isVisibleWithTarget(Element element) {
if (element is LocalElement) {
bool _isVisibleWithTarget(Element2 element) {
if (element is LocalElement2) {
var targetRange = _getVisibleRange(target);
var elementRange = _getVisibleRange(element);
return elementRange != null && elementRange.intersects(targetRange);
Expand All @@ -121,15 +120,15 @@ class RenameLocalRefactoringImpl extends RenameRefactoringImpl {
RenameLocalRefactoringImpl(
super.workspace,
super.sessionHelper,
LocalElement super.element,
);
LocalElement2 super.element,
) : super.c2();

@override
LocalElement get element => super.element as LocalElement;
LocalElement2 get element2 => super.element2 as LocalElement2;

@override
String get refactoringName {
if (element is FunctionElement) {
if (element2 is LocalFunctionElement) {
return 'Rename Local Function';
}
return 'Rename Local Variable';
Expand All @@ -138,13 +137,13 @@ class RenameLocalRefactoringImpl extends RenameRefactoringImpl {
@override
Future<RefactoringStatus> checkFinalConditions() async {
var result = RefactoringStatus();
var resolvedUnit = await sessionHelper.getResolvedUnitByElement(element);
var resolvedUnit = await sessionHelper.getResolvedUnitByElement2(element2);
var unit = resolvedUnit?.unit;
unit?.accept(
ConflictValidatorVisitor(
result,
newName,
element,
element2,
VisibleRangesComputer.forNode(unit),
),
);
Expand All @@ -154,9 +153,9 @@ class RenameLocalRefactoringImpl extends RenameRefactoringImpl {
@override
RefactoringStatus checkNewName() {
var result = super.checkNewName();
if (element is LocalVariableElement) {
if (element2 is LocalVariableElement2) {
result.addStatus(validateVariableName(newName));
} else if (element is FunctionElement) {
} else if (element2 is LocalFunctionElement) {
result.addStatus(validateFunctionName(newName));
}
return result;
Expand All @@ -166,36 +165,37 @@ class RenameLocalRefactoringImpl extends RenameRefactoringImpl {
Future<void> fillChange() async {
var processor = RenameProcessor(workspace, sessionHelper, change, newName);

var element = this.element;
if (element is PatternVariableElementImpl) {
var rootVariable = element.rootVariable;
var declaredElements =
var element = element2;
if (element is PatternVariableElement2) {
var rootVariable =
(element.firstFragment as PatternVariableElementImpl).rootVariable;
var declaredFragments =
rootVariable is JoinPatternVariableElementImpl
? rootVariable.transitiveVariables
.whereType<BindPatternVariableElementImpl>()
.toList()
: [element];
for (var declaredElement in declaredElements) {
processor.addDeclarationEdit(declaredElement);
if (declaredElement is BindPatternVariableElementImpl) {
: [element.firstFragment];
for (var declaredFragment in declaredFragments) {
processor.addDeclarationEdit2(declaredFragment.element);
if (declaredFragment is BindPatternVariableElementImpl) {
// If a variable is used to resolve a named field with an implicit
// name, we need to make the field name explicit.
var fieldName = declaredElement.node.fieldNameWithImplicitName;
var fieldName = declaredFragment.node.fieldNameWithImplicitName;
if (fieldName != null) {
processor.replace(
processor.replace2(
referenceElement: element,
offset: fieldName.colon.offset,
length: 0,
code: element.name,
code: element.name3!,
);
}
}
}
} else {
processor.addDeclarationEdit(element);
processor.addDeclarationEdit2(element);
}

var references = await searchEngine.searchReferences(element);
var references = await searchEngine.searchReferences2(element);
processor.addReferenceEdits(references);
}
}
Loading

0 comments on commit 2d7bb5d

Please sign in to comment.