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