Search in sources :

Example 31 with CompletionList

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

the class CompletionHandlerTest method testCompletion_constructor.

@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("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());
}
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) ArrayList(java.util.ArrayList) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test)

Example 32 with CompletionList

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

the class CompletionHandlerTest method testCompletion_field.

@Test
public void testCompletion_field() throws JavaModelException {
    ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "import java.sq \n" + "public class Foo {\n" + "private String myTestString;\n" + "	void foo() {\n" + "   this.myTestS\n" + "	}\n" + "}\n");
    int[] loc = findCompletionLocation(unit, "this.myTestS");
    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);
    assertEquals(CompletionItemKind.Field, item.getKind());
    assertEquals("myTestString", item.getInsertText());
    assertNull(item.getAdditionalTextEdits());
    assertNull(item.getTextEdit());
    CompletionItem resolvedItem = server.resolveCompletionItem(item).join();
    assertNotNull(resolvedItem.getTextEdit());
    assertTextEdit(4, 8, 15, "myTestString", resolvedItem.getTextEdit());
// Not checking the range end character
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionList(org.eclipse.lsp4j.CompletionList) CompletionItem(org.eclipse.lsp4j.CompletionItem) Test(org.junit.Test)

Example 33 with CompletionList

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

the class CompletionHandlerTest method testCompletion_interfaceMethodOverride.

private void testCompletion_interfaceMethodOverride(String projectName, boolean supportSnippets, boolean overridesInterface) 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 implements Runnable{\n" + "    ru" + "}\n");
    int[] loc = findCompletionLocation(unit, " ru");
    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("run", oride.getInsertText());
    assertNull(oride.getTextEdit());
    oride = server.resolveCompletionItem(oride).join();
    assertNotNull(oride.getTextEdit());
    String text = oride.getTextEdit().getNewText();
    StringBuilder expectedText = new StringBuilder();
    if (overridesInterface) {
        expectedText.append("@Override\n");
    }
    expectedText.append("public void run() {\n\t");
    if (supportSnippets) {
        expectedText.append("${0}");
    }
    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 34 with CompletionList

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

the class CompletionHandlerTest method testCompletion_AnonymousTypeMoreMethods.

@Test
public void testCompletion_AnonymousTypeMoreMethods() 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" + "        void setName(String name);\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 void setName(String name) {\n" + "			${0}\n" + "		}\n" + "\n		@Override\n" + "		public String getName() {\n" + "			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 35 with CompletionList

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

the class CompletionHandlerTest method testCompletion_AnonymousDeclarationType4.

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