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

The following examples show how to use org.eclipse.lsp4j.CompletionItem#getInsertText() . 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: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSnippet_class_with_package() throws JavaModelException {
	ICompilationUnit unit = getWorkingCopy("src/org/sample/Test.java", "package org.sample;\n");
	int[] loc = findCompletionLocation(unit, "package org.sample;\n");
	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(7);
	assertEquals("class", item.getLabel());
	String te = item.getInsertText();
	assertEquals("/**\n * Test\n */\npublic class Test {\n\n\t${0}\n}", te);
}
 
Example 2
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSnippet_nested_inner_interface() throws JavaModelException {
	ICompilationUnit unit = getWorkingCopy("src/org/sample/Test.java", "package org.sample;\npublic interface Test {}\npublic interface InnerTest{\n");
	int[] loc = findCompletionLocation(unit, "package org.sample;\npublic interface Test {}\npublic interface InnerTest{\n");
	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(23);
	assertEquals("interface", item.getLabel());
	String te = item.getInsertText();
	assertEquals("/**\n * ${1:InnerTest_1}\n */\npublic interface ${1:InnerTest_1} {\n\n\t${0}\n}", te);
}
 
Example 3
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSnippet_sibling_inner_interface() throws JavaModelException {
	ICompilationUnit unit = getWorkingCopy("src/org/sample/Test.java", "package org.sample;\npublic interface Test {}\npublic interface InnerTest{}\n");
	int[] loc = findCompletionLocation(unit, "package org.sample;\npublic interface Test {}\npublic interface InnerTest{}\n");
	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(6);
	assertEquals("interface", item.getLabel());
	String te = item.getInsertText();
	assertEquals("/**\n * ${1:InnerTest_1}\n */\npublic interface ${1:InnerTest_1} {\n\n\t${0}\n}", te);
}
 
Example 4
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSnippet_inner_interface() throws JavaModelException {
	ICompilationUnit unit = getWorkingCopy("src/org/sample/Test.java", "package org.sample;\npublic interface Test {}\n");
	int[] loc = findCompletionLocation(unit, "package org.sample;\npublic interface Test {}\n");
	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(6);
	assertEquals("interface", item.getLabel());
	String te = item.getInsertText();
	assertEquals("/**\n * ${1:InnerTest}\n */\npublic interface ${1:InnerTest} {\n\n\t${0}\n}", te);
}
 
Example 5
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSnippet_context_method1() throws JavaModelException {
	ICompilationUnit unit = getWorkingCopy("src/org/sample/Test.java",
	//@formatter:off
			"package org.sample;\n"
		+	"public class Test {\n\n"
		+	"	void test() {\n\n"
		+	"	}\n"
		+	"}\n");
		//@formatter:off
	int[] loc = findCompletionLocation(unit, "{\n\n");
	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("class") && item.getKind() == CompletionItemKind.Snippet))
			.findFirst().orElse(null);
	assertNotNull(ci);
	String text = ci.getInsertText();
	assertEquals("class ${1:InnerTest} {\n\n\t${0}\n}", text);
	ci = list.getItems().stream()
			.filter( item->  (item.getLabel().matches("interface") && item.getKind() == CompletionItemKind.Snippet))
			.findFirst().orElse(null);
	assertNull(ci);
}
 
Example 6
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSnippet_interface() throws JavaModelException {
	ICompilationUnit unit = getWorkingCopy("src/org/sample/Test.java", "");
	int[] loc = findCompletionLocation(unit, "");
	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(9);
	assertEquals("interface", item.getLabel());
	String te = item.getInsertText();
	assertEquals("package org.sample;\n\n/**\n * Test\n */\npublic interface Test {\n\n\t${0}\n}", te);

	//check resolution doesn't blow up (https://github.com/eclipse/eclipse.jdt.ls/issues/675)
	assertSame(item, server.resolveCompletionItem(item).join());
}
 
Example 7
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSnippet_ifnotnull() throws JavaModelException {
	//@formatter:off
	ICompilationUnit unit = getWorkingCopy(
		"src/org/sample/Test.java",
		"package org.sample;\n" +
		"public class Test {\n" +
		"	public void testMethod(Object obj) {\n" +
		"		ifnotnull" +
		"	}\n" +
		"}"
	);
	//@formatter:on
	int[] loc = findCompletionLocation(unit, "ifnotnull");
	CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();

	assertNotNull(list);

	List<CompletionItem> items = new ArrayList<>(list.getItems());
	CompletionItem item = items.get(0);
	assertEquals("ifnotnull", item.getLabel());
	String insertText = item.getInsertText();
	assertEquals("if (${1:obj} != null) {\n\t$TM_SELECTED_TEXT${0}\n}", insertText);
}
 
Example 8
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSnippet_ifnull() throws JavaModelException {
	//@formatter:off
	ICompilationUnit unit = getWorkingCopy(
		"src/org/sample/Test.java",
		"package org.sample;\n" +
		"public class Test {\n" +
		"	public void testMethod(Object obj) {\n" +
		"		ifnull" +
		"	}\n" +
		"}"
	);
	//@formatter:on
	int[] loc = findCompletionLocation(unit, "ifnull");
	CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();

	assertNotNull(list);

	List<CompletionItem> items = new ArrayList<>(list.getItems());
	CompletionItem item = items.get(0);
	assertEquals("ifnull", item.getLabel());
	String insertText = item.getInsertText();
	assertEquals("if (${1:obj} == null) {\n\t$TM_SELECTED_TEXT${0}\n}", insertText);
}
 
Example 9
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSnippet_ifelse() throws JavaModelException {
	//@formatter:off
	ICompilationUnit unit = getWorkingCopy(
		"src/org/sample/Test.java",
		"package org.sample;\n" +
		"public class Test {\n" +
		"	public void testMethod(boolean con) {\n" +
		"		ifelse" +
		"	}\n" +
		"}"
	);
	//@formatter:on
	int[] loc = findCompletionLocation(unit, "ifelse");
	CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();

	assertNotNull(list);

	List<CompletionItem> items = new ArrayList<>(list.getItems());
	CompletionItem item = items.get(0);
	assertEquals("ifelse", item.getLabel());
	String insertText = item.getInsertText();
	assertEquals("if (${1:con}) {\n\t${2}\n} else {\n\t${0}\n}", insertText);
}
 
Example 10
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSnippet_inner_class() throws JavaModelException {
	ICompilationUnit unit = getWorkingCopy("src/org/sample/Test.java", "package org.sample;\npublic class Test {}\n");
	int[] loc = findCompletionLocation(unit, "");
	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(5);
	assertEquals("class", item.getLabel());
	String te = item.getInsertText();
	assertEquals("/**\n * ${1:InnerTest}\n */\npublic class ${1:InnerTest} {\n\n\t${0}\n}", te);
}
 
Example 11
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSnippet_while() throws JavaModelException {
	//@formatter:off
	ICompilationUnit unit = getWorkingCopy(
		"src/org/sample/Test.java",
		"package org.sample;\n" +
		"public class Test {\n" +
		"	public void testMethod(boolean con) {\n" +
		"		while" +
		"	}\n" +
		"}"
	);
	//@formatter:on
	int[] loc = findCompletionLocation(unit, "while");
	CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();

	assertNotNull(list);

	List<CompletionItem> items = new ArrayList<>(list.getItems());
	CompletionItem item = items.get(1);
	assertEquals("while", item.getLabel());
	String insertText = item.getInsertText();
	assertEquals("while (${1:con}) {\n\t$TM_SELECTED_TEXT${0}\n}", insertText);
}
 
Example 12
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSnippet_array_fori() throws JavaModelException {
	//@formatter:off
	ICompilationUnit unit = getWorkingCopy(
		"src/org/sample/Test.java",
		"package org.sample;\n" +
		"public class Test {\n" +
		"	public void testMethod(String[] args) {\n" +
		"		fori" +
		"	}\n" +
		"}"
	);
	//@formatter:on
	int[] loc = findCompletionLocation(unit, "fori");
	CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();

	assertNotNull(list);

	List<CompletionItem> items = new ArrayList<>(list.getItems());
	CompletionItem item = items.get(0);
	assertEquals("fori", item.getLabel());
	String insertText = item.getInsertText();
	assertEquals("for (${1:int} ${2:i} = ${3:0}; ${2:i} < ${4:args.length}; ${2:i}++) {\n\t$TM_SELECTED_TEXT${0}\n}", insertText);
}
 
Example 13
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSnippet_class() throws JavaModelException {
	ICompilationUnit unit = getWorkingCopy("src/org/sample/Test.java", "");
	int[] loc = findCompletionLocation(unit, "");
	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(8);
	assertEquals("class", item.getLabel());
	String te = item.getInsertText();
	assertEquals("package org.sample;\n\n/**\n * Test\n */\npublic class Test {\n\n\t${0}\n}", te);
}
 
Example 14
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSnippet_array_foreach() throws JavaModelException {
	//@formatter:off
	ICompilationUnit unit = getWorkingCopy(
		"src/org/sample/Test.java",
		"package org.sample;\n" +
		"public class Test {\n" +
		"	public void testMethod(String[] args) {\n" +
		"		foreach" +
		"	}\n" +
		"}"
	);
	//@formatter:on
	int[] loc = findCompletionLocation(unit, "foreach");
	CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();

	assertNotNull(list);

	List<CompletionItem> items = new ArrayList<>(list.getItems());
	CompletionItem item = items.get(0);
	assertEquals("foreach", item.getLabel());
	String insertText = item.getInsertText();
	assertEquals("for (${1:String} ${2:string} : ${3:args}) {\n\t$TM_SELECTED_TEXT${0}\n}", insertText);
}
 
Example 15
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Ignore(value = "When running tests, in SnippetCompletionProposal.getSnippetContent(), cu.getAllTypes() returns en empty array, so inner record name is not computed")
@Test
public void testSnippet_sibling_inner_record() throws Exception {
	importProjects("eclipse/records");
	project = WorkspaceHelper.getProject("records");
	ICompilationUnit unit = getWorkingCopy("src/main/java/org/sample/Test.java", "package org.sample;\npublic record Test() {}\npublic record InnerTest(){}\n");
	int[] loc = findCompletionLocation(unit, "package org.sample;\npublic record Test {}\npublic record InnerTest(){}\n");
	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(7);
	assertEquals("record", item.getLabel());
	String te = item.getInsertText();
	assertEquals("/**\n * ${1:InnerTest_1}\n */\npublic record ${1:InnerTest_1}(${0) {\n}", te);
}
 
Example 16
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSnippet_record() throws Exception {
	importProjects("eclipse/records");
	project = WorkspaceHelper.getProject("records");
	ICompilationUnit unit = getWorkingCopy("src/main/java/org/sample/Test.java", "");
	int[] loc = findCompletionLocation(unit, "");
	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(10);
	assertEquals("record", item.getLabel());
	String te = item.getInsertText();
	assertEquals("package org.sample;\n\n/**\n * Test\n */\npublic record Test(${0}) {\n}", te);

	//check resolution doesn't blow up (https://github.com/eclipse/eclipse.jdt.ls/issues/675)
	assertSame(item, server.resolveCompletionItem(item).join());
}
 
Example 17
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSnippet_sysout() throws JavaModelException {
	//@formatter:off
	ICompilationUnit unit = getWorkingCopy(
		"src/org/sample/Test.java",
		"package org.sample;\n" +
		"public class Test {\n" +
		"	public void testMethod() {\n" +
		"		sysout" +
		"	}\n" +
		"}"
	);
	//@formatter:on
	int[] loc = findCompletionLocation(unit, "sysout");
	CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();

	assertNotNull(list);

	List<CompletionItem> items = new ArrayList<>(list.getItems());
	CompletionItem item = items.get(0);
	assertEquals("sysout", item.getLabel());
	String insertText = item.getInsertText();
	assertEquals("System.out.println(${0});", insertText);
}
 
Example 18
Source File: CompletionHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSnippet_record_with_package() throws Exception {
	importProjects("eclipse/records");
	project = WorkspaceHelper.getProject("records");
	ICompilationUnit unit = getWorkingCopy("src/main/java/org/sample/Test.java", "package org.sample;\n");
	int[] loc = findCompletionLocation(unit, "package org.sample;\n");
	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(9);
	assertEquals("record", item.getLabel());
	String te = item.getInsertText();
	assertEquals("/**\n * Test\n */\npublic record Test(${0}) {\n}", te);
}
 
Example 19
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 20
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();
}