From 5b860287fa92747500a048cd5e92d9a791a3c7f0 Mon Sep 17 00:00:00 2001 From: Prad Nukala Date: Sat, 11 Jul 2026 15:15:26 -0400 Subject: [PATCH] fix(plugins): enhance safety checks for applying LSP code actions on save --- lua/plugins/conform.lua | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lua/plugins/conform.lua b/lua/plugins/conform.lua index f5c2fd5..4142d7e 100644 --- a/lua/plugins/conform.lua +++ b/lua/plugins/conform.lua @@ -4,8 +4,11 @@ -- Action kinds we consider safe to apply on save. Matches exact kinds and -- any subkinds via "." 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 = { - "source", + "source.organizeImports", + "source.fixAll", "quickfix", } @@ -41,14 +44,19 @@ local function lsp_diagnostics(bufnr) return out 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) if not result then return end for _, action in ipairs(result) do - if action.edit then - vim.lsp.util.apply_workspace_edit(action.edit, client.offset_encoding or "utf-8") - end - if type(action.command) == "table" then - client:exec_cmd(action.command, { bufnr = bufnr }) + if is_safe_kind(action.kind) then + if action.edit then + vim.lsp.util.apply_workspace_edit(action.edit, client.offset_encoding or "utf-8") + end + if type(action.command) == "table" then + client:exec_cmd(action.command, { bufnr = bufnr }) + end end end end