|
35 | 35 | */
|
36 | 36 | public class LSPDiagnosticsForServer {
|
37 | 37 |
|
38 |
| - private final LanguageServerWrapper languageServerWrapper; |
| 38 | + private final LanguageServerWrapper languageServerWrapper; |
39 | 39 |
|
40 |
| - private final boolean codeActionSupported; |
| 40 | + private final VirtualFile file; |
41 | 41 |
|
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; |
43 | 44 |
|
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 | + } |
46 | 50 |
|
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 | + } |
53 | 60 |
|
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 | + } |
63 | 74 |
|
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 | + } |
73 | 83 |
|
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 | + } |
87 | 98 |
|
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 | + } |
110 | 108 |
|
111 | 109 | }
|
0 commit comments