Java Code Examples for org.eclipse.lsp4j.CompletionItem#getTextEdit()

The following examples show how to use org.eclipse.lsp4j.CompletionItem#getTextEdit() . 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: CamelCompletionInsertAndReplaceTest.java    From camel-language-server with Apache License 2.0 6 votes vote down vote up
@Test
void testComponent() throws Exception {
	CamelLanguageServer camelLanguageServer = initializeLanguageServer("<from uri=\"ahc-wss:httpUri?binding=#true&amp;bufferSize=10&amp;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 2
Source File: CamelCompletionInsertAndReplaceTest.java    From camel-language-server with Apache License 2.0 6 votes vote down vote up
@Test
void testAttribute() throws Exception {
	CamelLanguageServer camelLanguageServer = initializeLanguageServer("<from uri=\"ahc-wss:httpUri?binding=#true&amp;bufferSize=10&amp;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 3
Source File: XMLAssert.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
private static void assertCompletion(CompletionList completions, CompletionItem expected, TextDocument document,
		int offset, Integer expectedCount) {
	List<CompletionItem> matches = completions.getItems().stream().filter(completion -> {
		return expected.getLabel().equals(completion.getLabel());
	}).collect(Collectors.toList());

	if (expectedCount != null) {
		assertTrue(matches.size() >= 1, () -> {
			return expected.getLabel() + " should only exist once: Actual: "
					+ completions.getItems().stream().map(c -> c.getLabel()).collect(Collectors.joining(","));
		});
	} else {
		assertEquals(1, matches.size(), () -> {
			return expected.getLabel() + " should only exist once: Actual: "
					+ completions.getItems().stream().map(c -> c.getLabel()).collect(Collectors.joining(","));
		});
	}

	CompletionItem match = getCompletionMatch(matches, expected);
	if (expected.getTextEdit() != null && match.getTextEdit() != null) {
		if (expected.getTextEdit().getNewText() != null) {
			assertEquals(expected.getTextEdit().getNewText(), match.getTextEdit().getNewText());
		}
		Range r = expected.getTextEdit().getRange();
		if (r != null && r.getStart() != null && r.getEnd() != null) {
			assertEquals(expected.getTextEdit().getRange(), match.getTextEdit().getRange());
		}
	}
	if (expected.getFilterText() != null && match.getFilterText() != null) {
		assertEquals(expected.getFilterText(), match.getFilterText());
	}

	if (expected.getDocumentation() != null) {
		assertEquals(expected.getDocumentation(), match.getDocumentation());
	}

}
 
Example 4
Source File: CompleteIdOnDirectEndpointsTest.java    From camel-language-server with Apache License 2.0 5 votes vote down vote up
@Test
void testDirectEndpointCompletion() throws Exception {
	CamelLanguageServer camelLanguageServer = initializeLanguageServer(new FileInputStream("src/test/resources/workspace/direct-endpoint-test.xml"), ".xml");
	Position positionInMiddleOfcomponentPart = new Position(9, 42);
	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();
		assertThat(textEdit.getNewText()).isIn("direct:name", "direct:processing");
	}
}
 
Example 5
Source File: CompleteIdOnDirectEndpointsTest.java    From camel-language-server with Apache License 2.0 5 votes vote down vote up
@Test
void testDirectVMEndpointCompletion() throws Exception {
	CamelLanguageServer camelLanguageServer = initializeLanguageServer(new FileInputStream("src/test/resources/workspace/direct-endpoint-test.xml"), ".xml");
	Position positionInMiddleOfcomponentPart = new Position(17, 45);
	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();
		assertThat(textEdit.getNewText()).isIn("direct-vm:name", "direct-vm:processing");
	}
}
 
Example 6
Source File: CompleteIdOnDirectEndpointsTest.java    From camel-language-server with Apache License 2.0 5 votes vote down vote up
@Test
void testVMEndpointCompletion() throws Exception {
	CamelLanguageServer camelLanguageServer = initializeLanguageServer(new FileInputStream("src/test/resources/workspace/direct-endpoint-test.xml"), ".xml");
	Position positionInMiddleOfcomponentPart = new Position(25, 39);
	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();
		assertThat(textEdit.getNewText()).isIn("vm:name", "vm:processing");
	}
}
 
Example 7
Source File: CompleteIdOnDirectEndpointsTest.java    From camel-language-server with Apache License 2.0 5 votes vote down vote up
@Test
void testSEDAEndpointCompletion() throws Exception {
	CamelLanguageServer camelLanguageServer = initializeLanguageServer(new FileInputStream("src/test/resources/workspace/direct-endpoint-test.xml"), ".xml");
	Position positionInMiddleOfcomponentPart = new Position(33, 41);
	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();
		assertThat(textEdit.getNewText()).isIn("seda:name", "seda:processing");
	}
}
 
Example 8
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@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 9
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@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 10
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@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 11
Source File: EditorEventManager.java    From lsp4intellij with Apache License 2.0 4 votes vote down vote up
/**
 * 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 12
Source File: AbstractCamelLanguageServerTest.java    From camel-language-server with Apache License 2.0 4 votes vote down vote up
protected boolean hasTextEdit(CompletionItem item) {
	return item != null && item.getTextEdit() != null;
}
 
Example 13
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@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());

}
 
Example 14
Source File: CompletionTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected String _toExpectation(final CompletionItem it) {
  StringConcatenation _builder = new StringConcatenation();
  {
    if (this.withKind) {
      _builder.append("(");
      CompletionItemKind _kind = it.getKind();
      _builder.append(_kind);
      {
        InsertTextFormat _insertTextFormat = it.getInsertTextFormat();
        boolean _tripleNotEquals = (_insertTextFormat != null);
        if (_tripleNotEquals) {
          _builder.append("|");
          InsertTextFormat _insertTextFormat_1 = it.getInsertTextFormat();
          _builder.append(_insertTextFormat_1);
        }
      }
      _builder.append(") ");
    }
  }
  String _label = it.getLabel();
  _builder.append(_label);
  {
    boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(it.getDetail());
    boolean _not = (!_isNullOrEmpty);
    if (_not) {
      _builder.append(" (");
      String _detail = it.getDetail();
      _builder.append(_detail);
      _builder.append(")");
    }
  }
  {
    TextEdit _textEdit = it.getTextEdit();
    boolean _tripleNotEquals_1 = (_textEdit != null);
    if (_tripleNotEquals_1) {
      _builder.append(" -> ");
      String _expectation = this.toExpectation(it.getTextEdit());
      _builder.append(_expectation);
      {
        boolean _isNullOrEmpty_1 = IterableExtensions.isNullOrEmpty(it.getAdditionalTextEdits());
        boolean _not_1 = (!_isNullOrEmpty_1);
        if (_not_1) {
          _builder.append("   + ");
          final Function1<TextEdit, String> _function = (TextEdit it_1) -> {
            return this.toExpectation(it_1);
          };
          String _join = IterableExtensions.join(ListExtensions.<TextEdit, String>map(it.getAdditionalTextEdits(), _function), "   + ");
          _builder.append(_join);
        }
      }
    } else {
      if (((it.getInsertText() != null) && (!Objects.equal(it.getInsertText(), it.getLabel())))) {
        _builder.append(" -> ");
        String _insertText = it.getInsertText();
        _builder.append(_insertText);
      }
    }
  }
  _builder.newLineIfNotEmpty();
  return _builder.toString();
}
 
Example 15
Source File: AbstractLanguageServerTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected String _toExpectation(final CompletionItem it) {
  StringConcatenation _builder = new StringConcatenation();
  String _label = it.getLabel();
  _builder.append(_label);
  {
    boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(it.getDetail());
    boolean _not = (!_isNullOrEmpty);
    if (_not) {
      _builder.append(" (");
      String _detail = it.getDetail();
      _builder.append(_detail);
      _builder.append(")");
    }
  }
  {
    TextEdit _textEdit = it.getTextEdit();
    boolean _tripleNotEquals = (_textEdit != null);
    if (_tripleNotEquals) {
      _builder.append(" -> ");
      String _expectation = this.toExpectation(it.getTextEdit());
      _builder.append(_expectation);
      {
        boolean _isNullOrEmpty_1 = IterableExtensions.isNullOrEmpty(it.getAdditionalTextEdits());
        boolean _not_1 = (!_isNullOrEmpty_1);
        if (_not_1) {
          _builder.append("   + ");
          final Function1<TextEdit, String> _function = (TextEdit it_1) -> {
            return this.toExpectation(it_1);
          };
          String _join = IterableExtensions.join(ListExtensions.<TextEdit, String>map(it.getAdditionalTextEdits(), _function), "   + ");
          _builder.append(_join);
        }
      }
    } else {
      if (((it.getInsertText() != null) && (!Objects.equal(it.getInsertText(), it.getLabel())))) {
        _builder.append(" -> ");
        String _insertText = it.getInsertText();
        _builder.append(_insertText);
      }
    }
  }
  _builder.newLineIfNotEmpty();
  return _builder.toString();
}