From d0d42d42a3d9213baccce17ebd791e29779b1f2a Mon Sep 17 00:00:00 2001 From: kyazdani42 Date: Fri, 5 Jun 2020 09:33:14 +0200 Subject: [PATCH] fix: fs and refresh bugs - refreshing delete from entries before adding new entries to avoid conflicts with indexes - delete is synchronous because on large folders it was not recursing properly thus causing errors and not deleting content. --- lua/lib/fs.lua | 81 +++++++++++++++++++------------------------- lua/lib/populate.lua | 18 +++++----- 2 files changed, 43 insertions(+), 56 deletions(-) diff --git a/lua/lib/fs.lua b/lua/lib/fs.lua index 5500f0f2..9282e58b 100644 --- a/lua/lib/fs.lua +++ b/lua/lib/fs.lua @@ -77,23 +77,10 @@ function M.create(node) end end -local remove_ok = true - -local function remove_callback(name, absolute_path) - return function(err, success) - if err ~= nil then - api.nvim_err_writeln(err) - remove_ok = false - elseif not success then - remove_ok = false - api.nvim_err_writeln('Could not remove '..name) - else - api.nvim_out_write(name..' has been removed\n') - for _, buf in pairs(api.nvim_list_bufs()) do - if api.nvim_buf_get_name(buf) == absolute_path then - api.nvim_command(':bd! '..buf) - end - end +local function clear_buffer(absolute_path) + for _, buf in pairs(api.nvim_list_bufs()) do + if api.nvim_buf_get_name(buf) == absolute_path then + api.nvim_command(':bd! '..buf) end end end @@ -110,14 +97,16 @@ local function remove_dir(cwd) local new_cwd = cwd..'/'..name if t == 'directory' then - remove_dir(new_cwd) + local success = remove_dir(new_cwd) + if not success then return false end else - luv.fs_unlink(new_cwd, vim.schedule_wrap(remove_callback(new_cwd, new_cwd))) + local success = luv.fs_unlink(new_cwd) + if not success then return false end + clear_buffer(new_cwd) end - if not remove_ok then return end end - luv.fs_rmdir(cwd, vim.schedule_wrap(remove_callback(cwd, cwd))) + return luv.fs_rmdir(cwd) end function M.remove(node) @@ -126,44 +115,42 @@ function M.remove(node) local ans = vim.fn.input("Remove " ..node.name.. " ? y/n: ") clear_prompt() if ans:match('^y') then - remove_ok = true if node.entries ~= nil then - remove_dir(node.absolute_path) + local success = remove_dir(node.absolute_path) + if not success then + return api.nvim_err_writeln('Could not remove '..node.name) + end + api.nvim_out_write(node.name..' has been removed\n') else - luv.fs_unlink(node.absolute_path, vim.schedule_wrap( - remove_callback(node.name, node.absolute_path) - )) + local success = luv.fs_unlink(node.absolute_path) + if not success then + return api.nvim_err_writeln('Could not remove '..node.name) + end + api.nvim_out_write(node.name..' has been removed\n') + clear_buffer(node.absolute_path) end refresh_tree() end end -local function rename_callback(node, new_name) - return function(err, success) - if err ~= nil then - api.nvim_err_writeln(err) - elseif not success then - api.nvim_err_writeln('Could not rename '..node.absolute_path..' to '..new_name) - else - api.nvim_out_write(node.absolute_path..' ➜ '..new_name..'\n') - for _, buf in pairs(api.nvim_list_bufs()) do - if api.nvim_buf_get_name(buf) == node.absolute_path then - api.nvim_buf_set_name(buf, new_name) - end - end - refresh_tree() - end - end -end - function M.rename(node) if node.name == '..' then return end - local ans = vim.fn.input("Rename " ..node.name.. " to ", node.absolute_path) + local new_name = vim.fn.input("Rename " ..node.name.. " to ", node.absolute_path) clear_prompt() - if not ans or #ans == 0 then return end + if not new_name or #new_name == 0 then return end - luv.fs_rename(node.absolute_path, ans, vim.schedule_wrap(rename_callback(node, ans))) + local success = luv.fs_rename(node.absolute_path, new_name) --, vim.schedule_wrap(rename_callback(node, ans))) + if not success then + return api.nvim_err_writeln('Could not rename '..node.absolute_path..' to '..new_name) + end + api.nvim_out_write(node.absolute_path..' ➜ '..new_name..'\n') + for _, buf in pairs(api.nvim_list_bufs()) do + if api.nvim_buf_get_name(buf) == node.absolute_path then + api.nvim_buf_set_name(buf, new_name) + end + end + refresh_tree() end return M diff --git a/lua/lib/populate.lua b/lua/lib/populate.lua index bfeef983..c6d24bf2 100644 --- a/lua/lib/populate.lua +++ b/lua/lib/populate.lua @@ -104,6 +104,15 @@ function M.refresh_entries(entries, cwd) ::continue:: end + local idx = 1 + for _, name in ipairs(cached_entries) do + if not new_entries[name] then + table.remove(entries, idx, idx + 1) + else + idx = idx + 1 + end + end + local all = { { entries = dirs, fn = dir_new }, { entries = links, fn = link_new }, @@ -127,15 +136,6 @@ function M.refresh_entries(entries, cwd) prev = name end end - - local idx = 1 - for _, name in ipairs(cached_entries) do - if not new_entries[name] then - table.remove(entries, idx, idx + 1) - else - idx = idx + 1 - end - end end function M.populate(entries, cwd)