Skip to content

Commit 31cd2bd

Browse files
DanTupcommit-bot@chromium.org
authored andcommitted
Ensure LSP completion filterText doesn't include '()' or '(…)'
Change-Id: I01c380ee3a36dbdd15706cb55b108899cacd9fc5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/112249 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Danny Tuppeny <[email protected]>
1 parent 0ce7cc5 commit 31cd2bd

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

pkg/analysis_server/lib/src/lsp/mapping.dart

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,12 @@ lsp.CompletionItem declarationToCompletionItem(
153153
int replacementOffset,
154154
int replacementLength,
155155
) {
156-
// Build display labels and text to insert. insertText may differ from label
157-
// if the label includes things like (…).
156+
// Build display labels and text to insert. insertText and filterText may
157+
// differ from label (for ex. if the label includes things like (…)). If
158+
// either are missing then label will be used by the client.
158159
String label;
159160
String insertText;
161+
String filterText;
160162
switch (declaration.kind) {
161163
case DeclarationKind.ENUM_CONSTANT:
162164
label = '${declaration.parent.name}.${declaration.name}';
@@ -167,11 +169,13 @@ lsp.CompletionItem declarationToCompletionItem(
167169
label += '.${declaration.name}';
168170
}
169171
insertText = label;
172+
filterText = label;
170173
label += declaration.parameterNames?.isNotEmpty ?? false ? '(…)' : '()';
171174
break;
172175
case DeclarationKind.FUNCTION:
173176
label = declaration.name;
174177
insertText = label;
178+
filterText = label;
175179
label += declaration.parameterNames?.isNotEmpty ?? false ? '(…)' : '()';
176180
break;
177181
default:
@@ -206,8 +210,8 @@ lsp.CompletionItem declarationToCompletionItem(
206210
// 10 -> 999990
207211
// 1 -> 999999
208212
(1000000 - itemRelevance).toString(),
209-
null, // filterText uses label if not set
210-
insertText, // insertText uses label if not set
213+
filterText != label ? filterText : null, // filterText uses label if not set
214+
insertText != label ? insertText : null, // insertText uses label if not set
211215
null, // insertTextFormat (we always use plain text so can ommit this)
212216
null, // textEdit - added on during resolve
213217
null, // additionalTextEdits, used for adding imports, etc.
@@ -564,11 +568,12 @@ lsp.CompletionItem toCompletionItem(
564568
int replacementOffset,
565569
int replacementLength,
566570
) {
567-
// Build display labels and text to insert. insertText may differ from label
568-
// if the label includes things like (…). If insertText is left as null then
569-
// label is used.
571+
// Build display labels and text to insert. insertText and filterText may
572+
// differ from label (for ex. if the label includes things like (…)). If
573+
// either are missing then label will be used by the client.
570574
String label;
571575
String insertText;
576+
String filterText;
572577
if (suggestion.displayText != null) {
573578
label = suggestion.displayText;
574579
insertText = suggestion.completion;
@@ -580,6 +585,7 @@ lsp.CompletionItem toCompletionItem(
580585
label = suggestion.completion;
581586
// Label is the insert text plus the parens to indicate it's callable.
582587
insertText = label;
588+
filterText = label;
583589
label += suggestion.parameterNames?.isNotEmpty ?? false ? '(…)' : '()';
584590
break;
585591
default:
@@ -613,7 +619,7 @@ lsp.CompletionItem toCompletionItem(
613619
// 10 -> 999990
614620
// 1 -> 999999
615621
(1000000 - suggestion.relevance).toString(),
616-
null, // filterText uses label if not set
622+
filterText != label ? filterText : null, // filterText uses label if not set
617623
insertText != label ? insertText : null, // insertText uses label if not set
618624
null, // insertTextFormat (we always use plain text so can ommit this)
619625
new lsp.TextEdit(

pkg/analysis_server/test/lsp/completion_test.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,29 @@ class CompletionTest extends AbstractLspAnalysisServerTest {
229229
final item = res.singleWhere((c) => c.label == 'abcdefghij');
230230
expect(item.insertTextFormat,
231231
anyOf(equals(InsertTextFormat.PlainText), isNull));
232-
// ignore: deprecated_member_use_from_same_package
233232
expect(item.insertText, anyOf(equals('abcdefghij'), isNull));
234233
final updated = applyTextEdits(withoutMarkers(content), [item.textEdit]);
235234
expect(updated, contains('a.abcdefghij'));
236235
}
237236

237+
test_parensNotInFilterTextInsertText() async {
238+
final content = '''
239+
class MyClass {}
240+
241+
main() {
242+
MyClass a = new MyCla^
243+
}
244+
''';
245+
246+
await initialize();
247+
await openFile(mainFileUri, withoutMarkers(content));
248+
final res = await getCompletion(mainFileUri, positionFromMarker(content));
249+
expect(res.any((c) => c.label == 'MyClass()'), isTrue);
250+
final item = res.singleWhere((c) => c.label == 'MyClass()');
251+
expect(item.filterText, equals('MyClass'));
252+
expect(item.insertText, equals('MyClass'));
253+
}
254+
238255
test_suggestionSets() async {
239256
newFile(
240257
join(projectFolderPath, 'other_file.dart'),
@@ -768,7 +785,6 @@ main() {
768785
final item = res.singleWhere((c) => c.label == 'abcdefghij');
769786
expect(item.insertTextFormat,
770787
anyOf(equals(InsertTextFormat.PlainText), isNull));
771-
// ignore: deprecated_member_use_from_same_package
772788
expect(item.insertText, anyOf(equals('abcdefghij'), isNull));
773789
final updated = applyTextEdits(withoutMarkers(content), [item.textEdit]);
774790
expect(updated, contains('a.abcdefghij'));

0 commit comments

Comments
 (0)