forked from nvim-neo-tree/neo-tree.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.lua
738 lines (664 loc) · 21.9 KB
/
commands.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
--This file should contain all commands meant to be used by mappings.
local vim = vim
local fs_actions = require("neo-tree.sources.filesystem.lib.fs_actions")
local fs = require("neo-tree.sources.filesystem")
local utils = require("neo-tree.utils")
local renderer = require("neo-tree.ui.renderer")
local events = require("neo-tree.events")
local inputs = require("neo-tree.ui.inputs")
local popups = require("neo-tree.ui.popups")
local log = require("neo-tree.log")
local help = require("neo-tree.sources.common.help")
local Preview = require("neo-tree.sources.common.preview")
local async = require("plenary.async")
---Gets the node parent folder
---@param state table to look for nodes
---@return table? node
local function get_folder_node(state)
local tree = state.tree
local node = tree:get_node()
local last_id = node:get_id()
while node do
local insert_as_local = state.config.insert_as
local insert_as_global = require("neo-tree").config.window.insert_as
local use_parent
if insert_as_local then
use_parent = insert_as_local == "sibling"
else
use_parent = insert_as_global == "sibling"
end
local is_open_dir = node.type == "directory" and (node:is_expanded() or node.empty_expanded)
if use_parent and not is_open_dir then
return tree:get_node(node:get_parent_id())
end
if node.type == "directory" then
return node
end
local parent_id = node:get_parent_id()
if not parent_id or parent_id == last_id then
return node
else
last_id = parent_id
node = tree:get_node(parent_id)
end
end
end
---The using_root_directory is used to decide what part of the filename to show
-- the user when asking for a new filename to e.g. create, copy to or move to.
---@param state table The state of the source
---@return string The root path from which the relative source path should be taken
local function get_using_root_directory(state)
-- default to showing only the basename of the path
local using_root_directory = get_folder_node(state):get_id()
local show_path = state.config.show_path
if show_path == "absolute" then
using_root_directory = ""
elseif show_path == "relative" then
using_root_directory = state.path
elseif show_path ~= nil and show_path ~= "none" then
log.warn(
'A neo-tree mapping was setup with a config.show_path option with invalid value: "'
.. show_path
.. '", falling back to its default: nil/"none"'
)
end
return using_root_directory
end
local M = {}
---Adds all missing common commands to the given module
---@param to_source_command_module table The commands module for a source
---@param pattern string? A pattern specifying which commands to add, nil to add all
M._add_common_commands = function(to_source_command_module, pattern)
for name, func in pairs(M) do
if
type(name) == "string"
and not to_source_command_module[name]
and (not pattern or name:find(pattern))
and not name:find("^_")
then
to_source_command_module[name] = func
end
end
end
---Add a new file or dir at the current node
---@param state table The state of the source
---@param callback function The callback to call when the command is done. Called with the parent node as the argument.
M.add = function(state, callback)
local node = get_folder_node(state)
local in_directory = node:get_id()
local using_root_directory = get_using_root_directory(state)
fs_actions.create_node(in_directory, callback, using_root_directory)
end
---Add a new file or dir at the current node
---@param state table The state of the source
---@param callback function The callback to call when the command is done. Called with the parent node as the argument.
M.add_directory = function(state, callback)
local node = get_folder_node(state)
local in_directory = node:get_id()
local using_root_directory = get_using_root_directory(state)
fs_actions.create_directory(in_directory, callback, using_root_directory)
end
---Expand all nodes
---@param state table The state of the source
---@param node table A node to expand
M.expand_all_nodes = function(state, node)
log.debug("Expanding all nodes under " .. node:get_id())
local task = function ()
fs.expand_directory(state, node)
end
async.run(
task,
function ()
log.debug("All nodes expanded - redrawing")
vim.schedule_wrap(function()
renderer.redraw(state)
end)
end
)
end
M.close_node = function(state, callback)
local tree = state.tree
local node = tree:get_node()
local parent_node = tree:get_node(node:get_parent_id())
local target_node
if node:has_children() and node:is_expanded() then
target_node = node
else
target_node = parent_node
end
local root = tree:get_nodes()[1]
local is_root = target_node:get_id() == root:get_id()
if target_node and target_node:has_children() and not is_root then
target_node:collapse()
renderer.redraw(state)
renderer.focus_node(state, target_node:get_id())
end
end
M.close_all_subnodes = function(state)
local tree = state.tree
local node = tree:get_node()
local parent_node = tree:get_node(node:get_parent_id())
local target_node
if node:has_children() and node:is_expanded() then
target_node = node
else
target_node = parent_node
end
renderer.collapse_all_nodes(tree, target_node:get_id())
renderer.redraw(state)
renderer.focus_node(state, target_node:get_id())
end
M.close_all_nodes = function(state)
renderer.collapse_all_nodes(state.tree)
renderer.redraw(state)
end
M.close_window = function(state)
renderer.close(state)
end
M.toggle_auto_expand_width = function(state)
if state.window.position == "float" then
return
end
state.window.auto_expand_width = state.window.auto_expand_width == false
local width = utils.resolve_width(state.window.width)
if not state.window.auto_expand_width then
if (state.window.last_user_width or width) >= vim.api.nvim_win_get_width(0) then
state.window.last_user_width = width
end
vim.api.nvim_win_set_width(0, state.window.last_user_width)
state.win_width = state.window.last_user_width
state.longest_width_exact = 0
log.trace(string.format("Collapse auto_expand_width."))
end
renderer.redraw(state)
end
local copy_node_to_clipboard = function(state, node)
state.clipboard = state.clipboard or {}
local existing = state.clipboard[node.id]
if existing and existing.action == "copy" then
state.clipboard[node.id] = nil
else
state.clipboard[node.id] = { action = "copy", node = node }
log.info("Copied " .. node.name .. " to clipboard")
end
end
---Marks node as copied, so that it can be pasted somewhere else.
M.copy_to_clipboard = function(state, callback)
local node = state.tree:get_node()
if node.type == "message" then
return
end
copy_node_to_clipboard(state, node)
if callback then
callback()
end
end
M.copy_to_clipboard_visual = function(state, selected_nodes, callback)
for _, node in ipairs(selected_nodes) do
if node.type ~= "message" then
copy_node_to_clipboard(state, node)
end
end
if callback then
callback()
end
end
local cut_node_to_clipboard = function(state, node)
state.clipboard = state.clipboard or {}
local existing = state.clipboard[node.id]
if existing and existing.action == "cut" then
state.clipboard[node.id] = nil
else
state.clipboard[node.id] = { action = "cut", node = node }
log.info("Cut " .. node.name .. " to clipboard")
end
end
---Marks node as cut, so that it can be pasted (moved) somewhere else.
M.cut_to_clipboard = function(state, callback)
local node = state.tree:get_node()
cut_node_to_clipboard(state, node)
if callback then
callback()
end
end
M.cut_to_clipboard_visual = function(state, selected_nodes, callback)
for _, node in ipairs(selected_nodes) do
if node.type ~= "message" then
cut_node_to_clipboard(state, node)
end
end
if callback then
callback()
end
end
--------------------------------------------------------------------------------
-- Git commands
--------------------------------------------------------------------------------
M.git_add_file = function(state)
local node = state.tree:get_node()
if node.type == "message" then
return
end
local path = node:get_id()
local cmd = { "git", "add", path }
vim.fn.system(cmd)
events.fire_event(events.GIT_EVENT)
end
M.git_add_all = function(state)
local cmd = { "git", "add", "-A" }
vim.fn.system(cmd)
events.fire_event(events.GIT_EVENT)
end
M.git_commit = function(state, and_push)
local width = vim.fn.winwidth(0) - 2
local row = vim.api.nvim_win_get_height(0) - 3
local popup_options = {
relative = "win",
position = {
row = row,
col = 0,
},
size = width,
}
inputs.input("Commit message: ", "", function(msg)
local cmd = { "git", "commit", "-m", msg }
local title = "git commit"
local result = vim.fn.systemlist(cmd)
if vim.v.shell_error ~= 0 or (#result > 0 and vim.startswith(result[1], "fatal:")) then
popups.alert("ERROR: git commit", result)
return
end
if and_push then
title = "git commit && git push"
cmd = { "git", "push" }
local result2 = vim.fn.systemlist(cmd)
table.insert(result, "")
for i = 1, #result2 do
table.insert(result, result2[i])
end
end
events.fire_event(events.GIT_EVENT)
popups.alert(title, result)
end, popup_options)
end
M.git_commit_and_push = function(state)
M.git_commit(state, true)
end
M.git_push = function(state)
inputs.confirm("Are you sure you want to push your changes?", function(yes)
if yes then
local result = vim.fn.systemlist({ "git", "push" })
events.fire_event(events.GIT_EVENT)
popups.alert("git push", result)
end
end)
end
M.git_unstage_file = function(state)
local node = state.tree:get_node()
if node.type == "message" then
return
end
local path = node:get_id()
local cmd = { "git", "reset", "--", path }
vim.fn.system(cmd)
events.fire_event(events.GIT_EVENT)
end
M.git_revert_file = function(state)
local node = state.tree:get_node()
if node.type == "message" then
return
end
local path = node:get_id()
local cmd = { "git", "checkout", "HEAD", "--", path }
local msg = string.format("Are you sure you want to revert %s?", node.name)
inputs.confirm(msg, function(yes)
if yes then
vim.fn.system(cmd)
events.fire_event(events.GIT_EVENT)
end
end)
end
--------------------------------------------------------------------------------
-- END Git commands
--------------------------------------------------------------------------------
M.next_source = function(state)
local sources = require("neo-tree").config.sources
local sources = require("neo-tree").config.source_selector.sources
local next_source = sources[1]
for i, source_info in ipairs(sources) do
if source_info.source == state.name then
next_source = sources[i + 1]
if not next_source then
next_source = sources[1]
end
break
end
end
require("neo-tree.command").execute({
source = next_source.source,
position = state.current_position,
action = "focus",
})
end
M.prev_source = function(state)
local sources = require("neo-tree").config.sources
local sources = require("neo-tree").config.source_selector.sources
local next_source = sources[#sources]
for i, source_info in ipairs(sources) do
if source_info.source == state.name then
next_source = sources[i - 1]
if not next_source then
next_source = sources[#sources]
end
break
end
end
require("neo-tree.command").execute({
source = next_source.source,
position = state.current_position,
action = "focus",
})
end
M.show_debug_info = function(state)
print(vim.inspect(state))
end
---Pastes all items from the clipboard to the current directory.
---@param state table The state of the source
---@param callback function The callback to call when the command is done. Called with the parent node as the argument.
M.paste_from_clipboard = function(state, callback)
if state.clipboard then
local folder = get_folder_node(state):get_id()
-- Convert to list so to make it easier to pop items from the stack.
local clipboard_list = {}
for _, item in pairs(state.clipboard) do
table.insert(clipboard_list, item)
end
state.clipboard = nil
local handle_next_paste, paste_complete
paste_complete = function(source, destination)
if callback then
local insert_as = require("neo-tree").config.window.insert_as
-- open the folder so the user can see the new files
local node = insert_as == "sibling" and state.tree:get_node() or state.tree:get_node(folder)
if not node then
log.warn("Could not find node for " .. folder)
end
callback(node, destination)
end
local next_item = table.remove(clipboard_list)
if next_item then
handle_next_paste(next_item)
end
end
handle_next_paste = function(item)
if item.action == "copy" then
fs_actions.copy_node(
item.node.path,
folder .. utils.path_separator .. item.node.name,
paste_complete
)
elseif item.action == "cut" then
fs_actions.move_node(
item.node.path,
folder .. utils.path_separator .. item.node.name,
paste_complete
)
end
end
local next_item = table.remove(clipboard_list)
if next_item then
handle_next_paste(next_item)
end
end
end
---Copies a node to a new location, using typed input.
---@param state table The state of the source
---@param callback function The callback to call when the command is done. Called with the parent node as the argument.
M.copy = function(state, callback)
local node = state.tree:get_node()
if node.type == "message" then
return
end
local using_root_directory = get_using_root_directory(state)
fs_actions.copy_node(node.path, nil, callback, using_root_directory)
end
---Moves a node to a new location, using typed input.
---@param state table The state of the source
---@param callback function The callback to call when the command is done. Called with the parent node as the argument.
M.move = function(state, callback)
local node = state.tree:get_node()
if node.type == "message" then
return
end
local using_root_directory = get_using_root_directory(state)
fs_actions.move_node(node.path, nil, callback, using_root_directory)
end
M.delete = function(state, callback)
local tree = state.tree
local node = tree:get_node()
if node.type == "file" or node.type == "directory" then
fs_actions.delete_node(node.path, callback)
else
log.warn("The `delete` command can only be used on files and directories")
end
end
M.delete_visual = function(state, selected_nodes, callback)
local paths_to_delete = {}
for _, node_to_delete in pairs(selected_nodes) do
if node_to_delete.type == "file" or node_to_delete.type == "directory" then
table.insert(paths_to_delete, node_to_delete.path)
end
end
fs_actions.delete_nodes(paths_to_delete, callback)
end
M.preview = function(state)
Preview.show(state)
end
M.revert_preview = function()
Preview.hide()
end
--
-- Multi-purpose function to back out of whatever we are in
M.cancel = function(state)
if Preview.is_active() then
Preview.hide()
else
if state.current_position == "float" then
renderer.close_all_floating_windows()
end
end
end
M.toggle_preview = function(state)
Preview.toggle(state)
end
M.focus_preview = function()
Preview.focus()
end
---Open file or directory
---@param state table The state of the source
---@param open_cmd string The vim command to use to open the file
---@param toggle_directory function The function to call to toggle a directory
---open/closed
local open_with_cmd = function(state, open_cmd, toggle_directory, open_file)
local tree = state.tree
local success, node = pcall(tree.get_node, tree)
if node.type == "message" then
return
end
if not (success and node) then
log.debug("Could not get node.")
return
end
local function open()
M.revert_preview()
local path = node.path or node:get_id()
local bufnr = node.extra and node.extra.bufnr
if node.type == "terminal" then
path = node:get_id()
end
if type(open_file) == "function" then
open_file(state, path, open_cmd, bufnr)
else
utils.open_file(state, path, open_cmd, bufnr)
end
local extra = node.extra or {}
local pos = extra.position or extra.end_position
if pos ~= nil then
vim.api.nvim_win_set_cursor(0, { (pos[1] or 0) + 1, pos[2] or 0 })
vim.api.nvim_win_call(0, function()
vim.cmd("normal! zvzz") -- expand folds and center cursor
end)
end
end
if utils.is_expandable(node) then
if toggle_directory and node.type == "directory" then
toggle_directory(node)
elseif node:has_children() then
if node:is_expanded() and node.type ~= "directory" then
return open()
end
local updated = false
if node:is_expanded() then
updated = node:collapse()
else
updated = node:expand()
end
if updated then
renderer.redraw(state)
end
end
else
open()
end
end
---Open file or directory in the closest window
---@param state table The state of the source
---@param toggle_directory function The function to call to toggle a directory
---open/closed
M.open = function(state, toggle_directory)
open_with_cmd(state, "e", toggle_directory)
end
---Open file or directory in a split of the closest window
---@param state table The state of the source
---@param toggle_directory function The function to call to toggle a directory
---open/closed
M.open_split = function(state, toggle_directory)
open_with_cmd(state, "split", toggle_directory)
end
---Open file or directory in a vertical split of the closest window
---@param state table The state of the source
---@param toggle_directory function The function to call to toggle a directory
---open/closed
M.open_vsplit = function(state, toggle_directory)
open_with_cmd(state, "vsplit", toggle_directory)
end
---Open file or directory in a new tab
---@param state table The state of the source
---@param toggle_directory function The function to call to toggle a directory
---open/closed
M.open_tabnew = function(state, toggle_directory)
open_with_cmd(state, "tabnew", toggle_directory)
end
---Open file or directory or focus it if a buffer already exists with it
---@param state table The state of the source
---@param toggle_directory function The function to call to toggle a directory
---open/closed
M.open_drop = function(state, toggle_directory)
open_with_cmd(state, "drop", toggle_directory)
end
---Open file or directory in new tab or focus it if a buffer already exists with it
---@param state table The state of the source
---@param toggle_directory function The function to call to toggle a directory
---open/closed
M.open_tab_drop = function(state, toggle_directory)
open_with_cmd(state, "tab drop", toggle_directory)
end
M.rename = function(state, callback)
local tree = state.tree
local node = tree:get_node()
if node.type == "message" then
return
end
fs_actions.rename_node(node.path, callback)
end
---Expands or collapses the current node.
M.toggle_node = function(state, toggle_directory)
local tree = state.tree
local node = tree:get_node()
if not utils.is_expandable(node) then
return
end
if node.type == "directory" and toggle_directory then
toggle_directory(node)
elseif node:has_children() then
local updated = false
if node:is_expanded() then
updated = node:collapse()
else
updated = node:expand()
end
if updated then
renderer.redraw(state)
end
end
end
---Expands or collapses the current node.
M.toggle_directory = function(state, toggle_directory)
local tree = state.tree
local node = tree:get_node()
if node.type ~= "directory" then
return
end
M.toggle_node(state, toggle_directory)
end
---Marks potential windows with letters and will open the give node in the picked window.
---@param state table The state of the source
---@param path string The path to open
---@param cmd string Command that is used to perform action on picked window
local use_window_picker = function(state, path, cmd)
local success, picker = pcall(require, "window-picker")
if not success then
print(
"You'll need to install window-picker to use this command: https://github.com/s1n7ax/nvim-window-picker"
)
return
end
local events = require("neo-tree.events")
local event_result = events.fire_event(events.FILE_OPEN_REQUESTED, {
state = state,
path = path,
open_cmd = cmd,
}) or {}
if event_result.handled then
events.fire_event(events.FILE_OPENED, path)
return
end
local picked_window_id = picker.pick_window()
if picked_window_id then
vim.api.nvim_set_current_win(picked_window_id)
local result, err = pcall(vim.cmd, cmd .. " " .. vim.fn.fnameescape(path))
if result or err == "Vim(edit):E325: ATTENTION" then
-- fixes #321
vim.api.nvim_buf_set_option(0, "buflisted", true)
events.fire_event(events.FILE_OPENED, path)
else
log.error("Error opening file:", err)
end
end
end
---Marks potential windows with letters and will open the give node in the picked window.
M.open_with_window_picker = function(state, toggle_directory)
open_with_cmd(state, "edit", toggle_directory, use_window_picker)
end
---Marks potential windows with letters and will open the give node in a split next to the picked window.
M.split_with_window_picker = function(state, toggle_directory)
open_with_cmd(state, "split", toggle_directory, use_window_picker)
end
---Marks potential windows with letters and will open the give node in a vertical split next to the picked window.
M.vsplit_with_window_picker = function(state, toggle_directory)
open_with_cmd(state, "vsplit", toggle_directory, use_window_picker)
end
M.show_help = function(state)
help.show(state)
end
return M