|
12 | 12 | *******************************************************************************/
|
13 | 13 | package org.eclipse.jdt.ls.core.internal.text.correction;
|
14 | 14 |
|
15 |
| -import java.util.Arrays; |
16 | 15 | 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; |
21 | 16 |
|
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; |
24 | 18 | import org.eclipse.lsp4j.CodeAction;
|
25 | 19 | import org.eclipse.lsp4j.Command;
|
26 | 20 | import org.eclipse.lsp4j.jsonrpc.messages.Either;
|
27 | 21 |
|
28 | 22 | public class QuickAssistComparator implements Comparator<Either<Command, CodeAction>> {
|
29 | 23 |
|
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; |
67 | 33 |
|
68 | 34 | 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 | + } |
78 | 47 | }
|
79 |
| - return index1 - index2; |
| 48 | + return 0; |
80 | 49 | }
|
81 | 50 | }
|
0 commit comments