From ad27913a5b8b9dbda957977962107c6b3b13714a Mon Sep 17 00:00:00 2001 From: Henrik Bakken Date: Thu, 19 Mar 2026 23:19:21 +0100 Subject: [PATCH] nvim tweaks (gemini) --- CONFIG/nvim/init.lua | 110 ++++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 43 deletions(-) diff --git a/CONFIG/nvim/init.lua b/CONFIG/nvim/init.lua index a147170..d668ec4 100644 --- a/CONFIG/nvim/init.lua +++ b/CONFIG/nvim/init.lua @@ -103,9 +103,7 @@ vim.lsp.set_log_level(2) vim.api.nvim_create_autocmd("FileType", { desc = "Disable auto-commenting on new line", pattern = "*", - callback = function() - vim.opt_local.formatoptions:remove({ "r", "o" }) - end, + callback = function() vim.opt_local.formatoptions:remove({ "r", "o" }) end, }) -- ---------------------------------------- @@ -216,29 +214,52 @@ map( ) -- LSP -map("n", "ld", "e ~/.local/state/nvim/lsp.log") -map("n", "lc", "checkhealth") +-- Global Diagnostic mappings (Diagnostics work independently of LSP) local diagnostic_goto = function(count, severity) severity = severity and vim.diagnostic.severity[severity] or nil - return function() vim.diagnostic.jump({ severity, count = count }) end + return function() vim.diagnostic.jump({ severity = severity, count = count }) end end -map("n", "", function() vim.diagnostic.open_float({ source = true }) end) + +map("n", "ld", "e ~/.local/state/nvim/lsp.log", { desc = "LSP Log" }) +map("n", "lc", "checkhealth", { desc = "Checkhealth" }) +map("n", "", function() vim.diagnostic.open_float({ source = true }) end, { desc = "Line Diagnostics" }) map("n", "", diagnostic_goto(1), { desc = "Next Diagnostic" }) map("n", "", diagnostic_goto(-1), { desc = "Prev Diagnostic" }) map("n", "[e", diagnostic_goto(-1, "ERROR"), { desc = "Prev Error" }) map("n", "[w", diagnostic_goto(-1, "WARN"), { desc = "Prev Warning" }) map("n", "]e", diagnostic_goto(1, "ERROR"), { desc = "Next Error" }) map("n", "]w", diagnostic_goto(1, "WARN"), { desc = "Next Warning" }) -map("n", "gd", vim.lsp.buf.definition, { desc = "Goto Definition" }) -map("n", "gD", vim.lsp.buf.type_definition, { desc = "Goto Type Definition" }) -map("n", "gi", vim.lsp.buf.declaration, { desc = "Goto Declaration" }) -map("n", "gI", vim.lsp.buf.implementation, { desc = "Goto Implementation" }) map("n", "gl", vim.diagnostic.setloclist, { desc = "Diagnostics to Location List" }) -map("n", "gr", function() vim.lsp.buf.references({ includeDeclaration = false }) end, { desc = "Goto References" }) -map({ "n", "i" }, "", vim.lsp.buf.signature_help) -map("n", "K", vim.lsp.buf.hover) -map("n", "", vim.lsp.buf.rename) -map({ "n", "x" }, "ca", vim.lsp.buf.code_action, { desc = "Code Action" }) + +-- Buffer-local LSP mappings +vim.api.nvim_create_autocmd("LspAttach", { + group = vim.api.nvim_create_augroup("UserLspConfig", {}), + callback = function(ev) + local bmap = function(mode, keys, func, desc) vim.keymap.set(mode, keys, func, { buffer = ev.buf, desc = desc }) end + + bmap("n", "gd", vim.lsp.buf.definition, "Goto Definition") + bmap("n", "gD", vim.lsp.buf.type_definition, "Goto Type Definition") + bmap("n", "gi", vim.lsp.buf.declaration, "Goto Declaration") + bmap("n", "gI", vim.lsp.buf.implementation, "Goto Implementation") + bmap("n", "gr", function() vim.lsp.buf.references({ includeDeclaration = false }) end, "Goto References") + + bmap({ "n", "i" }, "", vim.lsp.buf.signature_help, "Signature Help") + bmap("n", "", vim.lsp.buf.rename, "Rename Symbol") + bmap({ "n", "x" }, "ca", vim.lsp.buf.code_action, "Code Action") + + -- K is automatically mapped to vim.lsp.buf.hover() in Neovim 0.10+ + -- If your LSP supports inlay hints, you can add a toggle here: + local client = vim.lsp.get_client_by_id(ev.data.client_id) + if client and client.server_capabilities.inlayHintProvider and vim.lsp.inlay_hint then + bmap( + "n", + "uh", + function() vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled()) end, + "Toggle Inlay Hints" + ) + end + end, +}) -- ---------------------------------------- -- AUTOCMD @@ -856,8 +877,9 @@ local function makespec_fugitive() "GBrowse", }, config = function() - vim.api.nvim_create_autocmd("User", { - pattern = { "FugitiveCommit", "BufReadPost" }, + vim.api.nvim_create_autocmd("FileType", { + group = vim.api.nvim_create_augroup("fugitive_folds", { clear = true }), + pattern = { "fugitive", "gitcommit" }, callback = function() vim.opt_local.foldmethod = "syntax" vim.opt_local.foldlevel = 0 @@ -865,7 +887,7 @@ local function makespec_fugitive() }) end, keys = { - { "gg", "vertical Git", desc = "Fugitive" }, + { "gg", "vertical Git", desc = "Fugitive Status" }, { "gp", "Git! push", desc = "Git Push" }, { "gP", "Git! push -f", desc = "Git Force Push" }, }, @@ -874,7 +896,7 @@ end local function makespec_gitsigns() local function on_gitsigns_attach(bufnr) - local gs = require("gitsigns") + local gs = package.loaded.gitsigns local function next_hunk() if vim.wo.diff then vim.cmd.normal({ "]c", bang = true }) @@ -890,43 +912,45 @@ local function makespec_gitsigns() end end - local function bmap(l, r, desc, mode) vim.keymap.set(mode or "n", l, r, { buffer = bufnr, desc = desc }) end + local function bmap(mode, l, r, desc) vim.keymap.set(mode, l, r, { buffer = bufnr, desc = desc }) end -- Navigation - bmap("", next_hunk, "Prev Hunk") - bmap("", prev_hunk, "Next Hunk") - bmap("[h", prev_hunk, "Prev Hunk") - bmap("]h", next_hunk, "Next Hunk") + bmap("n", "", next_hunk, "Prev Hunk") + bmap("n", "", prev_hunk, "Next Hunk") + bmap("n", "[h", prev_hunk, "Prev Hunk") + bmap("n", "]h", next_hunk, "Next Hunk") -- Blame - bmap("gB", gs.blame, "Blame Buffer") - bmap("gb", function() gs.blame_line({ full = true }) end, "Blame Line") + bmap("n", "gB", gs.blame, "Blame Buffer") + bmap("n", "gb", function() gs.blame_line({ full = true }) end, "Blame Line") -- Hunk - bmap("gI", gs.preview_hunk_inline, "Preview Hunk Inline") - bmap("gi", gs.preview_hunk, "Preview Hunk") - bmap("gq", gs.setqflist, "File Hunks to QuickFix") - bmap("gQ", function() gs.setqflist("all") end, "All Hunks to QuickFix") - bmap("gx", gs.reset_hunk, "Reset Hunk") - bmap("gx", function() gs.reset_hunk({ vim.fn.line("."), vim.fn.line("v") }) end, "Reset Hunk", "v") - bmap("gR", gs.reset_buffer, "Reset Buffer") - bmap("gs", gs.stage_hunk, "Stage Hunk") - bmap("gs", function() gs.stage_hunk({ vim.fn.line("."), vim.fn.line("v") }) end, "Stage Hunk", "v") - bmap("gS", gs.stage_buffer, "Stage Buffer") - bmap("gv", gs.select_hunk, "Select Hunk", { "n", "v" }) + bmap("n", "gi", gs.preview_hunk_inline, "Preview Hunk Inline") + bmap("n", "gp", gs.preview_hunk, "Preview Hunk") + bmap("n", "gq", gs.setqflist, "File Hunks to QuickFix") + bmap("n", "gQ", function() gs.setqflist("all") end, "All Hunks to QuickFix") + bmap("n", "gx", gs.reset_hunk, "Reset Hunk") + bmap("v", "gx", function() gs.reset_hunk({ vim.fn.line("."), vim.fn.line("v") }) end, "Reset Selection") + bmap("n", "gR", gs.reset_buffer, "Reset Buffer") + bmap("n", "gs", gs.stage_hunk, "Stage Hunk") + bmap("v", "gs", function() gs.stage_hunk({ vim.fn.line("."), vim.fn.line("v") }) end, "Stage Selection") + bmap("n", "gS", gs.stage_buffer, "Stage Buffer") + bmap({ "n", "v" }, "gv", gs.select_hunk, "Select Hunk") -- Toggles - bmap("gtw", gs.toggle_word_diff, "Toggle Diff Word Colors") - bmap("gtl", gs.toggle_linehl, "Toggle Diff Line Highlight") - bmap("gtb", gs.toggle_current_line_blame, "Toggle Line Blame") - bmap("gtn", gs.toggle_numhl, "Toggle Diff Line Number Highlight") + bmap("n", "gtw", gs.toggle_word_diff, "Toggle Diff Word Colors") + bmap("n", "gtl", gs.toggle_linehl, "Toggle Diff Line Highlight") + bmap("n", "gtn", gs.toggle_numhl, "Toggle Diff Line Number Highlight") + bmap("n", "gtb", gs.toggle_current_line_blame, "Toggle Line Blame") + bmap("n", "gtd", gs.toggle_deleted, "Toggle Deleted") -- Text object, e.g. for dih to delete hunk - bmap("ih", "Gitsigns select_hunk", "Select Hunk", { "o", "x" }) + bmap({ "o", "x" }, "ih", ":Gitsigns select_hunk", "Select Hunk") end return { "lewis6991/gitsigns.nvim", + event = { "BufReadPre", "BufNewFile" }, opts = { signcolumn = true, numhl = false,