-
-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathfile_panel.lua
More file actions
436 lines (375 loc) · 10.8 KB
/
file_panel.lua
File metadata and controls
436 lines (375 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
local config = require("diffview.config")
local oop = require("diffview.oop")
local renderer = require("diffview.renderer")
local utils = require("diffview.utils")
local Panel = require("diffview.ui.panel").Panel
local api = vim.api
local M = {}
---@class TreeOptions
---@field flatten_dirs boolean
---@field folder_statuses "never"|"only_folded"|"always"
---@class FilePanel : Panel
---@field adapter VCSAdapter
---@field files FileDict
---@field path_args string[]
---@field rev_pretty_name string|nil
---@field cur_file FileEntry
---@field listing_style "list"|"tree"
---@field tree_options TreeOptions
---@field render_data RenderData
---@field components CompStruct
---@field constrain_cursor function
---@field help_mapping string
---@field selected_files table<FileEntry, boolean>
local FilePanel = oop.create_class("FilePanel", Panel)
FilePanel.winopts = vim.tbl_extend("force", Panel.winopts, {
cursorline = true,
winhl = {
"EndOfBuffer:DiffviewEndOfBuffer",
"Normal:DiffviewNormal",
"CursorLine:DiffviewCursorLine",
"WinSeparator:DiffviewWinSeparator",
"SignColumn:DiffviewNormal",
"StatusLine:DiffviewStatusLine",
"StatusLineNC:DiffviewStatuslineNC",
opt = { method = "prepend" },
},
})
FilePanel.bufopts = vim.tbl_extend("force", Panel.bufopts, {
filetype = "DiffviewFiles",
})
---FilePanel constructor.
---@param adapter VCSAdapter
---@param files FileEntry[]
---@param path_args string[]
function FilePanel:init(adapter, files, path_args, rev_pretty_name)
local conf = config.get_config()
self:super({
config = conf.file_panel.win_config,
bufname = "DiffviewFilePanel",
})
self.adapter = adapter
self.files = files
self.path_args = path_args
self.rev_pretty_name = rev_pretty_name
self.listing_style = conf.file_panel.listing_style
self.tree_options = conf.file_panel.tree_options
self.selected_files = {}
self:on_autocmd("BufNew", {
callback = function()
self:setup_buffer()
end,
})
end
---@override
function FilePanel:open()
FilePanel.super_class.open(self)
vim.cmd("wincmd =")
end
function FilePanel:setup_buffer()
local conf = config.get_config()
local default_opt = { silent = true, nowait = true, buffer = self.bufid }
for _, mapping in ipairs(conf.keymaps.file_panel) do
local opt = vim.tbl_extend("force", default_opt, mapping[4] or {}, { buffer = self.bufid })
vim.keymap.set(mapping[1], mapping[2], mapping[3], opt)
end
local help_keymap = config.find_help_keymap(conf.keymaps.file_panel)
if help_keymap then self.help_mapping = help_keymap[2] end
end
function FilePanel:update_components()
local conflicting_files
local working_files
local staged_files
if self.listing_style == "list" then
conflicting_files = { name = "files" }
working_files = { name = "files" }
staged_files = { name = "files" }
for _, file in ipairs(self.files.conflicting) do
table.insert(conflicting_files, {
name = "file",
context = file,
})
end
for _, file in ipairs(self.files.working) do
table.insert(working_files, {
name = "file",
context = file,
})
end
for _, file in ipairs(self.files.staged) do
table.insert(staged_files, {
name = "file",
context = file,
})
end
elseif self.listing_style == "tree" then
self.files.conflicting_tree:update_statuses()
self.files.working_tree:update_statuses()
self.files.staged_tree:update_statuses()
conflicting_files = utils.tbl_merge(
{ name = "files" },
self.files.conflicting_tree:create_comp_schema({
flatten_dirs = self.tree_options.flatten_dirs,
})
)
working_files = utils.tbl_merge(
{ name = "files" },
self.files.working_tree:create_comp_schema({
flatten_dirs = self.tree_options.flatten_dirs,
})
)
staged_files = utils.tbl_merge(
{ name = "files" },
self.files.staged_tree:create_comp_schema({
flatten_dirs = self.tree_options.flatten_dirs,
})
)
end
---@type CompStruct
self.components = self.render_data:create_component({
{ name = "path" },
{
name = "conflicting",
{ name = "title" },
conflicting_files,
{ name = "margin" },
},
{
name = "working",
{ name = "title" },
working_files,
{ name = "margin" },
},
{
name = "staged",
{ name = "title" },
staged_files,
{ name = "margin" },
},
{
name = "info",
{ name = "title" },
{ name = "entries" },
},
})
self.constrain_cursor = renderer.create_cursor_constraint({
self.components.conflicting.files.comp,
self.components.working.files.comp,
self.components.staged.files.comp,
})
end
---@return FileEntry[]
function FilePanel:ordered_file_list()
if self.listing_style == "list" then
local list = {}
for _, file in self.files:iter() do
list[#list + 1] = file
end
return list
else
local nodes = utils.vec_join(
self.files.conflicting_tree.root:leaves(),
self.files.working_tree.root:leaves(),
self.files.staged_tree.root:leaves()
)
return vim.tbl_map(function(node)
return node.data
end, nodes) --[[@as vector ]]
end
end
function FilePanel:set_cur_file(file)
if self.cur_file then
self.cur_file:set_active(false)
end
self.cur_file = file
if self.cur_file then
self.cur_file:set_active(true)
end
end
function FilePanel:prev_file()
local files = self:ordered_file_list()
if not self.cur_file and self.files:len() > 0 then
self:set_cur_file(files[1])
return self.cur_file
end
local i = utils.vec_indexof(files, self.cur_file)
if i ~= -1 then
self:set_cur_file(files[(i - vim.v.count1 - 1) % #files + 1])
return self.cur_file
end
end
function FilePanel:next_file()
local files = self:ordered_file_list()
if not self.cur_file and self.files:len() > 0 then
self:set_cur_file(files[1])
return self.cur_file
end
local i = utils.vec_indexof(files, self.cur_file)
if i ~= -1 then
self:set_cur_file(files[(i + vim.v.count1 - 1) % #files + 1])
return self.cur_file
end
end
---Get the file entry under the cursor.
---@return (FileEntry|DirData)?
function FilePanel:get_item_at_cursor()
if not self:is_open() and self:buf_loaded() then return end
local line = api.nvim_win_get_cursor(self.winid)[1]
local comp = self.components.comp:get_comp_on_line(line)
if comp and comp.name == "file" then
return comp.context
elseif comp and comp.name == "dir_name" then
return comp.parent.context
end
end
---Get the parent directory data of the item under the cursor.
---@return DirData?
---@return RenderComponent?
function FilePanel:get_dir_at_cursor()
if self.listing_style ~= "tree" then return end
if not self:is_open() and self:buf_loaded() then return end
local line = api.nvim_win_get_cursor(self.winid)[1]
local comp = self.components.comp:get_comp_on_line(line)
if not comp then return end
if comp.name == "dir_name" then
local dir_comp = comp.parent
return dir_comp.context, dir_comp
elseif comp.name == "file" then
local dir_comp = comp.parent.parent
return dir_comp.context, dir_comp
end
end
function FilePanel:highlight_file(file)
if not (self:is_open() and self:buf_loaded()) then
return
end
if self.listing_style == "list" then
for _, file_list in ipairs({
self.components.conflicting.files,
self.components.working.files,
self.components.staged.files,
}) do
for _, comp_struct in ipairs(file_list) do
if file == comp_struct.comp.context then
utils.set_cursor(self.winid, comp_struct.comp.lstart + 1, 0)
end
end
end
else -- tree
for _, comp_struct in ipairs({
self.components.conflicting.files,
self.components.working.files,
self.components.staged.files,
}) do
comp_struct.comp:deep_some(function(cur)
if file == cur.context then
local was_concealed = false
local dir = cur.parent.parent
while dir and dir.name == "directory" do
if dir.context and dir.context.collapsed then
was_concealed = true
dir.context.collapsed = false
end
dir = utils.tbl_access(dir, { "parent", "parent" })
end
if was_concealed then
self:render()
self:redraw()
end
utils.set_cursor(self.winid, cur.lstart + 1, 0)
return true
end
return false
end)
end
end
-- Needed to update the cursorline highlight when the panel is not focused.
utils.update_win(self.winid)
end
function FilePanel:highlight_cur_file()
if self.cur_file then
self:highlight_file(self.cur_file)
end
end
function FilePanel:highlight_prev_file()
if not (self:is_open() and self:buf_loaded()) or self.files:len() == 0 then
return
end
pcall(
api.nvim_win_set_cursor,
self.winid,
{ self.constrain_cursor(self.winid, -vim.v.count1), 0 }
)
utils.update_win(self.winid)
end
function FilePanel:highlight_next_file()
if not (self:is_open() and self:buf_loaded()) or self.files:len() == 0 then
return
end
pcall(api.nvim_win_set_cursor, self.winid, {
self.constrain_cursor(self.winid, vim.v.count1),
0,
})
utils.update_win(self.winid)
end
function FilePanel:reconstrain_cursor()
if not (self:is_open() and self:buf_loaded()) or self.files:len() == 0 then
return
end
pcall(api.nvim_win_set_cursor, self.winid, {
self.constrain_cursor(self.winid, 0),
0,
})
end
---@param item DirData|any
---@param open boolean
function FilePanel:set_item_fold(item, open)
if type(item.collapsed) == "boolean" and open == item.collapsed then
item.collapsed = not open
self:render()
self:redraw()
if item.collapsed then
self.components.comp:deep_some(function(comp, _, _)
if comp.context == item then
utils.set_cursor(self.winid, comp.lstart + 1)
return true
end
end)
end
end
end
function FilePanel:toggle_item_fold(item)
self:set_item_fold(item, item.collapsed)
end
function FilePanel:render()
require("diffview.scene.views.diff.render")(self)
end
---Toggle selection for a file entry
---@param file FileEntry
function FilePanel:toggle_file_selection(file)
if self.selected_files[file] then
self.selected_files[file] = nil
file.selected = false
else
self.selected_files[file] = true
file.selected = true
end
end
---Get all currently selected files as a list
---@return FileEntry[]
function FilePanel:get_selected_files()
local selected = {}
for file, _ in pairs(self.selected_files) do
table.insert(selected, file)
end
return selected
end
---Clear all file selections
function FilePanel:clear_selections()
for file, _ in pairs(self.selected_files) do
file.selected = false
end
self.selected_files = {}
end
M.FilePanel = FilePanel
return M