@@ -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
@@ -64,6 +65,12 @@ function Window:is_focused()
6465 return self :is_valid () and api .nvim_get_current_win () == self .id
6566end
6667
68+ function Window :is_file_open ()
69+ return self :is_valid ()
70+ and self .file :is_valid ()
71+ and api .nvim_win_get_buf (self .id ) == self .file .bufnr
72+ end
73+
6774--- @param callback fun ( file : vcs.File )
6875function Window :load_file (callback )
6976 assert (self .file )
@@ -89,17 +96,23 @@ function Window:open_file(callback)
8996 self :_save_winopts ()
9097 end
9198
99+ local winopt_overrides
92100 local base_rev = utils .tbl_access (self , " parent.parent.revs.a" ) --[[ @as Rev? ]]
101+ local use_inline_diff = self .file .kind ~= " conflicting"
102+ and self .parent :instanceof (Diff1 .__get ())
103+ and self .file .adapter :instanceof (GitAdapter .__get ())
93104
94- self :apply_file_winopts ()
105+ if use_inline_diff then
106+ winopt_overrides = { foldmethod = " manual" , diff = false }
107+ end
108+
109+ self :apply_file_winopts (winopt_overrides )
95110 self .file :attach_buffer (false , {
96111 keymaps = config .get_layout_keymaps (self .parent ),
97112 disable_diagnostics = self .file .kind == " conflicting"
98113 and config .get_config ().view .merge_tool .disable_diagnostics ,
99114 inline_diff = {
100- enable = self .file .kind ~= " conflicting"
101- and self .parent :instanceof (Diff1 .__get ())
102- and self .file .adapter :instanceof (GitAdapter .__get ()),
115+ enabled = use_inline_diff ,
103116 base = base_rev and base_rev :object_name (),
104117 }
105118 })
@@ -173,10 +186,15 @@ function Window:_restore_winopts()
173186 end
174187end
175188
176- function Window :apply_file_winopts ()
189+ --- @param overrides WindowOptions ?
190+ function Window :apply_file_winopts (overrides )
177191 assert (self .file )
178192 if self .file .winopts then
179- utils .set_local (self .id , self .file .winopts )
193+ if overrides then
194+ utils .set_local (self .id , vim .tbl_extend (" force" , self .file .winopts , overrides ))
195+ else
196+ utils .set_local (self .id , self .file .winopts )
197+ end
180198 end
181199end
182200
@@ -194,5 +212,47 @@ function Window:set_file(file)
194212 self .file = file
195213end
196214
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+
197257M .Window = Window
198258return M
0 commit comments