Skip to content

Commit

Permalink
feat(files): prompt for new name when pasting would create dupe
Browse files Browse the repository at this point in the history
  • Loading branch information
cseickel committed Jan 4, 2022
1 parent 844d345 commit fddd6f1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 44 deletions.
44 changes: 34 additions & 10 deletions lua/neo-tree/sources/filesystem/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,47 @@ M.paste_from_clipboard = function(state)
if at_node.type == "file" then
folder = at_node:get_parent_id()
end

-- 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()
-- open the folder so the user can see the new files
local node = state.tree:get_node(folder)
if not node then
print("Could not find node for " .. folder)
return
end
fs.show_new_children(node)
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)
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)
fs_actions.move_node(
item.node.path,
folder .. utils.path_separator .. item.node.name,
paste_complete)
end
end
state.clipboard = nil
fs.refresh()

-- open the folder so the user can see the new files
local node = state.tree:get_node(folder)
if not node then
print("Could not find node for " .. folder)
return
local next_item = table.remove(clipboard_list)
if next_item then
handle_next_paste(next_item)
end
fs.show_new_children(node)
end
end

Expand Down
6 changes: 5 additions & 1 deletion lua/neo-tree/sources/filesystem/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ local reveal_file = function(path)
if node.indent then
col = string.len(node.indent)
end
vim.api.nvim_set_current_win(state.split.winid)
if renderer.window_exists(state) then
vim.api.nvim_set_current_win(state.split.winid)
else
renderer.draw(state.tree:get_nodes(), state, nil)
end
vim.api.nvim_win_set_cursor(state.split.winid, { linenr, col })
return true
end
Expand Down
74 changes: 41 additions & 33 deletions lua/neo-tree/sources/filesystem/lib/fs_actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,52 @@ local function clear_buffer(path)
end
end

-- Move Node
M.move_node = function(source, destination, callback)
-- If aleady exists
local get_unused_name

function get_unused_name(destination, name_chosen_callback)
if loop.fs_stat(destination) then
print(destination, " already exists")
return
local parent_path, name = utils.split_path(destination)
inputs.input(name .. " already exists. Please enter a new name: ",
name, function(new_name)
if new_name and string.len(new_name) > 0 then
local new_path = parent_path .. utils.path_separator .. new_name
get_unused_name(new_path, name_chosen_callback)
end
end)
else
name_chosen_callback(destination)
end

--create_dirs_if_needed(destination)
loop.fs_rename(source, destination, function(err)
if err then
print("Could not move the files")
return
end
if callback then
vim.schedule_wrap(callback)()
end
end
-- Move Node
M.move_node = function(source, destination, callback)
get_unused_name(destination, function(dest)
loop.fs_rename(source, dest, function(err)
if err then
print("Could not move the files")
return
end
if callback then
vim.schedule_wrap(callback)()
end
end)
end)
end

-- Copy Node
M.copy_node = function(source, destination, callback)
if loop.fs_stat(destination) then
print("Node already exists")
return
end

loop.fs_copyfile(source, destination)
local handle
handle = loop.spawn( "cp", {args = {"-r",source, destination}}, function(code)
handle:close()
if code ~= 0 then
print("copy failed")
return
end
if callback then
vim.schedule_wrap(callback)()
end
M.copy_node = function(source, _destination, callback)
get_unused_name(_destination, function(destination)
loop.fs_copyfile(source, destination)
local handle
handle = loop.spawn( "cp", {args = {"-r",source, destination}}, function(code)
handle:close()
if code ~= 0 then
print("copy failed")
return
end
if callback then
vim.schedule_wrap(callback)()
end
end)
end)
end

Expand Down Expand Up @@ -117,7 +125,7 @@ M.delete_node = function(path, callback)
depth = 1,
})
if #children > 0 then
msg = msg .. "\nWARNING: Directory is not empty!"
msg = "WARNING: Dir not empty! " .. msg
end
end

Expand Down

0 comments on commit fddd6f1

Please sign in to comment.