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());
}
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());
}
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());
}
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());
}
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)));
}
}
Aggregations