diff --git a/init.lua b/init.lua index 013fcc2..75ff178 100644 --- a/init.lua +++ b/init.lua @@ -255,6 +255,58 @@ require('lazy').setup({ topdelete = { text = '‾' }, changedelete = { text = '~' }, }, + on_attach = function(bufnr) + local gs = package.loaded.gitsigns + + local function map(mode, l, r, opts) + opts = opts or {} + opts.buffer = bufnr + vim.keymap.set(mode, l, r, opts) + end + + -- Navigation + map({ 'n', 'v' }, ']c', function() + if vim.wo.diff then + return ']c' + end + vim.schedule(gs.next_hunk) + return '' + end, { expr = true, desc = 'Jump to next git [c]hange' }) + + map({ 'n', 'v' }, '[c', function() + if vim.wo.diff then + return '[c' + end + vim.schedule(gs.prev_hunk) + return '' + end, { expr = true, desc = 'Jump to previous git [c]hange' }) + + -- Actions + -- visual mode + map('v', 'hs', function() + gs.stage_hunk { vim.fn.line '.', vim.fn.line 'v' } + end, { desc = 'stage git hunk' }) + map('v', 'hr', function() + gs.reset_hunk { vim.fn.line '.', vim.fn.line 'v' } + end, { desc = 'reset git hunk' }) + -- normal mode + map('n', 'hs', gs.stage_hunk, { desc = 'git [s]tage hunk' }) + map('n', 'hr', gs.reset_hunk, { desc = 'git [r]eset hunk' }) + map('n', 'hS', gs.stage_buffer, { desc = 'git [S]tage buffer' }) + map('n', 'hu', gs.undo_stage_hunk, { desc = 'git [u]ndo stage hunk' }) + map('n', 'hR', gs.reset_buffer, { desc = 'git [R]eset buffer' }) + map('n', 'hp', gs.preview_hunk, { desc = 'git [p]review hunk' }) + map('n', 'hb', function() + gs.blame_line { full = false } + end, { desc = 'git [b]lame line' }) + map('n', 'hd', gs.diffthis, { desc = 'git [d]iff against index' }) + map('n', 'hD', function() + gs.diffthis '@' + end, { desc = 'git [D]iff against last commit' }) + -- Toggles + map('n', 'tb', gs.toggle_current_line_blame, { desc = '[T]oggle git show [b]lame line' }) + map('n', 'td', gs.toggle_deleted, { desc = '[T]oggle git show [d]eleted' }) + end, }, }, @@ -286,7 +338,13 @@ require('lazy').setup({ ['r'] = { name = '[R]ename', _ = 'which_key_ignore' }, ['s'] = { name = '[S]earch', _ = 'which_key_ignore' }, ['w'] = { name = '[W]orkspace', _ = 'which_key_ignore' }, + ['h'] = { name = 'Git [H]unk', _ = 'which_key_ignore' }, + ['t'] = { name = '[T]oggle', _ = 'which_key_ignore' }, } + -- visual mode + require('which-key').register({ + ['h'] = { 'Git [H]unk' }, + }, { mode = 'v' }) end, },