1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
--[[ 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)
`<Leader>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", "<Leader>hs", gitsigns.stage_hunk, "Stage/unstage hunk")
map("n", "<Leader>hr", gitsigns.reset_hunk, "Reset hunk")
map("x", "<Leader>hs", stage_selection, "Stage selected hunk")
map("x", "<Leader>hr", reset_selection, "Reset selected hunk")
map("n", "<Leader>hS", gitsigns.stage_buffer, "Stage buffer")
map("n", "<Leader>hR", gitsigns.reset_buffer, "Reset buffer")
-- Inspecting.
map("n", "<Leader>hp", gitsigns.preview_hunk, "Preview hunk")
map("n", "<Leader>hb", blame_line, "Blame line")
map("n", "<Leader>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 })
|