Skip to content

Commit

Permalink
[analysis_server] Fix finding references for the offset between a typ…
Browse files Browse the repository at this point in the history
…e 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 <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
DanTup authored and Commit Queue committed Jul 31, 2023
1 parent 1a4c889 commit 8abaa7b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
23 changes: 22 additions & 1 deletion pkg/analysis_server/lib/src/lsp/handlers/handler_references.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ class ReferencesHandler
ReferenceParams params,
ResolvedUnitResult unit,
OperationPerformanceImpl performance) async {
final node = NodeLocator(offset).searchWithin(result.unit);
var node = NodeLocator(offset).searchWithin(result.unit);
node = _getReferenceTargetNode(node);
var element = server.getElementOfNode(node);
if (element == null) {
return success(null);
Expand Down Expand Up @@ -104,4 +105,24 @@ class ReferencesHandler

return success(referenceResults);
}

/// Gets the nearest node that should be used for finding references.
///
/// This is usually the same node but allows some adjustments such as
/// considering the offset between a type name and type arguments as part
/// of the type.
AstNode? _getReferenceTargetNode(AstNode? node) {
// Consider the angle brackets for type arguments part of the leading type,
// otherwise we don't navigate in the common situation of having the type
// name selected, where VS Code provides the end of the selection as the
// position to search.
//
// In `A^<String>` node will be `TypeParameterList` and we will not find any
// references.
if (node is TypeParameterList) {
node = node.parent;
}

return node;
}
}
20 changes: 20 additions & 0 deletions pkg/analysis_server/test/lsp/references_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,26 @@ f^oo() {
await _checkRanges(content, includeDeclarations: false);
}

Future<void> test_type() async {
final content = '''
class A^aa<T> {}
[!Aaa!]<String>? a;
''';

await _checkRanges(content);
}

Future<void> test_type_generic_end() async {
final content = '''
class Aaa^<T> {}
[!Aaa!]<String>? a;
''';

await _checkRanges(content);
}

Future<void> test_unopenFile() async {
final code = TestCode.parse('''
f^oo() {
Expand Down

0 comments on commit 8abaa7b

Please sign in to comment.