Skip to content

Commit fbceff2

Browse files
committed
sort all code actions
Signed-off-by: Shi Chen <[email protected]>
1 parent 58dbf56 commit fbceff2

File tree

4 files changed

+55
-35
lines changed

4 files changed

+55
-35
lines changed

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/CodeActionHandler.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.eclipse.jdt.ls.core.internal.text.correction.AssignToVariableAssistCommandProposal;
5252
import org.eclipse.jdt.ls.core.internal.text.correction.CUCorrectionCommandProposal;
5353
import org.eclipse.jdt.ls.core.internal.text.correction.NonProjectFixProcessor;
54+
import org.eclipse.jdt.ls.core.internal.text.correction.CodeActionComparator;
5455
import org.eclipse.jdt.ls.core.internal.text.correction.QuickAssistProcessor;
5556
import org.eclipse.jdt.ls.core.internal.text.correction.RefactoringCorrectionCommandProposal;
5657
import org.eclipse.jdt.ls.core.internal.text.correction.SourceAssistProcessor;
@@ -216,6 +217,7 @@ public List<Either<Command, CodeAction>> getCodeActionCommands(CodeActionParams
216217
return Collections.emptyList();
217218
}
218219

220+
codeActions.sort(new CodeActionComparator());
219221
populateDataFields(codeActions);
220222
return codeActions;
221223
}
@@ -372,7 +374,7 @@ public static class CodeActionData {
372374

373375
public CodeActionData(Object proposal) {
374376
this.proposal = proposal;
375-
this.priority = 0;
377+
this.priority = CodeActionComparator.LOWEST_PRIORITY;
376378
}
377379

378380
public CodeActionData(Object proposal, int priority) {
+34-13
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,58 @@
1414

1515
import java.util.Comparator;
1616

17+
import org.eclipse.jdt.ls.core.internal.JavaCodeActionKind;
1718
import org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler.CodeActionData;
1819
import org.eclipse.lsp4j.CodeAction;
20+
import org.eclipse.lsp4j.CodeActionKind;
1921
import org.eclipse.lsp4j.Command;
2022
import org.eclipse.lsp4j.jsonrpc.messages.Either;
2123

22-
public class QuickAssistComparator implements Comparator<Either<Command, CodeAction>> {
24+
public class CodeActionComparator implements Comparator<Either<Command, CodeAction>> {
2325

2426
public static int ORGANIZE_IMPORTS_PRIORITY = 0;
25-
public static int GENERATE_ACCESSORS_PRIORITY = 100;
26-
public static int GENERATE_CONSTRUCTORS_PRIORITY = 200;
27-
public static int GENERATE_HASHCODE_EQUALS_PRIORITY = 300;
28-
public static int GENERATE_TOSTRING_PRIORITY = 400;
29-
public static int GENERATE_OVERRIDE_IMPLEMENT_PRIORITY = 500;
30-
public static int GENERATE_DELEGATE_METHOD_PRIORITY = 600;
31-
public static int CHANGE_MODIFIER_TO_FINAL_PRIORITY = 700;
32-
public static int MAX_PRIORITY = 1000;
27+
public static int GENERATE_ACCESSORS_PRIORITY = 10;
28+
public static int GENERATE_CONSTRUCTORS_PRIORITY = 20;
29+
public static int GENERATE_HASHCODE_EQUALS_PRIORITY = 30;
30+
public static int GENERATE_TOSTRING_PRIORITY = 40;
31+
public static int GENERATE_OVERRIDE_IMPLEMENT_PRIORITY = 50;
32+
public static int GENERATE_DELEGATE_METHOD_PRIORITY = 60;
33+
public static int CHANGE_MODIFIER_TO_FINAL_PRIORITY = 70;
34+
public static int LOWEST_PRIORITY = 100;
3335

3436
public int compare(Either<Command, CodeAction> e1, Either<Command, CodeAction> e2) {
3537
if (e1.isRight() && e2.isRight()) {
36-
Object data1 = e1.getRight().getData();
37-
Object data2 = e2.getRight().getData();
38+
CodeAction action1 = e1.getRight();
39+
CodeAction action2 = e2.getRight();
40+
int kindDiff = getCodeActionKindOrdinal(action1.getKind()) - getCodeActionKindOrdinal(action2.getKind());
41+
if (kindDiff != 0) {
42+
return kindDiff;
43+
}
44+
Object data1 = action1.getData();
45+
Object data2 = action2.getData();
3846
if (data1 instanceof CodeActionData && data2 instanceof CodeActionData) {
3947
int priority1 = ((CodeActionData) data1).getPriority();
4048
int priority2 = ((CodeActionData) data2).getPriority();
4149
return priority1 - priority2;
4250
} else if (data1 instanceof CodeActionData) {
43-
return -100;
51+
return 10;
4452
} else if (data2 instanceof CodeActionData) {
45-
return 100;
53+
return -10;
4654
}
4755
}
4856
return 0;
4957
}
58+
59+
private int getCodeActionKindOrdinal(String kind) {
60+
if (kind.equals(CodeActionKind.QuickFix)) {
61+
return 0;
62+
} else if (kind.startsWith(CodeActionKind.Refactor)) {
63+
return 1000;
64+
} else if (kind.equals(JavaCodeActionKind.QUICK_ASSIST)) {
65+
return 2000;
66+
} else if (kind.startsWith(CodeActionKind.Source)) {
67+
return 3000;
68+
}
69+
return 4000;
70+
}
5071
}

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/text/correction/SourceAssistProcessor.java

+15-16
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,12 @@ public List<Either<Command, CodeAction>> getSourceActionCommands(CodeActionParam
146146
// Generate QuickAssist
147147
if (isInImportDeclaration) {
148148
Optional<Either<Command, CodeAction>> sourceOrganizeImports = getCodeActionFromProposal(params.getContext(), context.getCompilationUnit(), CorrectionMessages.ReorgCorrectionsSubProcessor_organizeimports_description,
149-
JavaCodeActionKind.QUICK_ASSIST, organizeImportsProposal, QuickAssistComparator.ORGANIZE_IMPORTS_PRIORITY);
149+
JavaCodeActionKind.QUICK_ASSIST, organizeImportsProposal, CodeActionComparator.ORGANIZE_IMPORTS_PRIORITY);
150150
addSourceActionCommand($, params.getContext(), sourceOrganizeImports);
151151
}
152152
// Generate Source Action
153153
Optional<Either<Command, CodeAction>> sourceOrganizeImports = getCodeActionFromProposal(params.getContext(), context.getCompilationUnit(), CorrectionMessages.ReorgCorrectionsSubProcessor_organizeimports_description,
154-
CodeActionKind.SourceOrganizeImports, organizeImportsProposal, QuickAssistComparator.ORGANIZE_IMPORTS_PRIORITY);
154+
CodeActionKind.SourceOrganizeImports, organizeImportsProposal, CodeActionComparator.ORGANIZE_IMPORTS_PRIORITY);
155155
addSourceActionCommand($, params.getContext(), sourceOrganizeImports);
156156
}
157157

@@ -213,12 +213,12 @@ public List<Either<Command, CodeAction>> getSourceActionCommands(CodeActionParam
213213
// Generate QuickAssist
214214
if (isInTypeDeclaration) {
215215
Optional<Either<Command, CodeAction>> generateToStringQuickAssist = getCodeActionFromProposal(params.getContext(), context.getCompilationUnit(), ActionMessages.GenerateToStringAction_label,
216-
JavaCodeActionKind.QUICK_ASSIST, generateToStringProposal, QuickAssistComparator.GENERATE_TOSTRING_PRIORITY);
216+
JavaCodeActionKind.QUICK_ASSIST, generateToStringProposal, CodeActionComparator.GENERATE_TOSTRING_PRIORITY);
217217
addSourceActionCommand($, params.getContext(), generateToStringQuickAssist);
218218
}
219219
// Generate Source Action
220220
Optional<Either<Command, CodeAction>> generateToStringCommand = getCodeActionFromProposal(params.getContext(), context.getCompilationUnit(), ActionMessages.GenerateToStringAction_label,
221-
JavaCodeActionKind.SOURCE_GENERATE_TO_STRING, generateToStringProposal, QuickAssistComparator.GENERATE_TOSTRING_PRIORITY);
221+
JavaCodeActionKind.SOURCE_GENERATE_TO_STRING, generateToStringProposal, CodeActionComparator.GENERATE_TOSTRING_PRIORITY);
222222
addSourceActionCommand($, params.getContext(), generateToStringCommand);
223223
}
224224
}
@@ -231,7 +231,6 @@ public List<Either<Command, CodeAction>> getSourceActionCommands(CodeActionParam
231231
Optional<Either<Command, CodeAction>> generateFinalModifiers = addFinalModifierWherePossibleAction(context);
232232
addSourceActionCommand($, params.getContext(), generateFinalModifiers);
233233

234-
$.sort(new QuickAssistComparator());
235234
return $;
236235
}
237236

@@ -349,7 +348,7 @@ private Optional<Either<Command, CodeAction>> getOrganizeImportsAction(CodeActio
349348
CodeAction codeAction = new CodeAction(CorrectionMessages.ReorgCorrectionsSubProcessor_organizeimports_description);
350349
codeAction.setKind(kind);
351350
codeAction.setCommand(command);
352-
codeAction.setData(new CodeActionData(null, QuickAssistComparator.ORGANIZE_IMPORTS_PRIORITY));
351+
codeAction.setData(new CodeActionData(null, CodeActionComparator.ORGANIZE_IMPORTS_PRIORITY));
353352
codeAction.setDiagnostics(Collections.emptyList());
354353
return Optional.of(Either.forRight(codeAction));
355354

@@ -365,7 +364,7 @@ private Optional<Either<Command, CodeAction>> getOverrideMethodsAction(CodeActio
365364
CodeAction codeAction = new CodeAction(ActionMessages.OverrideMethodsAction_label);
366365
codeAction.setKind(kind);
367366
codeAction.setCommand(command);
368-
codeAction.setData(new CodeActionData(null, QuickAssistComparator.GENERATE_OVERRIDE_IMPLEMENT_PRIORITY));
367+
codeAction.setData(new CodeActionData(null, CodeActionComparator.GENERATE_OVERRIDE_IMPLEMENT_PRIORITY));
369368
codeAction.setDiagnostics(Collections.emptyList());
370369
return Optional.of(Either.forRight(codeAction));
371370
} else {
@@ -400,7 +399,7 @@ private Optional<Either<Command, CodeAction>> getGetterSetterAction(CodeActionPa
400399
TextEdit edit = operation.createTextEdit(pm, accessors);
401400
return convertToWorkspaceEdit(context.getCompilationUnit(), edit);
402401
};
403-
return getCodeActionFromProposal(params.getContext(), context.getCompilationUnit(), actionMessage, kind, getAccessorsProposal, QuickAssistComparator.GENERATE_ACCESSORS_PRIORITY);
402+
return getCodeActionFromProposal(params.getContext(), context.getCompilationUnit(), actionMessage, kind, getAccessorsProposal, CodeActionComparator.GENERATE_ACCESSORS_PRIORITY);
404403
} else {
405404
String actionMessage;
406405
switch (accessorKind) {
@@ -422,7 +421,7 @@ private Optional<Either<Command, CodeAction>> getGetterSetterAction(CodeActionPa
422421
CodeAction codeAction = new CodeAction(actionMessage);
423422
codeAction.setKind(kind);
424423
codeAction.setCommand(command);
425-
codeAction.setData(new CodeActionData(null, QuickAssistComparator.GENERATE_ACCESSORS_PRIORITY));
424+
codeAction.setData(new CodeActionData(null, CodeActionComparator.GENERATE_ACCESSORS_PRIORITY));
426425
codeAction.setDiagnostics(Collections.emptyList());
427426
return Optional.of(Either.forRight(codeAction));
428427
} else {
@@ -462,7 +461,7 @@ private Optional<Either<Command, CodeAction>> getHashCodeEqualsAction(CodeAction
462461
CodeAction codeAction = new CodeAction(ActionMessages.GenerateHashCodeEqualsAction_label);
463462
codeAction.setKind(kind);
464463
codeAction.setCommand(command);
465-
codeAction.setData(new CodeActionData(null, QuickAssistComparator.GENERATE_HASHCODE_EQUALS_PRIORITY));
464+
codeAction.setData(new CodeActionData(null, CodeActionComparator.GENERATE_HASHCODE_EQUALS_PRIORITY));
466465
codeAction.setDiagnostics(Collections.emptyList());
467466
return Optional.of(Either.forRight(codeAction));
468467
} else {
@@ -491,7 +490,7 @@ private Optional<Either<Command, CodeAction>> getGenerateToStringAction(CodeActi
491490
CodeAction codeAction = new CodeAction(ActionMessages.GenerateToStringAction_ellipsisLabel);
492491
codeAction.setKind(kind);
493492
codeAction.setCommand(command);
494-
codeAction.setData(new CodeActionData(null, QuickAssistComparator.GENERATE_TOSTRING_PRIORITY));
493+
codeAction.setData(new CodeActionData(null, CodeActionComparator.GENERATE_TOSTRING_PRIORITY));
495494
codeAction.setDiagnostics(Collections.emptyList());
496495
return Optional.of(Either.forRight(codeAction));
497496
} else {
@@ -518,15 +517,15 @@ private Optional<Either<Command, CodeAction>> getGenerateConstructorsAction(Code
518517
TextEdit edit = GenerateConstructorsHandler.generateConstructors(type, status.constructors, status.fields, params.getRange(), pm);
519518
return convertToWorkspaceEdit(type.getCompilationUnit(), edit);
520519
};
521-
return getCodeActionFromProposal(params.getContext(), type.getCompilationUnit(), ActionMessages.GenerateConstructorsAction_label, kind, generateConstructorsProposal, QuickAssistComparator.GENERATE_CONSTRUCTORS_PRIORITY);
520+
return getCodeActionFromProposal(params.getContext(), type.getCompilationUnit(), ActionMessages.GenerateConstructorsAction_label, kind, generateConstructorsProposal, CodeActionComparator.GENERATE_CONSTRUCTORS_PRIORITY);
522521
}
523522

524523
Command command = new Command(ActionMessages.GenerateConstructorsAction_ellipsisLabel, COMMAND_ID_ACTION_GENERATECONSTRUCTORSPROMPT, Collections.singletonList(params));
525524
if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(JavaCodeActionKind.SOURCE_GENERATE_CONSTRUCTORS)) {
526525
CodeAction codeAction = new CodeAction(ActionMessages.GenerateConstructorsAction_ellipsisLabel);
527526
codeAction.setKind(kind);
528527
codeAction.setCommand(command);
529-
codeAction.setData(new CodeActionData(null, QuickAssistComparator.GENERATE_CONSTRUCTORS_PRIORITY));
528+
codeAction.setData(new CodeActionData(null, CodeActionComparator.GENERATE_CONSTRUCTORS_PRIORITY));
530529
codeAction.setDiagnostics(Collections.emptyList());
531530
return Optional.of(Either.forRight(codeAction));
532531
} else {
@@ -551,7 +550,7 @@ private Optional<Either<Command, CodeAction>> getGenerateDelegateMethodsAction(C
551550
CodeAction codeAction = new CodeAction(ActionMessages.GenerateDelegateMethodsAction_label);
552551
codeAction.setKind(JavaCodeActionKind.SOURCE_GENERATE_DELEGATE_METHODS);
553552
codeAction.setCommand(command);
554-
codeAction.setData(new CodeActionData(null, QuickAssistComparator.GENERATE_DELEGATE_METHOD_PRIORITY));
553+
codeAction.setData(new CodeActionData(null, CodeActionComparator.GENERATE_DELEGATE_METHOD_PRIORITY));
555554
codeAction.setDiagnostics(Collections.EMPTY_LIST);
556555
return Optional.of(Either.forRight(codeAction));
557556
} else {
@@ -570,7 +569,7 @@ private Optional<Either<Command, CodeAction>> addFinalModifierWherePossibleActio
570569
if (this.preferenceManager.getClientPreferences().isResolveCodeActionSupported()) {
571570
CodeAction codeAction = new CodeAction(ActionMessages.GenerateFinalModifiersAction_label);
572571
codeAction.setKind(proposal.getKind());
573-
codeAction.setData(new CodeActionData(proposal, QuickAssistComparator.CHANGE_MODIFIER_TO_FINAL_PRIORITY));
572+
codeAction.setData(new CodeActionData(proposal, CodeActionComparator.CHANGE_MODIFIER_TO_FINAL_PRIORITY));
574573
codeAction.setDiagnostics(Collections.EMPTY_LIST);
575574
return Optional.of(Either.forRight(codeAction));
576575
} else {
@@ -590,7 +589,7 @@ private Optional<Either<Command, CodeAction>> addFinalModifierWherePossibleActio
590589
CodeAction codeAction = new CodeAction(ActionMessages.GenerateFinalModifiersAction_label);
591590
codeAction.setKind(proposal.getKind());
592591
codeAction.setCommand(command);
593-
codeAction.setData(new CodeActionData(null, QuickAssistComparator.CHANGE_MODIFIER_TO_FINAL_PRIORITY));
592+
codeAction.setData(new CodeActionData(null, CodeActionComparator.CHANGE_MODIFIER_TO_FINAL_PRIORITY));
594593
codeAction.setDiagnostics(Collections.EMPTY_LIST);
595594
return Optional.of(Either.forRight(codeAction));
596595
} else {

org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateConstructorsActionTest.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,9 @@ public void testGenerateConstructorsQuickAssist() throws JavaModelException {
9191
CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "String name");
9292
List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join();
9393
Assert.assertNotNull(codeActions);
94-
Either<Command, CodeAction> constructorAction = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.QUICK_ASSIST, "Generate Constructors...");
95-
Assert.assertNotNull(constructorAction);
96-
Command constructorCommand = CodeActionHandlerTest.getCommand(constructorAction);
97-
Assert.assertNotNull(constructorCommand);
98-
Assert.assertEquals(SourceAssistProcessor.COMMAND_ID_ACTION_GENERATECONSTRUCTORSPROMPT, constructorCommand.getCommand());
94+
List<Either<Command, CodeAction>> quickAssistActions = CodeActionHandlerTest.findActions(codeActions, JavaCodeActionKind.QUICK_ASSIST);
95+
Assert.assertNotNull(quickAssistActions);
96+
Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, SourceAssistProcessor.COMMAND_ID_ACTION_GENERATECONSTRUCTORSPROMPT));
9997
// test for type declaration
10098
params = CodeActionUtil.constructCodeActionParams(unit, "A");
10199
codeActions = server.codeAction(params).join();

0 commit comments

Comments
 (0)