Skip to content

Commit 1372538

Browse files
FMorschelCommit Queue
authored and
Commit Queue
committed
[DAS] Fixes import prefix go to definition (multiple)
[email protected] Fixes #56931 Change-Id: Ida0cb58cdb4267af31a87a29862a3f4f2e28907f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/405201 Auto-Submit: Felipe Morschel <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Keerti Parthasarathy <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent 1cfbde8 commit 1372538

File tree

2 files changed

+125
-2
lines changed

2 files changed

+125
-2
lines changed

pkg/analysis_server/test/lsp/definition_test.dart

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ void [!^f!]() {}
119119
await testContents(contents);
120120
}
121121

122+
Future<void> test_atDeclaration_importPrefix() async {
123+
var contents = '''
124+
import 'dart:math' as [!^math!];
125+
''';
126+
127+
await testContents(contents);
128+
}
129+
122130
Future<void> test_atDeclaration_method() async {
123131
var contents = '''
124132
class A {
@@ -185,6 +193,15 @@ extension StringExtension on String {
185193
await testContents(contents);
186194
}
187195

196+
Future<void> test_comment_importPrefix() async {
197+
var contents = '''
198+
/// This is a comment for [^math]
199+
import 'dart:math' as [!math!];
200+
''';
201+
202+
await testContents(contents);
203+
}
204+
188205
Future<void> test_comment_instanceMember_qualified() async {
189206
var contents = '''
190207
/// [A.myFi^eld].
@@ -450,6 +467,91 @@ foo(Object pair) {
450467
await testContents(contents);
451468
}
452469

470+
Future<void> test_importPrefix() async {
471+
var contents = '''
472+
import 'dart:math' as [!math!];
473+
474+
^math.Random? r;
475+
''';
476+
477+
await testContents(contents);
478+
}
479+
480+
Future<void> test_importPrefix_multiple() async {
481+
setLocationLinkSupport();
482+
483+
var code = TestCode.parse('''
484+
import 'dart:math' as /*[0*/math/*0]*/;
485+
import 'dart:async' as /*[1*/math/*1]*/;
486+
487+
/*[2*/^math/*2]*/.Random? r;
488+
''');
489+
490+
await initialize();
491+
await openFile(mainFileUri, code.code);
492+
var res = await getDefinitionAsLocationLinks(
493+
mainFileUri,
494+
code.position.position,
495+
);
496+
497+
expect(res, hasLength(2));
498+
for (var (index, loc) in res.indexed) {
499+
expect(loc.originSelectionRange, equals(code.ranges.last.range));
500+
expect(loc.targetRange, equals(code.ranges[index].range));
501+
expect(loc.targetSelectionRange, equals(code.ranges[index].range));
502+
}
503+
}
504+
505+
Future<void> test_importPrefix_multiple_alone() async {
506+
var code = TestCode.parse('''
507+
import 'dart:math' as /*[0*/math/*0]*/;
508+
import 'dart:async' as /*[1*/math/*1]*/;
509+
510+
void foo() {
511+
// ignore: prefix_identifier_not_followed_by_dot
512+
/*[2*/^math/*2]*/;
513+
}
514+
''');
515+
516+
await initialize();
517+
await openFile(mainFileUri, code.code);
518+
var res = await getDefinitionAsLocation(
519+
mainFileUri,
520+
code.position.position,
521+
);
522+
523+
expect(res, hasLength(2));
524+
for (var (index, loc) in res.indexed) {
525+
expect(loc.range, equals(code.ranges[index].range));
526+
}
527+
}
528+
529+
Future<void> test_importPrefix_multiple_comment() async {
530+
setLocationLinkSupport();
531+
532+
var code = TestCode.parse('''
533+
import 'dart:math' as /*[0*/math/*0]*/;
534+
import 'dart:async' as /*[1*/math/*1]*/;
535+
536+
/// This is a comment that talks about [/*[2*/^math/*2]*/].
537+
math.Random? r;
538+
''');
539+
540+
await initialize();
541+
await openFile(mainFileUri, code.code);
542+
var res = await getDefinitionAsLocationLinks(
543+
mainFileUri,
544+
code.position.position,
545+
);
546+
547+
expect(res, hasLength(2));
548+
for (var (index, loc) in res.indexed) {
549+
expect(loc.originSelectionRange, equals(code.ranges.last.range));
550+
expect(loc.targetRange, equals(code.ranges[index].range));
551+
expect(loc.targetSelectionRange, equals(code.ranges[index].range));
552+
}
553+
}
554+
453555
Future<void> test_locationLink_class() async {
454556
setLocationLinkSupport();
455557

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ class _DartNavigationCollector {
136136
var offset = nodeOrToken.offset;
137137
var length = nodeOrToken.length;
138138

139+
_addRegionForFragmentRange(offset, length, fragment);
140+
}
141+
142+
void _addRegionForFragmentRange(
143+
int? offset, int? length, Fragment? fragment) {
144+
if (offset == null || length == null || fragment == null) return;
145+
139146
// If this fragment is for a synthetic element, use the first fragment for
140147
// the non-synthetic element.
141148
if (fragment.element.isSynthetic) {
@@ -424,7 +431,11 @@ class _DartNavigationComputerVisitor extends RecursiveAstVisitor<void> {
424431

425432
@override
426433
void visitImportPrefixReference(ImportPrefixReference node) {
427-
computer._addRegionForElement(node.name, node.element2);
434+
var element = node.element2;
435+
if (element == null) return;
436+
for (var fragment in element.fragments) {
437+
computer._addRegionForFragment(node.name, fragment);
438+
}
428439
}
429440

430441
@override
@@ -559,7 +570,17 @@ class _DartNavigationComputerVisitor extends RecursiveAstVisitor<void> {
559570
@override
560571
void visitSimpleIdentifier(SimpleIdentifier node) {
561572
var element = node.writeOrReadElement2;
562-
computer._addRegionForElement(node, element);
573+
if (element case PrefixElement2(:var fragments, :var name3)) {
574+
for (var fragment in fragments) {
575+
computer._addRegionForFragmentRange(
576+
node.offset,
577+
name3?.length,
578+
fragment,
579+
);
580+
}
581+
} else {
582+
computer._addRegionForElement(node, element);
583+
}
563584
}
564585

565586
@override

0 commit comments

Comments
 (0)