From 9085969edf939c332ddac8c021d9032060e2a45e Mon Sep 17 00:00:00 2001 From: Henrik Bakken Date: Sun, 20 Apr 2025 20:05:54 +0200 Subject: [PATCH] nvim gitsigns tweaks --- nvim/init.lua | 68 ++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/nvim/init.lua b/nvim/init.lua index d55861b..ac857f9 100644 --- a/nvim/init.lua +++ b/nvim/init.lua @@ -830,61 +830,63 @@ local function makespec_gitsigns() local function on_gitsigns_attach(bufnr) local gs = require("gitsigns") local function next_hunk() - if vim.wo.diff then return "]c" end - vim.schedule(function() gs.nav_hunk("next") end) - return "" + if vim.wo.diff then + vim.cmd.normal({ "]c", bang = true }) + else + gs.nav_hunk("next") + end end local function prev_hunk() - if vim.wo.diff then return "[c" end - vim.schedule(function() gs.nav_hunk("prev") end) - return "" + if vim.wo.diff then + vim.cmd.normal({ "[c", bang = true }) + else + gs.nav_hunk("prev") + end end - local function bmap(mode, l, r, desc) vim.keymap.set(mode, l, r, { buffer = bufnr, desc = desc }) end + local function bmap(l, r, desc, mode) vim.keymap.set(mode or "n", l, r, { buffer = bufnr, desc = desc }) end -- Navigation - 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") + bmap("", next_hunk, "Prev Hunk") + bmap("", prev_hunk, "Next Hunk") + bmap("[h", prev_hunk, "Prev Hunk") + bmap("]h", next_hunk, "Next Hunk") -- Blame - bmap("n", "gB", gs.blame, "Blame Buffer") - bmap("n", "gb", function() gs.blame_line({ full = true }) end, "Blame Line") + bmap("gB", gs.blame, "Blame Buffer") + bmap("gb", function() gs.blame_line({ full = true }) end, "Blame Line") -- Hunk - bmap("n", "gI", gs.preview_hunk_inline, "Preview Hunk Inline") - bmap("n", "gi", 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 Hunk") - 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 Hunk") - bmap("n", "gS", gs.stage_buffer, "Stage Buffer") - bmap("n", "gu", gs.undo_stage_hunk, "Undo Stage Hunk") - bmap({ "n", "v" }, "gv", gs.select_hunk, "Select 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" }) -- Toggles - bmap("n", "gtd", gs.toggle_deleted, "Toggle Show Deleted Lines") - bmap("n", "gtl", gs.toggle_linehl, "Toggle Diff Line Highlight") - bmap("n", "gtb", gs.toggle_current_line_blame, "Toggle Line Blame") - bmap("n", "gth", gs.toggle_word_diff, "Toggle Diff Word Colors") - bmap("n", "gtn", gs.toggle_numhl, "Toggle Diff Line Number Highlight") + 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") -- Text object, e.g. for dih to delete hunk - bmap({ "o", "x" }, "ih", "Gitsigns select_hunk") + bmap("ih", "Gitsigns select_hunk", "Select Hunk", { "o", "x" }) end return { "lewis6991/gitsigns.nvim", opts = { signcolumn = true, - numhl = true, + numhl = false, linehl = false, word_diff = false, - signs_staged_enable = false, + signs_staged_enable = true, on_attach = on_gitsigns_attach, }, }