Java Code Examples for org.eclipse.lsp4j.TextEdit#getRange()
The following examples show how to use
org.eclipse.lsp4j.TextEdit#getRange() .
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: LSIncompleteCompletionProposal.java From intellij-quarkus with Eclipse Public License 2.0 | 6 votes |
private int computeNewOffset(List<TextEdit> additionalTextEdits, int insertionOffset, Document doc) { if (additionalTextEdits != null && !additionalTextEdits.isEmpty()) { int adjustment = 0; for (TextEdit edit : additionalTextEdits) { try { Range rng = edit.getRange(); int start = LSPIJUtils.toOffset(rng.getStart(), doc); if (start <= insertionOffset) { int end = LSPIJUtils.toOffset(rng.getEnd(), doc); int orgLen = end - start; int newLeng = edit.getNewText().length(); int editChange = newLeng - orgLen; adjustment += editChange; } } catch (RuntimeException e) { LOGGER.warn(e.getLocalizedMessage(), e); } } return insertionOffset + adjustment; } return insertionOffset; }
Example 2
Source File: CamelCompletionInsertAndReplaceTest.java From camel-language-server with Apache License 2.0 | 6 votes |
@Test void testComponent() throws Exception { CamelLanguageServer camelLanguageServer = initializeLanguageServer("<from uri=\"ahc-wss:httpUri?binding=#true&bufferSize=10&synchronous=true\" xmlns=\"http://camel.apache.org/schema/blueprint\"/>\n", ".xml"); Position positionInMiddleOfcomponentPart = new Position(0, 15); CompletableFuture<Either<List<CompletionItem>, CompletionList>> completions = getCompletionFor(camelLanguageServer, positionInMiddleOfcomponentPart); List<CompletionItem> items = completions.get().getLeft(); assertThat(items).hasSize(2); for (CompletionItem completionItem : items) { TextEdit textEdit = completionItem.getTextEdit(); Range range = textEdit.getRange(); assertThat(range.getStart().getLine()).isZero(); assertThat(range.getStart().getCharacter()).isEqualTo(11 /*start of URI */); assertThat(range.getEnd().getLine()).isZero(); assertThat(range.getEnd().getCharacter()).isEqualTo(26 /* just before the '?' */); } }
Example 3
Source File: CamelCompletionInsertAndReplaceTest.java From camel-language-server with Apache License 2.0 | 6 votes |
@Test void testAttribute() throws Exception { CamelLanguageServer camelLanguageServer = initializeLanguageServer("<from uri=\"ahc-wss:httpUri?binding=#true&bufferSize=10&synchronous=true\" xmlns=\"http://camel.apache.org/schema/blueprint\"/>\n", ".xml"); Position positionBeforeBufferSizeAttribute = new Position(0, 45); CompletableFuture<Either<List<CompletionItem>, CompletionList>> completions = getCompletionFor(camelLanguageServer, positionBeforeBufferSizeAttribute); List<CompletionItem> items = completions.get().getLeft(); assertThat(items).hasSize(14); for (CompletionItem completionItem : items) { TextEdit textEdit = completionItem.getTextEdit(); Range range = textEdit.getRange(); assertThat(range.getStart().getLine()).isZero(); assertThat(range.getStart().getCharacter()).isEqualTo(45 /* just before 'bufferSize' */); assertThat(range.getEnd().getLine()).isZero(); assertThat(range.getEnd().getCharacter()).isEqualTo(55 /* end of 'bufferSize' */); } }
Example 4
Source File: XDocument.java From n4js with Eclipse Public License 1.0 | 6 votes |
/** * Only use for testing. * * All positions in the {@link TextEdit}s refer to the same original document (this). */ @Override public XDocument applyChanges(Iterable<? extends TextEdit> changes) { List<? extends TextEdit> changesSorted = IterableExtensions.sortWith(changes, textEditComparator); String newContent = contents; for (TextEdit change : changesSorted) { if (change.getRange() == null) { newContent = change.getNewText(); } else { int start = getOffSet(change.getRange().getStart()); int end = getOffSet(change.getRange().getEnd()); newContent = newContent.substring(0, start) + change.getNewText() + newContent.substring(end); } } XDocument document = new XDocument(version + 1, newContent, true, true); return document; }
Example 5
Source File: TextEditUtil.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
public static String apply(Document doc, Collection<? extends TextEdit> edits) throws BadLocationException { Assert.isNotNull(doc); Assert.isNotNull(edits); List<TextEdit> sortedEdits = new ArrayList<>(edits); sortByLastEdit(sortedEdits); String text = doc.get(); for (int i = sortedEdits.size() - 1; i >= 0; i--) { TextEdit te = sortedEdits.get(i); Range r = te.getRange(); if (r != null && r.getStart() != null && r.getEnd() != null) { int start = getOffset(doc, r.getStart()); int end = getOffset(doc, r.getEnd()); text = text.substring(0, start) + te.getNewText() + text.substring(end, text.length()); } } return text; }
Example 6
Source File: Document.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
/** * Only use for testing. * * All positions in the {@link TextEdit}s refer to the same original document (this). */ public Document applyChanges(Iterable<? extends TextEdit> changes) { String newContent = contents; for (TextEdit change : changes) { if (change.getRange() == null) { newContent = change.getNewText(); } else { int start = getOffSet(change.getRange().getStart()); int end = getOffSet(change.getRange().getEnd()); newContent = newContent.substring(0, start) + change.getNewText() + newContent.substring(end); } } Integer newVersion = null; if (version != null) { newVersion = Integer.valueOf(version.intValue() + 1); } return new Document(newVersion, newContent); }
Example 7
Source File: UnknownPropertyQuickfixTest.java From camel-language-server with Apache License 2.0 | 5 votes |
private void checkRetrievedCodeAction(TextDocumentIdentifier textDocumentIdentifier, Diagnostic diagnostic, CompletableFuture<List<Either<Command, CodeAction>>> codeActions) throws InterruptedException, ExecutionException { assertThat(codeActions.get()).hasSize(1); CodeAction codeAction = codeActions.get().get(0).getRight(); assertThat(codeAction.getDiagnostics()).containsOnly(diagnostic); assertThat(codeAction.getKind()).isEqualTo(CodeActionKind.QuickFix); List<TextEdit> createdChanges = codeAction.getEdit().getChanges().get(textDocumentIdentifier.getUri()); assertThat(createdChanges).isNotEmpty(); TextEdit textEdit = createdChanges.get(0); Range range = textEdit.getRange(); new RangeChecker().check(range, 9, 33, 9, 37); assertThat(textEdit.getNewText()).isEqualTo("delay"); }
Example 8
Source File: InvalidEnumQuickfixTest.java From camel-language-server with Apache License 2.0 | 5 votes |
private void checkRetrievedCodeAction(TextDocumentIdentifier textDocumentIdentifier, Diagnostic diagnostic, CompletableFuture<List<Either<Command, CodeAction>>> codeActions) throws InterruptedException, ExecutionException { assertThat(codeActions.get()).hasSize(1); CodeAction codeAction = codeActions.get().get(0).getRight(); assertThat(codeAction.getDiagnostics()).containsOnly(diagnostic); assertThat(codeAction.getKind()).isEqualTo(CodeActionKind.QuickFix); List<TextEdit> createdChanges = codeAction.getEdit().getChanges().get(textDocumentIdentifier.getUri()); assertThat(createdChanges).isNotEmpty(); TextEdit textEdit = createdChanges.get(0); Range range = textEdit.getRange(); new RangeChecker().check(range, 9, 49, 9, 54); assertThat(textEdit.getNewText()).isEqualTo("InOnly"); }
Example 9
Source File: CompletionHandlerTest.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
@Test public void testCompletion_constructor() throws Exception{ ICompilationUnit unit = getWorkingCopy( "src/java/Foo.java", "public class Foo {\n"+ " void foo() {\n"+ " Object o = new O\n"+ " }\n"+ "}\n"); int[] loc = findCompletionLocation(unit, "new O"); CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight(); assertNotNull(list); assertFalse("No proposals were found",list.getItems().isEmpty()); List<CompletionItem> items = new ArrayList<>(list.getItems()); Comparator<CompletionItem> comparator = (CompletionItem a, CompletionItem b) -> a.getSortText().compareTo(b.getSortText()); Collections.sort(items, comparator); CompletionItem ctor = items.get(0); assertEquals("Object()", ctor.getLabel()); assertEquals("java.lang.Object.Object()", ctor.getDetail()); assertEquals("Object", ctor.getInsertText()); CompletionItem resolvedItem = server.resolveCompletionItem(ctor).join(); assertNotNull(resolvedItem); TextEdit te = resolvedItem.getTextEdit(); assertNotNull(te); assertEquals("Object()",te.getNewText()); assertNotNull(te.getRange()); Range range = te.getRange(); assertEquals(2, range.getStart().getLine()); assertEquals(17, range.getStart().getCharacter()); assertEquals(2, range.getEnd().getLine()); assertEquals(18, range.getEnd().getCharacter()); }
Example 10
Source File: CompletionHandlerTest.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
@Test public void testCompletion_import_package() throws JavaModelException{ ICompilationUnit unit = getWorkingCopy( "src/java/Foo.java", "import java.sq \n" + "public class Foo {\n"+ " void foo() {\n"+ " }\n"+ "}\n"); int[] loc = findCompletionLocation(unit, "java.sq"); CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight(); assertNotNull(list); assertEquals(1, list.getItems().size()); CompletionItem item = list.getItems().get(0); // Check completion item assertNull(item.getInsertText()); assertEquals("java.sql",item.getLabel()); assertEquals("(package) java.sql", item.getDetail()); assertEquals(CompletionItemKind.Module, item.getKind() ); assertEquals("999999215", item.getSortText()); assertNotNull(item.getTextEdit()); TextEdit te = item.getTextEdit(); assertNotNull(te); assertEquals("java.sql.*;",te.getNewText()); assertNotNull(te.getRange()); Range range = te.getRange(); assertEquals(0, range.getStart().getLine()); assertEquals(7, range.getStart().getCharacter()); assertEquals(0, range.getEnd().getLine()); //Not checking the range end character }
Example 11
Source File: CompletionHandlerTest.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
@Test public void testCompletion_package() throws JavaModelException{ ClientPreferences mockCapabilies = Mockito.mock(ClientPreferences.class); Mockito.when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies); ICompilationUnit unit = getWorkingCopy( "src/org/sample/Baz.java", "package o"+ "public class Baz {\n"+ "}\n"); int[] loc = findCompletionLocation(unit, "package o"); CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight(); assertNotNull(list); List<CompletionItem> items = new ArrayList<>(list.getItems()); assertFalse(items.isEmpty()); items.sort((i1, i2) -> i1.getSortText().compareTo(i2.getSortText())); CompletionItem item = items.get(0); // current package should appear 1st assertEquals("org.sample",item.getLabel()); CompletionItem resolvedItem = server.resolveCompletionItem(item).join(); assertNotNull(resolvedItem); TextEdit te = item.getTextEdit(); assertNotNull(te); assertEquals("org.sample", te.getNewText()); assertNotNull(te.getRange()); Range range = te.getRange(); assertEquals(0, range.getStart().getLine()); assertEquals(8, range.getStart().getCharacter()); assertEquals(0, range.getEnd().getLine()); assertEquals(15, range.getEnd().getCharacter()); }
Example 12
Source File: CompletionHandlerTest.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
@Test public void testCompletion_import_static() throws JavaModelException{ ICompilationUnit unit = getWorkingCopy( "src/java/Foo.java", "import static java.util.concurrent.TimeUnit. \n" + "public class Foo {\n"+ " void foo() {\n"+ " }\n"+ "}\n"); int[] loc = findCompletionLocation(unit, "java.util.concurrent.TimeUnit."); CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight(); assertNotNull(list); assertEquals(9, list.getItems().size()); //// .DAYS - enum value CompletionItem daysFieldItem = list.getItems().get(0); // Check completion item assertEquals("DAYS", daysFieldItem.getInsertText()); assertEquals("DAYS : TimeUnit", daysFieldItem.getLabel()); assertEquals(CompletionItemKind.EnumMember, daysFieldItem.getKind()); assertEquals("999999210", daysFieldItem.getSortText()); TextEdit teDays = daysFieldItem.getTextEdit(); assertNotNull(teDays); assertEquals("DAYS;", teDays.getNewText()); assertNotNull(teDays.getRange()); Range secondsRange = teDays.getRange(); assertEquals(0, secondsRange.getStart().getLine()); assertEquals(44, secondsRange.getStart().getCharacter()); assertEquals(0, secondsRange.getEnd().getLine()); //Check other fields are listed alphabetically assertEquals("HOURS;", list.getItems().get(1).getTextEdit().getNewText()); assertEquals("MICROSECONDS;", list.getItems().get(2).getTextEdit().getNewText()); assertEquals("MILLISECONDS;", list.getItems().get(3).getTextEdit().getNewText()); assertEquals("MINUTES;", list.getItems().get(4).getTextEdit().getNewText()); assertEquals("NANOSECONDS;", list.getItems().get(5).getTextEdit().getNewText()); assertEquals("SECONDS;", list.getItems().get(6).getTextEdit().getNewText()); //// .values() - static method CompletionItem valuesMethodItem = list.getItems().get(7); // Check completion item assertEquals("valueOf", valuesMethodItem.getInsertText()); assertEquals("valueOf(String) : TimeUnit", valuesMethodItem.getLabel()); assertEquals(CompletionItemKind.Module, valuesMethodItem.getKind()); assertEquals("999999211", valuesMethodItem.getSortText()); TextEdit teValues = valuesMethodItem.getTextEdit(); assertNotNull(teValues); assertEquals("valueOf;", teValues.getNewText()); assertNotNull(teValues.getRange()); Range valuesRange = teValues.getRange(); assertEquals(0, valuesRange.getStart().getLine()); assertEquals(44, valuesRange.getStart().getCharacter()); assertEquals(0, valuesRange.getEnd().getLine()); }