feat: gitsigns.nvim

This commit is contained in:
punkfairie 2024-12-04 21:35:35 -08:00
parent 7d4b907416
commit df3282c64d
Signed by: punkfairie
GPG key ID: A509E8F77FB9D696
6 changed files with 156 additions and 2 deletions

View file

@ -54,6 +54,7 @@ with final.pkgs.lib; let
grug-far-nvim
flash-nvim
which-key-nvim
gitsigns-nvim
];
extraPackages = with pkgs; [

View file

@ -16,5 +16,13 @@ return {
removed = '',
unstaged = '󰄱',
staged = '󰱒',
diff = '󰫙',
},
left = '',
prev = '',
right = '',
next = '',
first = '󰘀',
last = '󰘁',
undo = '',
}

View file

@ -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 } },

View file

@ -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(
'<LEADER>ghs',
'<CMD>Gitsigns stage_hunk<CR>',
icons.git.staged,
{ mode = { 'n', 'v' }, desc = 'stage hunk' }
),
mkKey(
'<LEADER>ghr',
'<CMD>Gitsigns reset_hunk<CR>',
icons.git.unstaged,
{ mode = { 'n', 'v' }, desc = 'reset hunk' }
),
mkKey(
'<LEADER>ghS',
gitsigns.stage_buffer,
icons.git.staged,
{ desc = 'stage buffer' }
),
mkKey(
'<LEADER>ghu',
gitsigns.undo_stage_hunk,
icons.undo,
{ desc = 'undo stage hunk' }
),
mkKey(
'<LEADER>ghR',
gitsigns.reset_buffer,
icons.git.unstaged,
{ desc = 'reset buffer' }
),
mkKey(
'<LEADER>ghp',
gitsigns.preview_hunk_inline,
'',
{ desc = 'preview hunk inline' }
),
mkKey('<LEADER>ghb', function()
gitsigns.blame_line({ full = true })
end, { cat = 'filetype', name = 'git' }, {
desc = 'blame line',
}),
mkKey(
'<LEADER>ghB',
gitsigns.blame,
{ cat = 'filetype', name = 'git' },
{ desc = 'blame buffer' }
),
mkKey(
'<LEADER>ghd',
gitsigns.diffthis,
icons.git.diff,
{ desc = 'diff file' }
),
mkKey('<LEADER>ghD', function()
gitsigns.diffthis('~')
end, icons.git.diff, { desc = 'diff file from ~' }),
mkKey(
'ih',
'<CMD><C-u>Gitsigns select_hunk<CR>',
'󰿚',
{ mode = { 'o', 'x' }, desc = 'select hunk' }
),
})
end,
})
end,
}

View file

@ -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'),

View file

@ -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,