Skip to content

Commit 8abaa7b

Browse files
DanTupCommit Queue
authored and
Commit Queue
committed
[analysis_server] Fix finding references for the offset between a type name and generic type parameters
Fixes Dart-Code/Dart-Code#4668 Change-Id: Ibdb45b5c09ab8de0746239e351b5e581af4bb7df Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/317080 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 1a4c889 commit 8abaa7b

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ class ReferencesHandler
6464
ReferenceParams params,
6565
ResolvedUnitResult unit,
6666
OperationPerformanceImpl performance) async {
67-
final node = NodeLocator(offset).searchWithin(result.unit);
67+
var node = NodeLocator(offset).searchWithin(result.unit);
68+
node = _getReferenceTargetNode(node);
6869
var element = server.getElementOfNode(node);
6970
if (element == null) {
7071
return success(null);
@@ -104,4 +105,24 @@ class ReferencesHandler
104105

105106
return success(referenceResults);
106107
}
108+
109+
/// Gets the nearest node that should be used for finding references.
110+
///
111+
/// This is usually the same node but allows some adjustments such as
112+
/// considering the offset between a type name and type arguments as part
113+
/// of the type.
114+
AstNode? _getReferenceTargetNode(AstNode? node) {
115+
// Consider the angle brackets for type arguments part of the leading type,
116+
// otherwise we don't navigate in the common situation of having the type
117+
// name selected, where VS Code provides the end of the selection as the
118+
// position to search.
119+
//
120+
// In `A^<String>` node will be `TypeParameterList` and we will not find any
121+
// references.
122+
if (node is TypeParameterList) {
123+
node = node.parent;
124+
}
125+
126+
return node;
127+
}
107128
}

pkg/analysis_server/test/lsp/references_test.dart

+20
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,26 @@ f^oo() {
178178
await _checkRanges(content, includeDeclarations: false);
179179
}
180180

181+
Future<void> test_type() async {
182+
final content = '''
183+
class A^aa<T> {}
184+
185+
[!Aaa!]<String>? a;
186+
''';
187+
188+
await _checkRanges(content);
189+
}
190+
191+
Future<void> test_type_generic_end() async {
192+
final content = '''
193+
class Aaa^<T> {}
194+
195+
[!Aaa!]<String>? a;
196+
''';
197+
198+
await _checkRanges(content);
199+
}
200+
181201
Future<void> test_unopenFile() async {
182202
final code = TestCode.parse('''
183203
f^oo() {

0 commit comments

Comments
 (0)