Skip to content

Commit 2c62efb

Browse files
bwilkersonCommit Queue
authored andcommitted
[migration] cider/rename.dart
This also contains some minor, but unrelated, changes to kythe_visitors.dart. I'm happy to back them out if you'd like me to. Change-Id: I89dac691c3bb1c642a343c010fb71b66680272c1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/410901 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 4d5d8eb commit 2c62efb

File tree

4 files changed

+226
-78
lines changed

4 files changed

+226
-78
lines changed

pkg/analysis_server/lib/src/cider/rename.dart

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +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-
// ignore_for_file: analyzer_use_new_elements
6-
75
import 'package:analysis_server/src/protocol_server.dart' hide Element;
86
import 'package:analysis_server/src/services/correction/status.dart';
97
import 'package:analysis_server/src/services/correction/util.dart';
@@ -12,7 +10,6 @@ import 'package:analysis_server/src/services/refactoring/legacy/refactoring.dart
1210
import 'package:analysis_server/src/services/search/hierarchy.dart';
1311
import 'package:analysis_server/src/utilities/change_builder.dart';
1412
import 'package:analyzer/dart/ast/ast.dart';
15-
import 'package:analyzer/dart/element/element.dart';
1613
import 'package:analyzer/dart/element/element2.dart';
1714
import 'package:analyzer/source/line_info.dart';
1815
import 'package:analyzer/src/dart/ast/utilities.dart';
@@ -38,31 +35,32 @@ class CanRenameResponse {
3835
this.filePath,
3936
);
4037

41-
String get oldName => refactoringElement.element.displayName;
38+
String get oldName => refactoringElement.element2.displayName;
4239

4340
CheckNameResponse? checkNewName(String name) {
44-
var element = refactoringElement.element;
41+
var element = refactoringElement.element2;
4542

4643
RefactoringStatus? status;
47-
if (element is ParameterElement) {
44+
if (element is FormalParameterElement) {
4845
status = validateParameterName(name);
49-
} else if (element is VariableElement) {
46+
} else if (element is VariableElement2) {
5047
status = validateVariableName(name);
51-
} else if (element is FunctionElement) {
48+
} else if (element is LocalFunctionElement ||
49+
element is TopLevelFunctionElement) {
5250
status = validateFunctionName(name);
53-
} else if (element is FieldElement) {
51+
} else if (element is FieldElement2) {
5452
status = validateFieldName(name);
55-
} else if (element is MethodElement) {
53+
} else if (element is MethodElement2) {
5654
status = validateMethodName(name);
57-
} else if (element is TypeAliasElement) {
55+
} else if (element is TypeAliasElement2) {
5856
status = validateTypeAliasName(name);
59-
} else if (element is InterfaceElement) {
57+
} else if (element is InterfaceElement2) {
6058
status = validateClassName(name);
61-
_flutterWidgetState = _findFlutterStateClass(element.asElement2, name);
62-
} else if (element is ConstructorElement) {
59+
_flutterWidgetState = _findFlutterStateClass(element, name);
60+
} else if (element is ConstructorElement2) {
6361
status = validateConstructorName(name);
64-
_analyzePossibleConflicts(element.asElement2, status, name);
65-
} else if (element is LibraryImportElement) {
62+
_analyzePossibleConflicts(element, status, name);
63+
} else if (element is MockLibraryImportElement) {
6664
status = validateImportPrefixName(name);
6765
}
6866

@@ -127,15 +125,15 @@ class CheckNameResponse {
127125

128126
LineInfo get lineInfo => canRename.lineInfo;
129127

130-
String get oldName => canRename.refactoringElement.element.displayName;
128+
String get oldName => canRename.refactoringElement.element2.displayName;
131129

132130
Future<RenameResponse?> computeRenameRanges2() async {
133-
var elements = <Element>[];
134-
var element = canRename.refactoringElement.element;
135-
if (element is PropertyInducingElement && element.isSynthetic) {
131+
var elements = <Element2>[];
132+
var element = canRename.refactoringElement.element2;
133+
if (element is PropertyInducingElement2 && element.isSynthetic) {
136134
var property = element;
137-
var getter = property.getter;
138-
var setter = property.setter;
135+
var getter = property.getter2;
136+
var setter = property.setter2;
139137
elements.addIfNotNull(getter);
140138
elements.addIfNotNull(setter);
141139
} else {
@@ -144,14 +142,14 @@ class CheckNameResponse {
144142
var fileResolver = canRename._fileResolver;
145143
var matches = <CiderSearchMatch>[];
146144
for (var element in elements) {
147-
matches.addAll(await fileResolver.findReferences2(element));
145+
matches.addAll(await fileResolver.findReferences(element));
148146
}
149147
FlutterWidgetRename? flutterRename;
150148
if (canRename._flutterWidgetState != null) {
151149
flutterRename = await _computeFlutterStateName();
152150
}
153151
var replaceMatches = <CiderReplaceMatch>[];
154-
if (element is ConstructorElement) {
152+
if (element is ConstructorElement2) {
155153
for (var match in matches) {
156154
var replaceInfo = <ReplaceInfo>[];
157155
for (var ref in match.references) {
@@ -176,7 +174,7 @@ class CheckNameResponse {
176174
replaceMatches.addMatch(result.path, result.matches.toList());
177175
}
178176
}
179-
} else if (element is LibraryImportElement) {
177+
} else if (element is MockLibraryImportElement) {
180178
var replaceInfo = <ReplaceInfo>[];
181179
for (var match in matches) {
182180
for (var ref in match.references) {
@@ -204,7 +202,7 @@ class CheckNameResponse {
204202
}
205203
}
206204
replaceMatches.addMatch(match.path, replaceInfo);
207-
var sourcePath = element.source.fullName;
205+
var sourcePath = element.libraryFragment.source.fullName;
208206
var infos = await _addElementDeclaration(element, sourcePath);
209207
replaceMatches.addMatch(sourcePath, infos);
210208
}
@@ -220,7 +218,7 @@ class CheckNameResponse {
220218
);
221219
}
222220
// add element declaration
223-
var sourcePath = element.source!.fullName;
221+
var sourcePath = element.library2!.firstFragment.source.fullName;
224222
var infos = await _addElementDeclaration(element, sourcePath);
225223
replaceMatches.addMatch(sourcePath, infos);
226224
}
@@ -233,32 +231,36 @@ class CheckNameResponse {
233231
}
234232

235233
Future<List<ReplaceInfo>> _addElementDeclaration(
236-
Element element,
234+
Element2 element,
237235
String sourcePath,
238236
) async {
239237
var infos = <ReplaceInfo>[];
240-
if (element is PropertyInducingElement && element.isSynthetic) {
241-
if (element.getter != null) {
238+
if (element is PropertyInducingElement2 && element.isSynthetic) {
239+
var getter = element.getter2;
240+
if (getter != null) {
242241
infos.add(
243242
ReplaceInfo(
244243
newName,
245-
lineInfo.getLocation(element.getter!.nameOffset),
246-
element.getter!.nameLength,
244+
lineInfo.getLocation(getter.firstFragment.nameOffset2!),
245+
getter.name3!.length,
247246
),
248247
);
249248
}
250-
if (element.setter != null) {
249+
var setter = element.setter2;
250+
if (setter != null) {
251251
infos.add(
252252
ReplaceInfo(
253253
newName,
254-
lineInfo.getLocation(element.setter!.nameOffset),
255-
element.setter!.nameLength,
254+
lineInfo.getLocation(setter.firstFragment.nameOffset2!),
255+
setter.name3!.length,
256256
),
257257
);
258258
}
259-
} else if (element is LibraryImportElement) {
259+
} else if (element is MockLibraryImportElement) {
260260
var unit = (await canRename._fileResolver.resolve(path: sourcePath)).unit;
261-
var index = element.enclosingElement3.libraryImports.indexOf(element);
261+
var index = element.libraryFragment.libraryImports2.indexOf(
262+
element.import,
263+
);
262264
var node = unit.directives.whereType<ImportDirective>().elementAt(index);
263265
var prefixNode = node.prefix;
264266
if (newName.isEmpty) {
@@ -289,8 +291,8 @@ class CheckNameResponse {
289291
} else {
290292
var location = (await canRename._fileResolver.resolve(
291293
path: sourcePath,
292-
)).lineInfo.getLocation(element.nameOffset);
293-
infos.add(ReplaceInfo(newName, location, element.nameLength));
294+
)).lineInfo.getLocation(element.firstFragment.nameOffset2!);
295+
infos.add(ReplaceInfo(newName, location, element.name3!.length));
294296
}
295297
return infos;
296298
}
@@ -360,13 +362,15 @@ class CheckNameResponse {
360362
}
361363

362364
Future<CiderReplaceMatch?> _replaceSyntheticConstructor() async {
363-
var element = canRename.refactoringElement.element;
364-
var interfaceElement = element.enclosingElement3;
365+
var element = canRename.refactoringElement.element2;
366+
var interfaceElement = element.enclosingElement2!;
365367

366368
var fileResolver = canRename._fileResolver;
367-
var libraryPath = interfaceElement!.library!.source.fullName;
369+
var libraryPath = interfaceElement.library2!.firstFragment.source.fullName;
368370
var resolvedLibrary = await fileResolver.resolveLibrary2(path: libraryPath);
369-
var result = resolvedLibrary.getElementDeclaration(interfaceElement);
371+
var result = resolvedLibrary.getFragmentDeclaration(
372+
interfaceElement.firstFragment,
373+
);
370374
if (result == null) {
371375
return null;
372376
}
@@ -385,7 +389,7 @@ class CheckNameResponse {
385389
resolvedUnit: resolvedUnit,
386390
session: fileResolver.contextObjects!.analysisSession,
387391
(builder) => builder.writeConstructorDeclaration(
388-
interfaceElement.name!,
392+
interfaceElement.name3!,
389393
constructorName: newName,
390394
isConst: node is EnumDeclaration,
391395
),
@@ -421,47 +425,47 @@ class CiderRenameComputer {
421425
var offset = lineInfo.getOffsetOfLine(line) + column;
422426

423427
var node = NodeLocator(offset).searchWithin(resolvedUnit.unit);
424-
var element = getElementOfNode(node);
428+
var element = getElementOfNode2(node);
425429

426430
if (node == null || element == null) {
427431
return null;
428432
}
429-
if (element.library?.isInSdk == true) {
433+
if (element.library2?.isInSdk == true) {
430434
return null;
431435
}
432-
if (element is MethodElement && element.isOperator) {
436+
if (element is MethodElement2 && element.isOperator) {
433437
return null;
434438
}
435-
if (element is PropertyAccessorElement) {
436-
element = element.variable2;
439+
if (element is PropertyAccessorElement2) {
440+
element = element.variable3;
437441
if (element == null) {
438442
return null;
439443
}
440444
}
441445
if (!_canRenameElement(element)) {
442446
return null;
443447
}
444-
var refactoring = RenameRefactoring.getElementToRename(node, element);
448+
var refactoring = RenameRefactoring.getElementToRename2(node, element);
445449
if (refactoring != null) {
446450
return CanRenameResponse(lineInfo, refactoring, _fileResolver, filePath);
447451
}
448452
return null;
449453
}
450454

451-
bool _canRenameElement(Element element) {
452-
var enclosingElement = element.enclosingElement3;
453-
if (element is ConstructorElement) {
455+
bool _canRenameElement(Element2 element) {
456+
var enclosingElement = element.enclosingElement2;
457+
if (element is ConstructorElement2) {
454458
return true;
455459
}
456-
if (element is LibraryImportElement) {
460+
if (element is MockLibraryImportElement) {
457461
return true;
458462
}
459-
if (element is LabelElement || element is LocalElement) {
463+
if (element is LabelElement2 || element is LocalElement2) {
460464
return true;
461465
}
462-
if (enclosingElement is InterfaceElement ||
463-
enclosingElement is ExtensionElement ||
464-
enclosingElement is CompilationUnitElement) {
466+
if (enclosingElement is InterfaceElement2 ||
467+
enclosingElement is ExtensionElement2 ||
468+
enclosingElement is LibraryElement2) {
465469
return true;
466470
}
467471
return false;

pkg/analysis_server/lib/src/services/kythe/kythe_visitors.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ import 'package:path/path.dart' show relative;
1313

1414
import 'schema.dart' as schema;
1515

16-
/// Given some [ConstructorElement], this method returns '<class-name>' as the
17-
/// name of the constructor, unless the constructor is a named constructor in
18-
/// which '<class-name>.<constructor-name>' is returned.
19-
String _computeConstructorElementName(ConstructorElement2 element) {
20-
var name = element.enclosingElement2.name3!;
21-
var constructorName = element.name3;
16+
/// Returns the name of the [constructor].
17+
///
18+
/// This is either '<class-name>' or '<class-name>.<constructor-name>',
19+
/// depending on whether the constructor is a named constructor.
20+
String _computeConstructorElementName(ConstructorElement2 constructor) {
21+
var name = constructor.enclosingElement2.name3!;
22+
var constructorName = constructor.name3;
2223
if (constructorName != null && constructorName != 'new') {
2324
name = '$name.$constructorName';
2425
}
@@ -131,8 +132,7 @@ class CiderKytheHelper {
131132
return 'kythe://$corpus?lang=dart?path=${vname.path}#${vname.signature}';
132133
}
133134

134-
/// Given some [Element] and Kythe node kind, this method generates and
135-
/// returns the [_KytheVName].
135+
/// Returns the Kythe name for the [element].
136136
_KytheVName _vNameFromElement(Element2? e, String nodeKind) {
137137
assert(nodeKind != schema.FILE_KIND);
138138
// general case
@@ -199,7 +199,7 @@ class _SignatureBuilder {
199199
} else {
200200
buffer.write(element.name3);
201201
}
202-
if (enclosingElt is ExecutableElement) {
202+
if (enclosingElt is ExecutableElement2) {
203203
buffer
204204
..write('@')
205205
..write(

0 commit comments

Comments
 (0)