From 2fd6a1d4f03a6ea79aa8487f7e5443423b1d8a7a Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Thu, 30 May 2024 12:06:47 -0600 Subject: [PATCH] refactor(gitsigns): API integrations. Use `maps.wk` if possible, otherwise use `maps.kmap` instead of `maps.buf_map`. Rewrote code to accomodate. --- lua/lazy_cfg/gitsigns/init.lua | 95 +++++++++++++++++++++++----------- 1 file changed, 66 insertions(+), 29 deletions(-) diff --git a/lua/lazy_cfg/gitsigns/init.lua b/lua/lazy_cfg/gitsigns/init.lua index 7474db3f..ceaa276e 100644 --- a/lua/lazy_cfg/gitsigns/init.lua +++ b/lua/lazy_cfg/gitsigns/init.lua @@ -4,14 +4,18 @@ local User = require('user') local Check = User.check local types = User.types.gitsigns -local bufmap = User.maps.buf_map +local kmap = User.maps.kmap +local WK = User.maps.wk local exists = Check.exists.module local executable = Check.exists.executable local is_nil = Check.value.is_nil +local is_tbl = Check.value.is_tbl local is_int = Check.value.is_int local is_num = Check.value.is_num local is_fun = Check.value.is_fun +local empty = Check.value.empty +local desc = kmap.desc if not exists('gitsigns') or not executable('git') then return @@ -19,30 +23,59 @@ end local GS = require('gitsigns') ----@type table -local keys = { +---@type table +local Keys = { n = { - -- Navigati7on - { 'G]c', "&diff ? ']c' : 'Gitsigns next_hunk'", { expr = true } }, - { 'G[c', "&diff ? '[c' : 'Gitsigns prev_hunk'", { expr = true } }, + -- Navigation + ['G]c'] = { + "&diff ? ']c' : 'Gitsigns next_hunk'", + desc('Next Hunk', true, 0, true, true, true), + }, + ['G[c'] = { + "&diff ? '[c' : 'Gitsigns prev_hunk'", + desc('Previous Hunk', true, 0, true, true, true), + }, -- Actions - { 'Ghs', 'Gitsigns stage_hunk' }, - { 'Ghr', 'Gitsigns reset_hunk' }, - { 'GhS', 'Gitsigns stage_buffer' }, - { 'Ghu', 'Gitsigns undo_stage_hunk' }, - { 'GhR', 'Gitsigns reset_buffer' }, - { 'Ghp', 'Gitsigns preview_hunk' }, - { 'Ghb', 'lua require("gitsigns").blame_line{full=true}' }, - { 'Gtb', 'Gitsigns toggle_current_line_blame' }, - { 'Ghd', 'Gitsigns diffthis' }, - { 'GhD', 'lua require("gitsigns").diffthis("~")' }, - { 'Gtd', 'Gitsigns toggle_deleted' }, + ['Ghs'] = { 'Gitsigns stage_hunk', desc('Stage Current Hunk') }, + ['Ghr'] = { 'Gitsigns reset_hunk', desc('Reset Current Hunk') }, + ['Ghu'] = { 'Gitsigns undo_stage_hunk', desc('Undo Hunk Stage') }, + ['Ghp'] = { 'Gitsigns preview_hunk', desc('Preview Current Hunk') }, + ['GhS'] = { 'Gitsigns stage_buffer', desc('Stage The Whole Buffer') }, + ['GhR'] = { 'Gitsigns reset_buffer', desc('Reset The Whole Buffer') }, + ['Ghb'] = { + function() + GS.blame_line({ full = true }) + end, + desc('Blame Current Line'), + }, + ['Ghd'] = { 'Gitsigns diffthis', desc('Diff Against Index') }, + ['GhD'] = { + function() + GS.diffthis('~') + end, + desc('Diff This'), + }, + ['Gtb'] = { 'Gitsigns toggle_current_line_blame', desc('Toggle Line Blame') }, + ['Gtd'] = { 'Gitsigns toggle_deleted', desc('Toggle Deleted') }, }, - -- WARNING: Avoid using `` at all costs. v = { - { 'Ghs', ':Gitsigns stage_hunk' }, - { 'Ghr', ':Gitsigns reset_hunk' }, + { 'Ghs', ':Gitsigns stage_hunk', desc('Stage Selected Hunk(s)') }, + { 'Ghr', ':Gitsigns reset_hunk', desc('Reset Selected Hunk(s)') }, + }, +} +---@type table +local Names = { + n = { + ['G'] = { name = '+Gitsigns' }, + ['Gh'] = { name = '+Hunks' }, + ['Gt'] = { name = '+Toggles' }, + ['G['] = { name = '+Previous Hunk' }, + ['G]'] = { name = '+Next Hunk' }, + }, + v = { + ['G'] = { name = '+Gitsigns' }, + ['Gh'] = { name = '+Hunks' }, }, } @@ -59,17 +92,21 @@ local signs = { local opts = { ---@type fun(bufnr: integer) on_attach = function(bufnr) - bufnr = is_int(bufnr) and bufnr or 0 + bufnr = is_int(bufnr) and bufnr or vim.api.nvim_get_current_buf() + + for mode, t in next, Keys do + if WK.available() then + if is_tbl(Names[mode]) and not empty(Names[mode]) then + WK.register(Names[mode], { mode = mode, buffer = bufnr }) + end - for mode, v in next, keys do - ---@type BufMapFunction - local func = bufmap[mode] + WK.register(WK.convert_dict(t), { mode = mode, buffer = bufnr }) + else + for lhs, v in next, t do + v[2] = is_tbl(v[2]) and v[2] or {} + v[2].buffer = bufnr - for _, t in next, v do - if not is_nil(t.lhs) and not is_nil(t.rhs) then - func(bufnr, t.lhs, t.rhs, t.opts or {}) - elseif not is_nil(t[1]) and not is_nil(t[2]) then - func(bufnr, t[1], t[2], t[3] or {}) + kmap[mode](lhs, v[1], v[2]) end end end