feat(git): bind gd to open-under-cursor in log and object views

This commit is contained in:
2026-05-19 20:30:22 +02:00
parent 73fa92afc8
commit d132c00032
2 changed files with 23 additions and 11 deletions
+20 -11
View File
@@ -7,23 +7,32 @@ local LOG_FORMAT = "%h %ad {%an}%d %s"
local cr = vim.api.nvim_replace_termcodes("<CR>", true, false, true) local cr = vim.api.nvim_replace_termcodes("<CR>", true, false, true)
---@param buf integer
---@return boolean opened
local function open_under_cursor(buf)
local r = repo.resolve(buf)
-- Anchor past the leading graph chars (matches the leading sha column,
-- not any hex word that happens to appear later in the subject).
local sha = r
and vim.api.nvim_get_current_line():match("^[*|/\\_ ]*(%x%x%x%x%x%x%x+)")
if not sha then
return false
end
---@cast r -nil
require("git.object").open(r, sha, { split = false })
return true
end
---@param buf integer ---@param buf integer
local function attach_dispatch(buf) local function attach_dispatch(buf)
vim.keymap.set("n", "<CR>", function() vim.keymap.set("n", "<CR>", function()
local r = repo.resolve(buf) if not open_under_cursor(buf) then
-- Anchor past the leading graph chars (matches the leading sha column,
-- not any hex word that happens to appear later in the subject).
local sha = r
and vim.api
.nvim_get_current_line()
:match("^[*|/\\_ ]*(%x%x%x%x%x%x%x+)")
if sha then
---@cast r -nil
require("git.object").open(r, sha, { split = false })
else
vim.api.nvim_feedkeys(cr, "n", false) vim.api.nvim_feedkeys(cr, "n", false)
end end
end, { buffer = buf, silent = true, desc = "Open commit" }) end, { buffer = buf, silent = true, desc = "Open commit" })
vim.keymap.set("n", "gd", function()
open_under_cursor(buf)
end, { buffer = buf, silent = true, desc = "Open commit" })
end end
---@param worktree string ---@param worktree string
+3
View File
@@ -150,6 +150,9 @@ function M.attach_dispatch(buf)
vim.api.nvim_feedkeys(cr, "n", false) vim.api.nvim_feedkeys(cr, "n", false)
end end
end, { buffer = buf, silent = true, desc = "Open file at commit" }) end, { buffer = buf, silent = true, desc = "Open file at commit" })
vim.keymap.set("n", "gd", function()
M.open_under_cursor()
end, { buffer = buf, silent = true, desc = "Open file at commit" })
end end
---@param r ow.Git.Repo ---@param r ow.Git.Repo