Search in sources :

Example 11 with CompletionList

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

the class CompletionHandlerTest method testCompletion_classMethodOverride.

private void testCompletion_classMethodOverride(String projectName, boolean supportSnippets, boolean overridesSuperClass) throws Exception {
    if (project == null || !projectName.equals(project.getName())) {
        importProjects("eclipse/" + projectName);
        project = WorkspaceHelper.getProject(projectName);
    }
    mockClientPreferences(supportSnippets, true);
    ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "public class Foo {\n" + "    toStr" + "}\n");
    int[] loc = findCompletionLocation(unit, " toStr");
    CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();
    assertNotNull(list);
    List<CompletionItem> filtered = list.getItems().stream().filter((item) -> {
        return item.getDetail() != null && item.getDetail().startsWith("Override method in");
    }).collect(Collectors.toList());
    assertFalse("No override proposals", filtered.isEmpty());
    CompletionItem oride = filtered.get(0);
    assertEquals("toString", oride.getInsertText());
    assertNull(oride.getTextEdit());
    oride = server.resolveCompletionItem(oride).join();
    assertNotNull(oride.getTextEdit());
    String text = oride.getTextEdit().getNewText();
    StringBuilder expectedText = new StringBuilder();
    if (overridesSuperClass) {
        expectedText.append("@Override\n");
    }
    expectedText.append("public String toString() {\n\t");
    if (supportSnippets) {
        expectedText.append("${0:");
    }
    expectedText.append("return super.toString();");
    if (supportSnippets) {
        expectedText.append("}");
    }
    expectedText.append("\n}");
    assertEquals(expectedText.toString(), text);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) DidChangeTextDocumentParams(org.eclipse.lsp4j.DidChangeTextDocumentParams) RunWith(org.junit.runner.RunWith) Range(org.eclipse.lsp4j.Range) ArrayList(java.util.ArrayList) TextEdit(org.eclipse.lsp4j.TextEdit) CompletionItemKind(org.eclipse.lsp4j.CompletionItemKind) ClientPreferences(org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences) Map(java.util.Map) After(org.junit.After) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) Assert.fail(org.junit.Assert.fail) CompletionList(org.eclipse.lsp4j.CompletionList) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent) JavaClientConnection(org.eclipse.jdt.ls.core.internal.JavaClientConnection) Before(org.junit.Before) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) IJavaProject(org.eclipse.jdt.core.IJavaProject) Job(org.eclipse.core.runtime.jobs.Job) WorkspaceHelper(org.eclipse.jdt.ls.core.internal.WorkspaceHelper) Assert.assertNotNull(org.junit.Assert.assertNotNull) JavaCore(org.eclipse.jdt.core.JavaCore) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) JsonMessageHelper(org.eclipse.jdt.ls.core.internal.JsonMessageHelper) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) CompletionItem(org.eclipse.lsp4j.CompletionItem) Mockito(org.mockito.Mockito) List(java.util.List) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) Assert.assertNull(org.junit.Assert.assertNull) CoreASTProvider(org.eclipse.jdt.core.manipulation.CoreASTProvider) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Lsp4jAssertions.assertTextEdit(org.eclipse.jdt.ls.core.internal.Lsp4jAssertions.assertTextEdit) Assert.assertFalse(org.junit.Assert.assertFalse) Path(org.eclipse.core.runtime.Path) Comparator(java.util.Comparator) Mockito.reset(org.mockito.Mockito.reset) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) ComparisonFailure(org.junit.ComparisonFailure) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionList(org.eclipse.lsp4j.CompletionList) CompletionItem(org.eclipse.lsp4j.CompletionItem)

Example 12 with CompletionList

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

the class CompletionHandlerTest method testCompletion_AnonymousDeclarationType.

@Test
public void testCompletion_AnonymousDeclarationType() 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, 22, "(){\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 13 with CompletionList

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

the class CompletionHandlerTest method testCompletion_methodOverrideWithException.

@Test
public void testCompletion_methodOverrideWithException() throws Exception {
    ICompilationUnit unit = getWorkingCopy(// @formatter:off
    "src/org/sample/Test.java", "package org.sample;\n\n" + "public class Test extends Baz {\n" + "    dele" + "}\n");
    // @formatter:on
    int[] loc = findCompletionLocation(unit, " dele");
    CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();
    assertNotNull(list);
    List<CompletionItem> filtered = list.getItems().stream().filter((item) -> {
        return item.getDetail() != null && item.getDetail().startsWith("Override method in");
    }).collect(Collectors.toList());
    assertEquals("No override proposals", filtered.size(), 1);
    CompletionItem oride = filtered.get(0);
    assertEquals("deleteSomething", oride.getInsertText());
    assertNull(oride.getTextEdit());
    oride = server.resolveCompletionItem(oride).join();
    assertNotNull(oride.getTextEdit());
    String text = oride.getTextEdit().getNewText();
    String expectedText = "@Override\n" + "protected void deleteSomething() throws IOException {\n" + "\t${0:super.deleteSomething();}\n" + "}";
    assertEquals(expectedText, text);
    assertEquals("Missing required imports", 4, oride.getAdditionalTextEdits().size());
    assertTextEdit(0, 19, 19, "\n\n", oride.getAdditionalTextEdits().get(0));
    assertTextEdit(0, 19, 19, "import java.io.IOException;", oride.getAdditionalTextEdits().get(1));
    assertTextEdit(0, 19, 19, "\n\n", oride.getAdditionalTextEdits().get(2));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) DidChangeTextDocumentParams(org.eclipse.lsp4j.DidChangeTextDocumentParams) RunWith(org.junit.runner.RunWith) Range(org.eclipse.lsp4j.Range) ArrayList(java.util.ArrayList) TextEdit(org.eclipse.lsp4j.TextEdit) CompletionItemKind(org.eclipse.lsp4j.CompletionItemKind) ClientPreferences(org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences) Map(java.util.Map) After(org.junit.After) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) Assert.fail(org.junit.Assert.fail) CompletionList(org.eclipse.lsp4j.CompletionList) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent) JavaClientConnection(org.eclipse.jdt.ls.core.internal.JavaClientConnection) Before(org.junit.Before) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) IJavaProject(org.eclipse.jdt.core.IJavaProject) Job(org.eclipse.core.runtime.jobs.Job) WorkspaceHelper(org.eclipse.jdt.ls.core.internal.WorkspaceHelper) Assert.assertNotNull(org.junit.Assert.assertNotNull) JavaCore(org.eclipse.jdt.core.JavaCore) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) JsonMessageHelper(org.eclipse.jdt.ls.core.internal.JsonMessageHelper) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) CompletionItem(org.eclipse.lsp4j.CompletionItem) Mockito(org.mockito.Mockito) List(java.util.List) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) Assert.assertNull(org.junit.Assert.assertNull) CoreASTProvider(org.eclipse.jdt.core.manipulation.CoreASTProvider) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Lsp4jAssertions.assertTextEdit(org.eclipse.jdt.ls.core.internal.Lsp4jAssertions.assertTextEdit) Assert.assertFalse(org.junit.Assert.assertFalse) Path(org.eclipse.core.runtime.Path) Comparator(java.util.Comparator) Mockito.reset(org.mockito.Mockito.reset) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) ComparisonFailure(org.junit.ComparisonFailure) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionList(org.eclipse.lsp4j.CompletionList) CompletionItem(org.eclipse.lsp4j.CompletionItem) Test(org.junit.Test)

Example 14 with CompletionList

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

the class CompletionHandlerTest method testStaticImports2.

@Test
public void testStaticImports2() throws Exception {
    PreferenceManager.getPrefs(null).setJavaCompletionFavoriteMembers(Collections.emptyList());
    ICompilationUnit unit = getWorkingCopy("src/test1/B.java", // @formatter:off
    "package test1;\n" + "\n" + "public class B {\n" + "    public void bar() {\n" + "        /* */fo\n" + "    }\n" + // conflicting method, no static import possible
    "    public void foo(int x) {\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);
    for (CompletionItem it : list.getItems()) {
        if ("foo() : void".equals(it.getLabel())) {
            fail("there is a proposal for foo()");
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionList(org.eclipse.lsp4j.CompletionList) CompletionItem(org.eclipse.lsp4j.CompletionItem) Test(org.junit.Test)

Example 15 with CompletionList

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

the class CompletionHandlerTest method testCompletion_AnonymousDeclarationType5.

@Test
public void testCompletion_AnonymousDeclarationType5() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "public class Foo {\n" + "    public static void main(String[] args) {\n" + "        run(\"name\", new Runnable(");
    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, 33, 33, "(){\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)

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