Compare commits

...

2 commits

Author SHA1 Message Date
Bruno BELANYI a25b541650 home: vim: git: use lua in hunk mappings
All checks were successful
ci/woodpecker/push/check Pipeline was successful
2024-07-12 20:12:57 +01:00
Bruno BELANYI 11201a685d home: vim: git: use lua in visual mappings
I thought the partial staging feature had broken, but it looks to be
unrelated [1].

[1]: https://github.com/lewis6991/gitsigns.nvim/issues/1088
2024-07-12 20:12:57 +01:00

View file

@ -1,6 +1,29 @@
local gitsigns = require("gitsigns") local gitsigns = require("gitsigns")
local wk = require("which-key") local wk = require("which-key")
--- Transform `f` into a function which acts on the current visual selection
local function make_visual(f)
return function()
local first = vim.fn.line("v")
local last = vim.fn.line(".")
f({ first, last })
end
end
local function nav_hunk(dir)
return function()
if vim.wo.diff then
local map = {
prev = "[c",
next = "]c",
}
vim.cmd.normal({ map[dir], bang = true })
else
gitsigns.nav_hunk(dir)
end
end
end
gitsigns.setup({ gitsigns.setup({
current_line_blame_opts = { current_line_blame_opts = {
-- Show the blame quickly -- Show the blame quickly
@ -10,8 +33,8 @@ gitsigns.setup({
local keys = { local keys = {
-- Navigation -- Navigation
["[c"] = { "&diff ? '[c' : '<cmd>Gitsigns prev_hunk<CR>'", "Previous hunk/diff", expr = true }, ["[c"] = { nav_hunk("prev"), "Previous hunk/diff" },
["]c"] = { "&diff ? ']c' : '<cmd>Gitsigns next_hunk<CR>'", "Next hunk/diff", expr = true }, ["]c"] = { nav_hunk("next"), "Next hunk/diff" },
-- Commands -- Commands
["<leader>g"] = { ["<leader>g"] = {
@ -46,10 +69,10 @@ local visual = {
-- Only the actual command can make use of the visual selection... -- Only the actual command can make use of the visual selection...
["<leader>g"] = { ["<leader>g"] = {
name = "Git", name = "Git",
p = { ":Gitsigns preview_hunk<CR>", "Preview selection" }, p = { gitsigns.preview_hunk, "Preview selection" },
r = { ":Gitsigns reset_hunk<CR>", "Restore selection" }, r = { make_visual(gitsigns.reset_hunk), "Restore selection" },
s = { ":Gitsigns stage_hunk<CR>", "Stage selection" }, s = { make_visual(gitsigns.stage_hunk), "Stage selection" },
u = { ":Gitsigns undo_stage_hunk<CR>", "Undo stage selection" }, u = { gitsigns.undo_stage_hunk, "Undo stage selection" },
}, },
} }