From ba319f2bdee2baa8ab0256ba84790f97e804d362 Mon Sep 17 00:00:00 2001 From: Sanchayan Maity Date: Mon, 31 Oct 2022 12:36:08 +0530 Subject: [PATCH] nvim: after/plugin/gitsigns: Use on_attach callback to setup mappings keymaps is deprecated. --- nvim/.config/nvim/after/plugin/gitsigns.lua | 50 ++++++++++++--------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/nvim/.config/nvim/after/plugin/gitsigns.lua b/nvim/.config/nvim/after/plugin/gitsigns.lua index e4073de..4e96b13 100644 --- a/nvim/.config/nvim/after/plugin/gitsigns.lua +++ b/nvim/.config/nvim/after/plugin/gitsigns.lua @@ -39,27 +39,37 @@ require('gitsigns').setup { topdelete = { hl = 'GitSignsDelete', text = '‾', numhl='GitSignsDeleteNr', linehl='GitSignsDeleteLn' }, changedelete = { hl = 'GitSignsChange', text = '~', numhl='GitSignsChangeNr', linehl='GitSignsChangeLn' }, }, - keymaps = { - noremap = true, + on_attach = function(bufnr) + local gs = package.loaded.gitsigns - ['n ]c'] = { expr = true, "&diff ? ']c' : 'lua require\"gitsigns.actions\".next_hunk()'" }, - ['n [c'] = { expr = true, "&diff ? '[c' : 'lua require\"gitsigns.actions\".prev_hunk()'" }, + local function map(mode, l, r, opts) + opts = opts or {} + opts.buffer = bufnr + vim.keymap.set(mode, l, r, opts) + end - ['n hl'] = 'lua require"gitsigns".setloclist()', - ['n hp'] = 'lua require"gitsigns".preview_hunk()', - ['n hq'] = 'lua require"gitsigns".setqflist()', - ['n hQ'] = 'lua require"gitsigns".setqflist("attached")', - ['n hr'] = 'lua require"gitsigns".reset_hunk()', - ['v hr'] = 'lua require"gitsigns".reset_hunk({vim.fn.line("."), vim.fn.line("v")})', - ['n hR'] = 'lua require"gitsigns".reset_buffer()', - ['n hs'] = 'lua require"gitsigns".stage_hunk()', - ['v hs'] = 'lua require"gitsigns".stage_hunk({vim.fn.line("."), vim.fn.line("v")})', - ['n hS'] = 'lua require"gitsigns".stage_buffer()', - ['n ht'] = 'lua require"gitsigns".toggle_signs()', - ['n hu'] = 'lua require"gitsigns".undo_stage_hunk()', - ['n hU'] = 'lua require"gitsigns".reset_buffer_index()', + map('n', ']c', function() + if vim.wo.diff then return ']c' end + vim.schedule(function() gs.next_hunk() end) + return '' + end, {expr=true}) - ['o ih'] = ':lua require"gitsigns.actions".select_hunk()', - ['x ih'] = ':lua require"gitsigns.actions".select_hunk()' - }, + map('n', '[c', function() + if vim.wo.diff then return '[c' end + vim.schedule(function() gs.prev_hunk() end) + return '' + end, {expr=true}) + + map('n', 'hp', gs.preview_hunk) + map('n', 'hP', gs.preview_hunk_inline) + map('n', 'hl', gs.setloclist) + map('n', 'hq', gs.setqflist) + map('n', 'hQ', function() gs.setqflist('attached') end) + map('n', 'ht', gs.toggle_signs) + map('n', 'hu', gs.undo_stage_hunk) + + map({'n', 'v'}, 'hs', ':Gitsigns stage_hunk') + map({'n', 'v'}, 'hr', ':Gitsigns reset_hunk') + map({'o', 'x'}, 'ih', ':Gitsigns select_hunk') + end }