better code actions

This commit is contained in:
Mathias Schneider
2026-09-10 13:36:39 +02:00
parent 0e2d4030bd
commit fbf1442dbe
4 changed files with 110 additions and 11 deletions
+66 -2
View File
@@ -49,13 +49,78 @@ require("diff-base")
-- ============================================================================
-- STEP 4: LSP KEYMAPS
-- ============================================================================
local function fix_markdownlint()
local bufnr = vim.api.nvim_get_current_buf()
local filename = vim.api.nvim_buf_get_name(bufnr)
local markdownlint = vim.fn.exepath("markdownlint")
if markdownlint == "" then
markdownlint = vim.fn.stdpath("data") .. "/mason/bin/markdownlint"
end
if vim.fn.executable(markdownlint) == 0 then
vim.notify("markdownlint is not installed", vim.log.levels.WARN)
return
end
if filename == "" then
vim.notify("Save the Markdown buffer before fixing it", vim.log.levels.WARN)
return
end
vim.cmd.write()
vim.fn.jobstart({ markdownlint, "--fix", filename }, {
buffer = bufnr,
on_exit = function(_, exit_code)
vim.schedule(function()
if exit_code ~= 0 then
vim.notify("markdownlint could not fix this file", vim.log.levels.WARN)
return
end
vim.cmd.checktime()
vim.notify("markdownlint fixes applied", vim.log.levels.INFO)
vim.defer_fn(function()
require("lint").try_lint()
end, 100)
end)
end,
})
end
vim.keymap.set("n", "gd", vim.lsp.buf.definition, { desc = "Go to definition" })
vim.keymap.set("n", "gD", vim.lsp.buf.declaration, { desc = "Go to declaration" })
vim.keymap.set("n", "gi", vim.lsp.buf.implementation, { desc = "Go to implementation" })
vim.keymap.set("n", "gr", vim.lsp.buf.references, { desc = "Show references" })
vim.keymap.set("n", "K", vim.lsp.buf.hover, { desc = "Show hover docs" })
vim.keymap.set("n", "<leader>rn", vim.lsp.buf.rename, { desc = "Rename symbol" })
vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, { desc = "Code actions" })
vim.keymap.set("n", "<leader>ca", function()
if vim.bo.filetype == "markdown" then
fix_markdownlint()
return
end
vim.lsp.buf.code_action()
end, { desc = "Code actions / Markdown fixes" })
vim.keymap.set("n", "<leader>fx", function()
if vim.bo.filetype == "markdown" then
fix_markdownlint()
return
end
vim.lsp.buf.code_action({
context = {
diagnostics = vim.diagnostic.get(0, {
lnum = vim.fn.line(".") - 1,
}),
},
})
end, { desc = "Fix diagnostic" })
vim.keymap.set("n", "<leader>fA", function()
vim.lsp.buf.code_action({
apply = true,
context = {
only = { "source.fixAll" },
},
})
end, { desc = "Fix all diagnostics" })
vim.keymap.set("n", "<leader>e", vim.diagnostic.open_float, { desc = "Show diagnostic" })
vim.diagnostic.config({
@@ -138,4 +203,3 @@ vim.keymap.set("n", "<leader>ch", ":noh<CR>", { desc = "clear search highlightin
-- toggle word wrap
vim.keymap.set("n", "<leader>tw", ":set wrap!<CR>", { desc = "toggle word wrap" })