--[[ 50-gitsigns.lua — git diff signs plus hunk navigation/staging, via gitsigns.nvim. Signs render by default in the always-on sign column; changed lines and their line numbers are highlighted too (linehl/numhl). Keymaps are buffer-local (set in on_attach): `]c`/`[c` navigate hunks (falling through to Vim's builtin diff-mode nav when 'diff' is set) `h…` stage/reset/preview/blame, and `ih` is a hunk text object. lualine reads the diff counts from gitsigns (see 50-color.lua). ]] local gitsigns = require("gitsigns") -- Jump to the next/prev hunk, falling through to builtin ]c/[c when in diff mode. local function nav_hunk(direction) if vim.wo.diff then vim.cmd.normal({ direction == "next" and "]c" or "[c", bang = true }) else gitsigns.nav_hunk(direction) end end -- Stage/reset the visually-selected range of lines. local function stage_selection() gitsigns.stage_hunk({ vim.fn.line("."), vim.fn.line("v") }) end local function reset_selection() gitsigns.reset_hunk({ vim.fn.line("."), vim.fn.line("v") }) end local function blame_line() gitsigns.blame_line({ full = true }) end -- Buffer-local gitsigns keymaps; passed as `on_attach`. local function set_keymaps(bufnr) local function map(mode, lhs, rhs, desc) vim.keymap.set(mode, lhs, rhs, { buffer = bufnr, desc = desc }) end -- Hunk navigation. map("n", "]c", function() nav_hunk("next") end, "Next git hunk") map("n", "[c", function() nav_hunk("prev") end, "Previous git hunk") -- Staging / resetting. `stage_hunk` toggles, so it also unstages a staged hunk. map("n", "hs", gitsigns.stage_hunk, "Stage/unstage hunk") map("n", "hr", gitsigns.reset_hunk, "Reset hunk") map("x", "hs", stage_selection, "Stage selected hunk") map("x", "hr", reset_selection, "Reset selected hunk") map("n", "hS", gitsigns.stage_buffer, "Stage buffer") map("n", "hR", gitsigns.reset_buffer, "Reset buffer") -- Inspecting. map("n", "hp", gitsigns.preview_hunk, "Preview hunk") map("n", "hb", blame_line, "Blame line") map("n", "hd", gitsigns.diffthis, "Diff against index") -- Hunk text object. map({ "o", "x" }, "ih", gitsigns.select_hunk, "Inner git hunk") end gitsigns.setup({ on_attach = set_keymaps, linehl = true, numhl = true })