Search in sources :

Example 26 with Command

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

the class IntroduceParameterRefactorTest method testIntroduceParameterAction.

private void testIntroduceParameterAction(ICompilationUnit cu, Range range) throws JavaModelException {
    List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, range);
    assertEquals(1, codeActions.size());
    Either<Command, CodeAction> codeAction = codeActions.get(0);
    CodeAction action = codeAction.getRight();
    assertEquals(JavaCodeActionKind.REFACTOR_INTRODUCE_PARAMETER, action.getKind());
    assertEquals("Introduce Parameter...", action.getTitle());
    Command c = action.getCommand();
    assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand());
    assertEquals(1, c.getArguments().size());
    WorkspaceEdit workspaceEdit = (WorkspaceEdit) c.getArguments().get(0);
    Map<String, List<TextEdit>> changes = workspaceEdit.getChanges();
    List<TextEdit> textEdits = changes.values().iterator().next();
    assertEquals(1, textEdits.size());
    TextEdit textEdit = textEdits.get(0);
    assertEquals("\"Hello World\");\r\n    }\r\n\r\n    private void greeting(String string) {\r\n        System.out.println(string", textEdit.getNewText());
}
Also used : Command(org.eclipse.lsp4j.Command) TextEdit(org.eclipse.lsp4j.TextEdit) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) List(java.util.List)

Example 27 with Command

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

the class IntroduceParameterRefactorTest method testIntroduceParameterCommand.

private void testIntroduceParameterCommand(ICompilationUnit cu, Range range) throws JavaModelException {
    List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, range);
    assertEquals(1, codeActions.size());
    Either<Command, CodeAction> codeAction = codeActions.get(0);
    CodeAction action = codeAction.getRight();
    assertEquals(JavaCodeActionKind.REFACTOR_INTRODUCE_PARAMETER, action.getKind());
    assertEquals("Introduce Parameter...", action.getTitle());
    Command c = action.getCommand();
    assertEquals(RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID, c.getCommand());
    assertNotNull(c.getArguments());
    assertEquals(RefactorProposalUtility.INTRODUCE_PARAMETER_COMMAND, c.getArguments().get(0));
}
Also used : Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either)

Example 28 with Command

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

the class ReorgQuickFixTest method testWrongPackageStatementInEnum.

@Test
public void testWrongPackageStatementInEnum() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test2;\n");
    buf.append("\n");
    buf.append("public enum 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 enum 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 29 with Command

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

the class GenerateAccessorsActionTest method testGenerateAccessorsDisabled_emptyFields.

@Test
public void testGenerateAccessorsDisabled_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> generateAccessorsAction = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.SOURCE_GENERATE_ACCESSORS);
    Assert.assertNull(generateAccessorsAction);
}
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)

Example 30 with Command

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

the class GenerateAccessorsActionTest method testGenerateAccessorsEnabled.

@Test
public void testGenerateAccessorsEnabled() throws JavaModelException {
    // @formatter:off
    ICompilationUnit unit = fPackageP.createCompilationUnit("A.java", "package p;\r\n" + "\r\n" + "public class A {\r\n" + "	String name;\r\n" + "}", true, null);
    // @formatter:on
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "String name");
    List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join();
    Assert.assertNotNull(codeActions);
    Either<Command, CodeAction> generateAccessorsAction = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.SOURCE_GENERATE_ACCESSORS);
    Assert.assertNotNull(generateAccessorsAction);
    Command generateAccessorsCommand = CodeActionHandlerTest.getCommand(generateAccessorsAction);
    Assert.assertNotNull(generateAccessorsCommand);
    Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, generateAccessorsCommand.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