Skip to content

Commit 2f2efeb

Browse files
DanTupcommit-bot@chromium.org
authored andcommitted
Filter LSP definitions to not include variable names
Fixes Dart-Code/Dart-Code#2535. Change-Id: I2e64d0cd53abcde4ac3ecae1682b6b054b16407a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/150980 Commit-Queue: Danny Tuppeny <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 7b12c8e commit 2f2efeb

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,21 @@ class DefinitionHandler
9292
return navigationTargetToLocation(targetFilePath, target, lineInfo);
9393
}
9494

95-
return success(convert(mergedTargets, toLocation).toList());
95+
final results = convert(mergedTargets, toLocation).toList();
96+
97+
// If we fetch navigation on a keyword like `var`, the results will include
98+
// both the definition and also the variable name. This will cause the editor
99+
// to show the user both options unnecessarily (the variable name is always
100+
// adjacent to the var keyword, so providing navigation to it is not useful).
101+
// To prevent this, filter the list to only those on different lines (or
102+
// different files).
103+
final otherResults = results
104+
.where((element) =>
105+
element.uri != params.textDocument.uri ||
106+
element.range.start.line != pos.line)
107+
.toList();
108+
109+
return success(otherResults.isNotEmpty ? otherResults : results);
96110
});
97111
});
98112
}

pkg/analysis_server/test/lsp/definition_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ class DefinitionTest extends AbstractLspAnalysisServerTest {
8686
expect(res, isEmpty);
8787
}
8888

89+
Future<void> test_sameLine() async {
90+
final contents = '''
91+
int plusOne(int [[value]]) => 1 + val^ue;
92+
''';
93+
94+
await initialize();
95+
await openFile(mainFileUri, withoutMarkers(contents));
96+
final res = await getDefinition(mainFileUri, positionFromMarker(contents));
97+
98+
expect(res, hasLength(1));
99+
var loc = res.single;
100+
expect(loc.range, equals(rangeFromMarkers(contents)));
101+
expect(loc.uri, equals(mainFileUri.toString()));
102+
}
103+
89104
Future<void> test_singleFile() async {
90105
final contents = '''
91106
[[foo]]() {
@@ -119,4 +134,21 @@ class DefinitionTest extends AbstractLspAnalysisServerTest {
119134
expect(loc.range, equals(rangeFromMarkers(contents)));
120135
expect(loc.uri, equals(mainFileUri.toString()));
121136
}
137+
138+
Future<void> test_varKeyword() async {
139+
final contents = '''
140+
va^r a = MyClass();
141+
142+
class [[MyClass]] {}
143+
''';
144+
145+
await initialize();
146+
await openFile(mainFileUri, withoutMarkers(contents));
147+
final res = await getDefinition(mainFileUri, positionFromMarker(contents));
148+
149+
expect(res, hasLength(1));
150+
var loc = res.single;
151+
expect(loc.range, equals(rangeFromMarkers(contents)));
152+
expect(loc.uri, equals(mainFileUri.toString()));
153+
}
122154
}

0 commit comments

Comments
 (0)