feat(fugitive): line position tracking for keymaps
When pressing `du`/`dU` from a hunk line in the fugitive status buffer (after expanding with `=`), the unified diff now opens at the corresponding line instead of line 1. Implementation: - `fugitive.get_hunk_position()` returns @@ header and offset when on a hunk line - `commands.find_hunk_line()` finds matching @@ header in diff buffer - `commands.gdiff_file()` accepts optional `hunk_position` and jumps after opening Also updates @phanen's README credit for the previous two fixes. Closes #65
This commit is contained in:
parent
a6d4dcff1f
commit
9e857d4b29
5 changed files with 247 additions and 8 deletions
|
|
@ -3,6 +3,18 @@ local M = {}
|
|||
local git = require('diffs.git')
|
||||
local dbg = require('diffs.log').dbg
|
||||
|
||||
---@param diff_lines string[]
|
||||
---@param hunk_position { hunk_header: string, offset: integer }
|
||||
---@return integer?
|
||||
function M.find_hunk_line(diff_lines, hunk_position)
|
||||
for i, line in ipairs(diff_lines) do
|
||||
if line == hunk_position.hunk_header then
|
||||
return i + hunk_position.offset
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
---@param old_lines string[]
|
||||
---@param new_lines string[]
|
||||
---@param old_name string
|
||||
|
|
@ -98,6 +110,7 @@ end
|
|||
---@field staged? boolean
|
||||
---@field untracked? boolean
|
||||
---@field old_filepath? string
|
||||
---@field hunk_position? { hunk_header: string, offset: integer }
|
||||
|
||||
---@param filepath string
|
||||
---@param opts? diffs.GdiffFileOpts
|
||||
|
|
@ -175,6 +188,14 @@ function M.gdiff_file(filepath, opts)
|
|||
vim.cmd(opts.vertical and 'vsplit' or 'split')
|
||||
vim.api.nvim_win_set_buf(0, diff_buf)
|
||||
|
||||
if opts.hunk_position then
|
||||
local target_line = M.find_hunk_line(diff_lines, opts.hunk_position)
|
||||
if target_line then
|
||||
vim.api.nvim_win_set_cursor(0, { target_line, 0 })
|
||||
dbg('jumped to line %d for hunk', target_line)
|
||||
end
|
||||
end
|
||||
|
||||
dbg('opened diff buffer %d for %s (%s)', diff_buf, rel_path, diff_label)
|
||||
|
||||
vim.schedule(function()
|
||||
|
|
|
|||
|
|
@ -95,6 +95,42 @@ function M.get_file_at_line(bufnr, lnum)
|
|||
return nil, nil, false, nil
|
||||
end
|
||||
|
||||
---@class diffs.HunkPosition
|
||||
---@field hunk_header string
|
||||
---@field offset integer
|
||||
|
||||
---@param bufnr integer
|
||||
---@param lnum integer
|
||||
---@return diffs.HunkPosition?
|
||||
function M.get_hunk_position(bufnr, lnum)
|
||||
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, lnum, false)
|
||||
local current = lines[lnum]
|
||||
|
||||
if not current then
|
||||
return nil
|
||||
end
|
||||
|
||||
local prefix = current:sub(1, 1)
|
||||
if prefix ~= '+' and prefix ~= '-' and prefix ~= ' ' then
|
||||
return nil
|
||||
end
|
||||
|
||||
for i = lnum - 1, 1, -1 do
|
||||
local line = lines[i]
|
||||
if line:match('^@@.-@@') then
|
||||
return {
|
||||
hunk_header = line,
|
||||
offset = lnum - i,
|
||||
}
|
||||
end
|
||||
if line:match('^[MADRCU?!]%s') or line:match('^%w+ %(') then
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@return string?
|
||||
local function get_repo_root_from_fugitive(bufnr)
|
||||
|
|
@ -142,12 +178,14 @@ function M.diff_file_under_cursor(vertical)
|
|||
|
||||
local filepath = repo_root .. '/' .. filename
|
||||
local old_filepath = old_filename and (repo_root .. '/' .. old_filename) or nil
|
||||
local hunk_position = M.get_hunk_position(bufnr, lnum)
|
||||
|
||||
dbg(
|
||||
'diff_file_under_cursor: %s (section: %s, old: %s)',
|
||||
'diff_file_under_cursor: %s (section: %s, old: %s, hunk_offset: %s)',
|
||||
filename,
|
||||
section or 'unknown',
|
||||
old_filename or 'none'
|
||||
old_filename or 'none',
|
||||
hunk_position and tostring(hunk_position.offset) or 'none'
|
||||
)
|
||||
|
||||
commands.gdiff_file(filepath, {
|
||||
|
|
@ -155,6 +193,7 @@ function M.diff_file_under_cursor(vertical)
|
|||
staged = section == 'staged',
|
||||
untracked = section == 'untracked',
|
||||
old_filepath = old_filepath,
|
||||
hunk_position = hunk_position,
|
||||
})
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue