2023-05-06 19:58:30 +02:00
|
|
|
local gitsigns = require("gitsigns")
|
2024-07-12 21:38:40 +02:00
|
|
|
local utils = require("ambroisie.utils")
|
2022-04-20 14:01:00 +02:00
|
|
|
local wk = require("which-key")
|
2022-03-03 22:15:26 +01:00
|
|
|
|
2024-07-12 20:52:03 +02:00
|
|
|
--- 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
|
|
|
|
|
2024-07-12 21:11:51 +02:00
|
|
|
local function nav_hunk(dir)
|
2024-07-12 21:38:40 +02:00
|
|
|
if vim.wo.diff then
|
|
|
|
local map = {
|
|
|
|
prev = "[c",
|
|
|
|
next = "]c",
|
|
|
|
}
|
|
|
|
vim.cmd.normal({ map[dir], bang = true })
|
|
|
|
else
|
|
|
|
gitsigns.nav_hunk(dir)
|
2024-07-12 21:11:51 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-03 22:15:26 +01:00
|
|
|
gitsigns.setup({
|
|
|
|
current_line_blame_opts = {
|
|
|
|
-- Show the blame quickly
|
|
|
|
delay = 100,
|
|
|
|
},
|
2024-07-12 21:59:58 +02:00
|
|
|
-- Work-around for https://github.com/lewis6991/gitsigns.nvim/issues/929
|
|
|
|
signs_staged_enable = false,
|
2022-04-20 14:01:00 +02:00
|
|
|
})
|
2022-03-03 22:15:26 +01:00
|
|
|
|
2022-04-20 14:01:00 +02:00
|
|
|
local keys = {
|
|
|
|
-- Navigation
|
2024-07-20 23:43:19 +02:00
|
|
|
{ "[c", utils.partial(nav_hunk, "prev"), desc = "Previous hunk/diff" },
|
|
|
|
{ "]c", utils.partial(nav_hunk, "next"), desc = "Next hunk/diff" },
|
2022-04-20 14:01:00 +02:00
|
|
|
-- Commands
|
2024-07-20 23:43:19 +02:00
|
|
|
{ "<leader>g", group = "Git" },
|
|
|
|
{ "<leader>gb", gitsigns.toggle_current_line_blame, desc = "Toggle blame virtual text" },
|
|
|
|
{ "<leader>gd", gitsigns.diffthis, desc = "Diff buffer" },
|
|
|
|
{ "<leader>gD", utils.partial(gitsigns.diffthis, "~"), desc = "Diff buffer against last commit" },
|
|
|
|
{ "<leader>gg", "<cmd>Git<CR>", desc = "Git status" },
|
|
|
|
{ "<leader>gh", gitsigns.toggle_deleted, desc = "Show deleted hunks" },
|
|
|
|
{ "<leader>gL", "<cmd>:sp<CR><C-w>T:Gllog --follow -- %:p<CR>", desc = "Current buffer log" },
|
|
|
|
{ "<leader>gm", "<Plug>(git-messenger)", desc = "Current line blame" },
|
|
|
|
{ "<leader>gp", gitsigns.preview_hunk, desc = "Preview hunk" },
|
|
|
|
{ "<leader>gr", gitsigns.reset_hunk, desc = "Restore hunk" },
|
|
|
|
{ "<leader>gR", gitsigns.reset_buffer, desc = "Restore buffer" },
|
|
|
|
{ "<leader>gs", gitsigns.stage_hunk, desc = "Stage hunk" },
|
|
|
|
{ "<leader>gS", gitsigns.stage_buffer, desc = "Stage buffer" },
|
|
|
|
{ "<leader>gu", gitsigns.undo_stage_hunk, desc = "Undo stage hunk" },
|
|
|
|
{ "<leader>g[", utils.partial(gitsigns.nav_hunk, "prev"), desc = "Previous hunk" },
|
|
|
|
{ "<leader>g]", utils.partial(gitsigns.nav_hunk, "next"), desc = "Next hunk" },
|
2022-04-20 14:01:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
local objects = {
|
2024-07-20 23:43:19 +02:00
|
|
|
mode = "o",
|
|
|
|
{ "ih", gitsigns.select_hunk, desc = "Git hunk" },
|
2022-04-20 14:01:00 +02:00
|
|
|
}
|
2024-07-20 23:43:19 +02:00
|
|
|
-- Visual
|
2022-04-20 14:01:00 +02:00
|
|
|
local visual = {
|
2024-07-20 23:43:19 +02:00
|
|
|
mode = { "x" },
|
|
|
|
{ "ih", gitsigns.select_hunk, desc = "Git hunk" },
|
|
|
|
{ "<leader>g", group = "Git" },
|
|
|
|
{ "<leader>gp", gitsigns.preview_hunk, desc = "Preview selection" },
|
|
|
|
{ "<leader>gr", make_visual(gitsigns.reset_hunk), desc = "Restore selection" },
|
|
|
|
{ "<leader>gs", make_visual(gitsigns.stage_hunk), desc = "Stage selection" },
|
|
|
|
{ "<leader>gu", gitsigns.undo_stage_hunk, desc = "Undo stage selection" },
|
2022-04-20 14:01:00 +02:00
|
|
|
}
|
2022-03-03 22:15:26 +01:00
|
|
|
|
2024-07-20 23:43:19 +02:00
|
|
|
wk.add(keys)
|
|
|
|
wk.add(objects)
|
|
|
|
wk.add(visual)
|