fix(plugins): enhance safety checks for applying LSP code actions on save

This commit is contained in:
Prad Nukala committed 2026-07-11 15:15:26 -04:00
1 parent fd3d384d4b
commit 5b860287fa
1 file changed
+9 -1
+9 -1
View File
@@ -4,8 +4,11 @@
-- Action kinds we consider safe to apply on save. Matches exact kinds and -- Action kinds we consider safe to apply on save. Matches exact kinds and
-- any subkinds via "<kind>." prefix (LSP CodeActionKind is hierarchical). -- any subkinds via "<kind>." prefix (LSP CodeActionKind is hierarchical).
-- Deliberately NOT the bare "source" umbrella: it covers non-idempotent
-- document rewrites (e.g. marksman's "Create Table of Contents").
local SAFE_KINDS = { local SAFE_KINDS = {
"source", "source.organizeImports",
"source.fixAll",
"quickfix", "quickfix",
} }
@@ -41,9 +44,13 @@ local function lsp_diagnostics(bufnr)
return out return out
end end
-- Servers may ignore context.only (LSP allows it), so re-filter the returned
-- actions by kind before applying. Kind-less actions are skipped: we asked for
-- specific kinds, so anything untagged is not provably safe.
local function apply_result(client, bufnr, result) local function apply_result(client, bufnr, result)
if not result then return end if not result then return end
for _, action in ipairs(result) do for _, action in ipairs(result) do
if is_safe_kind(action.kind) then
if action.edit then if action.edit then
vim.lsp.util.apply_workspace_edit(action.edit, client.offset_encoding or "utf-8") vim.lsp.util.apply_workspace_edit(action.edit, client.offset_encoding or "utf-8")
end end
@@ -52,6 +59,7 @@ local function apply_result(client, bufnr, result)
end end
end end
end end
end
local function apply_safe_actions(bufnr) local function apply_safe_actions(bufnr)
local clients = vim.lsp.get_clients { bufnr = bufnr, method = "textDocument/codeAction" } local clients = vim.lsp.get_clients { bufnr = bufnr, method = "textDocument/codeAction" }