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