Skip to content

Commit

Permalink
[DAS] Fixes for static declarations on Go to Imports command
Browse files Browse the repository at this point in the history
[email protected]

Fixes #59823

Change-Id: I63f56e770bc53f7fc0f03e1c31a5c2b6b2fb7d54
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/402540
Reviewed-by: Samuel Rawlins <[email protected]>
Commit-Queue: Phil Quitslund <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
Auto-Submit: Felipe Morschel <[email protected]>
  • Loading branch information
FMorschel authored and Commit Queue committed Jan 8, 2025
1 parent c033dd2 commit 26a2bf2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ class ImportsHandler
return success(null);
}

String? prefix;
String? prefixName;
if (node is NamedType) {
prefix = node.importPrefix?.name.lexeme;
prefixName = node.importPrefix?.name.lexeme;
} else if (node.thisOrAncestorOfType<PrefixedIdentifier>()
case PrefixedIdentifier identifier) {
prefix = identifier.prefix.name;
case PrefixedIdentifier(:var prefix) when prefix != node) {
prefixName = prefix.name;
} else if (node is SimpleIdentifier) {
if (node.parent case MethodInvocation(
target: SimpleIdentifier target?,
)) {
prefix = target.toString();
prefixName = target.toString();
}
}

Expand All @@ -88,7 +88,7 @@ class ImportsHandler
element = enclosingElement;
}

var locations = _getImportLocations(library, unit, element, prefix);
var locations = _getImportLocations(library, unit, element, prefixName);

return success(nullIfEmpty(locations));
});
Expand Down
58 changes: 44 additions & 14 deletions pkg/analysis_server/test/lsp/import_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void foo() {
}

Future<void> test_import_double() async {
newFile('/home/my_project/lib/other.dart', '''
newFile(join(projectFolderPath, 'lib', 'other.dart'), '''
export 'dart:math';
''');
await _verifyGoToImports(
Expand All @@ -93,10 +93,10 @@ Rando^m? r;
}

Future<void> test_import_double_ambiguous() async {
newFile('/home/my_project/lib/a1.dart', '''
newFile(join(projectFolderPath, 'lib', 'a1.dart'), '''
class A {}
''');
newFile('/home/my_project/lib/a2.dart', '''
newFile(join(projectFolderPath, 'lib', 'a2.dart'), '''
class A {}
''');
await _verifyGoToImports(
Expand All @@ -111,10 +111,10 @@ class A {}
}

Future<void> test_import_double_hide() async {
newFile('/home/my_project/lib/a1.dart', '''
newFile(join(projectFolderPath, 'lib', 'a1.dart'), '''
class A {}
''');
newFile('/home/my_project/lib/a2.dart', '''
newFile(join(projectFolderPath, 'lib', 'a2.dart'), '''
class A {}
''');
await _verifyGoToImports(
Expand All @@ -128,7 +128,7 @@ import 'a1.dart' hide A;
}

Future<void> test_import_double_same_different_alias() async {
newFile('/home/my_project/lib/other.dart', '''
newFile(join(projectFolderPath, 'lib', 'other.dart'), '''
export 'dart:math';
''');
await _verifyGoToImports(
Expand All @@ -142,7 +142,7 @@ other.Rando^m? r;
}

Future<void> test_import_double_same_different_alias_prefix() async {
newFile('/home/my_project/lib/other.dart', '''
newFile(join(projectFolderPath, 'lib', 'other.dart'), '''
export 'dart:math';
''');
await _verifyGoToImports(
Expand All @@ -156,12 +156,12 @@ other.Ran^dom? r;
}

Future<void> test_import_double_show() async {
newFile('/home/my_project/lib/a1.dart', '''
newFile(join(projectFolderPath, 'lib', 'a1.dart'), '''
class A {}
class B {}
''');
newFile('/home/my_project/lib/a2.dart', '''
newFile(join(projectFolderPath, 'lib', 'a2.dart'), '''
class A {}
''');
await _verifyGoToImports(
Expand All @@ -175,10 +175,10 @@ import 'a1.dart' show B;
}

Future<void> test_import_double_unambiguous_aliased() async {
newFile('/home/my_project/lib/a1.dart', '''
newFile(join(projectFolderPath, 'lib', 'a1.dart'), '''
class A {}
''');
newFile('/home/my_project/lib/a2.dart', '''
newFile(join(projectFolderPath, 'lib', 'a2.dart'), '''
class A {}
''');
await _verifyGoToImports(
Expand Down Expand Up @@ -371,7 +371,7 @@ math.Rando^m? r;
}

Future<void> test_import_single_exported() async {
newFile('/home/my_project/lib/other.dart', '''
newFile(join(projectFolderPath, 'lib', 'other.dart'), '''
export 'dart:math';
''');
await _verifyGoToImports(
Expand All @@ -397,7 +397,7 @@ class LocalClass {}
}

Future<void> test_nestedInvocations() async {
newFile('/home/my_project/lib/other.dart', '''
newFile(join(projectFolderPath, 'lib', 'other.dart'), '''
class A {
const A();
A foo() => A();
Expand All @@ -414,7 +414,7 @@ var a = A().foo().ba^r();
}

Future<void> test_nestedInvocations_extension() async {
newFile('/home/my_project/lib/other.dart', '''
newFile(join(projectFolderPath, 'lib', 'other.dart'), '''
extension E on int {
void bar() {}
}
Expand All @@ -428,6 +428,36 @@ var a = 1.abs().ba^r();
);
}

Future<void> test_staticDeclarations() async {
newFile(join(projectFolderPath, 'lib', 'other.dart'), '''
class A {
static const a = 1;
}
''');
await _verifyGoToImports(
TestCode.parse('''
[!import 'other.dart';!]
var a = A^.a;
'''),
);
}

Future<void> test_staticDeclarations_prefixed() async {
newFile(join(projectFolderPath, 'lib', 'other.dart'), '''
class A {
static const a = 1;
}
''');
await _verifyGoToImports(
TestCode.parse('''
[!import 'other.dart' as o;!]
var a = o.A^.a;
'''),
);
}

Future<void> _verifyGoToImports(
TestCode code, {
Uri? fileUri,
Expand Down

0 comments on commit 26a2bf2

Please sign in to comment.