11local a = require (" plenary.async" )
22local git = require (" neogit.lib.git" )
33local util = require (" neogit.lib.util" )
4+ local config = require (" neogit.config" )
45local logger = require (" neogit.logger" )
56
67local 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
215216end
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
220256local 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 }
237274end
238275
0 commit comments