use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class CodeActionHandlerTest method testCodeAction_refactorActionsOnly.
@Test
public void testCodeAction_refactorActionsOnly() throws Exception {
ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "public class Foo {\n" + " void foo() {\n" + " String bar = \"astring\";" + " }\n" + "}\n");
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, "bar");
params.setRange(range);
CodeActionContext context = new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.LocalVariableIsNeverUsed), range)), Collections.singletonList(CodeActionKind.Refactor));
params.setContext(context);
List<Either<Command, CodeAction>> refactorActions = getCodeActions(params);
Assert.assertNotNull(refactorActions);
Assert.assertFalse("No refactor actions were found", refactorActions.isEmpty());
for (Either<Command, CodeAction> codeAction : refactorActions) {
Assert.assertTrue("Unexpected kind:" + codeAction.getRight().getKind(), codeAction.getRight().getKind().startsWith(CodeActionKind.Refactor));
}
}
use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class CodeActionHandlerTest method testCodeAction_superfluousSemicolon.
@Test
@Ignore
public void testCodeAction_superfluousSemicolon() throws Exception {
ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "public class Foo {\n" + " void foo() {\n" + ";" + " }\n" + "}\n");
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, ";");
params.setRange(range);
params.setContext(new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.SuperfluousSemicolon), range))));
List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
Assert.assertNotNull(codeActions);
Assert.assertEquals(1, codeActions.size());
Assert.assertEquals(codeActions.get(0), CodeActionKind.QuickFix);
Command c = getCommand(codeActions.get(0));
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);
List<org.eclipse.lsp4j.TextEdit> edits = we.getChanges().get(JDTUtils.toURI(unit));
Assert.assertEquals(1, edits.size());
Assert.assertEquals("", edits.get(0).getNewText());
Assert.assertEquals(range, edits.get(0).getRange());
}
use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class CodeActionHandlerTest method testCodeAction_errorFromOtherSources.
@Test
public void testCodeAction_errorFromOtherSources() throws Exception {
ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "public class Foo {\n" + " void foo() {\n" + " Integer bar = 2000;" + " }\n" + "}\n");
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, "bar");
params.setRange(range);
Diagnostic diagnostic = new Diagnostic();
diagnostic.setCode("MagicNumberCheck");
diagnostic.setRange(range);
diagnostic.setSeverity(DiagnosticSeverity.Error);
diagnostic.setMessage("'2000' is a magic number.");
diagnostic.setSource("Checkstyle");
CodeActionContext context = new CodeActionContext(Arrays.asList(diagnostic), Collections.singletonList(CodeActionKind.Refactor));
params.setContext(context);
List<Either<Command, CodeAction>> refactorActions = getCodeActions(params);
Assert.assertNotNull(refactorActions);
Assert.assertFalse("No refactor actions were found", refactorActions.isEmpty());
for (Either<Command, CodeAction> codeAction : refactorActions) {
Assert.assertTrue("Unexpected kind:" + codeAction.getRight().getKind(), codeAction.getRight().getKind().startsWith(CodeActionKind.Refactor));
}
}
use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class CodeLensHandlerTest method testResolveCodeLense.
@SuppressWarnings("unchecked")
@Test
public void testResolveCodeLense() {
String source = "src/java/Foo.java";
String payload = createCodeLensRequest(source, 5, 13, 16);
CodeLens lens = getParams(payload);
Range range = lens.getRange();
assertRange(5, 13, 16, range);
CodeLens result = handler.resolve(lens, monitor);
assertNotNull(result);
// Check if command found
Command command = result.getCommand();
assertNotNull(command);
assertEquals("1 reference", command.getTitle());
assertEquals("java.show.references", command.getCommand());
// Check codelens args
List<Object> args = command.getArguments();
assertEquals(3, args.size());
// Check we point to the Bar class
String sourceUri = args.get(0).toString();
assertTrue(sourceUri.endsWith(source));
// CodeLens position
Position p = (Position) args.get(1);
assertEquals(5, p.getLine());
assertEquals(13, p.getCharacter());
// Reference location
List<Location> locations = (List<Location>) args.get(2);
assertEquals(1, locations.size());
Location loc = locations.get(0);
assertTrue(loc.getUri().endsWith("src/java/Bar.java"));
assertRange(5, 25, 28, loc.getRange());
}
use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class GenerateConstructorsActionTest method testGenerateConstructorsEnabled_emptyFields.
@Test
public void testGenerateConstructorsEnabled_emptyFields() throws JavaModelException {
// @formatter:off
ICompilationUnit unit = fPackageP.createCompilationUnit("A.java", "package p;\r\n" + "\r\n" + "public class A {\r\n" + "}", true, null);
// @formatter:on
CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "class A");
List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join();
Assert.assertNotNull(codeActions);
Either<Command, CodeAction> constructorAction = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.SOURCE_GENERATE_CONSTRUCTORS);
Assert.assertNotNull(constructorAction);
Command constructorCommand = CodeActionHandlerTest.getCommand(constructorAction);
Assert.assertNotNull(constructorCommand);
Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, constructorCommand.getCommand());
}
Aggregations