Java Code Examples for org.eclipse.lsp4j.CompletionItem#getAdditionalTextEdits()
The following examples show how to use
org.eclipse.lsp4j.CompletionItem#getAdditionalTextEdits() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XContentAssistService.java From n4js with Eclipse Public License 1.0 | 5 votes |
/** * Convert to a completion item. */ protected CompletionItem toCompletionItem(ContentAssistEntry entry, int caretOffset, Position caretPosition, Document document) { CompletionItem result = new CompletionItem(); String label = null; if (entry.getLabel() != null) { label = entry.getLabel(); } else { label = entry.getProposal(); } result.setLabel(label); result.setDetail(entry.getDescription()); result.setDocumentation(entry.getDocumentation()); String prefix = null; if (entry.getPrefix() != null) { prefix = entry.getPrefix(); } else { prefix = ""; } Position prefixPosition = document.getPosition(caretOffset - prefix.length()); result.setTextEdit(new TextEdit(new Range(prefixPosition, caretPosition), entry.getProposal())); if (!entry.getTextReplacements().isEmpty()) { if (result.getAdditionalTextEdits() == null) { result.setAdditionalTextEdits(new ArrayList<>(entry.getTextReplacements().size())); } entry.getTextReplacements().forEach(it -> { result.getAdditionalTextEdits().add(toTextEdit(it, document)); }); } if (entry.getKind().startsWith(ContentAssistEntry.KIND_SNIPPET + ":")) { result.setInsertTextFormat(InsertTextFormat.Snippet); entry.setKind(entry.getKind().substring(ContentAssistEntry.KIND_SNIPPET.length() + 1)); } else if (Objects.equal(entry.getKind(), ContentAssistEntry.KIND_SNIPPET)) { result.setInsertTextFormat(InsertTextFormat.Snippet); } result.setKind(translateKind(entry)); return result; }
Example 2
Source File: CompletionHandlerTest.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
@Test public void testCompletion_method_withLSPV2() throws JavaModelException{ mockLSP2Client(); ICompilationUnit unit = getWorkingCopy( "src/java/Foo.java", "public class Foo {\n"+ " void foo() {\n"+ "System.out.print(\"Hello\");\n" + "System.out.println(\" World!\");\n"+ "HashMap<String, String> map = new HashMap<>();\n"+ "map.pu\n" + " }\n"+ "}\n"); int[] loc = findCompletionLocation(unit, "map.pu"); 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().matches("put\\(String \\w+, String \\w+\\) : String")) .findFirst().orElse(null); assertNotNull(ci); assertEquals("put", ci.getInsertText()); assertEquals(CompletionItemKind.Method, ci.getKind()); assertEquals("999999019", ci.getSortText()); assertNotNull(ci.getTextEdit()); assertTextEdit(5, 4, 6, "put", ci.getTextEdit()); assertNotNull(ci.getAdditionalTextEdits()); List<TextEdit> edits = ci.getAdditionalTextEdits(); assertEquals(2, edits.size()); }
Example 3
Source File: CompletionHandlerTest.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
@Test public void testCompletion_method_withLSPV3() throws JavaModelException{ ICompilationUnit unit = getWorkingCopy( "src/java/Foo.java", "public class Foo {\n"+ " void foo() {\n"+ "System.out.print(\"Hello\");\n" + "System.out.println(\" World!\");\n"+ "HashMap<String, String> map = new HashMap<>();\n"+ "map.pu\n" + " }\n"+ "}\n"); int[] loc = findCompletionLocation(unit, "map.pu"); 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().matches("put\\(String \\w+, String \\w+\\) : String")) .findFirst().orElse(null); assertNotNull(ci); assertEquals("put", ci.getInsertText()); assertTrue(ci.getDetail().matches("java.util.HashMap.put\\(String \\w+, String \\w+\\) : String")); assertEquals(CompletionItemKind.Method, ci.getKind()); assertEquals("999999019", ci.getSortText()); assertNotNull(ci.getTextEdit()); try { assertTextEdit(5, 4, 6, "put(${1:key}, ${2:value})", ci.getTextEdit()); } catch (ComparisonFailure e) { //In case the JDK has no sources assertTextEdit(5, 4, 6, "put(${1:arg0}, ${2:arg1})", ci.getTextEdit()); } assertNotNull(ci.getAdditionalTextEdits()); List<TextEdit> edits = ci.getAdditionalTextEdits(); assertEquals(2, edits.size()); }
Example 4
Source File: CompletionHandlerTest.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
@Test public void testCompletion_resolveAdditionalTextEdits() throws Exception { ClientPreferences mockCapabilies = mock(ClientPreferences.class); when(mockCapabilies.isResolveAdditionalTextEditsSupport()).thenReturn(true); when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies); //@formatter:off ICompilationUnit unit = getWorkingCopy( "src/java/Foo.java", "public class Foo {\n"+ " void foo() {\n"+ " HashMa\n"+ " }\n"+ "}\n"); //@formatter:on int[] loc = findCompletionLocation(unit, "HashMa"); CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight(); assertNotNull(list); assertFalse("No proposals were found",list.getItems().isEmpty()); CompletionItem ci = list.getItems().get(0); assertNull(ci.getAdditionalTextEdits()); assertEquals("HashMap - java.util", ci.getLabel()); CompletionItem resolvedItem = server.resolveCompletionItem(ci).join(); List<TextEdit> additionalEdits = resolvedItem.getAdditionalTextEdits(); assertNotNull(additionalEdits); assertEquals(1, additionalEdits.size()); assertEquals("import java.util.HashMap;\n\n", additionalEdits.get(0).getNewText()); }
Example 5
Source File: ContentAssistService.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
protected CompletionItem toCompletionItem(ContentAssistEntry entry, int caretOffset, Position caretPosition, Document document) { CompletionItem completionItem = new CompletionItem(); String label = entry.getLabel(); if (label == null) { label = entry.getProposal(); } completionItem.setLabel(label); completionItem.setDetail(entry.getDescription()); completionItem.setDocumentation(entry.getDocumentation()); String prefix = entry.getPrefix(); if (prefix == null) { prefix = ""; } int prefixOffset = caretOffset - prefix.length(); Position prefixPosition = document.getPosition(prefixOffset); completionItem.setTextEdit(new TextEdit(new Range(prefixPosition, caretPosition), entry.getProposal())); completionItem.setKind(translateKind(entry)); if (!entry.getTextReplacements().isEmpty()) { if (completionItem.getAdditionalTextEdits() == null) { completionItem.setAdditionalTextEdits(new ArrayList<>(entry.getTextReplacements().size())); } entry.getTextReplacements().forEach( (ReplaceRegion it) -> completionItem.getAdditionalTextEdits().add(toTextEdit(it, document))); } if (ContentAssistEntry.KIND_SNIPPET.equals(entry.getKind())) { completionItem.setInsertTextFormat(InsertTextFormat.Snippet); } return completionItem; }
Example 6
Source File: EditorEventManager.java From lsp4intellij with Apache License 2.0 | 4 votes |
/** * Creates a LookupElement given a CompletionItem * * @param item The CompletionItem * @return The corresponding LookupElement */ @SuppressWarnings("WeakerAccess") public LookupElement createLookupItem(CompletionItem item) { Command command = item.getCommand(); String detail = item.getDetail(); String insertText = item.getInsertText(); CompletionItemKind kind = item.getKind(); String label = item.getLabel(); TextEdit textEdit = item.getTextEdit(); List<TextEdit> addTextEdits = item.getAdditionalTextEdits(); String presentableText = StringUtils.isNotEmpty(label) ? label : (insertText != null) ? insertText : ""; String tailText = (detail != null) ? detail : ""; LSPIconProvider iconProvider = GUIUtils.getIconProviderFor(wrapper.getServerDefinition()); Icon icon = iconProvider.getCompletionIcon(kind); LookupElementBuilder lookupElementBuilder; String lookupString = null; if (textEdit != null) { lookupString = textEdit.getNewText(); } else if (StringUtils.isNotEmpty(insertText)) { lookupString = insertText; } else if (StringUtils.isNotEmpty(label)) { lookupString = label; } if (StringUtils.isEmpty(lookupString)) { return null; } // Fixes IDEA internal assertion failure in windows. lookupString = lookupString.replace(DocumentUtils.WIN_SEPARATOR, DocumentUtils.LINUX_SEPARATOR); if (item.getInsertTextFormat() == InsertTextFormat.Snippet) { lookupElementBuilder = LookupElementBuilder.create(convertPlaceHolders(lookupString)); } else { lookupElementBuilder = LookupElementBuilder.create(lookupString); } lookupElementBuilder = addCompletionInsertHandlers(item, lookupElementBuilder, lookupString); if (kind == CompletionItemKind.Keyword) { lookupElementBuilder = lookupElementBuilder.withBoldness(true); } return lookupElementBuilder.withPresentableText(presentableText).withTypeText(tailText, true).withIcon(icon) .withAutoCompletionPolicy(AutoCompletionPolicy.SETTINGS_DEPENDENT); }
Example 7
Source File: CompletionHandlerTest.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
@Test public void testStarImports() throws Exception { List<String> favorites = new ArrayList<>(); favorites.add("java.lang.Math.*"); Preferences prefs = PreferenceManager.getPrefs(null); List<String> oldFavorites = Arrays.asList(prefs.getJavaCompletionFavoriteMembers()); int onDemandThreshold = prefs.getImportOnDemandThreshold(); int staticOnDemandThreshold = prefs.getStaticImportOnDemandThreshold(); prefs.setJavaCompletionFavoriteMembers(favorites); prefs.setImportOnDemandThreshold(2); prefs.setStaticImportOnDemandThreshold(2); long timeout = Long.getLong("completion.timeout", 5000); try { System.setProperty("completion.timeout", String.valueOf(60000)); ICompilationUnit unit = getWorkingCopy("src/test1/B.java", //@formatter:off "package test1;\n" + "import static java.lang.Math.sqrt;\n" + "import java.util.List;\n" + "public class B {\n" + " List<String> list = new ArrayL\n" + " public static void main(String[] args) {\n" + " double d1 = sqrt(4);\n" + " double d2 = abs\n" + " }\n" + "}\n"); //@formatter:on int[] loc = findCompletionLocation(unit, "new ArrayL"); CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight(); assertNotNull(list); assertTrue(list.getItems().size() > 0); CompletionItem item = list.getItems().stream().filter(i -> "ArrayList()".equals(i.getLabel())).collect(Collectors.toList()).get(0); assertNotNull(item); List<TextEdit> textEdits = item.getAdditionalTextEdits(); assertEquals(1, textEdits.size()); TextEdit textEdit = textEdits.get(0); assertEquals("\n\nimport java.util.*;", textEdit.getNewText()); loc = findCompletionLocation(unit, "= abs"); list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight(); assertNotNull(list); assertTrue(list.getItems().size() > 0); item = list.getItems().stream().filter(i -> i.getLabel().startsWith("abs(double")).collect(Collectors.toList()).get(0); assertNotNull(item); textEdits = item.getAdditionalTextEdits(); assertEquals(1, textEdits.size()); textEdit = textEdits.get(0); assertEquals("import static java.lang.Math.*;\n\n", textEdit.getNewText()); } finally { prefs.setJavaCompletionFavoriteMembers(oldFavorites); prefs.setImportOnDemandThreshold(onDemandThreshold); prefs.setStaticImportOnDemandThreshold(staticOnDemandThreshold); System.setProperty("completion.timeout", String.valueOf(timeout)); } }