Skip to content

Commit 6036128

Browse files
committed
refactor: run the pager in neogit.lib.git.diff build_pager_contents()
1 parent 82be6e3 commit 6036128

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
lines changed

lua/neogit/buffers/common.lua

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,14 @@ M.Diff = Component.new(function(diff)
2626
end)
2727

2828
M.DiffHunks = Component.new(function(diff)
29-
local hunk_props = vim
30-
.iter(diff.hunks)
31-
:map(function(hunk)
32-
local header = diff.lines[hunk.diff_from]
33-
local content = vim.list_slice(diff.lines, hunk.diff_from + 1, hunk.diff_to)
34-
local job = nil
35-
if config.values.log_pager ~= nil then
36-
job = vim.system(config.values.log_pager, { stdin = true })
37-
for _, part in ipairs { diff.header, { header }, content } do
38-
for _, line in ipairs(part) do
39-
job:write(line .. "\n")
40-
end
41-
end
42-
job:write()
43-
end
44-
45-
return {
46-
header = header,
47-
content = content,
48-
job = job,
49-
hunk = hunk,
50-
folded = hunk._folded,
51-
}
52-
end)
53-
:totable()
54-
55-
if config.values.log_pager ~= nil then
56-
vim.iter(hunk_props):each(function(hunk)
57-
hunk.content = vim.split(hunk.job:wait().stdout, "\n")
58-
hunk.job = nil
59-
end)
29+
local hunk_props = {}
30+
for i, hunk in ipairs(diff.hunks) do
31+
table.insert(hunk_props, {
32+
header = diff.lines[hunk.diff_from],
33+
content = diff.pager_contents[i],
34+
hunk = hunk,
35+
folded = hunk._folded,
36+
})
6037
end
6138

6239
return col.tag("DiffContent") {

lua/neogit/lib/git/diff.lua

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local a = require("plenary.async")
22
local git = require("neogit.lib.git")
33
local util = require("neogit.lib.util")
4+
local config = require("neogit.config")
45
local logger = require("neogit.logger")
56

67
local insert = table.insert
@@ -12,13 +13,13 @@ local sha256 = vim.fn.sha256
1213
---@field staged_stats fun(): DiffStagedStats
1314
---
1415
---@class Diff
15-
---@field header string[]
1616
---@field kind string
1717
---@field lines string[]
1818
---@field file string
1919
---@field info table
2020
---@field stats table
2121
---@field hunks Hunk
22+
---@field pager_contents string[]
2223
---
2324
---@class DiffStats
2425
---@field additions number
@@ -214,25 +215,61 @@ local function build_hunks(lines)
214215
return hunks
215216
end
216217

218+
---@param diff_header string[]
219+
---@param lines string[]
220+
---@param hunks Hunk[]
221+
---@return string[][]
222+
local function build_pager_contents(diff_header, lines, hunks)
223+
local res = {}
224+
local jobs = {}
225+
vim.iter(hunks):each(function(hunk)
226+
local header = lines[hunk.diff_from]
227+
local content = vim.list_slice(lines, hunk.diff_from + 1, hunk.diff_to)
228+
if config.values.log_pager == nil then
229+
insert(res, content)
230+
return
231+
end
232+
233+
local job = vim.system(config.values.log_pager, { stdin = true })
234+
for _, part in ipairs { diff_header, { header }, content } do
235+
for _, line in ipairs(part) do
236+
job:write(line .. "\n")
237+
end
238+
end
239+
job:write()
240+
insert(jobs, job)
241+
end)
242+
243+
if config.values.log_pager ~= nil then
244+
vim.iter(jobs):each(function(job)
245+
local content = vim.split(job:wait().stdout, "\n")
246+
insert(res, content)
247+
end)
248+
end
249+
250+
return res
251+
end
252+
217253
---@param raw_diff string[]
218254
---@param raw_stats string[]
219255
---@return Diff
220256
local function parse_diff(raw_diff, raw_stats)
221257
local header, start_idx = build_diff_header(raw_diff)
222258
local lines = build_lines(raw_diff, start_idx)
223259
local hunks = build_hunks(lines)
260+
local pager_contents = build_pager_contents(header, lines, hunks)
224261
local kind, info = build_kind(header)
225262
local file = build_file(header, kind)
226263
local stats = parse_diff_stats(raw_stats or {})
227264

228265
return { ---@type Diff
229-
header = header,
230266
kind = kind,
231267
lines = lines,
232268
file = file,
233269
info = info,
234270
stats = stats,
235271
hunks = hunks,
272+
pager_contents = pager_contents,
236273
}
237274
end
238275

0 commit comments

Comments
 (0)