Search in sources :

Example 21 with CompletionList

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

the class CompletionHandlerTest method testCompletion_noPackage.

@Test
public void testCompletion_noPackage() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/NoPackage.java", "public class NoPackage {\n" + "    NoP" + "}\n");
    int[] loc = findCompletionLocation(unit, "    NoP");
    CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();
    assertNotNull(list);
    assertFalse("No proposals were found", list.getItems().isEmpty());
    assertEquals("NoPackage", list.getItems().get(0).getLabel());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionList(org.eclipse.lsp4j.CompletionList) Test(org.junit.Test)

Example 22 with CompletionList

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

the class CompletionHandlerTest method testCompletion_AnonymousType.

@Test
public void testCompletion_AnonymousType() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "public class Foo {\n" + "    public static void main(String[] args) {\n" + "        IFoo foo = new \n" + "    } \n" + "    interface IFoo {\n" + "        String getName();\n" + "    }\n" + "}\n");
    waitForBackgroundJobs();
    int[] loc = findCompletionLocation(unit, "new ");
    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.IFoo()  Anonymous Inner Type")).findFirst().orElse(null);
    assertNotNull(ci);
    assertEquals("Foo.IFoo", ci.getInsertText());
    assertEquals(CompletionItemKind.Constructor, ci.getKind());
    assertEquals("999998684", ci.getSortText());
    assertNull(ci.getTextEdit());
    CompletionItem resolvedItem = server.resolveCompletionItem(ci).join();
    assertNotNull(resolvedItem.getTextEdit());
    assertTextEdit(2, 23, 23, "IFoo(){\n" + "\n" + "		@Override\n" + "		public String getName() {\n" + "			${0:return null;}\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 23 with CompletionList

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

the class CompletionHandlerTest method testStaticImports1.

@Test
public void testStaticImports1() throws Exception {
    List<String> favorites = new ArrayList<>();
    favorites.add("test1.A.foo");
    PreferenceManager.getPrefs(null).setJavaCompletionFavoriteMembers(favorites);
    try {
        ICompilationUnit unit = getWorkingCopy("src/test1/B.java", // @formatter:off
        "package test1;\n" + "\n" + "public class B {\n" + "    public void bar() {\n" + "        fo\n" + "    }\n" + "}\n");
        // @formatter:on
        int[] loc = findCompletionLocation(unit, "fo");
        CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();
        assertNotNull(list);
        assertTrue(list.getItems().size() > 0);
        assertTrue("no proposal for foo()", "foo() : void".equals(list.getItems().get(0).getLabel()));
    } finally {
        PreferenceManager.getPrefs(null).setJavaCompletionFavoriteMembers(Collections.emptyList());
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionList(org.eclipse.lsp4j.CompletionList) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 24 with CompletionList

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

the class CompletionHandlerTest method testCompletion_javadoc.

@Test
public void testCompletion_javadoc() throws Exception {
    IJavaProject javaProject = JavaCore.create(project);
    ICompilationUnit unit = (ICompilationUnit) javaProject.findElement(new Path("org/sample/TestJavadoc.java"));
    unit.becomeWorkingCopy(null);
    String joinOnCompletion = System.getProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION);
    try {
        System.setProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION, "true");
        int[] loc = findCompletionLocation(unit, "inner.");
        TextDocumentPositionParams position = JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]));
        String source = unit.getSource();
        changeDocument(unit, source, 3);
        Job.getJobManager().join(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, new NullProgressMonitor());
        changeDocument(unit, source, 4);
        CompletionList list = server.completion(position).join().getRight();
        for (CompletionItem item : list.getItems()) {
            server.resolveCompletionItem(item);
        }
        CompletionItem resolved = list.getItems().get(0);
        assertEquals("Test ", resolved.getDocumentation());
    } finally {
        unit.discardWorkingCopy();
        if (joinOnCompletion == null) {
            System.clearProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION);
        } else {
            System.setProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION, joinOnCompletion);
        }
    }
}
Also used : Path(org.eclipse.core.runtime.Path) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) CompletionList(org.eclipse.lsp4j.CompletionList) IJavaProject(org.eclipse.jdt.core.IJavaProject) CompletionItem(org.eclipse.lsp4j.CompletionItem) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) Test(org.junit.Test)

Example 25 with CompletionList

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

the class CompletionHandlerTest method testCompletion_import_static.

@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());
    // // .SECONDS - enum value
    CompletionItem secondsFieldItem = list.getItems().get(0);
    // Check completion item
    assertEquals("SECONDS", secondsFieldItem.getInsertText());
    assertEquals("SECONDS : TimeUnit", secondsFieldItem.getLabel());
    assertEquals(CompletionItemKind.Field, secondsFieldItem.getKind());
    assertEquals("999999210", secondsFieldItem.getSortText());
    assertNull(secondsFieldItem.getTextEdit());
    assertNotNull(server.resolveCompletionItem(secondsFieldItem).join());
    TextEdit teSeconds = secondsFieldItem.getTextEdit();
    assertNotNull(teSeconds);
    assertEquals("SECONDS;", teSeconds.getNewText());
    assertNotNull(teSeconds.getRange());
    Range secondsRange = teSeconds.getRange();
    assertEquals(0, secondsRange.getStart().getLine());
    assertEquals(44, secondsRange.getStart().getCharacter());
    assertEquals(0, secondsRange.getEnd().getLine());
    // // .values() - static method
    CompletionItem valuesMethodItem = list.getItems().get(7);
    // Check completion item
    assertEquals("values", valuesMethodItem.getInsertText());
    assertEquals("values() : TimeUnit[]", valuesMethodItem.getLabel());
    assertEquals(CompletionItemKind.Module, valuesMethodItem.getKind());
    assertEquals("999999211", valuesMethodItem.getSortText());
    assertNull(valuesMethodItem.getTextEdit());
    assertNotNull(server.resolveCompletionItem(valuesMethodItem).join());
    TextEdit teValues = valuesMethodItem.getTextEdit();
    assertNotNull(teValues);
    assertEquals("values;", teValues.getNewText());
    assertNotNull(teValues.getRange());
    Range valuesRange = teValues.getRange();
    assertEquals(0, valuesRange.getStart().getLine());
    assertEquals(44, valuesRange.getStart().getCharacter());
    assertEquals(0, valuesRange.getEnd().getLine());
}
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)

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