Skip to content

Commit

Permalink
Hide completions when signature help is enabled
Browse files Browse the repository at this point in the history
Signed-off-by: Frederik Claus <[email protected]>
  • Loading branch information
fvclaus committed Sep 24, 2021
1 parent bae0b2b commit 9209c3f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ public List<CompletionItem> getCompletionItems() {
return completionItems;
}

private static Set<Integer> PREFER_SIGNATURE_HELP_COMPLETION_ITEMS = Set.of(CompletionProposal.METHOD_REF, CompletionProposal.CONSTRUCTOR_INVOCATION, CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION);

public CompletionItem toCompletionItem(CompletionProposal proposal, int index) {
final CompletionItem $ = new CompletionItem();
$.setKind(mapKind(proposal));
Expand All @@ -232,7 +234,12 @@ public CompletionItem toCompletionItem(CompletionProposal proposal, int index) {
proposalProvider.updateReplacement(proposal, $, '\0');
// Make sure `filterText` matches `textEdit`
// See https://github.com/eclipse/eclipse.jdt.ls/issues/1348
if ($.getTextEdit() != null) {
boolean isSignatureHelpEnabled = preferenceManager.getPreferences().isSignatureHelpEnabled();
// Hide completions when signature help is visible.
if (isSignatureHelpEnabled && PREFER_SIGNATURE_HELP_COMPLETION_ITEMS.contains(proposal.getKind())) {
$.setFilterText(new String(proposal.getName()));
}
if ($.getFilterText() == null && $.getTextEdit() != null) {
String newText = $.getTextEdit().isLeft() ? $.getTextEdit().getLeft().getNewText() : $.getTextEdit().getRight().getNewText();
Range range = $.getTextEdit().isLeft() ? $.getTextEdit().getLeft().getRange() : ($.getTextEdit().getRight().getInsert() != null ? $.getTextEdit().getRight().getInsert() : $.getTextEdit().getRight().getReplace());
if (proposal.getKind() == CompletionProposal.TYPE_REF && range != null && newText != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,23 @@ public void testCompletion_constructor() throws Exception{
assertEquals(18, range.getEnd().getCharacter());
}

@Test
public void testCompletion_constructor_withParameterHelpEnabled() throws JavaModelException {
enableSignatureHelp();

ICompilationUnit unit = getWorkingCopy(
"src/java/Foo.java",
"public class MyUniqueClassName {\n"+
" void foo() {\n"+
" new MyUniqueClass\n"+
" }\n"+
"}\n");


CompletionList list = requestCompletions(unit, "new MyUniqueClass");
assertEquals("MyUniqueClassName", list.getItems().get(0).getFilterText());
}


@Test
public void testCompletion_import_package() throws JavaModelException{
Expand Down Expand Up @@ -670,6 +687,21 @@ public void testCompletion_method_withLSPV3() throws JavaModelException{
assertEquals(2, edits.size());
}

@Test
public void testCompletion_method_withSignatureHelpEnabled() throws JavaModelException {
enableSignatureHelp();
ICompilationUnit unit = getWorkingCopy(
"src/java/Foo.java",
"public class Foo {\n"+
" void foo(String parameter) {\n"+
"parameter.lengt\n" +
" }\n"+
"}\n");

CompletionList list = requestCompletions(unit, "parameter.lengt");
assertEquals("put", list.getItems().get(0).getFilterText());
}

@Test
public void testCompletion_method_guessMethodArgumentsFalse() throws JavaModelException {
testCompletion_method_guessMethodArguments(false, "test(${1:name}, ${2:i});");
Expand Down Expand Up @@ -3124,4 +3156,10 @@ private void mockLSPClient(boolean isSnippetSupported, boolean isSignatureHelpSu
when(mockCapabilies.isSignatureHelpSupported()).thenReturn(isSignatureHelpSuported);
when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies);
}

private void enableSignatureHelp() {
Preferences preferencesSpy = Mockito.spy(preferences);
when(preferencesSpy.isSignatureHelpEnabled()).thenReturn(true);
when(preferenceManager.getPreferences()).thenReturn(preferencesSpy);
}
}

0 comments on commit 9209c3f

Please sign in to comment.