Skip to content

Commit 4236305

Browse files
Update the outdated diagnostics when the code action for creating a missing element is invoked
1 parent ab346ee commit 4236305

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTDelegateCommandHandler.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
9999
case "java.project.getAll":
100100
return ProjectCommand.getAllJavaProjects();
101101
case "java.project.refreshDiagnostics":
102-
return DiagnosticsCommand.refreshDiagnostics((String) arguments.get(0), (String) arguments.get(1), (boolean) arguments.get(2));
102+
if (arguments.size() < 4) {
103+
return DiagnosticsCommand.refreshDiagnostics((String) arguments.get(0), (String) arguments.get(1), (boolean) arguments.get(2));
104+
}
105+
return DiagnosticsCommand.refreshDiagnostics((String) arguments.get(0), (String) arguments.get(1), (boolean) arguments.get(2), (boolean) arguments.get(3));
103106
case "java.project.import":
104107
ProjectCommand.importProject(monitor);
105108
return null;

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/commands/DiagnosticsCommand.java

+10
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,21 @@
3838
import org.eclipse.jdt.ls.core.internal.JDTUtils;
3939
import org.eclipse.jdt.ls.core.internal.JavaClientConnection;
4040
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
41+
import org.eclipse.jdt.ls.core.internal.JobHelpers;
4142
import org.eclipse.jdt.ls.core.internal.handlers.DiagnosticsHandler;
43+
import org.eclipse.jdt.ls.core.internal.handlers.DocumentLifeCycleHandler;
4244

4345
public class DiagnosticsCommand {
4446

4547
public static Object refreshDiagnostics(String uri, String scope, boolean syntaxOnly) {
48+
return refreshDiagnostics(uri, scope, syntaxOnly, false);
49+
}
50+
51+
public static Object refreshDiagnostics(String uri, String scope, boolean syntaxOnly, boolean waitForLifecycleJob) {
52+
if (waitForLifecycleJob) {
53+
JobHelpers.waitForJobs(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, new NullProgressMonitor());
54+
}
55+
4656
DiagnosticsState state = JavaLanguageServerPlugin.getNonProjectDiagnosticsState();
4757
boolean refreshAll = false;
4858
if (Objects.equals(scope, "thisFile")) {

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
import org.eclipse.jdt.ls.core.internal.corrections.QuickFixProcessor;
4747
import org.eclipse.jdt.ls.core.internal.corrections.RefactorProcessor;
4848
import org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal;
49+
import org.eclipse.jdt.ls.core.internal.corrections.proposals.NewAnnotationMemberProposal;
50+
import org.eclipse.jdt.ls.core.internal.corrections.proposals.NewCUProposal;
51+
import org.eclipse.jdt.ls.core.internal.corrections.proposals.NewMethodCorrectionProposal;
52+
import org.eclipse.jdt.ls.core.internal.corrections.proposals.NewVariableCorrectionProposal;
4953
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
5054
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
5155
import org.eclipse.jdt.ls.core.internal.text.correction.AssignToVariableAssistCommandProposal;
@@ -198,7 +202,7 @@ public List<Either<Command, CodeAction>> getCodeActionCommands(CodeActionParams
198202
}
199203
try {
200204
for (ChangeCorrectionProposal proposal : proposals) {
201-
Optional<Either<Command, CodeAction>> codeActionFromProposal = getCodeActionFromProposal(proposal, params.getContext());
205+
Optional<Either<Command, CodeAction>> codeActionFromProposal = getCodeActionFromProposal(params.getTextDocument().getUri(), proposal, params.getContext());
202206
if (codeActionFromProposal.isPresent() && !codeActions.contains(codeActionFromProposal.get())) {
203207
codeActions.add(codeActionFromProposal.get());
204208
}
@@ -256,7 +260,7 @@ private void populateDataFields(List<Either<Command, CodeAction>> codeActions) {
256260
}
257261
}
258262

259-
private Optional<Either<Command, CodeAction>> getCodeActionFromProposal(ChangeCorrectionProposal proposal, CodeActionContext context) throws CoreException {
263+
private Optional<Either<Command, CodeAction>> getCodeActionFromProposal(String uri, ChangeCorrectionProposal proposal, CodeActionContext context) throws CoreException {
260264
String name = proposal.getName();
261265

262266
Command command = null;
@@ -281,6 +285,18 @@ private Optional<Either<Command, CodeAction>> getCodeActionFromProposal(ChangeCo
281285
CodeAction codeAction = new CodeAction(name);
282286
codeAction.setKind(proposal.getKind());
283287
if (command == null) { // lazy resolve the edit.
288+
if (!preferenceManager.getPreferences().isValidateAllOpenBuffersOnChanges()) {
289+
if (proposal instanceof NewCUProposal
290+
|| proposal instanceof NewMethodCorrectionProposal
291+
|| proposal instanceof NewAnnotationMemberProposal
292+
|| proposal instanceof NewVariableCorrectionProposal
293+
) {
294+
codeAction.setCommand(new Command("refresh Diagnostics", "java.project.refreshDiagnostics", Arrays.asList(
295+
uri, "thisFile", false, true
296+
)));
297+
}
298+
}
299+
284300
// The relevance is in descending order while CodeActionComparator sorts in ascending order
285301
codeAction.setData(new CodeActionData(proposal, -proposal.getRelevance()));
286302
} else {

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

+32
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*******************************************************************************/
1313
package org.eclipse.jdt.ls.core.internal.handlers;
1414

15+
import static org.junit.Assert.assertEquals;
1516
import static org.junit.Assert.assertNotNull;
1617
import static org.junit.Assert.assertTrue;
1718
import static org.mockito.Mockito.when;
@@ -344,6 +345,37 @@ public void testCodeAction_NPEInNewCUProposal() throws Exception {
344345
Assert.assertNotNull(codeActions);
345346
}
346347

348+
@Test
349+
public void testCodeAction_refreshDiagnosticsCommandInNewCUProposal() throws Exception {
350+
when(clientPreferences.isResolveCodeActionSupported()).thenReturn(true);
351+
preferences.setValidateAllOpenBuffersOnChanges(false);
352+
ICompilationUnit unit = getWorkingCopy(
353+
//@formatter:off
354+
"src/org/sample/Foo.java",
355+
"package org.sample;\n"+
356+
"public class Foo {\n"+
357+
" public static void main(String[] args) {\n"+
358+
" CU obj;\n"+
359+
" }\n"+
360+
"}\n");
361+
//@formatter:off
362+
CodeActionParams params = new CodeActionParams();
363+
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
364+
final Range range = CodeActionUtil.getRange(unit, "CU");
365+
params.setRange(range);
366+
CodeActionContext context = new CodeActionContext(
367+
Arrays.asList(getDiagnostic(Integer.toString(IProblem.UndefinedType), range))
368+
);
369+
params.setContext(context);
370+
List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
371+
Assert.assertNotNull(codeActions);
372+
Optional<Either<Command, CodeAction>> newCUProposal = codeActions.stream().filter(action -> action.isRight() && Objects.equals(action.getRight().getTitle(), "Create class 'CU'")).findFirst();
373+
assertTrue(newCUProposal.isPresent());
374+
assertNotNull(newCUProposal.get().getRight());
375+
assertNotNull(newCUProposal.get().getRight().getCommand());
376+
assertEquals("java.project.refreshDiagnostics", newCUProposal.get().getRight().getCommand().getCommand());
377+
}
378+
347379
private static String getBaseKind(String codeActionKind) {
348380
if (codeActionKind.contains(".")) {
349381
return codeActionKind.substring(0, codeActionKind.indexOf('.'));

0 commit comments

Comments
 (0)