Skip to content

Commit ec13f39

Browse files
committed
feat(files): add cp/mv commands that take text input, closes #145
1 parent 76f7223 commit ec13f39

File tree

6 files changed

+53
-18
lines changed

6 files changed

+53
-18
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ use {
124124
["c"] = "copy_to_clipboard",
125125
["x"] = "cut_to_clipboard",
126126
["p"] = "paste_from_clipboard",
127+
["m"] = "move", -- takes text input for destination
127128
["q"] = "close_window",
128129
}
129130
}

lua/neo-tree/defaults.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ local config = {
118118
["c"] = "copy_to_clipboard",
119119
["x"] = "cut_to_clipboard",
120120
["p"] = "paste_from_clipboard",
121+
["y"] = "copy", -- takes text input for destination
122+
["m"] = "move", -- takes text input for destination
123+
["q"] = "close_window",
121124
},
122125
},
123126
--find_command = "fd",

lua/neo-tree/sources/common/commands.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,22 @@ M.paste_from_clipboard = function(state, callback)
142142
end
143143
end
144144

145+
---Copies a node to a new location, using typed input.
146+
---@param state table The state of the source
147+
---@param callback function The callback to call when the command is done. Called with the parent node as the argument.
148+
M.copy = function(state, callback)
149+
local node = state.tree:get_node()
150+
fs_actions.copy_node(node.path, nil, callback)
151+
end
152+
153+
---Moves a node to a new location, using typed input.
154+
---@param state table The state of the source
155+
---@param callback function The callback to call when the command is done. Called with the parent node as the argument.
156+
M.move = function(state, callback)
157+
local node = state.tree:get_node()
158+
fs_actions.move_node(node.path, nil, callback)
159+
end
160+
145161
M.delete = function(state, callback)
146162
local tree = state.tree
147163
local node = tree:get_node()

lua/neo-tree/sources/filesystem/commands.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ M.close_all_nodes = cc.close_all_nodes
2222
M.close_node = cc.close_node
2323
M.close_window = cc.close_window
2424

25+
M.copy = function(state)
26+
cc.copy(state, refresh)
27+
end
28+
2529
---Marks node as copied, so that it can be pasted somewhere else.
2630
M.copy_to_clipboard = function(state)
2731
cc.copy_to_clipboard(state, redraw)
@@ -32,6 +36,10 @@ M.cut_to_clipboard = function(state)
3236
cc.cut_to_clipboard(state, redraw)
3337
end
3438

39+
M.move = function(state)
40+
cc.move(state, refresh)
41+
end
42+
3543
M.show_debug_info = cc.show_debug_info
3644

3745
---Pastes all items from the clipboard to the current directory.

lua/neo-tree/sources/filesystem/lib/fs_actions.lua

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ local M = {}
1717

1818
local function clear_buffer(path)
1919
local buf = utils.find_buffer_by_name(path)
20+
if buf < 1 then
21+
return
22+
end
2023
local alt = vim.fn.bufnr("#")
2124
-- Check all windows to see if they are using the buffer
2225
for _, win in ipairs(vim.api.nvim_list_wins()) do
@@ -52,10 +55,11 @@ local function create_all_parents(path)
5255
end
5356

5457
local get_unused_name
55-
function get_unused_name(destination, name_chosen_callback)
58+
function get_unused_name(destination, name_chosen_callback, first_message)
5659
if loop.fs_stat(destination) then
5760
local parent_path, name = utils.split_path(destination)
58-
inputs.input(name .. " already exists. Please enter a new name: ", name, function(new_name)
61+
local message = first_message or name .. " already exists. Please enter a new name: "
62+
inputs.input(message, name, function(new_name)
5963
if new_name and string.len(new_name) > 0 then
6064
local new_path = parent_path .. utils.path_separator .. new_name
6165
get_unused_name(new_path, name_chosen_callback)
@@ -67,7 +71,9 @@ function get_unused_name(destination, name_chosen_callback)
6771
end
6872
-- Move Node
6973
M.move_node = function(source, destination, callback)
70-
get_unused_name(destination, function(dest)
74+
local parent_path, name = utils.split_path(source)
75+
get_unused_name(destination or source, function(dest)
76+
create_all_parents(dest)
7177
loop.fs_rename(source, dest, function(err)
7278
if err then
7379
log.error("Could not move the files")
@@ -83,18 +89,17 @@ M.move_node = function(source, destination, callback)
8389
end
8490
end)
8591
end)
86-
end)
92+
end, 'Move "' .. name .. '" to:')
8793
end
8894

8995
-- Copy Node
9096
M.copy_node = function(source, _destination, callback)
91-
get_unused_name(_destination, function(destination)
92-
loop.fs_copyfile(source, destination)
93-
local handle
94-
handle = loop.spawn("cp", { args = { "-r", source, destination } }, function(code)
95-
handle:close()
96-
if code ~= 0 then
97-
log.error("copy failed")
97+
local parent_path, name = utils.split_path(source)
98+
get_unused_name(_destination or source, function(destination)
99+
create_all_parents(destination)
100+
loop.fs_copyfile(source, destination, function(err)
101+
if err then
102+
log.error("Could not copy the files")
98103
return
99104
end
100105
vim.schedule(function()
@@ -104,7 +109,7 @@ M.copy_node = function(source, _destination, callback)
104109
end
105110
end)
106111
end)
107-
end)
112+
end, 'Copy "' .. name .. '" to:')
108113
end
109114

110115
-- Create Node

lua/neo-tree/sources/filesystem/lib/fs_scan.lua

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ local function do_scan(context, path_to_scan)
3535
on_exit = vim.schedule_wrap(function()
3636
if state.use_libuv_file_watcher then
3737
local root = context.folders[path_to_scan]
38-
if root.is_link then
39-
log.trace("Adding fs watcher for ", root.link_to)
40-
fs_watch.watch_folder(root.link_to)
41-
else
42-
log.trace("Adding fs watcher for ", root.path)
43-
fs_watch.watch_folder(root.path)
38+
if root then
39+
if root.is_link then
40+
log.trace("Adding fs watcher for ", root.link_to)
41+
fs_watch.watch_folder(root.link_to)
42+
else
43+
log.trace("Adding fs watcher for ", root.path)
44+
fs_watch.watch_folder(root.path)
45+
end
4446
end
4547
end
4648
local scanned_folder = folders[path_to_scan]

0 commit comments

Comments
 (0)