Skip to content

Commit 092d2dc

Browse files
committed
refactor: run the pager in neogit.lib.git.diff build_pager_contents()
1 parent cad7467 commit 092d2dc

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
@@ -216,13 +217,49 @@ local function build_hunks(lines)
216217
return hunks
217218
end
218219

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

235272
return { ---@type Diff
236-
header = header,
237273
kind = kind,
238274
lines = lines,
239275
file = file,
240276
info = info,
241277
stats = stats,
242278
hunks = hunks,
279+
pager_contents = pager_contents,
243280
}
244281
end
245282

0 commit comments

Comments
 (0)