Skip to content

Commit c02b3a4

Browse files
bwilkersonCommit Queue
authored andcommitted
[Migration] occurrences_dart.dart
Change-Id: I55f821655ed5f2430e752748231bdf1a1c0279e0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/402840 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 2d23904 commit c02b3a4

File tree

4 files changed

+52
-46
lines changed

4 files changed

+52
-46
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
lib/plugin/protocol/protocol_dart.dart
22
lib/src/analysis_server.dart
33
lib/src/cider/rename.dart
4-
lib/src/domains/analysis/occurrences_dart.dart
54
lib/src/handler/legacy/edit_get_available_refactorings.dart
65
lib/src/handler/legacy/search_find_element_references.dart
76
lib/src/lsp/handlers/handler_completion.dart

pkg/analysis_server/lib/src/domains/analysis/occurrences_dart.dart

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@ import 'package:analysis_server/plugin/analysis/occurrences/occurrences_core.dar
66
import 'package:analysis_server/src/protocol_server.dart' as protocol;
77
import 'package:analyzer/dart/ast/ast.dart';
88
import 'package:analyzer/dart/ast/visitor.dart';
9-
import 'package:analyzer/dart/element/element.dart';
9+
import 'package:analyzer/dart/element/element2.dart';
1010
import 'package:analyzer/src/dart/ast/extensions.dart';
1111
import 'package:analyzer/src/dart/element/element.dart';
1212

1313
void addDartOccurrences(OccurrencesCollector collector, CompilationUnit unit) {
1414
var visitor = _DartUnitOccurrencesComputerVisitor();
1515
unit.accept(visitor);
1616
visitor.elementsOffsets.forEach((engineElement, offsets) {
17-
var length = engineElement.nameLength;
18-
var serverElement = protocol.convertElement(engineElement);
17+
var length = engineElement.name3?.length ?? 0;
18+
var serverElement = protocol.convertElement2(engineElement);
1919
var occurrences = protocol.Occurrences(serverElement, offsets, length);
2020
collector.addOccurrences(occurrences);
2121
});
2222
}
2323

2424
class _DartUnitOccurrencesComputerVisitor extends RecursiveAstVisitor<void> {
25-
final Map<Element, List<int>> elementsOffsets = <Element, List<int>>{};
25+
final Map<Element2, List<int>> elementsOffsets = {};
2626

2727
@override
2828
void visitAssignedVariablePattern(AssignedVariablePattern node) {
29-
var element = node.element;
29+
var element = node.element2;
3030
if (element != null) {
3131
_addOccurrence(element, node.name.offset);
3232
}
@@ -36,67 +36,67 @@ class _DartUnitOccurrencesComputerVisitor extends RecursiveAstVisitor<void> {
3636

3737
@override
3838
void visitClassDeclaration(ClassDeclaration node) {
39-
_addOccurrence(node.declaredElement!, node.name.offset);
39+
_addOccurrence(node.declaredFragment!.element, node.name.offset);
4040

4141
super.visitClassDeclaration(node);
4242
}
4343

4444
@override
4545
void visitClassTypeAlias(ClassTypeAlias node) {
46-
_addOccurrence(node.declaredElement!, node.name.offset);
46+
_addOccurrence(node.declaredFragment!.element, node.name.offset);
4747

4848
super.visitClassTypeAlias(node);
4949
}
5050

5151
@override
5252
void visitConstructorDeclaration(ConstructorDeclaration node) {
5353
if (node.name case var name?) {
54-
_addOccurrence(node.declaredElement!, name.offset);
54+
_addOccurrence(node.declaredFragment!.element, name.offset);
5555
}
5656

5757
super.visitConstructorDeclaration(node);
5858
}
5959

6060
@override
6161
void visitDeclaredIdentifier(DeclaredIdentifier node) {
62-
_addOccurrence(node.declaredElement!, node.name.offset);
62+
_addOccurrence(node.declaredFragment!.element, node.name.offset);
6363

6464
super.visitDeclaredIdentifier(node);
6565
}
6666

6767
@override
6868
void visitDeclaredVariablePattern(DeclaredVariablePattern node) {
69-
_addOccurrence(node.declaredElement!, node.name.offset);
69+
_addOccurrence(node.declaredElement2!, node.name.offset);
7070

7171
super.visitDeclaredVariablePattern(node);
7272
}
7373

7474
@override
7575
void visitEnumConstantDeclaration(EnumConstantDeclaration node) {
76-
_addOccurrence(node.declaredElement!, node.name.offset);
76+
_addOccurrence(node.declaredFragment!.element, node.name.offset);
7777

7878
super.visitEnumConstantDeclaration(node);
7979
}
8080

8181
@override
8282
void visitEnumDeclaration(EnumDeclaration node) {
83-
_addOccurrence(node.declaredElement!, node.name.offset);
83+
_addOccurrence(node.declaredFragment!.element, node.name.offset);
8484

8585
super.visitEnumDeclaration(node);
8686
}
8787

8888
@override
8989
void visitExtensionTypeDeclaration(ExtensionTypeDeclaration node) {
90-
_addOccurrence(node.declaredElement!, node.name.offset);
90+
_addOccurrence(node.declaredFragment!.element, node.name.offset);
9191

9292
super.visitExtensionTypeDeclaration(node);
9393
}
9494

9595
@override
9696
void visitFieldFormalParameter(FieldFormalParameter node) {
97-
var declaredElement = node.declaredElement;
98-
if (declaredElement is FieldFormalParameterElement) {
99-
var field = declaredElement.field;
97+
var declaredElement = node.declaredFragment?.element;
98+
if (declaredElement is FieldFormalParameterElement2) {
99+
var field = declaredElement.field2;
100100
if (field != null) {
101101
_addOccurrence(field, node.name.offset);
102102
}
@@ -107,40 +107,40 @@ class _DartUnitOccurrencesComputerVisitor extends RecursiveAstVisitor<void> {
107107

108108
@override
109109
void visitFunctionDeclaration(FunctionDeclaration node) {
110-
_addOccurrence(node.declaredElement!, node.name.offset);
110+
_addOccurrence(node.declaredFragment!.element, node.name.offset);
111111
super.visitFunctionDeclaration(node);
112112
}
113113

114114
@override
115115
void visitFunctionTypeAlias(FunctionTypeAlias node) {
116-
_addOccurrence(node.declaredElement!, node.name.offset);
116+
_addOccurrence(node.declaredFragment!.element, node.name.offset);
117117

118118
super.visitFunctionTypeAlias(node);
119119
}
120120

121121
@override
122122
void visitGenericTypeAlias(GenericTypeAlias node) {
123-
_addOccurrence(node.declaredElement!, node.name.offset);
123+
_addOccurrence(node.declaredFragment!.element, node.name.offset);
124124

125125
super.visitGenericTypeAlias(node);
126126
}
127127

128128
@override
129129
void visitMethodDeclaration(MethodDeclaration node) {
130-
_addOccurrence(node.declaredElement!, node.name.offset);
130+
_addOccurrence(node.declaredFragment!.element, node.name.offset);
131131
super.visitMethodDeclaration(node);
132132
}
133133

134134
@override
135135
void visitMixinDeclaration(MixinDeclaration node) {
136-
_addOccurrence(node.declaredElement!, node.name.offset);
136+
_addOccurrence(node.declaredFragment!.element, node.name.offset);
137137

138138
super.visitMixinDeclaration(node);
139139
}
140140

141141
@override
142142
void visitNamedType(NamedType node) {
143-
var element = node.element;
143+
var element = node.element2;
144144
if (element != null) {
145145
_addOccurrence(element, node.name2.offset);
146146
}
@@ -150,7 +150,7 @@ class _DartUnitOccurrencesComputerVisitor extends RecursiveAstVisitor<void> {
150150

151151
@override
152152
void visitPatternField(PatternField node) {
153-
var element = node.element;
153+
var element = node.element2;
154154
var pattern = node.pattern;
155155
// If no explicit field name, use the variables name.
156156
var name =
@@ -166,7 +166,10 @@ class _DartUnitOccurrencesComputerVisitor extends RecursiveAstVisitor<void> {
166166
@override
167167
void visitRepresentationDeclaration(RepresentationDeclaration node) {
168168
if (node.constructorName case var constructorName?) {
169-
_addOccurrence(node.constructorElement!, constructorName.name.offset);
169+
_addOccurrence(
170+
node.constructorFragment!.element,
171+
constructorName.name.offset,
172+
);
170173
}
171174

172175
super.visitRepresentationDeclaration(node);
@@ -176,15 +179,15 @@ class _DartUnitOccurrencesComputerVisitor extends RecursiveAstVisitor<void> {
176179
void visitSimpleFormalParameter(SimpleFormalParameter node) {
177180
var nameToken = node.name;
178181
if (nameToken != null) {
179-
_addOccurrence(node.declaredElement!, nameToken.offset);
182+
_addOccurrence(node.declaredFragment!.element, nameToken.offset);
180183
}
181184

182185
super.visitSimpleFormalParameter(node);
183186
}
184187

185188
@override
186189
void visitSimpleIdentifier(SimpleIdentifier node) {
187-
var element = node.writeOrReadElement;
190+
var element = node.writeOrReadElement2;
188191
if (element != null) {
189192
_addOccurrence(element, node.offset);
190193
}
@@ -193,17 +196,17 @@ class _DartUnitOccurrencesComputerVisitor extends RecursiveAstVisitor<void> {
193196

194197
@override
195198
void visitSuperFormalParameter(SuperFormalParameter node) {
196-
_addOccurrence(node.declaredElement!, node.name.offset);
199+
_addOccurrence(node.declaredFragment!.element, node.name.offset);
197200
super.visitSuperFormalParameter(node);
198201
}
199202

200203
@override
201204
void visitVariableDeclaration(VariableDeclaration node) {
202-
_addOccurrence(node.declaredElement!, node.name.offset);
205+
_addOccurrence(node.declaredFragment!.element, node.name.offset);
203206
super.visitVariableDeclaration(node);
204207
}
205208

206-
void _addOccurrence(Element element, int offset) {
209+
void _addOccurrence(Element2 element, int offset) {
207210
var canonicalElement = _canonicalizeElement(element);
208211
if (canonicalElement == null || element == DynamicElementImpl.instance) {
209212
return;
@@ -216,13 +219,13 @@ class _DartUnitOccurrencesComputerVisitor extends RecursiveAstVisitor<void> {
216219
offsets.add(offset);
217220
}
218221

219-
Element? _canonicalizeElement(Element element) {
220-
Element? canonicalElement = element;
221-
if (canonicalElement is FieldFormalParameterElement) {
222-
canonicalElement = canonicalElement.field;
223-
} else if (canonicalElement is PropertyAccessorElement) {
224-
canonicalElement = canonicalElement.variable2;
222+
Element2? _canonicalizeElement(Element2 element) {
223+
Element2? canonicalElement = element;
224+
if (canonicalElement is FieldFormalParameterElement2) {
225+
canonicalElement = canonicalElement.field2;
226+
} else if (canonicalElement is PropertyAccessorElement2) {
227+
canonicalElement = canonicalElement.variable3;
225228
}
226-
return canonicalElement?.declaration;
229+
return canonicalElement?.baseElement;
227230
}
228231
}

pkg/analysis_server/lib/src/protocol_server.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,15 @@ Location _locationForArgs(
477477
}
478478

479479
/// Creates a new [Location].
480-
Location _locationForArgs2(engine.Fragment fragment, engine.SourceRange range) {
481-
var lineInfo = fragment.libraryFragment!.lineInfo;
480+
Location? _locationForArgs2(
481+
engine.Fragment fragment,
482+
engine.SourceRange range,
483+
) {
484+
var libraryFragment = fragment.libraryFragment;
485+
if (libraryFragment == null) {
486+
return null;
487+
}
488+
var lineInfo = libraryFragment.lineInfo;
482489

483490
var startLocation = lineInfo.getLocation(range.offset);
484491
var endLocation = lineInfo.getLocation(range.end);

pkg/analysis_server/test/analysis/notification_occurrences_test.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -655,16 +655,13 @@ class /*[0*/A/*0]*/ {}
655655
}
656656

657657
Future<void> test_type_dynamic() async {
658-
addTestFile('''
658+
await assertOccurrences(kind: ElementKind.UNKNOWN, '''
659659
void f() {
660-
dynamic a = 1;
661-
dynamic b = 2;
660+
/*[0*/dynamic/*0]*/ a = 1;
661+
/*[1*/dynamic/*1]*/ b = 2;
662662
}
663-
dynamic V = 3;
663+
/*[2*/dynamic/*2]*/ V = 3;
664664
''');
665-
await prepareOccurrences();
666-
var offset = findOffset('dynamic a');
667-
findRegion(offset, 'dynamic'.length, exists: false);
668665
}
669666

670667
Future<void> test_type_void() async {

0 commit comments

Comments
 (0)