@@ -6,6 +6,7 @@ local File = lazy.access("diffview.vcs.file", "File") ---@type vcs.File|LazyModu
6
6
local GitAdapter = lazy .access (" diffview.vcs.adapters.git" , " GitAdapter" ) --- @type GitAdapter | LazyModule
7
7
local RevType = lazy .access (" diffview.vcs.rev" , " RevType" ) --- @type RevType | LazyModule
8
8
local config = lazy .require (" diffview.config" ) --- @module " diffview.config"
9
+ local gs_actions = lazy .require (" gitsigns.actions" ) --- @module " gitsigns.actions"
9
10
local lib = lazy .require (" diffview.lib" ) --- @module " diffview.lib"
10
11
local utils = lazy .require (" diffview.utils" ) --- @module " diffview.utils"
11
12
@@ -64,6 +65,12 @@ function Window:is_focused()
64
65
return self :is_valid () and api .nvim_get_current_win () == self .id
65
66
end
66
67
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
+
67
74
--- @param callback fun ( file : vcs.File )
68
75
function Window :load_file (callback )
69
76
assert (self .file )
@@ -89,17 +96,23 @@ function Window:open_file(callback)
89
96
self :_save_winopts ()
90
97
end
91
98
99
+ local winopt_overrides
92
100
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 ())
93
104
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 )
95
110
self .file :attach_buffer (false , {
96
111
keymaps = config .get_layout_keymaps (self .parent ),
97
112
disable_diagnostics = self .file .kind == " conflicting"
98
113
and config .get_config ().view .merge_tool .disable_diagnostics ,
99
114
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 ,
103
116
base = base_rev and base_rev :object_name (),
104
117
}
105
118
})
@@ -173,10 +186,15 @@ function Window:_restore_winopts()
173
186
end
174
187
end
175
188
176
- function Window :apply_file_winopts ()
189
+ --- @param overrides WindowOptions ?
190
+ function Window :apply_file_winopts (overrides )
177
191
assert (self .file )
178
192
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
180
198
end
181
199
end
182
200
@@ -194,5 +212,47 @@ function Window:set_file(file)
194
212
self .file = file
195
213
end
196
214
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
+
197
257
M .Window = Window
198
258
return M
0 commit comments