Skip to content

Commit 922ad1b

Browse files
DanTupCommit Queue
authored andcommitted
[analyzer/analysis_server] Migrate navigation to new element model
Change-Id: I5cb132de15e103c5fdba22e7033317ee2013f886 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/399106 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Ivan Inozemtsev <[email protected]>
1 parent 4c53dd6 commit 922ad1b

File tree

13 files changed

+296
-211
lines changed

13 files changed

+296
-211
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ lib/src/handler/legacy/edit_get_available_refactorings.dart
66
lib/src/handler/legacy/search_find_element_references.dart
77
lib/src/lsp/handlers/handler_completion.dart
88
lib/src/lsp/handlers/handler_completion_resolve.dart
9-
lib/src/lsp/handlers/handler_definition.dart
109
lib/src/lsp/handlers/handler_references.dart
1110
lib/src/lsp/handlers/handler_rename.dart
1211
lib/src/protocol_server.dart

pkg/analysis_server/lib/src/handler/legacy/analysis_get_navigation.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import 'package:analysis_server/src/request_handler_mixin.dart';
1313
import 'package:analyzer_plugin/protocol/protocol_common.dart';
1414
import 'package:analyzer_plugin/protocol/protocol_generated.dart' as plugin;
1515
import 'package:analyzer_plugin/src/utilities/navigation/navigation.dart';
16-
import 'package:analyzer_plugin/utilities/navigation/navigation_dart.dart';
16+
import 'package:analyzer_plugin/src/utilities/navigation/navigation_dart.dart';
1717

1818
/// The handler for the `analysis.getNavigation` request.
1919
class AnalysisGetNavigationHandler extends LegacyHandler

pkg/analysis_server/lib/src/legacy_analysis_server.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ import 'package:analyzer/src/utilities/cancellation.dart';
111111
import 'package:analyzer_plugin/protocol/protocol_common.dart' hide Element;
112112
import 'package:analyzer_plugin/src/utilities/client_uri_converter.dart';
113113
import 'package:analyzer_plugin/src/utilities/navigation/navigation.dart';
114-
import 'package:analyzer_plugin/utilities/navigation/navigation_dart.dart';
114+
import 'package:analyzer_plugin/src/utilities/navigation/navigation_dart.dart';
115115
import 'package:http/http.dart' as http;
116116
import 'package:meta/meta.dart';
117117
import 'package:telemetry/crash_reporting.dart';

pkg/analysis_server/lib/src/lsp/handlers/handler_definition.dart

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ import 'package:analysis_server/src/plugin/result_merger.dart';
1313
import 'package:analysis_server/src/protocol_server.dart' show NavigationTarget;
1414
import 'package:analyzer/dart/analysis/results.dart';
1515
import 'package:analyzer/dart/ast/ast.dart';
16-
import 'package:analyzer/dart/element/element.dart';
16+
import 'package:analyzer/dart/element/element2.dart';
1717
import 'package:analyzer/source/line_info.dart';
1818
import 'package:analyzer/src/dart/element/element.dart';
19-
import 'package:analyzer_plugin/protocol/protocol_common.dart' as protocol;
2019
import 'package:analyzer_plugin/protocol/protocol_generated.dart' as plugin;
2120
import 'package:analyzer_plugin/src/utilities/navigation/navigation.dart';
22-
import 'package:analyzer_plugin/utilities/analyzer_converter.dart';
23-
import 'package:analyzer_plugin/utilities/navigation/navigation_dart.dart';
21+
import 'package:analyzer_plugin/src/utilities/navigation/navigation_dart.dart';
2422

2523
typedef StaticOptions = Either2<bool, DefinitionOptions>;
2624

@@ -211,38 +209,42 @@ class DefinitionHandler
211209
return otherResults.isNotEmpty ? otherResults : results;
212210
}
213211

214-
/// Get the location of the code (excluding leading doc comments) for this element.
215-
Future<protocol.Location?> _getCodeLocation(Element element) async {
216-
Element? codeElement = element;
212+
/// Get the location of the code (excluding doc comments) for this fragment.
213+
Future<({int offset, int length})?> _getCodeLocation(
214+
Fragment fragment,
215+
) async {
216+
Fragment? codeFragment = fragment;
217217
// For synthetic getters created for fields, we need to access the associated
218218
// variable to get the codeOffset/codeLength.
219-
if (codeElement is PropertyAccessorElementImpl && codeElement.isSynthetic) {
220-
codeElement = codeElement.variable2!;
219+
if (codeFragment is PropertyAccessorFragment &&
220+
codeFragment.element.isSynthetic) {
221+
codeFragment = codeFragment.element.nonSynthetic2.firstFragment;
221222
}
222223

223224
// For extension types, the primary constructor has a range that covers only
224225
// the parameters / representation type but we want the whole declaration
225226
// for the code range because otherwise previews will just show `(int a)`
226227
// which is not what the user expects.
227-
if (codeElement.enclosingElement3 case ExtensionTypeElement enclosingElement
228-
when enclosingElement.primaryConstructor == codeElement) {
229-
codeElement = enclosingElement;
228+
if (codeFragment.element.enclosingElement2
229+
case ExtensionTypeElement2 enclosingElement
230+
when enclosingElement.primaryConstructor2 == codeFragment.element) {
231+
codeFragment = codeFragment.enclosingFragment;
230232
}
231233

232234
// Read the main codeOffset from the element. This may include doc comments
233235
// but will give the correct end position.
234236
int? codeOffset, codeLength;
235-
if (codeElement is ElementImpl) {
236-
codeOffset = codeElement.codeOffset;
237-
codeLength = codeElement.codeLength;
237+
if (codeFragment case ElementImpl codeFragment) {
238+
codeOffset = codeFragment.codeOffset;
239+
codeLength = codeFragment.codeLength;
238240
}
239241

240-
if (codeOffset == null || codeLength == null) {
242+
if (codeFragment == null || codeOffset == null || codeLength == null) {
241243
return null;
242244
}
243245

244246
// Read the declaration so we can get the offset after the doc comments.
245-
var declaration = await _parsedDeclaration(codeElement);
247+
var declaration = await _parsedDeclaration(codeFragment);
246248
var node = declaration?.node;
247249

248250
if (node is VariableDeclaration) {
@@ -264,11 +266,7 @@ class DefinitionHandler
264266
codeOffset = offsetAfterDocs;
265267
}
266268

267-
return AnalyzerConverter().locationFromElement(
268-
element,
269-
offset: codeOffset,
270-
length: codeLength,
271-
);
269+
return (offset: codeOffset, length: codeLength);
272270
}
273271

274272
Location? _toLocation(
@@ -308,7 +306,7 @@ class DefinitionHandler
308306
NavigationCollectorImpl collector,
309307
) async {
310308
for (var targetToUpdate in collector.targetsToUpdate) {
311-
var codeLocation = await _getCodeLocation(targetToUpdate.element);
309+
var codeLocation = await _getCodeLocation(targetToUpdate.fragment);
312310
if (codeLocation != null) {
313311
targetToUpdate.target
314312
..codeOffset = codeLocation.offset
@@ -318,14 +316,15 @@ class DefinitionHandler
318316
}
319317

320318
static Future<ElementDeclarationResult?> _parsedDeclaration(
321-
Element element,
319+
Fragment fragment,
322320
) async {
323-
var session = element.session;
321+
var session = fragment.element.session;
324322
if (session == null) {
325323
return null;
326324
}
327325

328-
var libraryPath = element.library?.source.fullName;
326+
var libraryPath =
327+
fragment.libraryFragment?.element.firstFragment.source.fullName;
329328
if (libraryPath == null) {
330329
return null;
331330
}
@@ -335,7 +334,7 @@ class DefinitionHandler
335334
return null;
336335
}
337336

338-
return parsedLibrary.getElementDeclaration(element);
337+
return parsedLibrary.getElementDeclaration2(fragment);
339338
}
340339
}
341340

pkg/analysis_server/test/analysis/get_navigation_test.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ final b = new Foo.named(); // 0
134134
expect(target.length, 5);
135135
}
136136

137-
Future<void> test_constructorInvocation_insideNullAwareElement_inList() async {
137+
Future<void>
138+
test_constructorInvocation_insideNullAwareElement_inList() async {
138139
addTestFile('''
139140
class Foo {
140141
Foo() {}
@@ -472,7 +473,7 @@ part of foo;
472473
assertHasRegionString('foo');
473474
expect(testTargets, hasLength(1));
474475
expect(testTargets[0].kind, ElementKind.LIBRARY);
475-
assertHasFileTarget(partOfFile.path, 8, 3); // library [[foo]]
476+
assertHasFileTarget(partOfFile.path, 0, 0);
476477
}
477478

478479
Future<void> test_partOfDirective_uri() async {

pkg/analysis_server/test/analysis/notification_navigation_test.dart

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,6 @@ class AbstractNavigationTest extends PubPackageAnalysisServerTest {
121121
assertHasFileTarget(dartCoreFile.path, offset, length);
122122
}
123123

124-
/// Validates that there is a target in [testTargets] with [testFile], at the
125-
/// offset of [str] in [testFile], and with the length of [str].
126-
void assertHasTargetString(String str) {
127-
assertHasTarget(str, length: str.length);
128-
}
129-
130124
/// Validates that there is not a region at [search] and with the given
131125
/// [length].
132126
void assertNoRegion(String search, int length) {
@@ -1476,7 +1470,7 @@ library my.lib;
14761470
''');
14771471
await prepareNavigation();
14781472
assertHasRegionString('my.lib');
1479-
assertHasTargetString('my.lib');
1473+
assertHasTarget('library', length: 0); // line 0, col 0 for unit targets.
14801474
}
14811475

14821476
Future<void>
@@ -1705,7 +1699,7 @@ void f() {
17051699
addTestFile('part of lib;');
17061700
await prepareNavigation();
17071701
assertHasRegionString('lib');
1708-
assertHasFileTarget(libFile, libCode.indexOf('lib;'), 'lib'.length);
1702+
assertHasFileTarget(libFile, 0, 0);
17091703
}
17101704

17111705
Future<void> test_propertyAccess_propertyName_read() async {
@@ -1788,7 +1782,7 @@ class A {
17881782
addTestFile('export "lib.dart";');
17891783
await prepareNavigation();
17901784
assertHasRegionString('"lib.dart"');
1791-
assertHasFileTarget(libFile, libCode.indexOf('lib;'), 'lib'.length);
1785+
assertHasFileTarget(libFile, 0, 0);
17921786
}
17931787

17941788
Future<void> test_string_export_unresolvedUri() async {
@@ -1803,7 +1797,7 @@ class A {
18031797
addTestFile('import "lib.dart";');
18041798
await prepareNavigation();
18051799
assertHasRegionString('"lib.dart"');
1806-
assertHasFileTarget(libFile, libCode.indexOf('lib;'), 'lib'.length);
1800+
assertHasFileTarget(libFile, 0, 0);
18071801
}
18081802

18091803
Future<void> test_string_import_noUri() async {

pkg/analyzer/lib/src/dart/ast/extensions.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ extension IndexExpressionExtension on IndexExpression {
278278
Element? get writeOrReadElement {
279279
return _writeElement(this) ?? staticElement;
280280
}
281+
282+
Element2? get writeOrReadElement2 {
283+
return _writeElement2(this) ?? element;
284+
}
281285
}
282286

283287
extension ListOfFormalParameterExtension on List<FormalParameter> {

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,15 @@ class ConstructorElementImpl2 extends ExecutableElementImpl2
16621662
@override
16631663
ElementKind get kind => ElementKind.CONSTRUCTOR;
16641664

1665+
@override
1666+
Element2 get nonSynthetic2 {
1667+
if (isSynthetic) {
1668+
return enclosingElement2;
1669+
} else {
1670+
return this;
1671+
}
1672+
}
1673+
16651674
@override
16661675
ConstructorElement2? get redirectedConstructor2 =>
16671676
(firstFragment.redirectedConstructor?.declaration
@@ -10120,6 +10129,21 @@ abstract class PropertyInducingElementImpl2 extends VariableElementImpl2
1012010129
return _fragments.any((f) => f.hasInitializer);
1012110130
}
1012210131

10132+
@override
10133+
Element2 get nonSynthetic2 {
10134+
if (isSynthetic) {
10135+
if (enclosingElement2 case EnumElementImpl2 enclosingElement2) {
10136+
// TODO(scheglov): remove 'index'?
10137+
if (name3 == 'index' || name3 == 'values') {
10138+
return enclosingElement2;
10139+
}
10140+
}
10141+
return (getter2 ?? setter2)!;
10142+
} else {
10143+
return this;
10144+
}
10145+
}
10146+
1012310147
List<PropertyInducingElementImpl> get _fragments;
1012410148
}
1012510149

pkg/analyzer_plugin/analyzer_use_new_elements.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ lib/src/utilities/completion/completion_target.dart
44
lib/src/utilities/completion/element_suggestion_builder.dart
55
lib/src/utilities/completion/optype.dart
66
lib/src/utilities/completion/suggestion_builder.dart
7-
lib/src/utilities/navigation/navigation.dart
7+
lib/src/utilities/navigation/navigation_dart.dart
88
lib/src/utilities/visitors/local_declaration_visitor.dart
99
lib/utilities/analyzer_converter.dart
1010
lib/utilities/change_builder/change_builder_dart.dart
1111
lib/utilities/completion/inherited_reference_contributor.dart
1212
lib/utilities/completion/suggestion_builder.dart
1313
lib/utilities/completion/type_member_contributor.dart
14-
lib/utilities/navigation/navigation.dart
15-
lib/utilities/navigation/navigation_dart.dart
1614
lib/utilities/range_factory.dart
1715
test/src/utilities/change_builder/change_builder_dart_test.dart
1816
test/src/utilities/change_builder/dart/import_library_element_test.dart

pkg/analyzer_plugin/lib/src/utilities/navigation/navigation.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/analysis/results.dart';
6-
import 'package:analyzer/dart/element/element.dart' as analyzer;
6+
import 'package:analyzer/dart/element/element2.dart' as analyzer;
77
import 'package:analyzer/file_system/file_system.dart';
88
import 'package:analyzer/source/source_range.dart';
99
import 'package:analyzer_plugin/protocol/protocol_common.dart';
@@ -58,19 +58,19 @@ class NavigationCollectorImpl implements NavigationCollector {
5858
@override
5959
void addRange(
6060
SourceRange range, ElementKind targetKind, Location targetLocation,
61-
{analyzer.Element? targetElement}) {
61+
{analyzer.Fragment? targetFragment}) {
6262
addRegion(range.offset, range.length, targetKind, targetLocation,
63-
targetElement: targetElement);
63+
targetFragment: targetFragment);
6464
}
6565

6666
@override
6767
void addRegion(
6868
int offset, int length, ElementKind targetKind, Location targetLocation,
69-
{analyzer.Element? targetElement}) {
69+
{analyzer.Fragment? targetFragment}) {
7070
var range = SourceRange(offset, length);
7171
// add new target
7272
var targets = regionMap.putIfAbsent(range, () => <int>[]);
73-
var targetIndex = _addTarget(targetKind, targetLocation, targetElement);
73+
var targetIndex = _addTarget(targetKind, targetLocation, targetFragment);
7474
targets.add(targetIndex);
7575
}
7676

@@ -95,7 +95,7 @@ class NavigationCollectorImpl implements NavigationCollector {
9595
}
9696

9797
int _addTarget(
98-
ElementKind kind, Location location, analyzer.Element? element) {
98+
ElementKind kind, Location location, analyzer.Fragment? fragment) {
9999
var pair = Pair<ElementKind, Location>(kind, location);
100100
var index = targetMap[pair];
101101
if (index == null) {
@@ -106,8 +106,8 @@ class NavigationCollectorImpl implements NavigationCollector {
106106
location.length, location.startLine, location.startColumn);
107107
targets.add(target);
108108
targetMap[pair] = index;
109-
if (element != null) {
110-
targetsToUpdate.add(TargetToUpdate(element, target));
109+
if (fragment != null) {
110+
targetsToUpdate.add(TargetToUpdate(fragment, target));
111111
}
112112
}
113113
return index;
@@ -118,8 +118,8 @@ class NavigationCollectorImpl implements NavigationCollector {
118118
///
119119
/// If code location feature is enabled, we update [target] using [element].
120120
class TargetToUpdate {
121-
final analyzer.Element element;
121+
final analyzer.Fragment fragment;
122122
final NavigationTarget target;
123123

124-
TargetToUpdate(this.element, this.target);
124+
TargetToUpdate(this.fragment, this.target);
125125
}

0 commit comments

Comments
 (0)