Search in sources :

Example 86 with Command

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

the class ConvertMethodReferenceToLambaTest method testMethodReferenceToLambda.

@Test
public void testMethodReferenceToLambda() throws Exception {
    setIgnoredCommands("Assign statement to new field", "Extract to constant", "Extract to field", "Extract to local variable (replace all occurrences)", "Extract to local variable", "Introduce Parameter...", ActionMessages.GenerateConstructorsAction_ellipsisLabel, ActionMessages.GenerateConstructorsAction_label);
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("import java.util.stream.Stream;\n");
    buf.append("public class E {\n");
    buf.append("    private Stream<String> asHex(Stream<Integer> stream) {\n");
    buf.append("        return stream.map(Integer::toHexString);\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    Range range = new Range(new Position(4, 34), new Position(4, 34));
    List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, range);
    assertEquals(1, codeActions.size());
    Either<Command, CodeAction> codeAction = codeActions.get(0);
    CodeAction action = codeAction.getRight();
    assertEquals(CodeActionKind.QuickFix, action.getKind());
    assertEquals("Convert to lambda expression", action.getTitle());
    Command c = action.getCommand();
    assertEquals("java.apply.workspaceEdit", c.getCommand());
    assertNotNull(c.getArguments());
    assertEquals("Convert to lambda expression", c.getTitle());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Position(org.eclipse.lsp4j.Position) Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test)

Example 87 with Command

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

the class MoveTest method evaluateCodeActionCommand.

@Override
protected String evaluateCodeActionCommand(Either<Command, CodeAction> codeAction) throws BadLocationException, JavaModelException {
    Command c = codeAction.isLeft() ? codeAction.getLeft() : codeAction.getRight().getCommand();
    Assert.assertEquals(RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID, c.getCommand());
    Assert.assertNotNull(c.getArguments());
    return "";
}
Also used : Command(org.eclipse.lsp4j.Command)

Example 88 with Command

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

the class AdvancedExtractTest method testExtractVariable.

@Test
public void testExtractVariable() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("class A{\n");
    buf.append("	void m(int i){\n");
    buf.append("		int x= /*]*/0/*[*/;\n");
    buf.append("	}\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    Range selection = getRange(cu, null);
    List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, selection);
    List<Either<Command, CodeAction>> extractCodeActions = codeActions.stream().filter(codeAction -> codeAction.getRight().getKind().startsWith(CodeActionKind.RefactorExtract)).collect(Collectors.toList());
    Assert.assertEquals(5, extractCodeActions.size());
    Command extractConstantCommand = CodeActionHandlerTest.getCommand(extractCodeActions.get(0));
    Assert.assertNotNull(extractConstantCommand);
    Assert.assertEquals(RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID, extractConstantCommand.getCommand());
    Assert.assertNotNull(extractConstantCommand.getArguments());
    Assert.assertEquals(2, extractConstantCommand.getArguments().size());
    Assert.assertEquals(RefactorProposalUtility.EXTRACT_CONSTANT_COMMAND, extractConstantCommand.getArguments().get(0));
    Command extractFieldCommand = CodeActionHandlerTest.getCommand(extractCodeActions.get(1));
    Assert.assertNotNull(extractFieldCommand);
    Assert.assertEquals(RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID, extractFieldCommand.getCommand());
    Assert.assertNotNull(extractFieldCommand.getArguments());
    Assert.assertEquals(3, extractFieldCommand.getArguments().size());
    Assert.assertEquals(RefactorProposalUtility.EXTRACT_FIELD_COMMAND, extractFieldCommand.getArguments().get(0));
    Command extractMethodCommand = CodeActionHandlerTest.getCommand(extractCodeActions.get(2));
    Assert.assertNotNull(extractMethodCommand);
    Assert.assertEquals(RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID, extractMethodCommand.getCommand());
    Assert.assertNotNull(extractMethodCommand.getArguments());
    Assert.assertEquals(2, extractMethodCommand.getArguments().size());
    Assert.assertEquals(RefactorProposalUtility.EXTRACT_METHOD_COMMAND, extractMethodCommand.getArguments().get(0));
    Command extractVariableAllCommand = CodeActionHandlerTest.getCommand(extractCodeActions.get(3));
    Assert.assertNotNull(extractVariableAllCommand);
    Assert.assertEquals(RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID, extractVariableAllCommand.getCommand());
    Assert.assertNotNull(extractVariableAllCommand.getArguments());
    Assert.assertEquals(2, extractVariableAllCommand.getArguments().size());
    Assert.assertEquals(RefactorProposalUtility.EXTRACT_VARIABLE_ALL_OCCURRENCE_COMMAND, extractVariableAllCommand.getArguments().get(0));
    Command extractVariableCommand = CodeActionHandlerTest.getCommand(extractCodeActions.get(4));
    Assert.assertNotNull(extractVariableCommand);
    Assert.assertEquals(RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID, extractVariableCommand.getCommand());
    Assert.assertNotNull(extractVariableCommand.getArguments());
    Assert.assertEquals(2, extractVariableCommand.getArguments().size());
    Assert.assertEquals(RefactorProposalUtility.EXTRACT_VARIABLE_COMMAND, extractVariableCommand.getArguments().get(0));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CodeAction(org.eclipse.lsp4j.CodeAction) ASTNode(org.eclipse.jdt.core.dom.ASTNode) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) IJavaProject(org.eclipse.jdt.core.IJavaProject) JavaCodeActionKind(org.eclipse.jdt.ls.core.internal.JavaCodeActionKind) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) RefactorProposalUtility(org.eclipse.jdt.ls.core.internal.text.correction.RefactorProposalUtility) List(java.util.List) Command(org.eclipse.lsp4j.Command) ClientPreferences(org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) TestOptions(org.eclipse.jdt.ls.core.internal.correction.TestOptions) AbstractSelectionTest(org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest) CodeActionHandlerTest(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandlerTest) Assert(org.junit.Assert) Hashtable(java.util.Hashtable) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Before(org.junit.Before) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Command(org.eclipse.lsp4j.Command) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test) AbstractSelectionTest(org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest) CodeActionHandlerTest(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandlerTest)

Example 89 with Command

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

the class AdvancedExtractTest method testMoveStaticMember.

@Test
public void testMoveStaticMember() throws Exception {
    when(preferenceManager.getClientPreferences().isMoveRefactoringSupported()).thenReturn(true);
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    public static void print() {\n");
    buf.append("        /*[*//*]*/\n");
    buf.append("    }\n");
    buf.append("}");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    Range selection = getRange(cu, null);
    List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, selection);
    Assert.assertNotNull(codeActions);
    Either<Command, CodeAction> moveAction = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.REFACTOR_MOVE);
    Assert.assertNotNull(moveAction);
    Command moveCommand = CodeActionHandlerTest.getCommand(moveAction);
    Assert.assertNotNull(moveCommand);
    Assert.assertEquals(RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID, moveCommand.getCommand());
    Assert.assertNotNull(moveCommand.getArguments());
    Assert.assertEquals(3, moveCommand.getArguments().size());
    Assert.assertEquals(RefactorProposalUtility.MOVE_STATIC_MEMBER_COMMAND, moveCommand.getArguments().get(0));
    Assert.assertTrue(moveCommand.getArguments().get(2) instanceof RefactorProposalUtility.MoveMemberInfo);
    Assert.assertEquals(fJProject1.getProject().getName(), ((RefactorProposalUtility.MoveMemberInfo) moveCommand.getArguments().get(2)).projectName);
    Assert.assertEquals("print()", ((RefactorProposalUtility.MoveMemberInfo) moveCommand.getArguments().get(2)).displayName);
    Assert.assertEquals(ASTNode.METHOD_DECLARATION, ((RefactorProposalUtility.MoveMemberInfo) moveCommand.getArguments().get(2)).memberType);
    Assert.assertEquals("test1.E", ((RefactorProposalUtility.MoveMemberInfo) moveCommand.getArguments().get(2)).enclosingTypeName);
}
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) RefactorProposalUtility(org.eclipse.jdt.ls.core.internal.text.correction.RefactorProposalUtility) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test) AbstractSelectionTest(org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest) CodeActionHandlerTest(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandlerTest)

Example 90 with Command

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

the class AdvancedExtractTest method testMoveFile.

@Test
public void testMoveFile() throws Exception {
    when(preferenceManager.getClientPreferences().isMoveRefactoringSupported()).thenReturn(true);
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("\n");
    buf.append("public /*[*/class E /*]*/{\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    Range selection = getRange(cu, null);
    List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, selection);
    Assert.assertNotNull(codeActions);
    Either<Command, CodeAction> moveAction = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.REFACTOR_MOVE);
    Assert.assertNotNull(moveAction);
    Command moveCommand = CodeActionHandlerTest.getCommand(moveAction);
    Assert.assertNotNull(moveCommand);
    Assert.assertEquals(RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID, moveCommand.getCommand());
    Assert.assertNotNull(moveCommand.getArguments());
    Assert.assertEquals(3, moveCommand.getArguments().size());
    Assert.assertEquals(RefactorProposalUtility.MOVE_FILE_COMMAND, moveCommand.getArguments().get(0));
    Assert.assertTrue(moveCommand.getArguments().get(2) instanceof RefactorProposalUtility.MoveFileInfo);
    Assert.assertEquals(JDTUtils.toURI(cu), ((RefactorProposalUtility.MoveFileInfo) moveCommand.getArguments().get(2)).uri);
}
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) RefactorProposalUtility(org.eclipse.jdt.ls.core.internal.text.correction.RefactorProposalUtility) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test) AbstractSelectionTest(org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest) CodeActionHandlerTest(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandlerTest)

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