From b963827ecf074c16cbf7cacd4c1e692a281b313a Mon Sep 17 00:00:00 2001 From: Yan Zhang Date: Mon, 29 Jun 2020 23:17:33 +0800 Subject: [PATCH] use placeholder only when client supports snippet Signed-off-by: Yan Zhang --- .../AnonymousTypeCompletionProposal.java | 4 +-- .../handlers/CompletionHandlerTest.java | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/contentassist/AnonymousTypeCompletionProposal.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/contentassist/AnonymousTypeCompletionProposal.java index fd2f8503d8..cc8fbc2cab 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/contentassist/AnonymousTypeCompletionProposal.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/contentassist/AnonymousTypeCompletionProposal.java @@ -93,9 +93,9 @@ public AnonymousTypeCompletionProposal(ICompilationUnit cu, int replacementOffse public String updateReplacementString(IDocument document, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException { // Construct empty body for performance concern // See https://github.com/microsoft/language-server-protocol/issues/1032#issuecomment-648748013 - String newBody = "{\n\t${0}\n}"; + String newBody = fSnippetSupport ? "{\n\t${0}\n}" : "{\n\n}"; - StringBuffer buf = new StringBuffer("new A()"); //$NON-NLS-1$ + StringBuilder buf = new StringBuilder("new A()"); //$NON-NLS-1$ buf.append(newBody); // use the code formatter String lineDelim = TextUtilities.getDefaultLineDelimiter(document); diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/CompletionHandlerTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/CompletionHandlerTest.java index 387bbd12fa..3f3523664c 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/CompletionHandlerTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/CompletionHandlerTest.java @@ -2105,6 +2105,37 @@ public void testCompletion_AnonymousDeclarationType5() throws Exception { "}", ci.getTextEdit()); } + @Test + public void testCompletion_AnonymousDeclarationType_noSnippet() throws Exception { + ClientPreferences mockCapabilies = mock(ClientPreferences.class); + when(mockCapabilies.isCompletionSnippetsSupported()).thenReturn(false); + when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies); + + ICompilationUnit unit = getWorkingCopy( + "src/java/Foo.java", + "public class Foo {\n"+ + " public static void main(String[] args) {\n" + + " new Runnable()\n" + + " }\n" + + "}\n"); + waitForBackgroundJobs(); + int[] loc = findCompletionLocation(unit, "Runnable("); + CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight(); + assertNotNull(list); + CompletionItem ci = list.getItems().stream() + .filter(item -> item.getLabel().startsWith("Runnable() Anonymous Inner Type")) + .findFirst().orElse(null); + assertNotNull(ci); + + assertEquals("Runnable", ci.getInsertText()); + assertEquals(CompletionItemKind.Class, ci.getKind()); + assertEquals("999999372", ci.getSortText()); + assertNotNull(ci.getTextEdit()); + assertTextEdit(2, 20, 22, "() {\n" + + "\n" + + "}", ci.getTextEdit()); + } + @Test public void testCompletion_type() throws Exception { ICompilationUnit unit = getWorkingCopy(