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.
This commit is contained in:
kyazdani42 2020-06-05 09:33:14 +02:00
parent f6b38bb364
commit d0d42d42a3
2 changed files with 43 additions and 56 deletions

View File

@ -77,23 +77,10 @@ function M.create(node)
end end
end end
local remove_ok = true local function clear_buffer(absolute_path)
for _, buf in pairs(api.nvim_list_bufs()) do
local function remove_callback(name, absolute_path) if api.nvim_buf_get_name(buf) == absolute_path then
return function(err, success) api.nvim_command(':bd! '..buf)
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
end end
end end
end end
@ -110,14 +97,16 @@ local function remove_dir(cwd)
local new_cwd = cwd..'/'..name local new_cwd = cwd..'/'..name
if t == 'directory' then if t == 'directory' then
remove_dir(new_cwd) local success = remove_dir(new_cwd)
if not success then return false end
else 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 end
if not remove_ok then return end
end end
luv.fs_rmdir(cwd, vim.schedule_wrap(remove_callback(cwd, cwd))) return luv.fs_rmdir(cwd)
end end
function M.remove(node) function M.remove(node)
@ -126,44 +115,42 @@ function M.remove(node)
local ans = vim.fn.input("Remove " ..node.name.. " ? y/n: ") local ans = vim.fn.input("Remove " ..node.name.. " ? y/n: ")
clear_prompt() clear_prompt()
if ans:match('^y') then if ans:match('^y') then
remove_ok = true
if node.entries ~= nil then 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 else
luv.fs_unlink(node.absolute_path, vim.schedule_wrap( local success = luv.fs_unlink(node.absolute_path)
remove_callback(node.name, 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 end
refresh_tree() refresh_tree()
end end
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) function M.rename(node)
if node.name == '..' then return end 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() 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 end
return M return M

View File

@ -104,6 +104,15 @@ function M.refresh_entries(entries, cwd)
::continue:: ::continue::
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
local all = { local all = {
{ entries = dirs, fn = dir_new }, { entries = dirs, fn = dir_new },
{ entries = links, fn = link_new }, { entries = links, fn = link_new },
@ -127,15 +136,6 @@ function M.refresh_entries(entries, cwd)
prev = name prev = name
end end
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 end
function M.populate(entries, cwd) function M.populate(entries, cwd)