Search in sources :

Example 76 with Command

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));
    }
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Range(org.eclipse.lsp4j.Range) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) Test(org.junit.Test)

Example 77 with Command

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());
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) Range(org.eclipse.lsp4j.Range) Ignore(org.junit.Ignore) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) Test(org.junit.Test)

Example 78 with Command

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));
    }
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) Diagnostic(org.eclipse.lsp4j.Diagnostic) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Range(org.eclipse.lsp4j.Range) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) Test(org.junit.Test)

Example 79 with Command

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());
}
Also used : CodeLens(org.eclipse.lsp4j.CodeLens) Command(org.eclipse.lsp4j.Command) Position(org.eclipse.lsp4j.Position) List(java.util.List) Range(org.eclipse.lsp4j.Range) Lsp4jAssertions.assertRange(org.eclipse.jdt.ls.core.internal.Lsp4jAssertions.assertRange) Location(org.eclipse.lsp4j.Location) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 80 with Command

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());
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) 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