Skip to content

Commit 1014753

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Fixes go to imports for annotations constructors
Fixes: #62258 Change-Id: Ia9c068ff7ea731953c91125aba92dd3e2f796184 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/468900 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Auto-Submit: Felipe Morschel <[email protected]>
1 parent ae81c0f commit 1014753

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

pkg/analysis_server/lib/src/lsp/handlers/custom/handler_imports.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ class ImportsHandler
8686
var enclosingElement = element.enclosingElement;
8787
if (enclosingElement is ExtensionElement) {
8888
element = enclosingElement;
89+
} else if (enclosingElement is InterfaceElement &&
90+
node.parent is Annotation) {
91+
// For annotations we need to reference the class element, not the
92+
// constructor element.
93+
element = enclosingElement;
8994
}
9095

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

pkg/analysis_server/test/lsp/import_test.dart

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,66 @@ class ImportTest extends AbstractLspAnalysisServerTest {
3030
return (uri: toUri(filePath), filePath: filePath, code: code);
3131
}
3232

33+
Future<void> test_annotationConstructor() async {
34+
newFile(join(projectFolderPath, 'lib', 'other.dart'), '''
35+
class A {
36+
const A();
37+
}
38+
''');
39+
await _verifyGoToImports(
40+
TestCode.parse('''
41+
[!import 'other.dart';!]
42+
43+
@A^()
44+
void foo() {}
45+
'''),
46+
);
47+
}
48+
49+
Future<void> test_annotationConstructor_prefixed() async {
50+
newFile(join(projectFolderPath, 'lib', 'other.dart'), '''
51+
class A {
52+
const A();
53+
}
54+
''');
55+
await _verifyGoToImports(
56+
TestCode.parse('''
57+
[!import 'other.dart' as p;!]
58+
59+
@p.A^()
60+
void foo() {}
61+
'''),
62+
);
63+
}
64+
65+
Future<void> test_annotationVariable() async {
66+
newFile(join(projectFolderPath, 'lib', 'other.dart'), '''
67+
const value = 42;
68+
''');
69+
await _verifyGoToImports(
70+
TestCode.parse('''
71+
[!import 'other.dart';!]
72+
73+
@value^
74+
void foo() {}
75+
'''),
76+
);
77+
}
78+
79+
Future<void> test_annotationVariable_prefixed() async {
80+
newFile(join(projectFolderPath, 'lib', 'other.dart'), '''
81+
const value = 42;
82+
''');
83+
await _verifyGoToImports(
84+
TestCode.parse('''
85+
[!import 'other.dart' as p;!]
86+
87+
@p.value^
88+
void foo() {}
89+
'''),
90+
);
91+
}
92+
3393
Future<void> test_constant() async {
3494
await _verifyGoToImports(
3595
TestCode.parse('''

0 commit comments

Comments
 (0)