Skip to content

Commit

Permalink
refactor: run the pager in neogit.lib.git.diff `build_pager_content…
Browse files Browse the repository at this point in the history
…s()`
  • Loading branch information
stevenxxiu committed Dec 9, 2024
1 parent 82be6e3 commit 6036128
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 33 deletions.
39 changes: 8 additions & 31 deletions lua/neogit/buffers/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,14 @@ M.Diff = Component.new(function(diff)
end)

M.DiffHunks = Component.new(function(diff)
local hunk_props = vim
.iter(diff.hunks)
:map(function(hunk)
local header = diff.lines[hunk.diff_from]
local content = vim.list_slice(diff.lines, hunk.diff_from + 1, hunk.diff_to)
local job = nil
if config.values.log_pager ~= nil then
job = vim.system(config.values.log_pager, { stdin = true })
for _, part in ipairs { diff.header, { header }, content } do
for _, line in ipairs(part) do
job:write(line .. "\n")
end
end
job:write()
end

return {
header = header,
content = content,
job = job,
hunk = hunk,
folded = hunk._folded,
}
end)
:totable()

if config.values.log_pager ~= nil then
vim.iter(hunk_props):each(function(hunk)
hunk.content = vim.split(hunk.job:wait().stdout, "\n")
hunk.job = nil
end)
local hunk_props = {}
for i, hunk in ipairs(diff.hunks) do
table.insert(hunk_props, {
header = diff.lines[hunk.diff_from],
content = diff.pager_contents[i],
hunk = hunk,
folded = hunk._folded,
})
end

return col.tag("DiffContent") {
Expand Down
41 changes: 39 additions & 2 deletions lua/neogit/lib/git/diff.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local a = require("plenary.async")
local git = require("neogit.lib.git")
local util = require("neogit.lib.util")
local config = require("neogit.config")
local logger = require("neogit.logger")

local insert = table.insert
Expand All @@ -12,13 +13,13 @@ local sha256 = vim.fn.sha256
---@field staged_stats fun(): DiffStagedStats
---
---@class Diff
---@field header string[]
---@field kind string
---@field lines string[]
---@field file string
---@field info table
---@field stats table
---@field hunks Hunk
---@field pager_contents string[]
---
---@class DiffStats
---@field additions number
Expand Down Expand Up @@ -214,25 +215,61 @@ local function build_hunks(lines)
return hunks
end

---@param diff_header string[]
---@param lines string[]
---@param hunks Hunk[]
---@return string[][]
local function build_pager_contents(diff_header, lines, hunks)
local res = {}
local jobs = {}
vim.iter(hunks):each(function(hunk)
local header = lines[hunk.diff_from]
local content = vim.list_slice(lines, hunk.diff_from + 1, hunk.diff_to)
if config.values.log_pager == nil then
insert(res, content)
return
end

local job = vim.system(config.values.log_pager, { stdin = true })
for _, part in ipairs { diff_header, { header }, content } do
for _, line in ipairs(part) do
job:write(line .. "\n")
end
end
job:write()
insert(jobs, job)
end)

if config.values.log_pager ~= nil then
vim.iter(jobs):each(function(job)
local content = vim.split(job:wait().stdout, "\n")
insert(res, content)
end)
end

return res
end

---@param raw_diff string[]
---@param raw_stats string[]
---@return Diff
local function parse_diff(raw_diff, raw_stats)
local header, start_idx = build_diff_header(raw_diff)
local lines = build_lines(raw_diff, start_idx)
local hunks = build_hunks(lines)
local pager_contents = build_pager_contents(header, lines, hunks)
local kind, info = build_kind(header)
local file = build_file(header, kind)
local stats = parse_diff_stats(raw_stats or {})

return { ---@type Diff
header = header,
kind = kind,
lines = lines,
file = file,
info = info,
stats = stats,
hunks = hunks,
pager_contents = pager_contents,
}
end

Expand Down

0 comments on commit 6036128

Please sign in to comment.