From c8c795e65b76c15a584cb16d8442567fca6f5ad7 Mon Sep 17 00:00:00 2001 From: Oliver Hartmann Date: Mon, 17 Feb 2025 10:25:53 +0000 Subject: [PATCH] some gitsings keymaps --- lua/plugins/gitsigns.lua | 83 +++++++++++++++++++++++++++++++++++----- 1 file changed, 74 insertions(+), 9 deletions(-) diff --git a/lua/plugins/gitsigns.lua b/lua/plugins/gitsigns.lua index b5acb86..23885c8 100644 --- a/lua/plugins/gitsigns.lua +++ b/lua/plugins/gitsigns.lua @@ -1,13 +1,78 @@ return { 'lewis6991/gitsigns.nvim', branch = 'main', - opts = { - current_line_blame = false, - current_line_blame_opts = { - virt_text = true, - virt_text_pos = 'eol', -- 'eol' | 'overlay' | 'right_align' - delay = 1000, - ignore_whitespace = false, - }, - }, + config = function() + require('gitsigns').setup { + current_line_blame = false, + current_line_blame_opts = { + virt_text = true, + virt_text_pos = 'eol', -- 'eol' | 'overlay' | 'right_align' + delay = 1000, + ignore_whitespace = false, + }, + on_attach = function(bufnr) + local gitsigns = require('gitsigns') + + + -- Navigation + vim.keymap.set('n', ']c', function() + if vim.wo.diff then + vim.cmd.normal({ ']c', bang = true }) + else + gitsigns.nav_hunk('next') + end + end, + { desc = 'Next hunk' } + ) + + vim.keymap.set('n', '[c', function() + if vim.wo.diff then + vim.cmd.normal({ '[c', bang = true }) + else + gitsigns.nav_hunk('prev') + end + end, + { desc = 'Previous hunk' } + ) + + -- Actions + vim.keymap.set('n', 'hs', gitsigns.stage_hunk, { desc = 'Stage hunk' }) + vim.keymap.set('n', 'hr', gitsigns.reset_hunk, { desc = 'Reset hunk' }) + + vim.keymap.set('v', 'hs', function() + gitsigns.stage_hunk({ vim.fn.line('.'), vim.fn.line('v') }) + end, + { desc = 'Stage hunk' } + ) + + vim.keymap.set('v', 'hr', function() + gitsigns.reset_hunk({ vim.fn.line('.'), vim.fn.line('v') }) + end, + { desc = 'Reset hunk' } + ) + + vim.keymap.set('n', 'hS', gitsigns.stage_buffer, { desc = 'Stage buffer' }) + vim.keymap.set('n', 'hR', gitsigns.reset_buffer, { desc = 'Reset buffer' }) + vim.keymap.set('n', 'hp', gitsigns.preview_hunk, { desc = 'Preview hunk' }) + vim.keymap.set('n', 'hi', gitsigns.preview_hunk_inline, { desc = 'Preview hunk inline' }) + + vim.keymap.set('n', 'hb', function() gitsigns.blame_line({ full = true }) end, { desc = 'Blame line' }) + + vim.keymap.set('n', 'hd', gitsigns.diffthis, { desc = 'Diff this' }) + + vim.keymap.set('n', 'hD', function() gitsigns.diffthis('~') end, { desc = 'Diff this (cached)' }) + + vim.keymap.set('n', 'hQ', function() gitsigns.setqflist('all') end, { desc = 'Set quickfix list all' }) + vim.keymap.set('n', 'hq', gitsigns.setqflist, { desc = 'Set quickfix list' }) + + -- Toggles + vim.keymap.set('n', 'tb', gitsigns.toggle_current_line_blame, { desc = 'Toggle blame' }) + vim.keymap.set('n', 'td', gitsigns.toggle_deleted, { desc = 'Toggle deleted' }) + vim.keymap.set('n', 'tw', gitsigns.toggle_word_diff, { desc = 'Toggle word diff' }) + + -- Text object + vim.keymap.set({ 'o', 'x' }, 'ih', ':Gitsigns select_hunk') + end + } + end }