Skip to content

Commit

Permalink
fix(blame): popupmenu error
Browse files Browse the repository at this point in the history
Fixes #1061
  • Loading branch information
lewis6991 committed Jun 21, 2024
1 parent 0dc8866 commit 93c38d9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
10 changes: 7 additions & 3 deletions doc/gitsigns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,13 @@ blame({callback?}) *gitsigns.blame()*
Run git-blame on the current file and open the results
in a scroll-bound vertical split.

<CR> is mapped to open a menu with the actions:
- [Show commit] in a vertical split.
- [Reblame at commit]
Mappings:
<CR> is mapped to open a menu with the other mappings
Note: <Alt> must be held to activate the mappings whilst the menu is
open.
s [Show commit] in a vertical split.
S [Show commit] in a new tab.
r [Reblame at commit]

Attributes: ~
{async}
Expand Down
10 changes: 7 additions & 3 deletions lua/gitsigns/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1053,9 +1053,13 @@ end
--- Run git-blame on the current file and open the results
--- in a scroll-bound vertical split.
---
--- <CR> is mapped to open a menu with the actions:
--- - [Show commit] in a vertical split.
--- - [Reblame at commit]
--- Mappings:
--- <CR> is mapped to open a menu with the other mappings
--- Note: <Alt> must be held to activate the mappings whilst the menu is
--- open.
--- s [Show commit] in a vertical split.
--- S [Show commit] in a new tab.
--- r [Reblame at commit]
---
--- Attributes: ~
--- {async}
Expand Down
55 changes: 41 additions & 14 deletions lua/gitsigns/blame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,17 @@ local function reblame(blame, win, revision)
)
end

--- @param sha string
--- @param git_obj Gitsigns.GitObj
local show_commit = async.create(2, function(sha, git_obj)
local res = git_obj:command({ 'show', sha })
--- @param win integer
--- @param open 'vsplit'|'tabnew'
--- @param bcache Gitsigns.CacheEntry
local show_commit = async.create(3, function(win, open, bcache)
local cursor = api.nvim_win_get_cursor(win)[1]
local sha = bcache.blame[cursor].commit.sha
local res = bcache.git_obj:command({ 'show', sha })
async.scheduler()
local commit_buf = api.nvim_create_buf(false, true)
api.nvim_buf_set_lines(commit_buf, 0, -1, false, res)
vim.cmd.vsplit({ mods = { keepalt = true } })
vim.cmd[open]({ mods = { keepalt = true } })
api.nvim_win_set_buf(0, commit_buf)
vim.bo[commit_buf].filetype = 'git'
end)
Expand Down Expand Up @@ -224,6 +227,24 @@ local function sync_cursors(augroup, wins)
end
end

--- @param name string
--- @param items [string, string][]
local function menu(name, items)
local max_len = 0
for _, item in ipairs(items) do
max_len = math.max(max_len, #item[1]) --- @type integer
end

for _, item in ipairs(items) do
local item_nm, action = item[1], item[2]
local pad = string.rep(' ', max_len - #item_nm)
local lhs = string.format('%s%s (%s)', item_nm, pad, action):gsub(' ', [[\ ]])
local cmd = string.format('nmenu <silent> ]%s.%s %s', name, lhs, action)

vim.cmd(cmd)
end
end

--- @async
M.blame = function()
local __FUNC__ = 'blame'
Expand Down Expand Up @@ -284,7 +305,7 @@ M.blame = function()
vim.cmd.syncbind()

vim.keymap.set('n', '<CR>', function()
vim.cmd.popup('GitsignsBlame')
vim.cmd.popup(']GitsignsBlame')
end, {
desc = 'Open blame context menu',
buffer = blm_bufnr,
Expand All @@ -298,18 +319,24 @@ M.blame = function()
})

vim.keymap.set('n', 's', function()
local cursor = api.nvim_win_get_cursor(blm_win)[1]
local sha = blame[cursor].commit.sha
show_commit(sha, bcache.git_obj)
show_commit(blm_win, 'vsplit', bcache)
end, {
desc = 'Show commit in a vertical split',
buffer = blm_bufnr,
})

vim.keymap.set('n', 'S', function()
show_commit(blm_win, 'tabnew', bcache)
end, {
desc = 'Show commit',
desc = 'Show commit in a new tab',
buffer = blm_bufnr,
})

vim.cmd([[
:nnoremenu <silent> GitsignsBlame.Reblame\ at\ commit\ \ (r) r
:nnoremenu <silent> GitsignsBlame.Show\ commit\ \ \ \ \ \ \ \ (s) s
]])
menu('GitsignsBlame', {
{ 'Reblame at commit', 'r' },
{ 'Show commit (vsplit)', 's' },
{ ' (tab)', 'S' },
})

local group = api.nvim_create_augroup('GitsignsBlame', {})

Expand Down

0 comments on commit 93c38d9

Please sign in to comment.