forked from simonmclean/triptych.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.lua
470 lines (420 loc) · 14.9 KB
/
view.lua
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
local icons = require 'triptych.icons'
local u = require 'triptych.utils'
local float = require 'triptych.float'
local fs = require 'triptych.fs'
local git = require 'triptych.git'
local diagnostics = require 'triptych.diagnostics'
local autocmds = require 'triptych.autocmds'
local plenary_async = require 'plenary.async'
local syntax_highlighting = require 'triptych.syntax_highlighting'
local M = {}
--- Conditionally filter lines and apply git and diagnostic statuses
---@param path_details PathDetails
---@param show_hidden boolean
---@param Diagnostics? Diagnostics
---@param Git? Git
---@return PathDetails
local function filter_and_encrich_dir_contents(path_details, show_hidden, Diagnostics, Git)
local filtered_children = u.cond(show_hidden, {
when_true = path_details.children,
when_false = function()
return u.filter(path_details.children, function(child)
local is_git_ignored = Git and Git:should_ignore(child.display_name, child.is_dir)
local is_dot_file = string.sub(child.display_name, 1, 1) == '.'
return not is_git_ignored and not is_dot_file
end)
end,
})
path_details.children = filtered_children
path_details.git_status = Git and Git:status_of(path_details.path) or nil
path_details.diagnostic_status = Diagnostics and Diagnostics:get(path_details.path) or nil
for index, child in ipairs(path_details.children) do
path_details.children[index].git_status = Git and Git:status_of(child.path) or nil
path_details.children[index].diagnostic_status = Diagnostics and Diagnostics:get(child.path) or nil
end
return path_details
end
--- Asyncronously read a directory, then publish the results to a User event
---@param path string
---@param show_hidden boolean
---@param win_type WinType
local function read_path_async(path, show_hidden, win_type)
fs.get_path_details(path, show_hidden, function(path_details)
autocmds.send_path_read(path_details, win_type)
end)
end
---Asyncronously read a file, then publish the results to a User event
---@param child_win_buf number
---@param path string
local function read_file_async(child_win_buf, path)
plenary_async.run(function()
fs.read_file_async(path, function(err, lines)
if err then
vim.notify(err, vim.log.levels.ERROR)
else
autocmds.send_file_read(child_win_buf, path, lines)
end
end)
end)
end
---Take a PathDetails and return lines and highlights for an nvim buffer
---@param State TriptychState
---@param path_details PathDetails
---@return string[] # Lines including icons
---@return HighlightDetails[]
local function path_details_to_lines(State, path_details)
local vim = _G.triptych_mock_vim or vim
local config_options = vim.g.triptych_config.options
local icons_enabled = config_options.file_icons.enabled
local lines = {}
local highlights = {}
for _, child in ipairs(path_details.children) do
local line, highlight_name = u.cond(child.is_dir, {
when_true = function()
local icon_length = string.len(config_options.file_icons.directory_icon)
local highlight = {
icon = {
highlight_name = 'Directory',
length = u.cond(icons_enabled, {
when_true = icon_length,
when_false = 0,
}),
},
text = {
highlight_name = config_options.highlights.directory_names,
starts = u.cond(icons_enabled, {
when_true = icon_length,
when_false = 0,
}),
},
}
local line = u.cond(icons_enabled, {
when_true = config_options.file_icons.directory_icon .. ' ' .. child.display_name,
when_false = child.display_name,
})
return line, highlight
end,
when_false = function()
local icon, icon_highlight = u.eval(function()
local maybe_icon, maybe_highlight = icons.get_icon_by_filetype(child.filetype)
local highlight = maybe_highlight or 'Comment'
local fallback_icon = config_options.file_icons.fallback_file_icon
local icon = maybe_icon or fallback_icon
return icon, highlight
end)
local icon_length = string.len(config_options.file_icons.fallback_file_icon)
local highlight = {
icon = {
highlight_name = icon_highlight,
length = u.cond(icons_enabled, {
when_true = icon_length,
when_false = 0,
}),
},
text = {
highlight_name = config_options.highlights.file_names,
starts = u.cond(icons_enabled, {
when_true = icon_length,
when_false = 0,
}),
},
}
local line = u.cond(icons_enabled, {
when_true = icon .. ' ' .. child.display_name,
when_false = child.display_name,
})
return line, highlight
end,
})
local cut_paths = u.map(State.cut_list, function(value)
return value.path
end)
local copy_paths = u.map(State.copy_list, function(value)
return value.path
end)
-- TODO: Replace these with the state methods
if u.list_includes(cut_paths, child.path) then
line = line .. ' (cut)'
end
if u.list_includes(copy_paths, child.path) then
line = line .. ' (copy)'
end
table.insert(lines, line)
table.insert(highlights, highlight_name)
end
return lines, highlights
end
---Get the PathDetails that correspond to the path under the cursor
---@param State TriptychState
---@return PathDetails?
function M.get_target_under_cursor(State)
local vim = _G.triptych_mock_vim or vim
local line_number = vim.api.nvim_win_get_cursor(0)[1]
local contents = State.windows.current.contents
if contents then
return contents.children[line_number]
end
end
---Get a list of PathDetails that correspond to all the paths under the visual selection
---@param State TriptychState
---@return PathDetails[]
function M.get_targets_in_selection(State)
local vim = _G.triptych_mock_vim or vim
local from = vim.fn.getpos('v')[2]
local to = vim.api.nvim_win_get_cursor(0)[1]
local results = {}
local paths = State.windows.current.contents.children
if paths then
-- need to check min and max to account for the directionality of the visual selection
for i = math.min(to, from), math.max(to, from), 1 do
table.insert(results, paths[i])
end
end
return results
end
---Get the line number of a particular path in the buffer
---@param path string
---@param path_details PathDetails
---@return integer
local function line_number_of_path(path, path_details)
local num = 1
for i, child in ipairs(path_details) do
if child.path == path then
num = i
break
end
end
return num
end
---Currently just return "(cwd)" if the path == cwd
---@param path string
---@return string?
local function get_title_postfix(path)
local vim = _G.triptych_mock_vim or vim
if path == vim.fn.getcwd() then
return '(cwd)'
end
end
---@param buf integer
---@param sign_name string
---@param group string
---@param line_num integer
local function place_sign(buf, sign_name, group, line_num)
-- If the sign isn't defined sign_getdefined will return an empty {}
if vim.fn.sign_getdefined(sign_name)[1] then
vim.fn.sign_place(0, group, sign_name, buf, { lnum = line_num })
end
end
---@param buf integer
---@param children PathDetails
---@param group string # see :h sign-group
---@return nil
local function set_sign_columns(buf, children, group)
local vim = _G.triptych_mock_vim or vim
vim.fn.sign_unplace(group)
for index, entry in ipairs(children) do
if entry.git_status then
local sign_name = git.status_to_sign[entry.git_status]
place_sign(buf, sign_name, group, index)
end
if entry.diagnostic_status then
local sign_name = diagnostics.get_sign(entry.diagnostic_status)
place_sign(buf, sign_name, group, index)
end
end
end
--- On the parent and primary windows, update the title and state, then trigger async directory read
---@param State TriptychState
---@param target_dir string
---@return nil
function M.set_primary_and_parent_window_targets(State, target_dir)
local vim = _G.triptych_mock_vim or vim
local focused_win = State.windows.current.win
local parent_win = State.windows.parent.win
local child_win = State.windows.child.win
local focused_title = vim.fs.basename(target_dir)
local parent_path = vim.fs.dirname(target_dir)
local parent_title = vim.fs.basename(parent_path)
float.win_set_title(parent_win, parent_title, '', 'Directory', get_title_postfix(parent_path))
float.win_set_title(focused_win, focused_title, '', 'Directory', get_title_postfix(target_dir))
State.windows = {
parent = {
path = parent_path,
contents = nil,
win = parent_win,
},
current = {
path = target_dir,
previous_path = State.windows.current.path,
contents = nil,
win = focused_win,
},
child = { -- This all gets populated by update_child_window
path = '',
is_dir = State.windows.child.is_dir,
contents = nil,
win = child_win,
},
}
read_path_async(parent_path, State.show_hidden, 'parent')
read_path_async(target_dir, State.show_hidden, 'primary')
end
--- Set lines for the parent or primary window
---@param State TriptychState
---@param path_details PathDetails
---@param win_type WinType
---@param Diagnostics? Diagnostics
---@param Git? Git
---@return nil
function M.set_parent_or_primary_window_lines(State, path_details, win_type, Diagnostics, Git)
local vim = _G.triptych_mock_vim or vim
local state = u.eval(function()
if win_type == 'parent' then
return State.windows.parent
elseif win_type == 'primary' then
return State.windows.current
end
return State.windows.child
end)
-- Because of async we may have moved onto a different path
if path_details.path ~= state.path then
return nil
end
local buf = vim.api.nvim_win_get_buf(state.win)
local contents = filter_and_encrich_dir_contents(path_details, State.show_hidden, Diagnostics, Git)
local lines, highlights = path_details_to_lines(State, contents)
float.win_set_lines(state.win, lines, win_type == 'primary')
float.buf_apply_highlights(buf, highlights)
set_sign_columns(buf, contents.children, 'triptych_sign_col_' .. win_type)
if win_type == 'primary' then
local line_number = u.eval(function()
if State.has_initial_cursor_pos_been_set then
return State.path_to_line_map[path_details.path] or 1
end
local opening_buf = vim.api.nvim_win_get_buf(State.opening_win)
local maybe_opening_buf_name = vim.api.nvim_buf_get_name(opening_buf)
if maybe_opening_buf_name then
return line_number_of_path(maybe_opening_buf_name, path_details.children)
end
return 1
end)
local buf_line_count = vim.api.nvim_buf_line_count(buf)
vim.api.nvim_win_set_cursor(0, { math.min(line_number, buf_line_count), 0 })
State.has_initial_cursor_pos_been_set = true
State.path_to_line_map[path_details.path] = line_number
State.windows.current.contents = contents
elseif win_type == 'parent' then
local line_number = line_number_of_path(State.windows.current.path, contents.children)
vim.api.nvim_win_set_cursor(state.win, { line_number, 0 })
State.path_to_line_map[State.windows.parent.path] = line_number
State.windows.parent.contents = contents
end
end
---In the child/preview window, update the title and state, then trigger async directory or file read
---@param State TriptychState
---@param path_details PathDetails
---@return nil
function M.set_child_window_target(State, path_details)
local vim = _G.triptych_mock_vim or vim
local buf = vim.api.nvim_win_get_buf(State.windows.child.win)
-- TODO: Can we make path_details mandatory to avoid the repeated checks
vim.api.nvim_buf_set_var(
buf,
'triptych_path',
u.cond(path_details == nil, {
when_true = nil,
when_false = function()
return path_details.path
end,
})
)
State.windows.child.is_dir = u.cond(path_details == nil, {
when_true = false,
when_false = function()
return path_details.is_dir
end,
})
State.windows.child.path = u.cond(path_details == nil, {
when_true = nil,
when_false = function()
return path_details.path
end,
})
if path_details == nil then
float.win_set_title(State.windows.child.win, '[empty directory]')
float.buf_set_lines(buf, {})
elseif path_details.is_dir then
syntax_highlighting.stop(buf)
float.win_set_title(
State.windows.child.win,
path_details.display_name,
'',
'Directory',
get_title_postfix(path_details.path)
)
read_path_async(path_details.path, State.show_hidden, 'child')
else
local filetype = fs.get_filetype_from_path(path_details.path) -- TODO: De-dupe this
local icon, highlight = icons.get_icon_by_filetype(filetype)
float.win_set_title(State.windows.child.win, path_details.display_name, icon, highlight)
float.buf_set_lines(buf, {})
local file_size = fs.get_file_size_in_kb(path_details.path)
if file_size < 300 then
read_file_async(buf, path_details.path)
else
local msg = '[File size too large to preview]'
float.buf_set_lines(buf, { msg })
end
end
end
--- Set lines for the child/preview window
---@param State TriptychState
---@param path_details PathDetails
---@param Diagnostics? Diagnostics
---@param Git? Git
---@return nil
function M.set_child_window_lines(State, path_details, Diagnostics, Git)
local buf = vim.api.nvim_win_get_buf(State.windows.child.win)
-- Because of async we may have moved onto a different path
if path_details.path ~= State.windows.child.path then
return nil
end
if path_details.is_dir then
local contents = filter_and_encrich_dir_contents(path_details, State.show_hidden, Diagnostics, Git)
local lines, highlights = path_details_to_lines(State, contents)
vim.treesitter.stop(buf)
vim.api.nvim_buf_set_option(buf, 'syntax', 'off')
float.buf_set_lines(buf, lines)
float.buf_apply_highlights(buf, highlights)
set_sign_columns(buf, contents.children, 'triptych_sign_col_child')
else
local ft = fs.get_filetype_from_path(path_details.path)
syntax_highlighting.stop(buf)
float.buf_set_lines(buf, {})
if vim.g.triptych_config.options.syntax_highlighting.enabled then
syntax_highlighting.start(buf, ft)
end
end
end
---@param State TriptychState
---@param path string
---@return nil
function M.jump_cursor_to(State, path)
local vim = _G.triptych_mock_vim or vim
local line_num
for index, item in ipairs(State.windows.current.contents.children) do
if item.path == path then
line_num = index
break
end
end
if line_num then
vim.api.nvim_win_set_cursor(0, { line_num, 0 })
end
end
---@param State TriptychState
---@return nil
function M.refresh_view(State)
M.set_primary_and_parent_window_targets(State, State.windows.current.path)
end
return M