Search in sources :

Example 21 with Command

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) DiagnosticsHandler(org.eclipse.jdt.ls.core.internal.handlers.DiagnosticsHandler) Arrays(java.util.Arrays) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) Range(org.eclipse.lsp4j.Range) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Function(java.util.function.Function) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) CodeActionHandler(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler) Document(org.eclipse.jface.text.Document) TextEdit(org.eclipse.lsp4j.TextEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) Map(java.util.Map) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) BadLocationException(org.eclipse.jface.text.BadLocationException) Position(org.eclipse.lsp4j.Position) Assert.fail(org.junit.Assert.fail) ResourceUtils(org.eclipse.jdt.ls.core.internal.ResourceUtils) TextEditUtil(org.eclipse.jdt.ls.core.internal.TextEditUtil) CodeAction(org.eclipse.lsp4j.CodeAction) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) CoreASTProvider(org.eclipse.jdt.core.manipulation.CoreASTProvider) Command(org.eclipse.lsp4j.Command) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Assert.assertFalse(org.junit.Assert.assertFalse) Entry(java.util.Map.Entry) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) IProblem(org.eclipse.jdt.core.compiler.IProblem) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) ResourceOperation(org.eclipse.lsp4j.ResourceOperation) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeAction(org.eclipse.lsp4j.CodeAction) ArrayList(java.util.ArrayList) IProblem(org.eclipse.jdt.core.compiler.IProblem) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) CodeActionHandler(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler) Command(org.eclipse.lsp4j.Command) Either(org.eclipse.lsp4j.jsonrpc.messages.Either)

Example 22 with Command

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());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Position(org.eclipse.lsp4j.Position) Command(org.eclipse.lsp4j.Command) TextEdit(org.eclipse.lsp4j.TextEdit) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test)

Example 23 with Command

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;
}
Also used : Entry(java.util.Map.Entry) Command(org.eclipse.lsp4j.Command) TextEdit(org.eclipse.lsp4j.TextEdit) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) List(java.util.List)

Example 24 with Command

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);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Test(org.junit.Test)

Example 25 with Command

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);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Test(org.junit.Test)

Aggregations

Command (org.eclipse.lsp4j.Command)95 CodeAction (org.eclipse.lsp4j.CodeAction)58 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)53 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 Test (org.junit.Test)46 Range (org.eclipse.lsp4j.Range)36 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)35 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)26 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)23 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)23 List (java.util.List)22 Position (org.eclipse.lsp4j.Position)22 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)21 AbstractQuickFixTest (org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)17 Diagnostic (org.eclipse.lsp4j.Diagnostic)17 Arrays (java.util.Arrays)14 Collections (java.util.Collections)14 JDTUtils (org.eclipse.jdt.ls.core.internal.JDTUtils)13 CodeActionKind (org.eclipse.lsp4j.CodeActionKind)13 Optional (java.util.Optional)12