Search in sources :

Example 6 with CompletionList

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

the class CompletionHandlerTest method testCompletion_package.

@Test
public void testCompletion_package() throws JavaModelException {
    ClientPreferences mockCapabilies = Mockito.mock(ClientPreferences.class);
    Mockito.when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies);
    ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java", "package o" + "public class Baz {\n" + "}\n");
    int[] loc = findCompletionLocation(unit, "package o");
    CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();
    assertNotNull(list);
    List<CompletionItem> items = new ArrayList<>(list.getItems());
    assertTrue(items.size() > 1);
    items.sort((i1, i2) -> i1.getSortText().compareTo(i2.getSortText()));
    CompletionItem item = items.get(0);
    // current package should appear 1st
    assertEquals("org.sample", item.getLabel());
    CompletionItem resolvedItem = server.resolveCompletionItem(item).join();
    assertNotNull(resolvedItem);
    TextEdit te = item.getTextEdit();
    assertNotNull(te);
    assertEquals("org.sample", te.getNewText());
    assertNotNull(te.getRange());
    Range range = te.getRange();
    assertEquals(0, range.getStart().getLine());
    assertEquals(8, range.getStart().getCharacter());
    assertEquals(0, range.getEnd().getLine());
    assertEquals(15, range.getEnd().getCharacter());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionList(org.eclipse.lsp4j.CompletionList) ClientPreferences(org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences) 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 7 with CompletionList

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

the class CompletionHandlerTest method testCompletion_booleangetter.

@Test
public void testCompletion_booleangetter() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "public class Foo {\n" + "    private boolean boolField;\n" + "    is\n" + "}\n");
    int[] loc = findCompletionLocation(unit, "is");
    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("isBoolField() : boolean")).findFirst().orElse(null);
    assertNotNull(ci);
    assertEquals("isBoolField", ci.getInsertText());
    assertEquals(CompletionItemKind.Function, ci.getKind());
    assertEquals("999999979", ci.getSortText());
    assertNull(ci.getTextEdit());
    CompletionItem resolvedItem = server.resolveCompletionItem(ci).join();
    assertNotNull(resolvedItem.getTextEdit());
    assertTextEdit(2, 4, 6, "/**\n" + " * @return the boolField\n" + " */\n" + "public boolean isBoolField() {\n" + "	return boolField;\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 8 with CompletionList

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

the class CompletionHandlerTest method testCompletion_testClassesDontLeakIntoMainCode.

@Test
public void testCompletion_testClassesDontLeakIntoMainCode() throws Exception {
    ICompilationUnit unit = getWorkingCopy(// @formatter:off
    "src/org/sample/Test.java", "package org.sample;\n\n" + "public class Test 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();
    assertEquals("Test proposals leaked:\n" + list.getItems(), 0, list.getItems().size());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionList(org.eclipse.lsp4j.CompletionList) Test(org.junit.Test)

Example 9 with CompletionList

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

the class CompletionHandlerTest method testCompletion_getter.

@Test
public void testCompletion_getter() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "public class Foo {\n" + "    private String strField;\n" + "    get" + "}\n");
    int[] loc = findCompletionLocation(unit, "get");
    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("getStrField() : String")).findFirst().orElse(null);
    assertNotNull(ci);
    assertEquals("getStrField", ci.getInsertText());
    assertEquals(CompletionItemKind.Function, ci.getKind());
    assertEquals("999999979", ci.getSortText());
    assertNull(ci.getTextEdit());
    CompletionItem resolvedItem = server.resolveCompletionItem(ci).join();
    assertNotNull(resolvedItem.getTextEdit());
    assertTextEdit(2, 4, 7, "/**\n" + " * @return the strField\n" + " */\n" + "public String getStrField() {\n" + "	return strField;\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 10 with CompletionList

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

the class CompletionHandlerTest method testCompletion_object.

// FIXME Something very fishy here: when run from command line as part of the whole test suite,
// no completions are returned maybe 80% of the time if this method runs first in this class,
// i.e. if this method is named testCompletion_1. It seems to fail in the IDE too but *very*
// infrequently.
// When running the test class only, completions are always returned.
@Test
public void testCompletion_object() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "public class Foo {\n" + "	void foo() {\n" + "		Objec\n" + "	}\n" + "}\n");
    int[] loc = findCompletionLocation(unit, "Objec");
    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 = list.getItems();
    for (CompletionItem item : items) {
        assertTrue(isNotBlank(item.getLabel()));
        assertNotNull(item.getKind());
        assertTrue(isNotBlank(item.getSortText()));
        // text edits are not set during calls to "completion"
        assertNull(item.getTextEdit());
        assertTrue(isNotBlank(item.getInsertText()));
        assertNotNull(item.getFilterText());
        assertFalse(item.getFilterText().contains(" "));
        assertTrue(item.getLabel().startsWith(item.getFilterText()));
        // Check contains data used for completionItem resolution
        @SuppressWarnings("unchecked") Map<String, String> data = (Map<String, String>) item.getData();
        assertNotNull(data);
        assertTrue(isNotBlank(data.get(CompletionResolveHandler.DATA_FIELD_URI)));
        assertTrue(isNotBlank(data.get(CompletionResolveHandler.DATA_FIELD_PROPOSAL_ID)));
        assertTrue(isNotBlank(data.get(CompletionResolveHandler.DATA_FIELD_REQUEST_ID)));
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionList(org.eclipse.lsp4j.CompletionList) CompletionItem(org.eclipse.lsp4j.CompletionItem) Map(java.util.Map) 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