Skip to content

Commit 5113eec

Browse files
committed
use data field in CodeAction
Signed-off-by: Shi Chen <[email protected]>
1 parent f6aa491 commit 5113eec

File tree

4 files changed

+100
-103
lines changed

4 files changed

+100
-103
lines changed

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

+35-6
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,21 @@ private void populateDataFields(List<Either<Command, CodeAction>> codeActions) {
226226
codeActions.forEach(action -> {
227227
if (action.isRight()) {
228228
Either<ChangeCorrectionProposal, CodeActionProposal> proposal = null;
229-
if (action.getRight().getData() instanceof ChangeCorrectionProposal) {
230-
proposal = Either.forLeft((ChangeCorrectionProposal) action.getRight().getData());
231-
} else if (action.getRight().getData() instanceof CodeActionProposal) {
232-
proposal = Either.forRight((CodeActionProposal) action.getRight().getData());
229+
Object originalData = action.getRight().getData();
230+
if (originalData instanceof CodeActionData) {
231+
Object originalProposal = ((CodeActionData) originalData).getProposal();
232+
if (originalProposal instanceof ChangeCorrectionProposal) {
233+
proposal = Either.forLeft((ChangeCorrectionProposal) originalProposal);
234+
} else if (originalProposal instanceof CodeActionProposal) {
235+
proposal = Either.forRight((CodeActionProposal) originalProposal);
236+
} else {
237+
action.getRight().setData(null);
238+
return;
239+
}
233240
} else {
234241
action.getRight().setData(null);
235242
return;
236243
}
237-
238244
Map<String, String> data = new HashMap<>();
239245
data.put(CodeActionResolveHandler.DATA_FIELD_REQUEST_ID, String.valueOf(response.getId()));
240246
data.put(CodeActionResolveHandler.DATA_FIELD_PROPOSAL_ID, String.valueOf(proposals.size()));
@@ -277,7 +283,7 @@ private Optional<Either<Command, CodeAction>> getCodeActionFromProposal(ChangeCo
277283
CodeAction codeAction = new CodeAction(name);
278284
codeAction.setKind(proposal.getKind());
279285
if (command == null) { // lazy resolve the edit.
280-
codeAction.setData(proposal);
286+
codeAction.setData(new CodeActionData(proposal));
281287
} else {
282288
codeAction.setCommand(command);
283289
}
@@ -358,4 +364,27 @@ private static boolean containsKind(List<String> codeActionKinds, String baseKin
358364
return codeActionKinds.stream().anyMatch(kind -> kind.startsWith(baseKind));
359365
}
360366

367+
public static class CodeActionData {
368+
private final Object proposal;
369+
private final int priority;
370+
371+
public CodeActionData(Object proposal) {
372+
this.proposal = proposal;
373+
this.priority = 0;
374+
}
375+
376+
public CodeActionData(Object proposal, int priority) {
377+
this.proposal = proposal;
378+
this.priority = priority;
379+
}
380+
381+
public Object getProposal() {
382+
return proposal;
383+
}
384+
385+
public int getPriority() {
386+
return priority;
387+
}
388+
}
389+
361390
}

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

+23-54
Original file line numberDiff line numberDiff line change
@@ -12,70 +12,39 @@
1212
*******************************************************************************/
1313
package org.eclipse.jdt.ls.core.internal.text.correction;
1414

15-
import java.util.Arrays;
1615
import java.util.Comparator;
17-
import java.util.HashMap;
18-
import java.util.List;
19-
import java.util.Map;
20-
import java.util.stream.Collectors;
2116

22-
import org.eclipse.jdt.ls.core.internal.Messages;
23-
import org.eclipse.jdt.ls.core.internal.corrections.CorrectionMessages;
17+
import org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler.CodeActionData;
2418
import org.eclipse.lsp4j.CodeAction;
2519
import org.eclipse.lsp4j.Command;
2620
import org.eclipse.lsp4j.jsonrpc.messages.Either;
2721

2822
public class QuickAssistComparator implements Comparator<Either<Command, CodeAction>> {
2923

30-
public enum ContextType {
31-
FIELD_DECLARATION,
32-
TYPE_DECLARATION,
33-
IMPORT_DECLARATION
34-
}
35-
36-
private List<String> prioritiesList;
37-
38-
private static Map<ContextType, List<String>> prioritiesMap = new HashMap<>();
39-
40-
static {
41-
prioritiesMap.put(ContextType.FIELD_DECLARATION, Arrays.asList(
42-
ActionMessages.GenerateGetterSetterAction_templateLabel,
43-
ActionMessages.GenerateGetterAction_templateLabel,
44-
ActionMessages.GenerateSetterAction_templateLabel,
45-
ActionMessages.GenerateConstructorsAction_ellipsisLabel,
46-
ActionMessages.GenerateConstructorsAction_label));
47-
48-
prioritiesMap.put(ContextType.TYPE_DECLARATION, Arrays.asList(
49-
ActionMessages.GenerateConstructorsAction_ellipsisLabel,
50-
ActionMessages.GenerateConstructorsAction_label,
51-
ActionMessages.OverrideMethodsAction_label,
52-
ActionMessages.GenerateGetterSetterAction_templateLabel,
53-
ActionMessages.GenerateGetterAction_templateLabel,
54-
ActionMessages.GenerateSetterAction_templateLabel,
55-
ActionMessages.GenerateHashCodeEqualsAction_label,
56-
ActionMessages.GenerateToStringAction_ellipsisLabel
57-
));
58-
59-
prioritiesMap.put(ContextType.IMPORT_DECLARATION, Arrays.asList(
60-
CorrectionMessages.ReorgCorrectionsSubProcessor_organizeimports_description
61-
));
62-
}
63-
64-
public QuickAssistComparator(ContextType contextType, String fieldName) {
65-
this.prioritiesList = prioritiesMap.get(contextType).stream().map(message -> Messages.format(message, fieldName)).collect(Collectors.toList());
66-
}
24+
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;
6733

6834
public int compare(Either<Command, CodeAction> e1, Either<Command, CodeAction> e2) {
69-
String title1 = e1.isLeft() ? e1.getLeft().getTitle() : e1.isRight() ? e1.getRight().getTitle() : null;
70-
String title2 = e2.isLeft() ? e2.getLeft().getTitle() : e2.isRight() ? e2.getRight().getTitle() : null;
71-
if (title1 == null || title2 == null) {
72-
return 0;
73-
}
74-
int index1 = this.prioritiesList.indexOf(title1);
75-
int index2 = this.prioritiesList.indexOf(title2);
76-
if (index1 == -1 || index2 == -1) {
77-
return 0;
35+
if (e1.isRight() && e2.isRight()) {
36+
Object data1 = e1.getRight().getData();
37+
Object data2 = e2.getRight().getData();
38+
if (data1 instanceof CodeActionData && data2 instanceof CodeActionData) {
39+
int priority1 = ((CodeActionData) data1).getPriority();
40+
int priority2 = ((CodeActionData) data2).getPriority();
41+
return priority1 - priority2;
42+
} else if (data1 instanceof CodeActionData) {
43+
return -100;
44+
} else if (data2 instanceof CodeActionData) {
45+
return 100;
46+
}
7847
}
79-
return index1 - index2;
48+
return 0;
8049
}
8150
}

0 commit comments

Comments
 (0)