Skip to content

Commit

Permalink
fix: Irrelevant Quick Fixes after applying code action
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Oct 4, 2024
1 parent 0dfc9fa commit f9adced
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ public interface LSPLazyCodeActionProvider {
* @return a code action at the given index and false otherwise.
*/
Either<CodeActionData, Boolean /* false when code action is not already loaded*/> getCodeActionAt(int index);

/**
* Clear code actions cache.
*/
void clear();
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,9 @@ private static boolean isValidCodeAction(@Nullable Either<org.eclipse.lsp4j.Comm
}
return true;
}

@Override
public void clear() {
super.cancel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,11 @@ public void cancel() {
lspCodeActionRequest.cancel(true);
}
}

@Override
public void clear() {
cancel();
lspCodeActionRequest = null;
codeActions.forEach(ca -> ((LSPLazyCodeActionIntentionAction)ca).setLazyCodeActions(this));
}
}

0 comments on commit f9adced

Please sign in to comment.