From df3282c64d72667d1aba06aa6a4d3a18e99dd568 Mon Sep 17 00:00:00 2001 From: punkfairie Date: Wed, 4 Dec 2024 21:35:35 -0800 Subject: [PATCH] feat: gitsigns.nvim --- nix/neovim-overlay.nix | 1 + nvim/lua/icons.lua | 8 ++ nvim/lua/lib/init.lua | 31 +++++- nvim/lua/plugins/editor/gitsigns-nvim.lua | 116 +++++++++++++++++++++ nvim/lua/plugins/editor/init.lua | 1 + nvim/lua/plugins/editor/which-key-nvim.lua | 1 + 6 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 nvim/lua/plugins/editor/gitsigns-nvim.lua diff --git a/nix/neovim-overlay.nix b/nix/neovim-overlay.nix index d2da6ad..f1b22a7 100644 --- a/nix/neovim-overlay.nix +++ b/nix/neovim-overlay.nix @@ -54,6 +54,7 @@ with final.pkgs.lib; let grug-far-nvim flash-nvim which-key-nvim + gitsigns-nvim ]; extraPackages = with pkgs; [ diff --git a/nvim/lua/icons.lua b/nvim/lua/icons.lua index cb25ea7..0983c85 100644 --- a/nvim/lua/icons.lua +++ b/nvim/lua/icons.lua @@ -16,5 +16,13 @@ return { removed = ' ', unstaged = '󰄱', staged = '󰱒', + diff = '󰫙', }, + left = '', + prev = '', + right = '', + next = '', + first = '󰘀', + last = '󰘁', + undo = '', } diff --git a/nvim/lua/lib/init.lua b/nvim/lua/lib/init.lua index 37545d7..8d3d8ef 100644 --- a/nvim/lua/lib/init.lua +++ b/nvim/lua/lib/init.lua @@ -21,12 +21,39 @@ end ---Generates a function that can be used to create which-key mappings. ---@param color string The color to use for the icon. +---@param setRhs? boolean Whether to allow setting the rhs. ---@return function -function M.wkSpec(color) +function M.wkSpec(color, setRhs) + if setRhs then + ---@param lhs string + ---@param rhs string | fun() + ---@param icon string | wk.Icon + ---@param opts? wk.Spec + return function(lhs, rhs, icon, opts) + if type(icon) == 'string' then + icon = { icon = icon, color = color } + else + icon = vim.tbl_deep_extend('force', icon, { color = color }) + end + + return vim.tbl_deep_extend( + 'force', + { lhs, rhs, icon = icon }, + (opts or {}) + ) + end + end + ---@param lhs string - ---@param icon string + ---@param icon string | wk.Icon ---@param opts? wk.Spec return function(lhs, icon, opts) + if type(icon) == 'string' then + icon = { icon = icon, color = color } + else + icon = vim.tbl_deep_extend('force', icon, { color = color }) + end + return vim.tbl_deep_extend( 'force', { lhs, icon = { icon = icon, color = color } }, diff --git a/nvim/lua/plugins/editor/gitsigns-nvim.lua b/nvim/lua/plugins/editor/gitsigns-nvim.lua new file mode 100644 index 0000000..b821cde --- /dev/null +++ b/nvim/lua/plugins/editor/gitsigns-nvim.lua @@ -0,0 +1,116 @@ +return { + 'gitsigns.nvim', + event = { 'BufReadPost', 'BufWritePost', 'BufNewFile' }, + before = function() + require('lz.n').trigger_load('which-key.nvim') + end, + after = function() + require('gitsigns').setup({ + signs = { + add = { text = '▎' }, + change = { text = '▎' }, + delete = { text = '' }, + topdelete = { text = '' }, + changedelete = { text = '▎' }, + untracked = { text = '▎' }, + }, + signs_staged = { + add = { text = '▎' }, + change = { text = '▎' }, + delete = { text = '' }, + topdelete = { text = '' }, + changedelete = { text = '▎' }, + }, + on_attach = function(buf) + local gitsigns = package.loaded.gitsigns + + local icons = require('icons') + local mkKey = MarleyVim.wkSpec(require('colors').git, true) + require('which-key').add({ + mkKey(']h', function() + if vim.wo.diff then + vim.cmd.normal({ ']c', bang = true }) + else + gitsigns.nav_hunk('next') + end + end, icons.next, { desc = 'next hunk' }), + mkKey('[h', function() + if vim.wo.diff then + vim.cmd.normal({ '[c', bang = true }) + else + gitsigns.nav_hunk('prev') + end + end, icons.prev, { desc = 'previous hunk' }), + mkKey(']H', function() + gitsigns.nav_hunk('last') + end, icons.last, { desc = 'last hunk' }), + mkKey('[H', function() + gitsigns.nav_hunk('first') + end, icons.first, { desc = 'first hunk' }), + mkKey( + 'ghs', + 'Gitsigns stage_hunk', + icons.git.staged, + { mode = { 'n', 'v' }, desc = 'stage hunk' } + ), + mkKey( + 'ghr', + 'Gitsigns reset_hunk', + icons.git.unstaged, + { mode = { 'n', 'v' }, desc = 'reset hunk' } + ), + mkKey( + 'ghS', + gitsigns.stage_buffer, + icons.git.staged, + { desc = 'stage buffer' } + ), + mkKey( + 'ghu', + gitsigns.undo_stage_hunk, + icons.undo, + { desc = 'undo stage hunk' } + ), + mkKey( + 'ghR', + gitsigns.reset_buffer, + icons.git.unstaged, + { desc = 'reset buffer' } + ), + mkKey( + 'ghp', + gitsigns.preview_hunk_inline, + '', + { desc = 'preview hunk inline' } + ), + mkKey('ghb', function() + gitsigns.blame_line({ full = true }) + end, { cat = 'filetype', name = 'git' }, { + desc = 'blame line', + }), + mkKey( + 'ghB', + gitsigns.blame, + { cat = 'filetype', name = 'git' }, + { desc = 'blame buffer' } + ), + mkKey( + 'ghd', + gitsigns.diffthis, + icons.git.diff, + { desc = 'diff file' } + ), + mkKey('ghD', function() + gitsigns.diffthis('~') + end, icons.git.diff, { desc = 'diff file from ~' }), + mkKey( + 'ih', + 'Gitsigns select_hunk', + '󰿚', + { mode = { 'o', 'x' }, desc = 'select hunk' } + ), + }) + end, + }) + end, +} diff --git a/nvim/lua/plugins/editor/init.lua b/nvim/lua/plugins/editor/init.lua index ef2588d..ca0cd8f 100644 --- a/nvim/lua/plugins/editor/init.lua +++ b/nvim/lua/plugins/editor/init.lua @@ -2,6 +2,7 @@ local req = MarleyVim.local_require('plugins.editor') return { req('flash-nvim'), + req('gitsigns-nvim'), req('grug-far-nvim'), req('neo-tree-nvim'), req('which-key-nvim'), diff --git a/nvim/lua/plugins/editor/which-key-nvim.lua b/nvim/lua/plugins/editor/which-key-nvim.lua index 4db3ddb..e4614e6 100644 --- a/nvim/lua/plugins/editor/which-key-nvim.lua +++ b/nvim/lua/plugins/editor/which-key-nvim.lua @@ -108,6 +108,7 @@ return { }, { pattern = 'message', icon = '󰈸', color = colors.notifications }, { pattern = 'session', icon = '', color = colors.sessions }, + { pattern = 'hunk', color = colors.git }, { pattern = 'diagnostic', icon = icons.diagnostics.Info,