Skip to content

Commit b657071

Browse files
committed
fix: CodeAction in Java file doesn't work in IDEA EAP 2023-3 EAP
Fixes #1214 Signed-off-by: azerr <azerr@redhat.com>
1 parent 373085c commit b657071

File tree

2 files changed

+64
-65
lines changed

2 files changed

+64
-65
lines changed

src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/diagnostics/LSPDiagnosticsForServer.java

Lines changed: 62 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -35,77 +35,75 @@
3535
*/
3636
public class LSPDiagnosticsForServer {
3737

38-
private final LanguageServerWrapper languageServerWrapper;
38+
private final LanguageServerWrapper languageServerWrapper;
3939

40-
private final boolean codeActionSupported;
40+
private final VirtualFile file;
4141

42-
private final VirtualFile file;
42+
// Map which contains all current diagnostics (as key) and future which load associated quick fixes (as value)
43+
private Map<Diagnostic, LSPLazyCodeActions> diagnostics;
4344

44-
// Map which contains all current diagnostics (as key) and future which load associated quick fixes (as value)
45-
private Map<Diagnostic, LSPLazyCodeActions> diagnostics;
45+
public LSPDiagnosticsForServer(LanguageServerWrapper languageServerWrapper, VirtualFile file) {
46+
this.languageServerWrapper = languageServerWrapper;
47+
this.file = file;
48+
this.diagnostics = Collections.emptyMap();
49+
}
4650

47-
public LSPDiagnosticsForServer(LanguageServerWrapper languageServerWrapper, VirtualFile file) {
48-
this.languageServerWrapper = languageServerWrapper;
49-
this.codeActionSupported = isCodeActionSupported(languageServerWrapper);
50-
this.file = file;
51-
this.diagnostics = Collections.emptyMap();
52-
}
51+
/**
52+
* Update the new LSP published diagnosics.
53+
*
54+
* @param diagnostics the new LSP published diagnosics
55+
*/
56+
public void update(List<Diagnostic> diagnostics) {
57+
// initialize diagnostics map
58+
this.diagnostics = toMap(diagnostics, this.diagnostics);
59+
}
5360

54-
private static boolean isCodeActionSupported(LanguageServerWrapper languageServerWrapper) {
55-
if (!languageServerWrapper.isActive() || languageServerWrapper.isStopping()) {
56-
// This use-case comes from when a diagnostics is published and the language server is stopped
57-
// We cannot use here languageServerWrapper.getServerCapabilities() otherwise it will restart the language server.
58-
return false;
59-
}
60-
ServerCapabilities serverCapabilities = languageServerWrapper.getServerCapabilities();
61-
return serverCapabilities != null && LSPIJUtils.hasCapability(serverCapabilities.getCodeActionProvider());
62-
}
61+
private Map<Diagnostic, LSPLazyCodeActions> toMap(List<Diagnostic> diagnostics, Map<Diagnostic, LSPLazyCodeActions> existingsDiagnostics) {
62+
Map<Diagnostic, LSPLazyCodeActions> map = new HashMap<>(diagnostics.size());
63+
for (Diagnostic diagnostic : diagnostics) {
64+
// Get the existing LSP lazy code actions for the current diagnostic
65+
LSPLazyCodeActions actions = existingsDiagnostics != null ? existingsDiagnostics.get(diagnostic) : null;
66+
if (actions != null) {
67+
// cancel the LSP textDocument/codeAction request if needed
68+
actions.cancel();
69+
}
70+
map.put(diagnostic, new LSPLazyCodeActions(diagnostic, file, languageServerWrapper));
71+
}
72+
return map;
73+
}
6374

64-
/**
65-
* Update the new LSP published diagnosics.
66-
*
67-
* @param diagnostics the new LSP published diagnosics
68-
*/
69-
public void update(List<Diagnostic> diagnostics) {
70-
// initialize diagnostics map
71-
this.diagnostics = toMap(diagnostics, this.diagnostics);
72-
}
75+
/**
76+
* Returns the current diagnostics for the file reported by the language server.
77+
*
78+
* @return the current diagnostics for the file reported by the language server.
79+
*/
80+
public Set<Diagnostic> getDiagnostics() {
81+
return diagnostics.keySet();
82+
}
7383

74-
private Map<Diagnostic, LSPLazyCodeActions> toMap(List<Diagnostic> diagnostics, Map<Diagnostic, LSPLazyCodeActions> existingsDiagnostics) {
75-
Map<Diagnostic, LSPLazyCodeActions> map = new HashMap<>(diagnostics.size());
76-
for (Diagnostic diagnostic : diagnostics) {
77-
// Get the existing LSP lazy code actions for the current diagnostic
78-
LSPLazyCodeActions actions = existingsDiagnostics != null ? existingsDiagnostics.get(diagnostic) : null;
79-
if (actions != null) {
80-
// cancel the LSP textDocument/codeAction request if needed
81-
actions.cancel();
82-
}
83-
map.put(diagnostic, new LSPLazyCodeActions(diagnostic, file, languageServerWrapper));
84-
}
85-
return map;
86-
}
84+
/**
85+
* Returns Intellij quickfixes for the given diagnostic if there available.
86+
*
87+
* @param diagnostic the diagnostic.
88+
* @return Intellij quickfixes for the given diagnostic if there available.
89+
*/
90+
public List<LSPLazyCodeActionIntentionAction> getQuickFixesFor(Diagnostic diagnostic) {
91+
boolean codeActionSupported = isCodeActionSupported(languageServerWrapper);
92+
if (!codeActionSupported || diagnostics.isEmpty()) {
93+
return Collections.emptyList();
94+
}
95+
LSPLazyCodeActions codeActions = diagnostics.get(diagnostic);
96+
return codeActions != null ? codeActions.getCodeActions() : Collections.emptyList();
97+
}
8798

88-
/**
89-
* Returns the current diagnostics for the file reported by the language server.
90-
*
91-
* @return the current diagnostics for the file reported by the language server.
92-
*/
93-
public Set<Diagnostic> getDiagnostics() {
94-
return diagnostics.keySet();
95-
}
96-
97-
/**
98-
* Returns Intellij quickfixes for the given diagnostic if there available.
99-
*
100-
* @param diagnostic the diagnostic.
101-
* @return Intellij quickfixes for the given diagnostic if there available.
102-
*/
103-
public List<LSPLazyCodeActionIntentionAction> getQuickFixesFor(Diagnostic diagnostic) {
104-
if (!codeActionSupported || diagnostics.isEmpty()) {
105-
return Collections.emptyList();
106-
}
107-
LSPLazyCodeActions codeActions = diagnostics.get(diagnostic);
108-
return codeActions != null ? codeActions.getCodeActions() : Collections.emptyList();
109-
}
99+
private static boolean isCodeActionSupported(LanguageServerWrapper languageServerWrapper) {
100+
if (!languageServerWrapper.isActive() || languageServerWrapper.isStopping()) {
101+
// This use-case comes from when a diagnostics is published and the language server is stopped
102+
// We cannot use here languageServerWrapper.getServerCapabilities() otherwise it will restart the language server.
103+
return false;
104+
}
105+
ServerCapabilities serverCapabilities = languageServerWrapper.getServerCapabilities();
106+
return serverCapabilities != null && LSPIJUtils.hasCapability(serverCapabilities.getCodeActionProvider());
107+
}
110108

111109
}

src/main/java/com/redhat/devtools/intellij/quarkus/lsp/QuarkusLanguageClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,12 @@ public CompletableFuture<List<CodeAction>> getJavaCodeAction(MicroProfileJavaCod
223223

224224
@Override
225225
public CompletableFuture<CodeAction> resolveCodeAction(CodeAction unresolved) {
226+
var coalesceBy = new CoalesceByKey("microprofile/java/resolveCodeAction");
226227
return runAsBackground("Computing Java resolve code actions", monitor -> {
227228
CodeActionResolveData data = JSONUtility.toModel(unresolved.getData(), CodeActionResolveData.class);
228229
unresolved.setData(data);
229230
return (CodeAction) PropertiesManagerForJava.getInstance().resolveCodeAction(unresolved, PsiUtilsLSImpl.getInstance(getProject()));
230-
});
231+
}, coalesceBy);
231232
}
232233

233234
@Override

0 commit comments

Comments
 (0)