From 4c8b3f0d3e717a398f282ff07796154db9e25fc7 Mon Sep 17 00:00:00 2001 From: Oscar Wallberg Date: Wed, 20 May 2026 07:55:54 +0200 Subject: [PATCH] fix(git): align hunk signs with :diffsplit --- lua/git/hunks.lua | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/lua/git/hunks.lua b/lua/git/hunks.lua index 31ae6f8..d52f5e2 100644 --- a/lua/git/hunks.lua +++ b/lua/git/hunks.lua @@ -42,15 +42,34 @@ local function resolve_buf(buf) return buf and buf ~= 0 and buf or vim.api.nvim_get_current_buf() end +---Mirror the hunk-affecting parts of the user's 'diffopt' so the gutter +---lines up with what `:diffsplit` shows. +---@return table +local function diff_opts() + local opts = { result_type = "indices", algorithm = "myers" } + for _, item in ipairs(vim.split(vim.o.diffopt, ",", { plain = true })) do + if item == "indent-heuristic" then + opts.indent_heuristic = true + else + local algorithm = item:match("^algorithm:(.+)$") + if algorithm then + opts.algorithm = algorithm + end + local linematch = item:match("^linematch:(%d+)$") + if linematch then + opts.linematch = tonumber(linematch) + end + end + end + return opts +end + ---@param state ow.Git.Hunks.BufState ---@param new_lines string[] local function compute_hunks(state, new_lines) local old = table.concat(state.index or {}, "\n") local new = table.concat(new_lines, "\n") - local raw = vim.text.diff(old, new, { - result_type = "indices", - algorithm = "histogram", - }) + local raw = vim.text.diff(old, new, diff_opts()) ---@type ow.Git.Hunks.Hunk[] local hunks = {} if type(raw) ~= "table" then