@@ -7,6 +7,7 @@ local FileHistoryView = lazy.access("diffview.scene.views.file_history.file_hist
77local GitAdapter = lazy .access (" diffview.vcs.adapters.git" , " GitAdapter" ) --- @type GitAdapter | LazyModule
88local RevType = lazy .access (" diffview.vcs.rev" , " RevType" ) --- @type RevType | LazyModule
99local config = lazy .require (" diffview.config" ) --- @module " diffview.config"
10+ local gs_actions = lazy .require (" gitsigns.actions" ) --- @module " gitsigns.actions"
1011local lib = lazy .require (" diffview.lib" ) --- @module " diffview.lib"
1112local utils = lazy .require (" diffview.utils" ) --- @module " diffview.utils"
1213
@@ -227,8 +228,8 @@ function Window:apply_file_winopts(overrides)
227228 if overrides then
228229 utils .set_local (self .id , vim .tbl_extend (" force" , self .file .winopts , overrides ))
229230 else
230- utils .set_local (self .id , self .file .winopts )
231- end
231+ utils .set_local (self .id , self .file .winopts )
232+ end
232233 end
233234end
234235
@@ -246,5 +247,47 @@ function Window:set_file(file)
246247 self .file = file
247248end
248249
250+ function Window :gs_update_folds ()
251+ if self :is_file_open () and vim .wo [self .id ].foldenable then
252+ api .nvim_win_call (self .id , function ()
253+ pcall (vim .cmd , " norm! zE" ) -- Delete all folds in window
254+ local hunks = gs_actions .get_hunks (self .file .bufnr ) or {}
255+ local context
256+
257+ for _ , v in ipairs (vim .opt .diffopt :get ()) do
258+ context = tonumber (v :match (" ^context:(%d+)" ))
259+ if context then break end
260+ end
261+
262+ context = math.max (1 , context or 6 )
263+
264+ local prev_last = - context + 1
265+ local lcount = api .nvim_buf_line_count (self .file .bufnr )
266+
267+ for i = 1 , # hunks + 1 do
268+ local hunk = hunks [i ]
269+ local first , last
270+
271+ if hunk then
272+ first = hunk .added .start
273+ last = first + hunk .added .count - 1
274+ else
275+ first = lcount + context
276+ last = first
277+ end
278+
279+ -- print(prev_last, first, last, hunk and "hunk" or "nil")
280+
281+ if first - prev_last > context * 2 + 1 then
282+ -- print("FOLD:", prev_last + context, first - context)
283+ vim .cmd ((" %d,%dfold" ):format (prev_last + context , first - context ))
284+ end
285+
286+ prev_last = last
287+ end
288+ end )
289+ end
290+ end
291+
249292M .Window = Window
250293return M
0 commit comments