use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class AbstractQuickFixTest method evaluateCodeActions.
protected List<Either<Command, CodeAction>> evaluateCodeActions(ICompilationUnit cu, Range range) throws JavaModelException {
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
IProblem[] problems = astRoot.getProblems();
CodeActionParams parms = new CodeActionParams();
TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
textDocument.setUri(JDTUtils.toURI(cu));
parms.setTextDocument(textDocument);
parms.setRange(range);
CodeActionContext context = new CodeActionContext();
context.setDiagnostics(DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problems), true));
context.setOnly(onlyKinds);
parms.setContext(context);
List<Either<Command, CodeAction>> codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(parms, new NullProgressMonitor());
if (onlyKinds != null && !onlyKinds.isEmpty()) {
for (Either<Command, CodeAction> codeAction : codeActions) {
Stream<String> acceptedActionKinds = onlyKinds.stream();
String kind = codeAction.getRight().getKind();
assertTrue(codeAction.getRight().getTitle() + " has kind " + kind + " but only " + onlyKinds + " are accepted", acceptedActionKinds.filter(k -> kind != null && kind.startsWith(k)).findFirst().isPresent());
}
}
if (this.ignoredKinds != null) {
List<Either<Command, CodeAction>> filteredList = codeActions.stream().filter(Either::isRight).filter(codeAction -> {
for (String str : this.ignoredKinds) {
if (codeAction.getRight().getKind().matches(str)) {
return true;
}
}
return false;
}).collect(Collectors.toList());
codeActions.removeAll(filteredList);
}
if (this.ignoredCommands != null) {
List<Either<Command, CodeAction>> filteredList = new ArrayList<>();
for (Either<Command, CodeAction> codeAction : codeActions) {
for (String str : this.ignoredCommands) {
if (getTitle(codeAction).matches(str)) {
filteredList.add(codeAction);
break;
}
}
}
codeActions.removeAll(filteredList);
}
return codeActions;
}
use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class MissingEnumQuickFixTest method testMissingEnumConstant.
@Test
public void testMissingEnumConstant() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public enum Numbers { One, Two};\n");
buf.append(" public void testing() {\n");
buf.append(" Numbers n = Numbers.One;\n");
buf.append(" switch (n) {\n");
buf.append(" case Two:\n");
buf.append(" return;\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
Range range = new Range(new Position(5, 16), new Position(5, 17));
setIgnoredCommands(ActionMessages.GenerateConstructorsAction_ellipsisLabel, ActionMessages.GenerateConstructorsAction_label);
List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, range);
assertEquals(2, codeActions.size());
Either<Command, CodeAction> codeAction = codeActions.get(0);
CodeAction action = codeAction.getRight();
assertEquals(CodeActionKind.QuickFix, action.getKind());
assertEquals("Add 'default' case", action.getTitle());
TextEdit edit = getTextEdit(codeAction);
assertEquals("\n default:\n break;", edit.getNewText());
codeAction = codeActions.get(1);
action = codeAction.getRight();
assertEquals(CodeActionKind.QuickFix, action.getKind());
assertEquals("Add missing case statements", action.getTitle());
edit = getTextEdit(codeAction);
assertEquals("\n case One:\n break;\n default:\n break;", edit.getNewText());
}
use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class MissingEnumQuickFixTest method getTextEdit.
private TextEdit getTextEdit(Either<Command, CodeAction> codeAction) {
Command c = codeAction.isLeft() ? codeAction.getLeft() : codeAction.getRight().getCommand();
Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand());
Assert.assertNotNull(c.getArguments());
Assert.assertTrue(c.getArguments().get(0) instanceof WorkspaceEdit);
WorkspaceEdit we = (WorkspaceEdit) c.getArguments().get(0);
Iterator<Entry<String, List<TextEdit>>> editEntries = we.getChanges().entrySet().iterator();
Entry<String, List<TextEdit>> entry = editEntries.next();
TextEdit edit = entry.getValue().get(0);
return edit;
}
use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class ConvertVarQuickFixTest method testConvertVarTypeToResolvedType.
@Test
public void testConvertVarTypeToResolvedType() throws Exception {
IPackageFragment pack1 = sourceFolder.createPackageFragment("foo.bar", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package foo.bar;\n");
buf.append("public class Test {\n");
buf.append(" public void test() {\n");
buf.append(" var name = \"test\";\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("Test.java", buf.toString(), false, null);
List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu);
Either<Command, CodeAction> codeAction = codeActions.stream().filter(c -> getTitle(c).matches("Change type of 'name' to 'String'")).findFirst().orElse(null);
assertNotNull(codeAction);
}
use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class ConvertVarQuickFixTest method testConvertResolvedTypeToVar.
@Test
public void testConvertResolvedTypeToVar() throws Exception {
IPackageFragment pack1 = sourceFolder.createPackageFragment("foo.bar", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package foo.bar;\n");
buf.append("public class Test {\n");
buf.append(" public void test() {\n");
buf.append(" String name = \"test\";\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("Test.java", buf.toString(), false, null);
List<Either<Command, CodeAction>> commands = evaluateCodeActions(cu);
Either<Command, CodeAction> codeAction = commands.stream().filter(c -> getTitle(c).matches("Change type of 'name' to 'var'")).findFirst().orElse(null);
assertNotNull(codeAction);
}
Aggregations