Refacto: rewrite everything
- The tree is created with libuv functions, which makes it blazingly fast. - The tree may now be faster than any other vim trees, it can handle directories with thousands of files without any latency at all (tested on 40K files, works flawlessly). - More solid logic for opening and closing the tree. - tree state is remembered (closing / opening a folder keeps opened subdirectories open) - detection of multiple git projects in the tree - more icon support - smart rendering - smart updates - ms windows support - gx replacement function running xdg-open on linux, open on macos
This commit is contained in:
202
lua/lib/fs.lua
202
lua/lib/fs.lua
@@ -1,77 +1,169 @@
|
||||
local api = vim.api
|
||||
local luv = vim.loop
|
||||
local open_mode = luv.constants.O_CREAT + luv.constants.O_WRONLY + luv.constants.O_TRUNC
|
||||
|
||||
local M = {}
|
||||
|
||||
function M.get_cwd() return luv.cwd() end
|
||||
|
||||
function M.is_dir(path)
|
||||
local stat = luv.fs_lstat(path)
|
||||
return stat and stat.type == 'directory' or false
|
||||
local function clear_prompt()
|
||||
vim.api.nvim_command('normal :esc<CR>')
|
||||
end
|
||||
|
||||
function M.is_symlink(path)
|
||||
local stat = luv.fs_lstat(path)
|
||||
return stat and stat.type == 'link' or false
|
||||
local function refresh_tree()
|
||||
vim.api.nvim_command(":LuaTreeRefresh")
|
||||
end
|
||||
|
||||
function M.link_to(path)
|
||||
return luv.fs_readlink(path) or ''
|
||||
end
|
||||
|
||||
function M.check_dir_access(path)
|
||||
if luv.fs_access(path, 'R') == true then
|
||||
return true
|
||||
else
|
||||
api.nvim_err_writeln('Permission denied: ' .. path)
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
local handle = nil
|
||||
|
||||
local function run_process(opt, err, cb)
|
||||
handle = luv.spawn(opt.cmd, { args = opt.args }, vim.schedule_wrap(function(code)
|
||||
handle:close()
|
||||
if code ~= 0 then
|
||||
return api.nvim_err_writeln(err)
|
||||
local function create_file(file)
|
||||
luv.fs_open(file, "w", open_mode, vim.schedule_wrap(function(err, fd)
|
||||
if err then
|
||||
api.nvim_err_writeln('Could not create file '..file)
|
||||
else
|
||||
-- FIXME: i don't know why but libuv keeps creating file with executable permissions
|
||||
-- this is why we need to chmod to default file permissions
|
||||
luv.fs_chmod(file, 0644)
|
||||
luv.fs_close(fd)
|
||||
api.nvim_out_write('File '..file..' was properly created\n')
|
||||
refresh_tree()
|
||||
end
|
||||
cb()
|
||||
end))
|
||||
end
|
||||
|
||||
function M.rm(path, cb)
|
||||
local opt = { cmd='rm', args = {'-rf', path } };
|
||||
run_process(opt, 'Error removing '..path, cb)
|
||||
local function get_num_entries(iter)
|
||||
i = 0
|
||||
for _ in iter do
|
||||
i = i + 1
|
||||
end
|
||||
return i
|
||||
end
|
||||
|
||||
|
||||
function M.rename(file, new_path, cb)
|
||||
local opt = { cmd='mv', args = {file, new_path } };
|
||||
run_process(opt, 'Error renaming '..file..' to '..new_path, cb)
|
||||
end
|
||||
|
||||
function M.create(path, file, folders, cb)
|
||||
local opt_file = nil
|
||||
local file_path = nil
|
||||
if file ~= nil then
|
||||
file_path = path..folders..file
|
||||
opt_file = { cmd='touch', args = {file_path} }
|
||||
function M.create(node)
|
||||
if node.name == '..' then return end
|
||||
|
||||
local add_into
|
||||
if node.entries ~= nil then
|
||||
add_into = node.absolute_path..'/'
|
||||
else
|
||||
add_into = node.absolute_path:sub(0, -(#node.name + 1))
|
||||
end
|
||||
|
||||
if folders ~= "" then
|
||||
local folder_path = path..folders
|
||||
local opt = {cmd = 'mkdir', args = {'-p', folder_path }}
|
||||
run_process(opt, 'Error creating folder '..folder_path, function()
|
||||
if opt_file then
|
||||
run_process(opt, 'Error creating file '..file_path, cb)
|
||||
else
|
||||
cb()
|
||||
local ans = vim.fn.input('Create file '..add_into)
|
||||
clear_prompt()
|
||||
if not ans or #ans == 0 then return end
|
||||
|
||||
if not ans:match('/') then
|
||||
return create_file(add_into..ans)
|
||||
end
|
||||
|
||||
-- create a foler for each element until / and create a file when element is not ending with /
|
||||
-- if element is ending with / and it's the last element, we need to manually refresh
|
||||
local relpath = ''
|
||||
local idx = 0
|
||||
local num_entries = get_num_entries(ans:gmatch('[^/]+/?'))
|
||||
for path in ans:gmatch('[^/]+/?') do
|
||||
idx = idx + 1
|
||||
relpath = relpath..path
|
||||
if relpath:match('.*/$') then
|
||||
local success = luv.fs_mkdir(add_into..relpath, 493)
|
||||
if not success then
|
||||
api.nvim_err_writeln('Could not create folder '..add_into..relpath)
|
||||
return
|
||||
end
|
||||
end)
|
||||
elseif opt_file then
|
||||
run_process(opt_file, 'Error creating file '..file_path, cb)
|
||||
if idx == num_entries then
|
||||
api.nvim_out_write('Folder '..add_into..relpath..' was properly created\n')
|
||||
refresh_tree()
|
||||
end
|
||||
else
|
||||
create_file(add_into..relpath)
|
||||
end
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function remove_dir(cwd)
|
||||
local handle = luv.fs_scandir(cwd)
|
||||
if type(handle) == 'string' then
|
||||
return api.nvim_err_writeln(handle)
|
||||
end
|
||||
|
||||
while true do
|
||||
local name, t = luv.fs_scandir_next(handle)
|
||||
if not name then break end
|
||||
|
||||
local new_cwd = cwd..'/'..name
|
||||
if t == 'directory' then
|
||||
remove_dir(new_cwd)
|
||||
else
|
||||
luv.fs_unlink(new_cwd, vim.schedule_wrap(remove_callback(new_cwd, new_cwd)))
|
||||
end
|
||||
if not remove_ok then return end
|
||||
end
|
||||
|
||||
luv.fs_rmdir(cwd, vim.schedule_wrap(remove_callback(cwd, cwd)))
|
||||
end
|
||||
|
||||
function M.remove(node)
|
||||
if node.name == '..' then return end
|
||||
|
||||
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)
|
||||
else
|
||||
luv.fs_unlink(node.absolute_path, vim.schedule_wrap(
|
||||
remove_callback(node.name, 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)
|
||||
clear_prompt()
|
||||
if not ans or #ans == 0 then return end
|
||||
|
||||
luv.fs_rename(node.absolute_path, ans, vim.schedule_wrap(rename_callback(node, ans)))
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user