From 9515f732805a978224415cb51cfef2985af82a36 Mon Sep 17 00:00:00 2001 From: Boris Staletic Date: Sat, 9 Nov 2019 20:29:38 +0100 Subject: [PATCH] Take advantage of CodeActionLiteral client capability. If client advertises `CodeActionLiteralSupport`, using that for `java.apply.workspaceEdit` would allow clients to use a generic algorithm, instead of being forced to provide a special case for jdt.ls. Fixes #376 Signed-off-by: Boris Staletic Signed-off-by: Anton Romanov Signed-off-by: Roland Grunberg --- .../internal/handlers/CodeActionHandler.java | 12 +- .../preferences/ClientPreferences.java | 2 +- .../correction/SourceAssistProcessor.java | 10 +- .../correction/AbstractQuickFixTest.java | 21 +++- .../correction/ReorgQuickFixTest.java | 16 ++- .../handlers/CodeActionHandlerTest.java | 110 +++++++++++++++--- .../handlers/GenerateAccessorsActionTest.java | 42 +++---- .../GenerateConstructorsActionTest.java | 1 + ...GenerateFinalModifiersQuickAssistTest.java | 24 ++-- .../handlers/GenerateToStringActionTest.java | 10 +- .../handlers/SortMembersActionTest.java | 21 ++-- 11 files changed, 180 insertions(+), 89 deletions(-) diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/CodeActionHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/CodeActionHandler.java index 91a0d95b13..6be5f60cd7 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/CodeActionHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/CodeActionHandler.java @@ -288,6 +288,7 @@ private Optional> getCodeActionFromProposal(String u String name = proposal.getName(); Command command = null; + WorkspaceEdit edit = null; if (proposal instanceof CUCorrectionCommandProposal commandProposal) { command = new Command(name, commandProposal.getCommand(), commandProposal.getCommandArguments()); } else if (proposal instanceof RefactoringCorrectionCommandProposal commandProposal) { @@ -296,7 +297,7 @@ private Optional> getCodeActionFromProposal(String u command = new Command(name, commandProposal.getCommand(), commandProposal.getCommandArguments()); } else { if (!this.preferenceManager.getClientPreferences().isResolveCodeActionSupported()) { - WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(proposal.getChange()); + edit = ChangeUtil.convertToWorkspaceEdit(proposal.getChange()); if (!ChangeUtil.hasChanges(edit)) { return Optional.empty(); } @@ -305,7 +306,6 @@ private Optional> getCodeActionFromProposal(String u } if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(pk.getKind())) { - // TODO: Should set WorkspaceEdit directly instead of Command CodeAction codeAction = new CodeAction(name); codeAction.setKind(pk.getKind()); if (command == null) { // lazy resolve the edit. @@ -324,7 +324,13 @@ private Optional> getCodeActionFromProposal(String u codeAction.setData(new CodeActionData(proposal, -proposal.getRelevance())); } else { codeAction.setCommand(command); - codeAction.setData(new CodeActionData(null, -proposal.getRelevance())); + if (edit != null) { + // no code action resolve support, but have code action literal support + codeAction.setEdit(edit); + } else { + codeAction.setCommand(command); + codeAction.setData(new CodeActionData(null, -proposal.getRelevance())); + } } if (pk.getKind() != JavaCodeActionKind.QUICK_ASSIST) { codeAction.setDiagnostics(context.getDiagnostics()); diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/ClientPreferences.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/ClientPreferences.java index 912319173b..ca6da2ac82 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/ClientPreferences.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/ClientPreferences.java @@ -346,7 +346,7 @@ public boolean isHierarchicalDocumentSymbolSupported() { } /** - * {@code true} if the client has explicitly set the + * {@code true} if the client has listed {@code kind} in * {@code textDocument.codeAction.codeActionLiteralSupport.codeActionKind.valueSet} * value. Otherwise, {@code false}. */ diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/text/correction/SourceAssistProcessor.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/text/correction/SourceAssistProcessor.java index 54092e14e0..c69f8ebbb2 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/text/correction/SourceAssistProcessor.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/text/correction/SourceAssistProcessor.java @@ -614,7 +614,7 @@ private Optional> getFinalModifierWherePossibleActio CodeAction codeAction = new CodeAction(actionMessage); codeAction.setKind(kind); codeAction.setData(new CodeActionData(proposal, CodeActionComparator.CHANGE_MODIFIER_TO_FINAL_PRIORITY)); - codeAction.setDiagnostics(Collections.EMPTY_LIST); + codeAction.setDiagnostics(Collections.emptyList()); return Optional.of(Either.forRight(codeAction)); } else { WorkspaceEdit edit; @@ -632,9 +632,8 @@ private Optional> getFinalModifierWherePossibleActio if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(kind)) { CodeAction codeAction = new CodeAction(actionMessage); codeAction.setKind(kind); - codeAction.setCommand(command); - codeAction.setData(new CodeActionData(null, CodeActionComparator.CHANGE_MODIFIER_TO_FINAL_PRIORITY)); - codeAction.setDiagnostics(Collections.EMPTY_LIST); + codeAction.setEdit(edit); + codeAction.setDiagnostics(Collections.emptyList()); return Optional.of(Either.forRight(codeAction)); } else { return Optional.of(Either.forLeft(command)); @@ -713,8 +712,7 @@ private Optional> getCodeActionFromProposal(CodeActi if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(kind)) { CodeAction codeAction = new CodeAction(name); codeAction.setKind(kind); - codeAction.setCommand(command); - codeAction.setData(new CodeActionData(null, priority)); + codeAction.setEdit(edit); codeAction.setDiagnostics(context.getDiagnostics()); return Optional.of(Either.forRight(codeAction)); } else { diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/AbstractQuickFixTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/AbstractQuickFixTest.java index 3f5d6fff86..7884c27ada 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/AbstractQuickFixTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/AbstractQuickFixTest.java @@ -315,11 +315,19 @@ protected String evaluateCodeActionCommand(Either codeActio throws BadLocationException, JavaModelException { Command c = codeAction.isLeft() ? codeAction.getLeft() : codeAction.getRight().getCommand(); - 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); - return evaluateWorkspaceEdit(we); + if (c != null) { + 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); + return evaluateWorkspaceEdit(we); + } else { + WorkspaceEdit we = codeAction.getRight().getEdit(); + if (we.getDocumentChanges() != null) { + return evaluateChanges(we.getDocumentChanges()); + } + return evaluateChanges(we.getChanges()); + } } public static String evaluateWorkspaceEdit(WorkspaceEdit edit) throws JavaModelException, BadLocationException { @@ -367,7 +375,8 @@ public Command getCommand(Either codeAction) { } public String getTitle(Either codeAction) { - return ResourceUtils.dos2Unix(getCommand(codeAction).getTitle()); + Command command = getCommand(codeAction); + return ResourceUtils.dos2Unix(command != null ? command.getTitle() : codeAction.getRight().getTitle()); } } diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/ReorgQuickFixTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/ReorgQuickFixTest.java index b4338150e6..f4d26ebf26 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/ReorgQuickFixTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/ReorgQuickFixTest.java @@ -215,10 +215,16 @@ private Either findAction(List> private WorkspaceEdit getWorkspaceEdit(Either codeAction) { Command c = codeAction.isLeft() ? codeAction.getLeft() : codeAction.getRight().getCommand(); - assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand()); - assertNotNull(c.getArguments()); - assertTrue(c.getArguments().get(0) instanceof WorkspaceEdit); - return (WorkspaceEdit) c.getArguments().get(0); + if (c != null) { + assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand()); + assertNotNull(c.getArguments()); + assertTrue(c.getArguments().get(0) instanceof WorkspaceEdit); + return (WorkspaceEdit) c.getArguments().get(0); + } else { + WorkspaceEdit e = (WorkspaceEdit) codeAction.getRight().getEdit(); + assertNotNull(e); + return e; + } } private void assertRenameFileOperation(Either codeAction, String newUri) { @@ -455,4 +461,4 @@ public void testWrongTypeNameInAnnot() throws Exception { Expected e1 = new Expected("Rename type to 'E'", buf.toString()); assertCodeActions(cu, e1); } -} \ No newline at end of file +} diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/CodeActionHandlerTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/CodeActionHandlerTest.java index 0d1ae5af0c..6cceb7e127 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/CodeActionHandlerTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/CodeActionHandlerTest.java @@ -43,6 +43,7 @@ import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; import org.eclipse.jdt.ls.core.internal.LanguageServerWorkingCopyOwner; import org.eclipse.jdt.ls.core.internal.ProjectUtils; +import org.eclipse.jdt.ls.core.internal.ResourceUtils; import org.eclipse.jdt.ls.core.internal.WorkspaceHelper; import org.eclipse.jdt.ls.core.internal.codemanipulation.AbstractSourceTestCase; import org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest; @@ -120,8 +121,34 @@ public void testCodeAction_removeUnusedImport() throws Exception{ List> organizeImportActions = findActions(codeActions, CodeActionKind.SourceOrganizeImports); Assert.assertNotNull(organizeImportActions); Assert.assertEquals(1, organizeImportActions.size()); - Command c = codeActions.get(0).getRight().getCommand(); - Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand()); + WorkspaceEdit e = codeActions.get(0).getRight().getEdit(); + Assert.assertNotNull(e); + } + + @Test + public void testCodeActionLiteral_removeUnusedImport() throws Exception{ + when(preferenceManager.getClientPreferences().isSupportedCodeActionKind(CodeActionKind.QuickFix)).thenReturn(true); + ICompilationUnit unit = getWorkingCopy( + "src/java/Foo.java", + "import java.sql.*; \n" + + "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, "java.sql"); + params.setRange(range); + params.setContext(new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.UnusedImport), range)))); + List> codeActions = getCodeActions(params); + Assert.assertNotNull(codeActions); + Assert.assertTrue(codeActions.size() >= 3); + Assert.assertEquals(codeActions.get(0).getRight().getKind(), CodeActionKind.QuickFix); + Assert.assertEquals(codeActions.get(1).getRight().getKind(), JavaCodeActionKind.QUICK_ASSIST); + Assert.assertEquals(codeActions.get(2).getRight().getKind(), CodeActionKind.SourceOrganizeImports); + WorkspaceEdit w = codeActions.get(0).getRight().getEdit(); + Assert.assertNotNull(w); } @Test @@ -192,13 +219,13 @@ public void testCodeAction_organizeImportsQuickFix() throws Exception { Assert.assertNotNull(codeActions); List> quickAssistActions = findActions(codeActions, JavaCodeActionKind.QUICK_ASSIST); - Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, CorrectionMessages.ReorgCorrectionsSubProcessor_organizeimports_description)); + Assert.assertTrue(CodeActionHandlerTest.titleExists(quickAssistActions, CorrectionMessages.ReorgCorrectionsSubProcessor_organizeimports_description)); // Test if the quick assist exists only for type declaration params = CodeActionUtil.constructCodeActionParams(unit, "String bar"); codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); quickAssistActions = CodeActionHandlerTest.findActions(codeActions, JavaCodeActionKind.QUICK_ASSIST); - Assert.assertFalse(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, CorrectionMessages.ReorgCorrectionsSubProcessor_organizeimports_description)); + Assert.assertFalse(CodeActionHandlerTest.titleExists(quickAssistActions, CorrectionMessages.ReorgCorrectionsSubProcessor_organizeimports_description)); } @Test @@ -403,8 +430,32 @@ public void testCodeAction_removeUnterminatedString() throws Exception{ Assert.assertNotNull(codeActions); Assert.assertFalse(codeActions.isEmpty()); Assert.assertEquals(codeActions.get(0).getRight().getKind(), CodeActionKind.QuickFix); - Command c = codeActions.get(0).getRight().getCommand(); - Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand()); + WorkspaceEdit e = codeActions.get(0).getRight().getEdit(); + Assert.assertNotNull(e); + } + + @Test + public void testCodeActionLiteral_removeUnterminatedString() throws Exception{ + when(preferenceManager.getClientPreferences().isSupportedCodeActionKind(CodeActionKind.QuickFix)).thenReturn(true); + ICompilationUnit unit = getWorkingCopy( + "src/java/Foo.java", + "public class Foo {\n"+ + " void foo() {\n"+ + "String s = \"some str\n" + + " }\n"+ + "}\n"); + + CodeActionParams params = new CodeActionParams(); + params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit))); + final Range range = CodeActionUtil.getRange(unit, "some str"); + params.setRange(range); + params.setContext(new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.UnterminatedString), range)))); + List> codeActions = getCodeActions(params); + Assert.assertNotNull(codeActions); + Assert.assertFalse(codeActions.isEmpty()); + Assert.assertEquals(codeActions.get(0).getRight().getKind(), CodeActionKind.QuickFix); + WorkspaceEdit w = codeActions.get(0).getRight().getEdit(); + Assert.assertNotNull(w); } @Test @@ -655,7 +706,7 @@ public void testQuickAssistForImportDeclarationOrder() throws JavaModelException List> codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); List> quickAssistActions = CodeActionHandlerTest.findActions(codeActions, JavaCodeActionKind.QUICK_ASSIST); - Assert.assertEquals(CodeActionHandlerTest.getCommand(quickAssistActions.get(0)).getTitle(), "Organize imports"); + Assert.assertEquals(CodeActionHandlerTest.getTitle(quickAssistActions.get(0)), "Organize imports"); } @Test @@ -672,10 +723,10 @@ public void testQuickAssistForFieldDeclarationOrder() throws JavaModelException List> codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); List> quickAssistActions = CodeActionHandlerTest.findActions(codeActions, JavaCodeActionKind.QUICK_ASSIST); - Assert.assertEquals(CodeActionHandlerTest.getCommand(quickAssistActions.get(0)).getTitle(), "Generate Getter and Setter for 'name'"); - Assert.assertEquals(CodeActionHandlerTest.getCommand(quickAssistActions.get(1)).getTitle(), "Generate Getter for 'name'"); - Assert.assertEquals(CodeActionHandlerTest.getCommand(quickAssistActions.get(2)).getTitle(), "Generate Setter for 'name'"); - Assert.assertEquals(CodeActionHandlerTest.getCommand(quickAssistActions.get(3)).getTitle(), "Generate Constructors..."); + Assert.assertEquals(CodeActionHandlerTest.getTitle(quickAssistActions.get(0)), "Generate Getter and Setter for 'name'"); + Assert.assertEquals(CodeActionHandlerTest.getTitle(quickAssistActions.get(1)), "Generate Getter for 'name'"); + Assert.assertEquals(CodeActionHandlerTest.getTitle(quickAssistActions.get(2)), "Generate Setter for 'name'"); + Assert.assertEquals(CodeActionHandlerTest.getTitle(quickAssistActions.get(3)), "Generate Constructors..."); } @Test @@ -692,13 +743,13 @@ public void testQuickAssistForTypeDeclarationOrder() throws JavaModelException { List> codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); List> quickAssistActions = CodeActionHandlerTest.findActions(codeActions, JavaCodeActionKind.QUICK_ASSIST); - Assert.assertEquals(CodeActionHandlerTest.getCommand(quickAssistActions.get(0)).getTitle(), "Generate Getters and Setters"); - Assert.assertEquals(CodeActionHandlerTest.getCommand(quickAssistActions.get(1)).getTitle(), "Generate Getters"); - Assert.assertEquals(CodeActionHandlerTest.getCommand(quickAssistActions.get(2)).getTitle(), "Generate Setters"); - Assert.assertEquals(CodeActionHandlerTest.getCommand(quickAssistActions.get(3)).getTitle(), "Generate Constructors..."); - Assert.assertEquals(CodeActionHandlerTest.getCommand(quickAssistActions.get(4)).getTitle(), "Generate hashCode() and equals()..."); - Assert.assertEquals(CodeActionHandlerTest.getCommand(quickAssistActions.get(5)).getTitle(), "Generate toString()..."); - Assert.assertEquals(CodeActionHandlerTest.getCommand(quickAssistActions.get(6)).getTitle(), "Override/Implement Methods..."); + Assert.assertEquals(CodeActionHandlerTest.getTitle(quickAssistActions.get(0)), "Generate Getters and Setters"); + Assert.assertEquals(CodeActionHandlerTest.getTitle(quickAssistActions.get(1)), "Generate Getters"); + Assert.assertEquals(CodeActionHandlerTest.getTitle(quickAssistActions.get(2)), "Generate Setters"); + Assert.assertEquals(CodeActionHandlerTest.getTitle(quickAssistActions.get(3)), "Generate Constructors..."); + Assert.assertEquals(CodeActionHandlerTest.getTitle(quickAssistActions.get(4)), "Generate hashCode() and equals()..."); + Assert.assertEquals(CodeActionHandlerTest.getTitle(quickAssistActions.get(5)), "Generate toString()..."); + Assert.assertEquals(CodeActionHandlerTest.getTitle(quickAssistActions.get(6)), "Override/Implement Methods..."); } private List> getCodeActions(CodeActionParams params) { @@ -709,6 +760,17 @@ public static Command getCommand(Either codeAction) { return codeAction.isLeft() ? codeAction.getLeft() : codeAction.getRight().getCommand(); } + public static String getTitle(Either codeAction) { + Command command = getCommand(codeAction); + return ResourceUtils.dos2Unix(command != null ? command.getTitle() : codeAction.getRight().getTitle()); + } + + public static WorkspaceEdit getEdit(Either codeAction) { + assertTrue(codeAction.isRight()); + assertNotNull(codeAction.getRight().getEdit()); + return codeAction.getRight().getEdit(); + } + private Diagnostic getDiagnostic(String code, Range range){ Diagnostic $ = new Diagnostic(); $.setCode(code); @@ -769,4 +831,16 @@ public static boolean commandExists(List> codeAction } return false; } + + public static boolean titleExists(List> codeActions, String title) { + if (codeActions.isEmpty()) { + return false; + } + for (Either codeAction : codeActions) { + if (title.equals(getTitle(codeAction))) { + return true; + } + } + return false; + } } diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateAccessorsActionTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateAccessorsActionTest.java index e8f5499fa5..1e54c069c4 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateAccessorsActionTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateAccessorsActionTest.java @@ -33,6 +33,7 @@ import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.CodeActionParams; import org.eclipse.lsp4j.Command; +import org.eclipse.lsp4j.WorkspaceEdit; import org.eclipse.lsp4j.jsonrpc.messages.Either; import org.junit.Assert; import org.junit.Before; @@ -75,9 +76,8 @@ public void testGenerateAccessorsEnabled() throws JavaModelException { Assert.assertNotNull(codeActions); Either 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()); + WorkspaceEdit generateAccessorsEdit = CodeActionHandlerTest.getEdit(generateAccessorsAction); + Assert.assertNotNull(generateAccessorsEdit); } @Test @@ -173,9 +173,9 @@ public void testGenerateAccessorsQuickAssistForTypeDeclaration() throws JavaMode List> codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); List> quickAssistActions = CodeActionHandlerTest.findActions(codeActions, JavaCodeActionKind.QUICK_ASSIST); - Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Generate Getters and Setters")); - Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Generate Getters")); - Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Generate Setters")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(quickAssistActions, "Generate Getters and Setters")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(quickAssistActions, "Generate Getters")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(quickAssistActions, "Generate Setters")); } @Test @@ -193,9 +193,9 @@ public void testGenerateAccessorsQuickAssistForFieldDeclaration() throws JavaMod List> codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); List> quickAssistActions = CodeActionHandlerTest.findActions(codeActions, JavaCodeActionKind.QUICK_ASSIST); - Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Generate Getter and Setter for 'name'")); - Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Generate Getter for 'name'")); - Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Generate Setter for 'name'")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(quickAssistActions, "Generate Getter and Setter for 'name'")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(quickAssistActions, "Generate Getter for 'name'")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(quickAssistActions, "Generate Setter for 'name'")); } @Test @@ -215,9 +215,9 @@ public void testGenerateAccessorsQuickAssistAtLine() throws JavaModelException { List> codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); List> quickAssistActions = CodeActionHandlerTest.findActions(codeActions, JavaCodeActionKind.QUICK_ASSIST); - Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Generate Getter and Setter for 'name'")); - Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Generate Getter for 'name'")); - Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Generate Setter for 'name'")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(quickAssistActions, "Generate Getter and Setter for 'name'")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(quickAssistActions, "Generate Getter for 'name'")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(quickAssistActions, "Generate Setter for 'name'")); } finally { this.preferences.setJavaQuickFixShowAt(value); } @@ -240,9 +240,9 @@ public void testGenerateAccessorsQuickAssistAtLine1() throws JavaModelException List> codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); List> quickAssistActions = CodeActionHandlerTest.findActions(codeActions, JavaCodeActionKind.QUICK_ASSIST); - Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Generate Getter and Setter for 'name'")); - Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Generate Getter for 'name'")); - Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Generate Setter for 'name'")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(quickAssistActions, "Generate Getter and Setter for 'name'")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(quickAssistActions, "Generate Getter for 'name'")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(quickAssistActions, "Generate Setter for 'name'")); } finally { this.preferences.setJavaQuickFixShowAt(value); } @@ -263,9 +263,9 @@ public void testGenerateAccessorsQuickAssistForMultipleFieldDeclaration() throws List> codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); List> quickAssistActions = CodeActionHandlerTest.findActions(codeActions, JavaCodeActionKind.QUICK_ASSIST); - Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Generate Getters and Setters")); - Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Generate Getters")); - Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Generate Setters")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(quickAssistActions, "Generate Getters and Setters")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(quickAssistActions, "Generate Getters")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(quickAssistActions, "Generate Setters")); } @Test @@ -283,8 +283,8 @@ public void testGenerateAccessorsQuickAssistForFinalField() throws JavaModelExce List> codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); List> quickAssistActions = CodeActionHandlerTest.findActions(codeActions, JavaCodeActionKind.QUICK_ASSIST); - Assert.assertFalse(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Generate Getter and Setter for 'name'")); - Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Generate Getter for 'name'")); - Assert.assertFalse(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Generate Setter for 'name'")); + Assert.assertFalse(CodeActionHandlerTest.titleExists(quickAssistActions, "Generate Getter and Setter for 'name'")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(quickAssistActions, "Generate Getter for 'name'")); + Assert.assertFalse(CodeActionHandlerTest.titleExists(quickAssistActions, "Generate Setter for 'name'")); } } diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateConstructorsActionTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateConstructorsActionTest.java index 8889d58473..8671050c27 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateConstructorsActionTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateConstructorsActionTest.java @@ -30,6 +30,7 @@ import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.CodeActionParams; import org.eclipse.lsp4j.Command; +import org.eclipse.lsp4j.WorkspaceEdit; import org.eclipse.lsp4j.jsonrpc.messages.Either; import org.junit.Assert; import org.junit.Before; diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateFinalModifiersQuickAssistTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateFinalModifiersQuickAssistTest.java index bb9a7d20d3..c36bc8bdba 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateFinalModifiersQuickAssistTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateFinalModifiersQuickAssistTest.java @@ -60,15 +60,15 @@ public void testInsertFinalModifierForFieldDeclarationQuickAssist() throws JavaM CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "String name"); List> codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); - Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Add final modifier for 'name'")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(codeActions, "Add final modifier for 'name'")); params = CodeActionUtil.constructCodeActionParams(unit, "a\";"); codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); - Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Add final modifier for 'name'")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(codeActions, "Add final modifier for 'name'")); params = CodeActionUtil.constructCodeActionParams(unit, "test;"); codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); - Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Add final modifier for 'test'")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(codeActions, "Add final modifier for 'test'")); } @Test @@ -85,7 +85,7 @@ public void testInsertFinalModifierForFieldDeclarationsQuickAssist() throws Java CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "private String name;\r\n private String name1 = \"b\";"); List> codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); - Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Change modifiers to final")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(codeActions, "Change modifiers to final")); } @Test @@ -101,11 +101,11 @@ public void testInsertFinalModifierForParameterQuickAssist() throws JavaModelExc CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "b"); List> codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); - Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Add final modifier for 'b'")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(codeActions, "Add final modifier for 'b'")); params = CodeActionUtil.constructCodeActionParams(unit, "String a, String b"); codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); - Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Change modifiers to final")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(codeActions, "Change modifiers to final")); } @Test @@ -124,15 +124,15 @@ public void testInsertFinalModifierForLocalVariableSelectionQuickAssist() throws CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "c"); List> codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); - Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Add final modifier for 'c'")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(codeActions, "Add final modifier for 'c'")); params = CodeActionUtil.constructCodeActionParams(unit, "String c = a;\r\n String d = b;"); codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); - Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Change modifiers to final")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(codeActions, "Change modifiers to final")); params = CodeActionUtil.constructCodeActionParams(unit, "c = a;\r\n String d = b;"); codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); - Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Change modifiers to final")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(codeActions, "Change modifiers to final")); } @Test @@ -151,14 +151,14 @@ public void testInsertFinalModifierForLocalVariableQuickAssist() throws JavaMode CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "c;"); List> codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); - Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Add final modifier for 'c'")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(codeActions, "Add final modifier for 'c'")); params = CodeActionUtil.constructCodeActionParams(unit, "String d, e;"); codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); - Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Change modifiers to final")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(codeActions, "Change modifiers to final")); params = CodeActionUtil.constructCodeActionParams(unit, "d"); codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); - Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Add final modifier for 'd'")); + Assert.assertTrue(CodeActionHandlerTest.titleExists(codeActions, "Add final modifier for 'd'")); } } diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateToStringActionTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateToStringActionTest.java index fb06ef0f8d..6fb7d4d632 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateToStringActionTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateToStringActionTest.java @@ -31,6 +31,7 @@ import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.CodeActionParams; import org.eclipse.lsp4j.Command; +import org.eclipse.lsp4j.WorkspaceEdit; import org.eclipse.lsp4j.jsonrpc.messages.Either; import org.junit.Assert; import org.junit.Before; @@ -93,9 +94,8 @@ public void testGenerateToStringEnabled_emptyFields() throws JavaModelException Assert.assertNotNull(codeActions); Either toStringAction = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.SOURCE_GENERATE_TO_STRING); Assert.assertNotNull(toStringAction); - Command toStringCommand = CodeActionHandlerTest.getCommand(toStringAction); - Assert.assertNotNull(toStringCommand); - Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, toStringCommand.getCommand()); + WorkspaceEdit toStringEdit = CodeActionHandlerTest.getEdit(toStringAction); + Assert.assertNotNull(toStringEdit); } @Test @@ -169,13 +169,13 @@ public void testGenerateToStringQuickAssist_emptyFields() throws JavaModelExcept List> codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); List> quickAssistActions = CodeActionHandlerTest.findActions(codeActions, JavaCodeActionKind.QUICK_ASSIST); - Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, ActionMessages.GenerateToStringAction_label)); + Assert.assertTrue(CodeActionHandlerTest.titleExists(quickAssistActions, ActionMessages.GenerateToStringAction_label)); // Test if the quick assist exists only for type declaration params = CodeActionUtil.constructCodeActionParams(unit, "String name"); codeActions = server.codeAction(params).join(); Assert.assertNotNull(codeActions); quickAssistActions = CodeActionHandlerTest.findActions(codeActions, JavaCodeActionKind.QUICK_ASSIST); - Assert.assertFalse(CodeActionHandlerTest.commandExists(quickAssistActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, ActionMessages.GenerateToStringAction_label)); + Assert.assertFalse(CodeActionHandlerTest.titleExists(quickAssistActions, ActionMessages.GenerateToStringAction_label)); } @Test diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/SortMembersActionTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/SortMembersActionTest.java index 2c41258abd..a3a5c5474c 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/SortMembersActionTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/SortMembersActionTest.java @@ -28,6 +28,7 @@ import org.eclipse.lsp4j.CodeAction; import org.eclipse.lsp4j.CodeActionParams; import org.eclipse.lsp4j.Command; +import org.eclipse.lsp4j.WorkspaceEdit; import org.eclipse.lsp4j.jsonrpc.messages.Either; import org.junit.Assert; import org.junit.Before; @@ -69,9 +70,8 @@ public void testSortMemberActionExists() throws JavaModelException { Assert.assertNotNull(codeActions); Either sortMemberAction = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.SOURCE_SORT_MEMBERS, "Sort Members for 'A.java'"); Assert.assertNotNull(sortMemberAction); - Command sortMemberCommand = CodeActionHandlerTest.getCommand(sortMemberAction); - Assert.assertNotNull(sortMemberCommand); - Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, sortMemberCommand.getCommand()); + WorkspaceEdit sortMemberEdit = CodeActionHandlerTest.getEdit(sortMemberAction); + Assert.assertNotNull(sortMemberEdit); } @Test @@ -109,9 +109,8 @@ public void testSortMemberActionExistsWithVolatileChanges() throws JavaModelExce Assert.assertNotNull(codeActions); Either sortMemberAction = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.SOURCE_SORT_MEMBERS, "Sort Members for 'A.java'"); Assert.assertNotNull(sortMemberAction); - Command sortMemberCommand = CodeActionHandlerTest.getCommand(sortMemberAction); - Assert.assertNotNull(sortMemberCommand); - Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, sortMemberCommand.getCommand()); + WorkspaceEdit sortMemberEdit = CodeActionHandlerTest.getEdit(sortMemberAction); + Assert.assertNotNull(sortMemberEdit); } @Test @@ -131,9 +130,8 @@ public void testSortMemberQuickAssistExistsForTypeDeclaration() throws JavaModel Assert.assertNotNull(codeActions); Either sortMemberQuickAssist = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.QUICK_ASSIST, "Sort Members for 'A.java'"); Assert.assertNotNull(sortMemberQuickAssist); - Command sortMemberCommand = CodeActionHandlerTest.getCommand(sortMemberQuickAssist); - Assert.assertNotNull(sortMemberCommand); - Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, sortMemberCommand.getCommand()); + WorkspaceEdit sortMemberEdit = CodeActionHandlerTest.getEdit(sortMemberQuickAssist); + Assert.assertNotNull(sortMemberEdit); } @Test @@ -153,8 +151,7 @@ public void testSortMemberQuickAssistExistsForSelection() throws JavaModelExcept Assert.assertNotNull(codeActions); Either sortMemberQuickAssist = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.QUICK_ASSIST, "Sort Selected Members"); Assert.assertNotNull(sortMemberQuickAssist); - Command sortMemberCommand = CodeActionHandlerTest.getCommand(sortMemberQuickAssist); - Assert.assertNotNull(sortMemberCommand); - Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, sortMemberCommand.getCommand()); + WorkspaceEdit sortMemberEdit = CodeActionHandlerTest.getEdit(sortMemberQuickAssist); + Assert.assertNotNull(sortMemberEdit); } }