From f9adced7327dea4ab04a16f903149c310c209fa1 Mon Sep 17 00:00:00 2001 From: azerr Date: Fri, 4 Oct 2024 14:42:29 +0200 Subject: [PATCH] fix: Irrelevant Quick Fixes after applying code action Fixes #410 --- .../LSPLazyCodeActionIntentionAction.java | 15 ++++++++++++++- .../codeAction/LSPLazyCodeActionProvider.java | 5 +++++ .../intention/LSPIntentionCodeActionSupport.java | 5 +++++ .../codeAction/quickfix/LSPLazyCodeActions.java | 7 +++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/redhat/devtools/lsp4ij/features/codeAction/LSPLazyCodeActionIntentionAction.java b/src/main/java/com/redhat/devtools/lsp4ij/features/codeAction/LSPLazyCodeActionIntentionAction.java index 97bc4640c..47ed82296 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/features/codeAction/LSPLazyCodeActionIntentionAction.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/features/codeAction/LSPLazyCodeActionIntentionAction.java @@ -120,13 +120,26 @@ private void apply(@Nullable CodeAction codeaction, executeCommand(codeaction.getCommand(), file, editor, languageServerItem); } } + // After applying code action, evict the code actions cache + // to avoid providing irrelevant Quick Fixes. + clearCodeActionsCache(); } - private static void executeCommand(@NotNull Command command, + private void executeCommand(@NotNull Command command, @NotNull PsiFile file, @NotNull Editor editor, @NotNull LanguageServerItem languageServer) { CommandExecutor.executeCommand(new LSPCommandContext(command, file, LSPCommandContext.ExecutedBy.CODE_ACTION, editor, languageServer)); + clearCodeActionsCache(); + } + + /** + * Clear code actions cache if needed. + */ + private void clearCodeActionsCache() { + if (lazyCodeActions != null) { + lazyCodeActions.clear(); + } } private LanguageServerItem getLanguageServer() { diff --git a/src/main/java/com/redhat/devtools/lsp4ij/features/codeAction/LSPLazyCodeActionProvider.java b/src/main/java/com/redhat/devtools/lsp4ij/features/codeAction/LSPLazyCodeActionProvider.java index 2a9008cc6..3806bc2b2 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/features/codeAction/LSPLazyCodeActionProvider.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/features/codeAction/LSPLazyCodeActionProvider.java @@ -24,4 +24,9 @@ public interface LSPLazyCodeActionProvider { * @return a code action at the given index and false otherwise. */ Either getCodeActionAt(int index); + + /** + * Clear code actions cache. + */ + void clear(); } diff --git a/src/main/java/com/redhat/devtools/lsp4ij/features/codeAction/intention/LSPIntentionCodeActionSupport.java b/src/main/java/com/redhat/devtools/lsp4ij/features/codeAction/intention/LSPIntentionCodeActionSupport.java index bfe381e41..c16d04d32 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/features/codeAction/intention/LSPIntentionCodeActionSupport.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/features/codeAction/intention/LSPIntentionCodeActionSupport.java @@ -142,4 +142,9 @@ private static boolean isValidCodeAction(@Nullable Either ((LSPLazyCodeActionIntentionAction)ca).setLazyCodeActions(this)); + } }