@@ -6,6 +6,7 @@ local File = lazy.access("diffview.vcs.file", "File") ---@type vcs.File|LazyModu
66local GitAdapter = lazy .access (" diffview.vcs.adapters.git" , " GitAdapter" ) --- @type GitAdapter | LazyModule
77local RevType = lazy .access (" diffview.vcs.rev" , " RevType" ) --- @type RevType | LazyModule
88local config = lazy .require (" diffview.config" ) --- @module " diffview.config"
9+ local gs_actions = lazy .require (" gitsigns.actions" ) --- @module " gitsigns.actions"
910local lib = lazy .require (" diffview.lib" ) --- @module " diffview.lib"
1011local utils = lazy .require (" diffview.utils" ) --- @module " diffview.utils"
1112
@@ -192,8 +193,8 @@ function Window:apply_file_winopts(overrides)
192193 if overrides then
193194 utils .set_local (self .id , vim .tbl_extend (" force" , self .file .winopts , overrides ))
194195 else
195- utils .set_local (self .id , self .file .winopts )
196- end
196+ utils .set_local (self .id , self .file .winopts )
197+ end
197198 end
198199end
199200
@@ -211,5 +212,47 @@ function Window:set_file(file)
211212 self .file = file
212213end
213214
215+ function Window :gs_update_folds ()
216+ if self :is_file_open () and vim .wo [self .id ].foldenable then
217+ api .nvim_win_call (self .id , function ()
218+ pcall (vim .cmd , " norm! zE" ) -- Delete all folds in window
219+ local hunks = gs_actions .get_hunks (self .file .bufnr ) or {}
220+ local context
221+
222+ for _ , v in ipairs (vim .opt .diffopt :get ()) do
223+ context = tonumber (v :match (" ^context:(%d+)" ))
224+ if context then break end
225+ end
226+
227+ context = math.max (1 , context or 6 )
228+
229+ local prev_last = - context + 1
230+ local lcount = api .nvim_buf_line_count (self .file .bufnr )
231+
232+ for i = 1 , # hunks + 1 do
233+ local hunk = hunks [i ]
234+ local first , last
235+
236+ if hunk then
237+ first = hunk .added .start
238+ last = first + hunk .added .count - 1
239+ else
240+ first = lcount + context
241+ last = first
242+ end
243+
244+ -- print(prev_last, first, last, hunk and "hunk" or "nil")
245+
246+ if first - prev_last > context * 2 + 1 then
247+ -- print("FOLD:", prev_last + context, first - context)
248+ vim .cmd ((" %d,%dfold" ):format (prev_last + context , first - context ))
249+ end
250+
251+ prev_last = last
252+ end
253+ end )
254+ end
255+ end
256+
214257M .Window = Window
215258return M
0 commit comments