Skip to content

Commit cc99ae9

Browse files
bwilkersonCommit Queue
authored andcommitted
Migrate convert_getter_to_method and convert_method_to_getter
Change-Id: I550022d206f0043789551280b35a1768557cfe61 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396625 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 9de2a62 commit cc99ae9

File tree

11 files changed

+144
-106
lines changed

11 files changed

+144
-106
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ lib/src/services/flutter/widget_descriptions.dart
4949
lib/src/services/kythe/kythe_visitors.dart
5050
lib/src/services/refactoring/agnostic/change_method_signature.dart
5151
lib/src/services/refactoring/framework/formal_parameter.dart
52-
lib/src/services/refactoring/legacy/convert_getter_to_method.dart
53-
lib/src/services/refactoring/legacy/convert_method_to_getter.dart
5452
lib/src/services/refactoring/legacy/extract_local.dart
5553
lib/src/services/refactoring/legacy/extract_method.dart
5654
lib/src/services/refactoring/legacy/extract_widget.dart

pkg/analysis_server/lib/src/protocol_server.dart

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ void doSourceChange_addElementEdit(
4444
doSourceChange_addSourceEdit(change, source, edit);
4545
}
4646

47+
/// Adds [edit] to the file containing the given [fragment].
48+
void doSourceChange_addFragmentEdit(
49+
SourceChange change,
50+
engine.Fragment fragment,
51+
SourceEdit edit,
52+
) {
53+
var source = fragment.libraryFragment!.source;
54+
doSourceChange_addSourceEdit(change, source, edit);
55+
}
56+
4757
/// Adds [edit] for the given [source] to the [change].
4858
void doSourceChange_addSourceEdit(
4959
SourceChange change,
@@ -302,15 +312,15 @@ Location? newLocation_fromElement2(engine.Element2? element) {
302312
if (element == null) {
303313
return null;
304314
}
305-
if (element is engine.FormalParameterElement &&
306-
element.enclosingElement2 == null) {
307-
return null;
308-
}
309-
var fragment = element.firstFragment;
310-
var offset = fragment.nameOffset2 ?? 0;
311-
var length = fragment.name2?.length ?? 0;
312-
var range = engine.SourceRange(offset, length);
313-
return _locationForArgs2(fragment, range);
315+
if (element is engine.FormalParameterElement &&
316+
element.enclosingElement2 == null) {
317+
return null;
318+
}
319+
var fragment = element.firstFragment;
320+
var offset = fragment.nameOffset2 ?? 0;
321+
var length = fragment.name2?.length ?? 0;
322+
var range = engine.SourceRange(offset, length);
323+
return _locationForArgs2(fragment, range);
314324
}
315325

316326
/// Create a Location based on an [engine.SearchMatch].

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

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'package:analysis_server/src/services/search/search_engine.dart';
1111
import 'package:analyzer/dart/analysis/session.dart';
1212
import 'package:analyzer/dart/ast/ast.dart';
1313
import 'package:analyzer/dart/ast/token.dart';
14-
import 'package:analyzer/dart/element/element.dart';
14+
import 'package:analyzer/dart/element/element2.dart';
1515
import 'package:analyzer/src/dart/analysis/session_helper.dart';
1616
import 'package:analyzer/src/util/file_paths.dart';
1717
import 'package:analyzer_plugin/utilities/range_factory.dart';
@@ -22,7 +22,7 @@ class ConvertGetterToMethodRefactoringImpl extends RefactoringImpl
2222
final RefactoringWorkspace workspace;
2323
final SearchEngine searchEngine;
2424
final AnalysisSession session;
25-
final PropertyAccessorElement element;
25+
final GetterElement element;
2626

2727
late SourceChange change;
2828

@@ -51,19 +51,19 @@ class ConvertGetterToMethodRefactoringImpl extends RefactoringImpl
5151
Future<SourceChange> createChange() async {
5252
change = SourceChange(refactoringName);
5353
// function
54-
if (element.enclosingElement3 is CompilationUnitElement) {
54+
if (element.enclosingElement2 is LibraryElement2) {
5555
await _updateElementDeclaration(element);
5656
await _updateElementReferences(element);
5757
}
5858
// method
59-
var field = element.variable2;
60-
if (field is FieldElement &&
61-
(field.enclosingElement3 is InterfaceElement ||
62-
field.enclosingElement3 is ExtensionElement)) {
63-
var elements = await getHierarchyMembers(searchEngine, field);
64-
await Future.forEach(elements, (Element member) async {
65-
if (member is FieldElement) {
66-
var getter = member.getter;
59+
var field = element.variable3;
60+
if (field is FieldElement2 &&
61+
(field.enclosingElement2 is InterfaceElement2 ||
62+
field.enclosingElement2 is ExtensionElement2)) {
63+
var elements = await getHierarchyMembers2(searchEngine, field);
64+
await Future.forEach(elements, (Element2 member) async {
65+
if (member is FieldElement2) {
66+
var getter = member.getter2;
6767
if (getter != null && !getter.isSynthetic) {
6868
await _updateElementDeclaration(getter);
6969
return _updateElementReferences(getter);
@@ -82,13 +82,13 @@ class ConvertGetterToMethodRefactoringImpl extends RefactoringImpl
8282

8383
/// Checks if [element] is valid to perform this refactor.
8484
RefactoringStatus _checkElement() {
85-
if (!workspace.containsElement(element)) {
85+
if (!workspace.containsElement2(element)) {
8686
return RefactoringStatus.fatal(
8787
'Only getters in your workspace can be converted.',
8888
);
8989
}
9090

91-
if (!element.isGetter || element.isSynthetic) {
91+
if (element.isSynthetic) {
9292
return RefactoringStatus.fatal(
9393
'Only explicit getters can be converted to methods.',
9494
);
@@ -100,14 +100,20 @@ class ConvertGetterToMethodRefactoringImpl extends RefactoringImpl
100100
return _checkElement();
101101
}
102102

103-
Future<void> _updateElementDeclaration(
104-
PropertyAccessorElement element,
105-
) async {
103+
Future<void> _updateElementDeclaration(GetterElement element) async {
106104
// prepare "get" keyword
107105
Token? getKeyword;
108-
{
106+
for (
107+
GetterFragment? fragment = element.firstFragment;
108+
fragment != null;
109+
fragment = fragment.nextFragment as GetterFragment?
110+
) {
111+
var nameRange = range.fragmentName(fragment);
112+
if (nameRange == null) {
113+
return;
114+
}
109115
var sessionHelper = AnalysisSessionHelper(session);
110-
var result = await sessionHelper.getElementDeclaration(element);
116+
var result = await sessionHelper.getElementDeclaration2(fragment);
111117
var declaration = result?.node;
112118
if (declaration is MethodDeclaration) {
113119
getKeyword = declaration.propertyKeyword;
@@ -116,34 +122,31 @@ class ConvertGetterToMethodRefactoringImpl extends RefactoringImpl
116122
} else {
117123
return;
118124
}
119-
}
120-
// remove "get "
121-
if (getKeyword != null) {
122-
var getRange = range.startOffsetEndOffset(
123-
getKeyword.offset,
124-
element.nameOffset,
125-
);
126-
var edit = newSourceEdit_range(getRange, '');
127-
doSourceChange_addElementEdit(change, element, edit);
128-
}
129-
// add parameters "()"
130-
{
131-
var edit = SourceEdit(range.elementName(element).end, 0, '()');
132-
doSourceChange_addElementEdit(change, element, edit);
125+
// remove "get "
126+
if (getKeyword != null) {
127+
var getRange = range.startOffsetEndOffset(
128+
getKeyword.offset,
129+
fragment.nameOffset2!,
130+
);
131+
var edit = newSourceEdit_range(getRange, '');
132+
doSourceChange_addFragmentEdit(change, fragment, edit);
133+
}
134+
// add parameters "()"
135+
var edit = SourceEdit(nameRange.end, 0, '()');
136+
doSourceChange_addFragmentEdit(change, fragment, edit);
133137
}
134138
}
135139

136-
Future<void> _updateElementReferences(Element element) async {
137-
var matches = await searchEngine.searchReferences(element);
140+
Future<void> _updateElementReferences(Element2 element) async {
141+
var matches = await searchEngine.searchReferences2(element);
138142
var references = getSourceReferences(matches);
139143
for (var reference in references) {
140144
// Don't update references in macro-generated files.
141145
if (isMacroGenerated(reference.file)) continue;
142-
var refElement = reference.element;
143146
var refRange = reference.range;
144147
// insert "()"
145148
var edit = SourceEdit(refRange.end, 0, '()');
146-
doSourceChange_addElementEdit(change, refElement, edit);
149+
doSourceChange_addSourceEdit(change, reference.unitSource, edit);
147150
}
148151
}
149152
}

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

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import 'package:analysis_server/src/services/search/hierarchy.dart';
1010
import 'package:analysis_server/src/services/search/search_engine.dart';
1111
import 'package:analyzer/dart/analysis/session.dart';
1212
import 'package:analyzer/dart/ast/ast.dart';
13-
import 'package:analyzer/dart/element/element.dart';
13+
import 'package:analyzer/dart/element/element2.dart';
1414
import 'package:analyzer/dart/element/type.dart';
1515
import 'package:analyzer/src/dart/analysis/session_helper.dart';
1616
import 'package:analyzer/src/dart/ast/utilities.dart';
@@ -23,7 +23,7 @@ class ConvertMethodToGetterRefactoringImpl extends RefactoringImpl
2323
final RefactoringWorkspace workspace;
2424
final SearchEngine searchEngine;
2525
final AnalysisSessionHelper sessionHelper;
26-
final ExecutableElement element;
26+
final ExecutableElement2 element;
2727

2828
late SourceChange change;
2929

@@ -53,15 +53,15 @@ class ConvertMethodToGetterRefactoringImpl extends RefactoringImpl
5353
change = SourceChange(refactoringName);
5454
// FunctionElement
5555
var element = this.element;
56-
if (element is FunctionElement) {
56+
if (element is TopLevelFunctionElement) {
5757
await _updateElementDeclaration(element);
5858
await _updateElementReferences(element);
5959
}
6060
// MethodElement
61-
if (element is MethodElement) {
62-
var elements = await getHierarchyMembers(searchEngine, element);
63-
await Future.forEach(elements, (Element element) async {
64-
await _updateElementDeclaration(element);
61+
if (element is MethodElement2) {
62+
var elements = await getHierarchyMembers2(searchEngine, element);
63+
await Future.forEach(elements, (Element2 element) async {
64+
await _updateElementDeclaration(element as ExecutableElement2);
6565
return _updateElementReferences(element);
6666
});
6767
}
@@ -76,22 +76,16 @@ class ConvertMethodToGetterRefactoringImpl extends RefactoringImpl
7676

7777
/// Checks if [element] is valid to perform this refactor.
7878
RefactoringStatus _checkElement() {
79-
if (!workspace.containsElement(element)) {
79+
if (!workspace.containsElement2(element)) {
8080
return RefactoringStatus.fatal(
8181
'Only methods in your workspace can be converted.',
8282
);
8383
}
8484

8585
// check Element type
86-
if (element is FunctionElement) {
87-
if (element.enclosingElement3 is! CompilationUnitElement) {
88-
return RefactoringStatus.fatal(
89-
'Only top-level functions can be converted to getters.',
90-
);
91-
}
92-
} else if (element is! MethodElement) {
86+
if (element is! MethodElement2 && element is! TopLevelFunctionElement) {
9387
return RefactoringStatus.fatal(
94-
'Only class methods or top-level functions can be converted to getters.',
88+
'Only methods or top-level functions can be converted to getters.',
9589
);
9690
}
9791
// returns a value
@@ -101,7 +95,7 @@ class ConvertMethodToGetterRefactoringImpl extends RefactoringImpl
10195
);
10296
}
10397
// no parameters
104-
if (element.parameters.isNotEmpty) {
98+
if (element.formalParameters.isNotEmpty) {
10599
return RefactoringStatus.fatal(
106100
'Only methods without parameters can be converted to getters.',
107101
);
@@ -110,11 +104,15 @@ class ConvertMethodToGetterRefactoringImpl extends RefactoringImpl
110104
return RefactoringStatus();
111105
}
112106

113-
Future<void> _updateElementDeclaration(Element element) async {
107+
Future<void> _updateElementDeclaration(ExecutableElement2 element) async {
114108
// prepare parameters
115109
FormalParameterList? parameters;
116-
{
117-
var result = await sessionHelper.getElementDeclaration(element);
110+
for (
111+
ExecutableFragment? fragment = element.firstFragment;
112+
fragment != null;
113+
fragment = fragment.nextFragment as GetterFragment?
114+
) {
115+
var result = await sessionHelper.getElementDeclaration2(fragment);
118116
var declaration = result?.node;
119117
if (declaration is MethodDeclaration) {
120118
parameters = declaration.parameters;
@@ -123,48 +121,48 @@ class ConvertMethodToGetterRefactoringImpl extends RefactoringImpl
123121
} else {
124122
return;
125123
}
126-
}
127-
if (parameters == null) {
128-
return;
129-
}
130-
// insert "get "
131-
{
132-
var edit = SourceEdit(element.nameOffset, 0, 'get ');
133-
doSourceChange_addElementEdit(change, element, edit);
134-
}
135-
// remove parameters
136-
{
137-
var edit = newSourceEdit_range(range.node(parameters), '');
138-
doSourceChange_addElementEdit(change, element, edit);
124+
if (parameters == null) {
125+
return;
126+
}
127+
// insert "get "
128+
{
129+
var edit = SourceEdit(fragment.nameOffset2 ?? -1, 0, 'get ');
130+
doSourceChange_addFragmentEdit(change, fragment, edit);
131+
}
132+
// remove parameters
133+
{
134+
var edit = newSourceEdit_range(range.node(parameters), '');
135+
doSourceChange_addFragmentEdit(change, fragment, edit);
136+
}
139137
}
140138
}
141139

142-
Future<void> _updateElementReferences(Element element) async {
143-
var matches = await searchEngine.searchReferences(element);
140+
Future<void> _updateElementReferences(Element2 element) async {
141+
var matches = await searchEngine.searchReferences2(element);
144142
var references = getSourceReferences(matches);
145143
for (var reference in references) {
146144
// Don't update references in macro-generated files.
147145
if (isMacroGenerated(reference.file)) continue;
148146

149-
var refElement = reference.element;
147+
var refElement = reference.element2;
150148
var refRange = reference.range;
151149
// prepare invocation
152-
MethodInvocation? invocation;
153-
{
154-
var resolvedUnit = await sessionHelper.getResolvedUnitByElement(
155-
refElement,
156-
);
157-
var refUnit = resolvedUnit?.unit;
158-
var refNode = NodeLocator(refRange.offset).searchWithin(refUnit);
159-
invocation = refNode?.thisOrAncestorOfType<MethodInvocation>();
160-
}
150+
151+
var resolvedUnit = await sessionHelper.getResolvedUnitByElement2(
152+
refElement,
153+
);
154+
var refUnit = resolvedUnit?.unit;
155+
if (refUnit == null) continue;
156+
var refNode = NodeLocator(refRange.offset).searchWithin(refUnit);
157+
var invocation = refNode?.thisOrAncestorOfType<MethodInvocation>();
158+
161159
// we need invocation
162160
if (invocation != null) {
163161
var edit = newSourceEdit_range(
164162
range.startOffsetEndOffset(refRange.end, invocation.end),
165163
'',
166164
);
167-
doSourceChange_addElementEdit(change, refElement, edit);
165+
doSourceChange_addSourceEdit(change, reference.unitSource, edit);
168166
}
169167
}
170168
}

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ abstract class ConvertGetterToMethodRefactoring implements Refactoring {
4646
AnalysisSession session,
4747
GetterElement element,
4848
) {
49-
return ConvertGetterToMethodRefactoringImpl(
50-
workspace,
51-
session,
52-
element.asElement as PropertyAccessorElement,
53-
);
49+
return ConvertGetterToMethodRefactoringImpl(workspace, session, element);
5450
}
5551

5652
/// Return `true` if refactoring is available, possibly without checking all
@@ -73,11 +69,7 @@ abstract class ConvertMethodToGetterRefactoring implements Refactoring {
7369
AnalysisSession session,
7470
ExecutableElement2 element,
7571
) {
76-
return ConvertMethodToGetterRefactoringImpl(
77-
workspace,
78-
session,
79-
element.asElement,
80-
);
72+
return ConvertMethodToGetterRefactoringImpl(workspace, session, element);
8173
}
8274

8375
/// Return `true` if refactoring is available, possibly without checking all
@@ -410,6 +402,11 @@ class RefactoringWorkspace {
410402
return containsFile(element.source!.fullName);
411403
}
412404

405+
/// Whether the [element] is defined in a file that is in a context root.
406+
bool containsElement2(Element2 element) {
407+
return containsFile(element.firstFragment.libraryFragment!.source.fullName);
408+
}
409+
413410
/// Whether the file with the given [path] is in a context root.
414411
bool containsFile(String path) {
415412
return drivers.any((driver) {

0 commit comments

Comments
 (0)