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