22
22
import java .util .Map ;
23
23
24
24
import org .eclipse .core .runtime .CoreException ;
25
+ import org .eclipse .core .runtime .IProgressMonitor ;
25
26
import org .eclipse .core .runtime .NullProgressMonitor ;
26
27
import org .eclipse .jdt .core .ICompilationUnit ;
27
28
import org .eclipse .jdt .core .IField ;
110
111
import org .eclipse .jdt .ui .text .java .IProblemLocation ;
111
112
import org .eclipse .lsp4j .CodeActionKind ;
112
113
import org .eclipse .lsp4j .CodeActionParams ;
114
+ import org .eclipse .ltk .core .refactoring .Change ;
113
115
import org .eclipse .ltk .core .refactoring .CheckConditionsOperation ;
114
116
import org .eclipse .ltk .core .refactoring .CreateChangeOperation ;
117
+ import org .eclipse .ltk .core .refactoring .NullChange ;
115
118
import org .eclipse .ltk .core .refactoring .RefactoringStatus ;
116
119
117
120
/**
@@ -127,38 +130,99 @@ public RefactorProcessor(PreferenceManager preferenceManager) {
127
130
}
128
131
129
132
public List <ProposalKindWrapper > getProposals (CodeActionParams params , IInvocationContext context , IProblemLocation [] locations ) throws CoreException {
133
+ return getProposals (params , context , locations , new NullProgressMonitor ());
134
+ }
135
+
136
+ public List <ProposalKindWrapper > getProposals (CodeActionParams params , IInvocationContext context , IProblemLocation [] locations , IProgressMonitor monitor ) throws CoreException {
130
137
ASTNode coveringNode = context .getCoveringNode ();
138
+ if (monitor != null && monitor .isCanceled ()) {
139
+ return Collections .emptyList ();
140
+ }
131
141
if (coveringNode != null ) {
132
142
ArrayList <ProposalKindWrapper > proposals = new ArrayList <>();
133
143
134
144
InvertBooleanUtility .getInverseConditionProposals (params , context , coveringNode , proposals );
145
+ if (monitor != null && monitor .isCanceled ()) {
146
+ return Collections .emptyList ();
147
+ }
135
148
getInverseLocalVariableProposals (params , context , coveringNode , proposals );
136
-
149
+ if (monitor != null && monitor .isCanceled ()) {
150
+ return Collections .emptyList ();
151
+ }
137
152
getMoveRefactoringProposals (params , context , coveringNode , proposals );
138
-
153
+ if (monitor != null && monitor .isCanceled ()) {
154
+ return Collections .emptyList ();
155
+ }
139
156
boolean noErrorsAtLocation = noErrorsAtLocation (locations , coveringNode );
140
157
if (noErrorsAtLocation ) {
141
158
boolean problemsAtLocation = locations .length != 0 ;
142
159
getExtractVariableProposal (params , context , problemsAtLocation , proposals );
160
+ if (monitor != null && monitor .isCanceled ()) {
161
+ return Collections .emptyList ();
162
+ }
143
163
getExtractMethodProposal (params , context , coveringNode , problemsAtLocation , proposals );
164
+ if (monitor != null && monitor .isCanceled ()) {
165
+ return Collections .emptyList ();
166
+ }
144
167
getExtractFieldProposal (params , context , problemsAtLocation , proposals );
145
- getInlineProposal (context , coveringNode , proposals );
146
-
168
+ if (monitor != null && monitor .isCanceled ()) {
169
+ return Collections .emptyList ();
170
+ }
147
171
getConvertAnonymousToNestedProposals (params , context , coveringNode , proposals );
172
+ if (monitor != null && monitor .isCanceled ()) {
173
+ return Collections .emptyList ();
174
+ }
148
175
getConvertAnonymousClassCreationsToLambdaProposals (context , coveringNode , proposals );
176
+ if (monitor != null && monitor .isCanceled ()) {
177
+ return Collections .emptyList ();
178
+ }
149
179
getConvertLambdaToAnonymousClassCreationsProposals (context , coveringNode , proposals );
180
+ if (monitor != null && monitor .isCanceled ()) {
181
+ return Collections .emptyList ();
182
+ }
150
183
151
184
getConvertVarTypeToResolvedTypeProposal (context , coveringNode , proposals );
185
+ if (monitor != null && monitor .isCanceled ()) {
186
+ return Collections .emptyList ();
187
+ }
152
188
getConvertResolvedTypeToVarTypeProposal (context , coveringNode , proposals );
189
+ if (monitor != null && monitor .isCanceled ()) {
190
+ return Collections .emptyList ();
191
+ }
153
192
154
193
getAddStaticImportProposals (context , coveringNode , proposals );
194
+ if (monitor != null && monitor .isCanceled ()) {
195
+ return Collections .emptyList ();
196
+ }
155
197
156
198
getConvertForLoopProposal (context , coveringNode , proposals );
199
+ if (monitor != null && monitor .isCanceled ()) {
200
+ return Collections .emptyList ();
201
+ }
157
202
getAssignToVariableProposals (context , coveringNode , locations , proposals , params );
158
- getIntroduceParameterProposals (params , context , coveringNode , locations , proposals );
203
+ if (monitor != null && monitor .isCanceled ()) {
204
+ return Collections .emptyList ();
205
+ }
159
206
getExtractInterfaceProposal (params , context , proposals );
160
- getChangeSignatureProposal (params , context , proposals );
207
+ if (monitor != null && monitor .isCanceled ()) {
208
+ return Collections .emptyList ();
209
+ }
161
210
getSurroundWithTryCatchProposal (context , proposals );
211
+ if (monitor != null && monitor .isCanceled ()) {
212
+ return Collections .emptyList ();
213
+ }
214
+ getChangeSignatureProposal (params , context , proposals );
215
+ if (monitor != null && monitor .isCanceled ()) {
216
+ return Collections .emptyList ();
217
+ }
218
+ getIntroduceParameterProposals (params , context , coveringNode , locations , proposals );
219
+ if (monitor != null && monitor .isCanceled ()) {
220
+ return Collections .emptyList ();
221
+ }
222
+ getInlineProposal (context , coveringNode , proposals );
223
+ if (monitor != null && monitor .isCanceled ()) {
224
+ return Collections .emptyList ();
225
+ }
162
226
}
163
227
return proposals ;
164
228
}
@@ -334,15 +398,27 @@ private boolean getInlineProposal(IInvocationContext context, ASTNode node, Coll
334
398
// Inline Constant (static final field)
335
399
if (RefactoringAvailabilityTesterCore .isInlineConstantAvailable ((IField ) varBinding .getJavaElement ())) {
336
400
InlineConstantRefactoring refactoring = new InlineConstantRefactoring (context .getCompilationUnit (), context .getASTRoot (), context .getSelectionOffset (), context .getSelectionLength ());
337
- if (refactoring != null && refactoring .checkInitialConditions (new NullProgressMonitor ()).isOK () && refactoring .getReferences (new NullProgressMonitor (), new RefactoringStatus ()).length > 0 ) {
338
- refactoring .setRemoveDeclaration (refactoring .isDeclarationSelected ());
339
- refactoring .setReplaceAllReferences (refactoring .isDeclarationSelected ());
340
- CheckConditionsOperation check = new CheckConditionsOperation (refactoring , CheckConditionsOperation .FINAL_CONDITIONS );
341
- final CreateChangeOperation create = new CreateChangeOperation (check , RefactoringStatus .FATAL );
342
- create .run (new NullProgressMonitor ());
401
+ if (refactoring != null ) {
343
402
String label = ActionMessages .InlineConstantRefactoringAction_label ;
344
403
int relevance = IProposalRelevance .INLINE_LOCAL ;
345
- ChangeCorrectionProposalCore proposal = new ChangeCorrectionProposalCore (label , create .getChange (), relevance );
404
+ ChangeCorrectionProposalCore proposal = new ChangeCorrectionProposalCore (label , null /*create.getChange()*/ , relevance ) {
405
+ @ Override
406
+ public Change getChange () throws CoreException {
407
+ if (refactoring .checkInitialConditions (new NullProgressMonitor ()).isOK () && refactoring .getReferences (new NullProgressMonitor (), new RefactoringStatus ()).length > 0 ) {
408
+ refactoring .setRemoveDeclaration (refactoring .isDeclarationSelected ());
409
+ refactoring .setReplaceAllReferences (refactoring .isDeclarationSelected ());
410
+ CheckConditionsOperation check = new CheckConditionsOperation (refactoring , CheckConditionsOperation .FINAL_CONDITIONS );
411
+ final CreateChangeOperation create = new CreateChangeOperation (check , RefactoringStatus .FATAL );
412
+ try {
413
+ create .run (new NullProgressMonitor ());
414
+ return create .getChange ();
415
+ } catch (CoreException e ) {
416
+ JavaLanguageServerPlugin .log (e );
417
+ }
418
+ }
419
+ return new NullChange ();
420
+ }
421
+ };
346
422
resultingCollections .add (CodeActionHandler .wrap (proposal , CodeActionKind .RefactorInline ));
347
423
return true ;
348
424
}
@@ -357,8 +433,10 @@ private boolean getInlineProposal(IInvocationContext context, ASTNode node, Coll
357
433
}
358
434
359
435
// Inline Local Variable
436
+ // https://github.com/eclipse-jdtls/eclipse.jdt.ls/issues/3321
437
+ // I haven't enhanced it because InlineVariableTest.testInlineLocalVariableWithNoReferences() fails; can be enhanced
360
438
if (binding .getJavaElement () instanceof ILocalVariable localVar && RefactoringAvailabilityTesterCore .isInlineTempAvailable (localVar )) {
361
- InlineTempRefactoring refactoring = new InlineTempRefactoring ((VariableDeclaration ) decl );
439
+ InlineTempRefactoring refactoring = new InlineTempRefactoring ((VariableDeclaration ) decl );
362
440
boolean status ;
363
441
try {
364
442
status = refactoring .checkAllConditions (new NullProgressMonitor ()).isOK ();
@@ -382,13 +460,25 @@ private boolean getInlineProposal(IInvocationContext context, ASTNode node, Coll
382
460
// Inline Method
383
461
if (RefactoringAvailabilityTesterCore .isInlineMethodAvailable ((IMethod ) binding .getJavaElement ())) {
384
462
InlineMethodRefactoring refactoring = InlineMethodRefactoring .create (context .getCompilationUnit (), context .getASTRoot (), context .getSelectionOffset (), context .getSelectionLength ());
385
- if (refactoring != null && refactoring .checkInitialConditions (new NullProgressMonitor ()).isOK ()) {
386
- CheckConditionsOperation check = new CheckConditionsOperation (refactoring , CheckConditionsOperation .FINAL_CONDITIONS );
387
- final CreateChangeOperation create = new CreateChangeOperation (check , RefactoringStatus .FATAL );
388
- create .run (new NullProgressMonitor ());
463
+ if (refactoring != null ) {
389
464
String label = ActionMessages .InlineMethodRefactoringAction_label ;
390
465
int relevance = IProposalRelevance .INLINE_LOCAL ;
391
- ChangeCorrectionProposalCore proposal = new ChangeCorrectionProposalCore (label , create .getChange (), relevance );
466
+ ChangeCorrectionProposalCore proposal = new ChangeCorrectionProposalCore (label , null /*create.getChange()*/ , relevance ) {
467
+ @ Override
468
+ public Change getChange () throws CoreException {
469
+ if (refactoring .checkInitialConditions (new NullProgressMonitor ()).isOK ()) {
470
+ CheckConditionsOperation check = new CheckConditionsOperation (refactoring , CheckConditionsOperation .FINAL_CONDITIONS );
471
+ final CreateChangeOperation create = new CreateChangeOperation (check , RefactoringStatus .FATAL );
472
+ try {
473
+ create .run (new NullProgressMonitor ());
474
+ return create .getChange ();
475
+ } catch (CoreException e ) {
476
+ JavaLanguageServerPlugin .log (e );
477
+ }
478
+ }
479
+ return new NullChange ();
480
+ }
481
+ };
392
482
resultingCollections .add (CodeActionHandler .wrap (proposal , CodeActionKind .RefactorInline ));
393
483
return true ;
394
484
}
0 commit comments