From 4adb03cd1dfa2dbc4993166d19bd47796e3831ae Mon Sep 17 00:00:00 2001 From: n3oney Date: Sat, 22 Apr 2023 18:47:19 +0200 Subject: [PATCH] feat: add gitsigns keybindings --- modules/git/config.nix | 77 +++++++++++++++++++++++++++--------------- modules/git/git.nix | 33 +++++++++++++----- 2 files changed, 73 insertions(+), 37 deletions(-) diff --git a/modules/git/config.nix b/modules/git/config.nix index fffda35..cda7279 100644 --- a/modules/git/config.nix +++ b/modules/git/config.nix @@ -1,5 +1,4 @@ { - pkgs, config, lib, ... @@ -7,40 +6,62 @@ with lib; with builtins; let cfg = config.vim.git; + + self = import ./git.nix {inherit lib;}; + gsMappingDefinitions = self.options.vim.git.gitsigns.mappings; + + gsMappings = addDescriptionsToMappings cfg.gitsigns.mappings gsMappingDefinitions; in { config = mkIf cfg.enable (mkMerge [ (mkIf cfg.gitsigns.enable (mkMerge [ { vim.startPlugins = ["gitsigns-nvim"]; + vim.maps.normal = mkMerge [ + (mkSetExprBinding gsMappings.nextHunk '' + function() + if vim.wo.diff then return ${toJSON gsMappings.nextHunk.value} end + + vim.schedule(function() package.loaded.gitsigns.next_hunk() end) + + return '' + end + '') + (mkSetExprBinding gsMappings.previousHunk '' + function() + if vim.wo.diff then return ${toJSON gsMappings.previousHunk.value} end + + vim.schedule(function() package.loaded.gitsigns.prev_hunk() end) + + return '' + end + '') + + (mkSetLuaBinding gsMappings.stageHunk "package.loaded.gitsigns.stage_hunk") + (mkSetLuaBinding gsMappings.resetHunk "package.loaded.gitsigns.reset_hunk") + (mkSetLuaBinding gsMappings.undoStageHunk "package.loaded.gitsigns.undo_stage_hunk") + + (mkSetLuaBinding gsMappings.stageBuffer "package.loaded.gitsigns.stage_buffer") + (mkSetLuaBinding gsMappings.resetBuffer "package.loaded.gitsigns.reset_buffer") + + (mkSetLuaBinding gsMappings.previewHunk "package.loaded.gitsigns.preview_hunk") + + (mkSetLuaBinding gsMappings.blameLine "function() package.loaded.gitsigns.blame_line{full=true} end") + (mkSetLuaBinding gsMappings.toggleBlame "package.loaded.gitsigns.toggle_current_line_blame") + + (mkSetLuaBinding gsMappings.diffThis "package.loaded.gitsigns.diffthis") + (mkSetLuaBinding gsMappings.diffProject "function() package.loaded.gitsigns.diffthis('~') end") + + (mkSetLuaBinding gsMappings.toggleDeleted "package.loaded.gitsigns.toggle_deleted") + ]; + + vim.maps.visual = mkMerge [ + (mkSetLuaBinding gsMappings.stageHunk "function() package.loaded.gitsigns.stage_hunk {vim.fn.line('.'), vim.fn.line('v')} end") + (mkSetLuaBinding gsMappings.resetHunk "function() package.loaded.gitsigns.reset_hunk {vim.fn.line('.'), vim.fn.line('v')} end") + ]; + vim.luaConfigRC.gitsigns = nvim.dag.entryAnywhere '' - require('gitsigns').setup { - keymaps = { - noremap = true, - - ['n gn'] = { expr = true, "&diff ? \'\' : 'Gitsigns next_hunk'"}, - ['n gp'] = { expr = true, "&diff ? \'\' : 'Gitsigns prev_hunk'"}, - - ['n gs'] = 'Gitsigns stage_hunk', - ['v gs'] = ':Gitsigns stage_hunk', - ['n gu'] = 'Gitsigns undo_stage_hunk', - ['n gr'] = 'Gitsigns reset_hunk', - ['v gr'] = ':Gitsigns reset_hunk', - ['n gR'] = 'Gitsigns reset_buffer', - ['n gp'] = 'Gitsigns preview_hunk', - ['n gb'] = 'lua require"gitsigns".blame_line{full=true}', - ['n gS'] = 'Gitsigns stage_buffer', - ['n gU'] = 'Gitsigns reset_buffer_index', - ['n gts'] = ':Gitsigns toggle_signs', - ['n gtn'] = ':Gitsigns toggle_numhl', - ['n gtl'] = ':Gitsigns toggle_linehl', - ['n gtw'] = ':Gitsigns toggle_word_diff', - - -- Text objects - ['o ih'] = ':Gitsigns select_hunk', - ['x ih'] = ':Gitsigns select_hunk' - }, - } + require('gitsigns').setup{} ''; } diff --git a/modules/git/git.nix b/modules/git/git.nix index 80da18c..51f162f 100644 --- a/modules/git/git.nix +++ b/modules/git/git.nix @@ -1,19 +1,34 @@ -{ - pkgs, - config, - lib, - ... -}: +{lib, ...}: with lib; -with builtins; let - cfg = config.vim.git; -in { +with builtins; { options.vim.git = { enable = mkEnableOption "Git support"; gitsigns = { enable = mkEnableOption "gitsigns"; + mappings = { + nextHunk = mkMappingOption "Next hunk [Gitsigns]" "]c"; + previousHunk = mkMappingOption "Previous hunk [Gitsigns]" "[c"; + + stageHunk = mkMappingOption "Stage hunk [Gitsigns]" "hs"; + undoStageHunk = mkMappingOption "Undo stage hunk [Gitsigns]" "hu"; + resetHunk = mkMappingOption "Reset hunk [Gitsigns]" "hr"; + + stageBuffer = mkMappingOption "Stage buffer [Gitsigns]" "hS"; + resetBuffer = mkMappingOption "Reset buffer [Gitsigns]" "hR"; + + previewHunk = mkMappingOption "Preview hunk [Gitsigns]" "hP"; + + blameLine = mkMappingOption "Blame line [Gitsigns]" "hb"; + toggleBlame = mkMappingOption "Toggle blame [Gitsigns]" "tb"; + + diffThis = mkMappingOption "Diff this [Gitsigns]" "hd"; + diffProject = mkMappingOption "Diff project [Gitsigns]" "hD"; + + toggleDeleted = mkMappingOption "Toggle deleted [Gitsigns]" "td"; + }; + codeActions = mkEnableOption "gitsigns codeactions through null-ls"; }; };