Search in sources :

Example 36 with CompletionList

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

the class CompletionHandlerTest method testCompletion_AnonymousDeclarationType3.

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

Example 37 with CompletionList

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

the class CompletionHandlerTest method testCompletion_testClassesAvailableIntoTestCode.

@Test
public void testCompletion_testClassesAvailableIntoTestCode() throws Exception {
    ICompilationUnit unit = getWorkingCopy(// @formatter:off
    "test/foo/bar/BaseTest.java", "package foo.bar;\n\n" + "public class BaseTest extends AbstractTe {\n" + "}\n");
    // @formatter:on
    int[] loc = findCompletionLocation(unit, " AbstractTe");
    CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();
    assertNotNull(list);
    assertEquals("Test proposals missing from :\n" + list, 1, list.getItems().size());
    assertEquals("AbstractTest - foo.bar", list.getItems().get(0).getLabel());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionList(org.eclipse.lsp4j.CompletionList) Test(org.junit.Test)

Example 38 with CompletionList

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

the class CompletionHandlerTest method testCompletion_method_withLSPV2.

@Test
public void testCompletion_method_withLSPV2() throws JavaModelException {
    mockLSP2Client();
    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());
    assertTextEdit(5, 4, 6, "put", 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) TextEdit(org.eclipse.lsp4j.TextEdit) Lsp4jAssertions.assertTextEdit(org.eclipse.jdt.ls.core.internal.Lsp4jAssertions.assertTextEdit) Test(org.junit.Test)

Example 39 with CompletionList

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

the class CompletionHandlerTest method testCompletion_class_name_contains_$withoutSnippetSupport.

@Test
public void testCompletion_class_name_contains_$withoutSnippetSupport() throws Exception {
    mockLSPClient(false, true);
    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 40 with CompletionList

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

the class CompletionHandlerTest method testCompletion_methodOverrideWithParams.

@Test
public void testCompletion_methodOverrideWithParams() throws Exception {
    ICompilationUnit unit = getWorkingCopy(// @formatter:off
    "src/org/sample/Test.java", "package org.sample;\n\n" + "public class Test extends Baz {\n" + "    getP" + "}\n");
    // @formatter:on
    int[] loc = findCompletionLocation(unit, " getP");
    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("getParent", oride.getInsertText());
    assertNull(oride.getTextEdit());
    oride = server.resolveCompletionItem(oride).join();
    assertNotNull(oride.getTextEdit());
    String text = oride.getTextEdit().getNewText();
    String expectedText = "@Override\n" + "protected File getParent(File file, int depth) {\n" + "\t${0:return super.getParent(file, depth);}\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.File;", 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)

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