Search in sources :

Example 91 with Command

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

the class MissingEnumQuickFixTest method testMissingEnumConstantDespiteDefault.

@Test
public void testMissingEnumConstantDespiteDefault() 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("        default:\n");
    buf.append("            break;\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(0, codeActions.size());
    Map<String, String> options = fJProject.getOptions(true);
    options.put(JavaCore.COMPILER_PB_MISSING_ENUM_CASE_DESPITE_DEFAULT, JavaCore.ENABLED);
    fJProject.setOptions(options);
    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 missing case statements", action.getTitle());
    TextEdit edit = getTextEdit(codeAction);
    assertEquals("\n        case One:", edit.getNewText());
    codeAction = codeActions.get(1);
    action = codeAction.getRight();
    assertEquals(CodeActionKind.QuickFix, action.getKind());
    assertEquals("Insert '//$CASES-OMITTED$'", action.getTitle());
    edit = getTextEdit(codeAction);
    assertEquals("            //$CASES-OMITTED$\n        ", edit.getNewText());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Position(org.eclipse.lsp4j.Position) CodeAction(org.eclipse.lsp4j.CodeAction) Range(org.eclipse.lsp4j.Range) Command(org.eclipse.lsp4j.Command) TextEdit(org.eclipse.lsp4j.TextEdit) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Test(org.junit.Test)

Example 92 with Command

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

the class ReorgQuickFixTest method testWrongDefaultPackageStatement.

@Test
public void testWrongDefaultPackageStatement() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test2", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("public class E {\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu);
    Either<Command, CodeAction> codeAction = findAction(codeActions, "Add package declaration 'test2;'");
    assertNotNull(codeAction);
    buf = new StringBuilder();
    buf.append("package test2;\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("}\n");
    assertEquals(buf.toString(), evaluateCodeActionCommand(codeAction));
    codeAction = findAction(codeActions, "Move 'E.java' to the default package");
    assertNotNull(codeAction);
    assertRenameFileOperation(codeAction, ResourceUtils.fixURI(pack1.getResource().getRawLocation().append("../E.java").toFile().toURI()));
}
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 93 with Command

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

the class ReorgQuickFixTest method testWrongPackageStatement.

@Test
public void testWrongPackageStatement() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test2;\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu);
    Either<Command, CodeAction> codeAction = findAction(codeActions, "Change package declaration to 'test1'");
    assertNotNull(codeAction);
    buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("}\n");
    assertEquals(buf.toString(), evaluateCodeActionCommand(codeAction));
    codeAction = findAction(codeActions, "Move 'E.java' to package 'test2'");
    assertNotNull(codeAction);
    assertRenameFileOperation(codeAction, ResourceUtils.fixURI(cu.getResource().getRawLocationURI()).replace("test1", "test2"));
}
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 94 with Command

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

the class ReorgQuickFixTest method getWorkspaceEdit.

private WorkspaceEdit getWorkspaceEdit(Either<Command, CodeAction> codeAction) {
    Command c = codeAction.isLeft() ? codeAction.getLeft() : codeAction.getRight().getCommand();
    assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand());
    assertNotNull(c.getArguments());
    assertTrue(c.getArguments().get(0) instanceof WorkspaceEdit);
    return (WorkspaceEdit) c.getArguments().get(0);
}
Also used : Command(org.eclipse.lsp4j.Command) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit)

Example 95 with Command

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

the class ReorgQuickFixTest method testWrongPackageStatementFromDefault.

@Test
public void testWrongPackageStatementFromDefault() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test2;\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu);
    Either<Command, CodeAction> codeAction = findAction(codeActions, "Remove package declaration 'package test2'");
    assertNotNull(codeAction);
    buf = new StringBuilder();
    buf.append("\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("}\n");
    assertEquals(buf.toString(), evaluateCodeActionCommand(codeAction));
    codeAction = findAction(codeActions, "Move 'E.java' to package 'test2'");
    assertNotNull(codeAction);
    assertRenameFileOperation(codeAction, ResourceUtils.fixURI(pack1.getResource().getRawLocation().append("test2/E.java").toFile().toURI()));
}
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