76
76
import org .eclipse .jdt .ls .core .internal .handlers .OrganizeImportsHandler ;
77
77
import org .eclipse .jdt .ls .core .internal .handlers .GenerateAccessorsHandler .AccessorCodeActionParams ;
78
78
import org .eclipse .jdt .ls .core .internal .preferences .PreferenceManager ;
79
+ import org .eclipse .jdt .ls .core .internal .text .correction .QuickAssistComparator .ContextType ;
79
80
import org .eclipse .lsp4j .CodeAction ;
80
81
import org .eclipse .lsp4j .CodeActionContext ;
81
82
import org .eclipse .lsp4j .CodeActionKind ;
@@ -107,6 +108,7 @@ public SourceAssistProcessor(PreferenceManager preferenceManager) {
107
108
108
109
public List <Either <Command , CodeAction >> getSourceActionCommands (CodeActionParams params , IInvocationContext context , IProblemLocationCore [] locations , IProgressMonitor monitor ) {
109
110
List <Either <Command , CodeAction >> $ = new ArrayList <>();
111
+ List <Either <Command , CodeAction >> quickAssists = new ArrayList <>();
110
112
ICompilationUnit cu = context .getCompilationUnit ();
111
113
IType type = getSelectionType (context );
112
114
ASTNode node = context .getCoveredNode ();
@@ -121,7 +123,7 @@ public List<Either<Command, CodeAction>> getSourceActionCommands(CodeActionParam
121
123
// Generate Constructor QuickAssist
122
124
if (isInFieldDeclaration || isInTypeDeclaration ) {
123
125
Optional <Either <Command , CodeAction >> quickAssistGenerateConstructors = getGenerateConstructorsAction (params , context , type , JavaCodeActionKind .QUICK_ASSIST , monitor );
124
- addSourceActionCommand ($ , params .getContext (), quickAssistGenerateConstructors );
126
+ addSourceActionCommand (quickAssists , params .getContext (), quickAssistGenerateConstructors );
125
127
}
126
128
// Generate Constructor Source Action
127
129
Optional <Either <Command , CodeAction >> sourceGenerateConstructors = getGenerateConstructorsAction (params , context , type , JavaCodeActionKind .SOURCE_GENERATE_CONSTRUCTORS , monitor );
@@ -132,7 +134,7 @@ public List<Either<Command, CodeAction>> getSourceActionCommands(CodeActionParam
132
134
// Generate QuickAssist
133
135
if (isInImportDeclaration ) {
134
136
Optional <Either <Command , CodeAction >> quickAssistOrganizeImports = getOrganizeImportsAction (params , JavaCodeActionKind .QUICK_ASSIST );
135
- addSourceActionCommand ($ , params .getContext (), quickAssistOrganizeImports );
137
+ addSourceActionCommand (quickAssists , params .getContext (), quickAssistOrganizeImports );
136
138
}
137
139
// Generate Source Action
138
140
Optional <Either <Command , CodeAction >> sourceOrganizeImports = getOrganizeImportsAction (params , CodeActionKind .SourceOrganizeImports );
@@ -146,7 +148,7 @@ public List<Either<Command, CodeAction>> getSourceActionCommands(CodeActionParam
146
148
if (isInImportDeclaration ) {
147
149
Optional <Either <Command , CodeAction >> sourceOrganizeImports = getCodeActionFromProposal (params .getContext (), context .getCompilationUnit (), CorrectionMessages .ReorgCorrectionsSubProcessor_organizeimports_description ,
148
150
JavaCodeActionKind .QUICK_ASSIST , organizeImportsProposal );
149
- addSourceActionCommand ($ , params .getContext (), sourceOrganizeImports );
151
+ addSourceActionCommand (quickAssists , params .getContext (), sourceOrganizeImports );
150
152
}
151
153
// Generate Source Action
152
154
Optional <Either <Command , CodeAction >> sourceOrganizeImports = getCodeActionFromProposal (params .getContext (), context .getCompilationUnit (), CorrectionMessages .ReorgCorrectionsSubProcessor_organizeimports_description ,
@@ -158,7 +160,7 @@ public List<Either<Command, CodeAction>> getSourceActionCommands(CodeActionParam
158
160
// Override/Implement Methods QuickAssist
159
161
if (isInTypeDeclaration ) {
160
162
Optional <Either <Command , CodeAction >> quickAssistOverrideMethods = getOverrideMethodsAction (params , JavaCodeActionKind .QUICK_ASSIST );
161
- addSourceActionCommand ($ , params .getContext (), quickAssistOverrideMethods );
163
+ addSourceActionCommand (quickAssists , params .getContext (), quickAssistOverrideMethods );
162
164
}
163
165
// Override/Implement Methods Source Action
164
166
Optional <Either <Command , CodeAction >> sourceOverrideMethods = getOverrideMethodsAction (params , JavaCodeActionKind .SOURCE_OVERRIDE_METHODS );
@@ -167,7 +169,7 @@ public List<Either<Command, CodeAction>> getSourceActionCommands(CodeActionParam
167
169
168
170
String fieldName = getFieldName (fieldDeclaration , node );
169
171
try {
170
- addGenerateAccessorsSourceActionCommand (params , context , $ , type , fieldName , isInTypeDeclaration );
172
+ addGenerateAccessorsSourceActionCommand (params , context , $ , quickAssists , type , fieldName , isInTypeDeclaration );
171
173
} catch (JavaModelException e ) {
172
174
JavaLanguageServerPlugin .logException ("Failed to generate Getter and Setter source action" , e );
173
175
}
@@ -177,7 +179,7 @@ public List<Either<Command, CodeAction>> getSourceActionCommands(CodeActionParam
177
179
// Generate QuickAssist
178
180
if (isInTypeDeclaration ) {
179
181
Optional <Either <Command , CodeAction >> quickAssistHashCodeEquals = getHashCodeEqualsAction (params , JavaCodeActionKind .QUICK_ASSIST );
180
- addSourceActionCommand ($ , params .getContext (), quickAssistHashCodeEquals );
182
+ addSourceActionCommand (quickAssists , params .getContext (), quickAssistHashCodeEquals );
181
183
}
182
184
183
185
// Generate Source Action
@@ -198,7 +200,7 @@ public List<Either<Command, CodeAction>> getSourceActionCommands(CodeActionParam
198
200
// Generate QuickAssist
199
201
if (isInTypeDeclaration ) {
200
202
Optional <Either <Command , CodeAction >> generateToStringQuickAssist = getGenerateToStringAction (params , JavaCodeActionKind .QUICK_ASSIST );
201
- addSourceActionCommand ($ , params .getContext (), generateToStringQuickAssist );
203
+ addSourceActionCommand (quickAssists , params .getContext (), generateToStringQuickAssist );
202
204
}
203
205
// Generate Source Action
204
206
Optional <Either <Command , CodeAction >> generateToStringCommand = getGenerateToStringAction (params , JavaCodeActionKind .SOURCE_GENERATE_TO_STRING );
@@ -213,7 +215,7 @@ public List<Either<Command, CodeAction>> getSourceActionCommands(CodeActionParam
213
215
if (isInTypeDeclaration ) {
214
216
Optional <Either <Command , CodeAction >> generateToStringQuickAssist = getCodeActionFromProposal (params .getContext (), context .getCompilationUnit (), ActionMessages .GenerateToStringAction_label ,
215
217
JavaCodeActionKind .QUICK_ASSIST , generateToStringProposal );
216
- addSourceActionCommand ($ , params .getContext (), generateToStringQuickAssist );
218
+ addSourceActionCommand (quickAssists , params .getContext (), generateToStringQuickAssist );
217
219
}
218
220
// Generate Source Action
219
221
Optional <Either <Command , CodeAction >> generateToStringCommand = getCodeActionFromProposal (params .getContext (), context .getCompilationUnit (), ActionMessages .GenerateToStringAction_label ,
@@ -230,6 +232,15 @@ public List<Either<Command, CodeAction>> getSourceActionCommands(CodeActionParam
230
232
Optional <Either <Command , CodeAction >> generateFinalModifiers = addFinalModifierWherePossibleAction (context );
231
233
addSourceActionCommand ($ , params .getContext (), generateFinalModifiers );
232
234
235
+ if (isInFieldDeclaration ) {
236
+ quickAssists .sort (new QuickAssistComparator (ContextType .FIELD_DECLARATION , fieldName ));
237
+ } else if (isInTypeDeclaration ) {
238
+ quickAssists .sort (new QuickAssistComparator (ContextType .TYPE_DECLARATION , fieldName ));
239
+ } else if (isInImportDeclaration ) {
240
+ quickAssists .sort (new QuickAssistComparator (ContextType .IMPORT_DECLARATION , fieldName ));
241
+ }
242
+
243
+ $ .addAll (quickAssists );
233
244
return $ ;
234
245
}
235
246
@@ -253,31 +264,31 @@ private String getFieldName(FieldDeclaration fieldDeclaration, ASTNode node) {
253
264
return null ;
254
265
}
255
266
256
- private void addGenerateAccessorsSourceActionCommand (CodeActionParams params , IInvocationContext context , List <Either <Command , CodeAction >> $ , IType type , String fieldName , boolean isInTypeDeclaration ) throws JavaModelException {
267
+ private void addGenerateAccessorsSourceActionCommand (CodeActionParams params , IInvocationContext context , List <Either <Command , CodeAction >> $ , List < Either < Command , CodeAction >> quickAssists , IType type , String fieldName , boolean isInTypeDeclaration ) throws JavaModelException {
257
268
AccessorField [] accessors = GenerateGetterSetterOperation .getUnimplementedAccessors (type , AccessorKind .BOTH );
258
269
AccessorField [] getters = GenerateGetterSetterOperation .getUnimplementedAccessors (type , AccessorKind .GETTER );
259
270
AccessorField [] setters = GenerateGetterSetterOperation .getUnimplementedAccessors (type , AccessorKind .SETTER );
260
271
261
272
if (fieldName != null ) {
262
273
Optional <AccessorField > accessorField = Arrays .stream (accessors ).filter (accessor -> accessor .fieldName .equals (fieldName )).findFirst ();
263
274
if (accessorField .isPresent () && accessorField .get ().generateGetter && accessorField .get ().generateSetter ) {
264
- addSourceActionCommand ($ , params .getContext (), getGetterSetterAction (params , context , type , JavaCodeActionKind .QUICK_ASSIST , isInTypeDeclaration , new AccessorField [] { accessorField .get () }, AccessorKind .BOTH ));
275
+ addSourceActionCommand (quickAssists , params .getContext (), getGetterSetterAction (params , context , type , JavaCodeActionKind .QUICK_ASSIST , isInTypeDeclaration , new AccessorField [] { accessorField .get () }, AccessorKind .BOTH ));
265
276
}
266
277
Optional <AccessorField > getterField = Arrays .stream (getters ).filter (getter -> getter .fieldName .equals (fieldName )).findFirst ();
267
278
if (getterField .isPresent ()) {
268
- addSourceActionCommand ($ , params .getContext (), getGetterSetterAction (params , context , type , JavaCodeActionKind .QUICK_ASSIST , isInTypeDeclaration , new AccessorField [] { getterField .get () }, AccessorKind .GETTER ));
279
+ addSourceActionCommand (quickAssists , params .getContext (), getGetterSetterAction (params , context , type , JavaCodeActionKind .QUICK_ASSIST , isInTypeDeclaration , new AccessorField [] { getterField .get () }, AccessorKind .GETTER ));
269
280
}
270
281
Optional <AccessorField > setterField = Arrays .stream (setters ).filter (setter -> setter .fieldName .equals (fieldName )).findFirst ();
271
282
if (setterField .isPresent ()) {
272
- addSourceActionCommand ($ , params .getContext (), getGetterSetterAction (params , context , type , JavaCodeActionKind .QUICK_ASSIST , isInTypeDeclaration , new AccessorField [] { setterField .get () }, AccessorKind .SETTER ));
283
+ addSourceActionCommand (quickAssists , params .getContext (), getGetterSetterAction (params , context , type , JavaCodeActionKind .QUICK_ASSIST , isInTypeDeclaration , new AccessorField [] { setterField .get () }, AccessorKind .SETTER ));
273
284
}
274
285
}
275
286
276
287
if (getters .length > 0 && setters .length > 0 ) {
277
288
// Generate Getter and Setter QuickAssist
278
289
if (isInTypeDeclaration ) {
279
290
Optional <Either <Command , CodeAction >> quickAssistGetterSetter = getGetterSetterAction (params , context , type , JavaCodeActionKind .QUICK_ASSIST , isInTypeDeclaration , accessors , AccessorKind .BOTH );
280
- addSourceActionCommand ($ , params .getContext (), quickAssistGetterSetter );
291
+ addSourceActionCommand (quickAssists , params .getContext (), quickAssistGetterSetter );
281
292
}
282
293
// Generate Getter and Setter Source Action
283
294
Optional <Either <Command , CodeAction >> sourceGetterSetter = getGetterSetterAction (params , context , type , JavaCodeActionKind .SOURCE_GENERATE_ACCESSORS , isInTypeDeclaration , accessors , AccessorKind .BOTH );
@@ -288,7 +299,7 @@ private void addGenerateAccessorsSourceActionCommand(CodeActionParams params, II
288
299
// Generate Getter QuickAssist
289
300
if (isInTypeDeclaration ) {
290
301
Optional <Either <Command , CodeAction >> quickAssistGetter = getGetterSetterAction (params , context , type , JavaCodeActionKind .QUICK_ASSIST , isInTypeDeclaration , getters , AccessorKind .GETTER );
291
- addSourceActionCommand ($ , params .getContext (), quickAssistGetter );
302
+ addSourceActionCommand (quickAssists , params .getContext (), quickAssistGetter );
292
303
}
293
304
// Generate Getter Source Action
294
305
Optional <Either <Command , CodeAction >> sourceGetter = getGetterSetterAction (params , context , type , JavaCodeActionKind .SOURCE_GENERATE_ACCESSORS , isInTypeDeclaration , getters , AccessorKind .GETTER );
@@ -299,7 +310,7 @@ private void addGenerateAccessorsSourceActionCommand(CodeActionParams params, II
299
310
// Generate Setter QuickAssist
300
311
if (isInTypeDeclaration ) {
301
312
Optional <Either <Command , CodeAction >> quickAssistSetter = getGetterSetterAction (params , context , type , JavaCodeActionKind .QUICK_ASSIST , isInTypeDeclaration , setters , AccessorKind .SETTER );
302
- addSourceActionCommand ($ , params .getContext (), quickAssistSetter );
313
+ addSourceActionCommand (quickAssists , params .getContext (), quickAssistSetter );
303
314
}
304
315
// Generate Setter Source Action
305
316
Optional <Either <Command , CodeAction >> sourceSetter = getGetterSetterAction (params , context , type , JavaCodeActionKind .SOURCE_GENERATE_ACCESSORS , isInTypeDeclaration , setters , AccessorKind .SETTER );
0 commit comments