feat(git): bind gd to open-under-cursor in log and object views
This commit is contained in:
+20
-11
@@ -7,23 +7,32 @@ local LOG_FORMAT = "%h %ad {%an}%d %s"
|
||||
|
||||
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
|
||||
local function attach_dispatch(buf)
|
||||
vim.keymap.set("n", "<CR>", function()
|
||||
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 sha then
|
||||
---@cast r -nil
|
||||
require("git.object").open(r, sha, { split = false })
|
||||
else
|
||||
if not open_under_cursor(buf) then
|
||||
vim.api.nvim_feedkeys(cr, "n", false)
|
||||
end
|
||||
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
|
||||
|
||||
---@param worktree string
|
||||
|
||||
@@ -150,6 +150,9 @@ function M.attach_dispatch(buf)
|
||||
vim.api.nvim_feedkeys(cr, "n", false)
|
||||
end
|
||||
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
|
||||
|
||||
---@param r ow.Git.Repo
|
||||
|
||||
Reference in New Issue
Block a user