Search in sources :

Example 16 with CompletionList

use of org.eclipse.lsp4j.CompletionList in project eclipse.jdt.ls by eclipse.

the class CompletionHandlerTest method testCompletion_import_package.

@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(CompletionItemKind.Module, item.getKind());
    assertEquals("999999215", item.getSortText());
    assertNull(item.getTextEdit());
    CompletionItem resolvedItem = server.resolveCompletionItem(item).join();
    assertNotNull(resolvedItem);
    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
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionList(org.eclipse.lsp4j.CompletionList) CompletionItem(org.eclipse.lsp4j.CompletionItem) TextEdit(org.eclipse.lsp4j.TextEdit) Lsp4jAssertions.assertTextEdit(org.eclipse.jdt.ls.core.internal.Lsp4jAssertions.assertTextEdit) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test)

Example 17 with CompletionList

use of org.eclipse.lsp4j.CompletionList in project eclipse.jdt.ls by eclipse.

the class CompletionHandlerTest method testCompletion_AnonymousDeclarationType2.

@Test
public void testCompletion_AnonymousDeclarationType2() throws Exception {
    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());
    assertNull(ci.getTextEdit());
    CompletionItem resolvedItem = server.resolveCompletionItem(ci).join();
    assertNotNull(resolvedItem.getTextEdit());
    assertTextEdit(2, 20, 24, "(){\n" + "\n" + "	@Override\n" + "	public void run() {\n" + "		${0}\n" + "	}\n" + "}", resolvedItem.getTextEdit());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionList(org.eclipse.lsp4j.CompletionList) CompletionItem(org.eclipse.lsp4j.CompletionItem) Test(org.junit.Test)

Example 18 with CompletionList

use of org.eclipse.lsp4j.CompletionList in project eclipse.jdt.ls by eclipse.

the class CompletionHandlerTest method testCompletion_method_withLSPV3.

@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());
    assertEquals(CompletionItemKind.Function, ci.getKind());
    assertEquals("999999019", ci.getSortText());
    assertNull(ci.getTextEdit());
    CompletionItem resolvedItem = server.resolveCompletionItem(ci).join();
    assertNotNull(resolvedItem.getTextEdit());
    try {
        assertTextEdit(5, 4, 6, "put(${1:key}, ${2:value})", resolvedItem.getTextEdit());
    } catch (ComparisonFailure e) {
        // In case the JDK has no sources
        assertTextEdit(5, 4, 6, "put(${1:arg0}, ${2:arg1})", resolvedItem.getTextEdit());
    }
    assertNotNull(resolvedItem.getAdditionalTextEdits());
    List<TextEdit> edits = resolvedItem.getAdditionalTextEdits();
    assertEquals(3, edits.size());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionList(org.eclipse.lsp4j.CompletionList) CompletionItem(org.eclipse.lsp4j.CompletionItem) ComparisonFailure(org.junit.ComparisonFailure) TextEdit(org.eclipse.lsp4j.TextEdit) Lsp4jAssertions.assertTextEdit(org.eclipse.jdt.ls.core.internal.Lsp4jAssertions.assertTextEdit) Test(org.junit.Test)

Example 19 with CompletionList

use of org.eclipse.lsp4j.CompletionList in project eclipse.jdt.ls by eclipse.

the class CompletionHandlerTest method testCompletion_class_name_contains_$.

@Test
public void testCompletion_class_name_contains_$() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/org/sample/Foo$Bar.java", "public class Foo$Bar {\n" + "    public static void main(String[] args) {\n" + "        new Foo\n" + "    }\n" + "}\n");
    waitForBackgroundJobs();
    int[] loc = findCompletionLocation(unit, "new Foo");
    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("Foo$Bar")).findFirst().orElse(null);
    assertNotNull(ci);
    assertEquals("Foo$Bar", ci.getInsertText());
    assertEquals(CompletionItemKind.Constructor, ci.getKind());
    assertEquals("999999115", ci.getSortText());
    assertNull(ci.getTextEdit());
    CompletionItem resolvedItem = server.resolveCompletionItem(ci).join();
    assertNotNull(resolvedItem.getTextEdit());
    assertTextEdit(2, 12, 15, "Foo\\$Bar()", resolvedItem.getTextEdit());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionList(org.eclipse.lsp4j.CompletionList) CompletionItem(org.eclipse.lsp4j.CompletionItem) Test(org.junit.Test)

Example 20 with CompletionList

use of org.eclipse.lsp4j.CompletionList in project eclipse.jdt.ls by eclipse.

the class CompletionHandlerTest method testCompletion_plainTextDoc.

public void testCompletion_plainTextDoc() throws Exception {
    ICompilationUnit unit = getWorkingCopy(// @formatter:off
    "src/java/Foo.java", "import java.sq \n" + "public class Foo {\n" + "	void foo() {\n" + "      zz\n" + "	}\n\"	}\\n\"+" + "\n" + "/** This should be <bold>plain</bold>.*/\n" + "	void zzz() {}\n" + "}\n");
    // @formatter:off
    int[] loc = findCompletionLocation(unit, "   zz");
    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 item = list.getItems().get(0);
    assertEquals("zzz() : void", item.getLabel());
    CompletionItem resolvedItem = server.resolveCompletionItem(item).join();
    assertEquals("This should be plain.", resolvedItem.getDocumentation());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionList(org.eclipse.lsp4j.CompletionList) CompletionItem(org.eclipse.lsp4j.CompletionItem)

Aggregations

CompletionList (org.eclipse.lsp4j.CompletionList)44 CompletionItem (org.eclipse.lsp4j.CompletionItem)36 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)33 Test (org.junit.Test)31 ArrayList (java.util.ArrayList)11 TextEdit (org.eclipse.lsp4j.TextEdit)11 List (java.util.List)10 Lsp4jAssertions.assertTextEdit (org.eclipse.jdt.ls.core.internal.Lsp4jAssertions.assertTextEdit)10 Range (org.eclipse.lsp4j.Range)9 TextDocumentPositionParams (org.eclipse.lsp4j.TextDocumentPositionParams)7 Map (java.util.Map)6 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)6 Collections (java.util.Collections)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)5 Path (org.eclipse.core.runtime.Path)5 IJavaProject (org.eclipse.jdt.core.IJavaProject)5 JavaModelException (org.eclipse.jdt.core.JavaModelException)5 ClientPreferences (org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences)5 DidChangeTextDocumentParams (org.eclipse.lsp4j.DidChangeTextDocumentParams)5 Comparator (java.util.Comparator)4