reformat, refactor code, run fs command with luv.spawn

This commit is contained in:
kiyan42
2020-04-26 14:13:09 +02:00
parent 81ee46e41d
commit 697912429c
9 changed files with 679 additions and 704 deletions

View File

@@ -1,6 +1,7 @@
local api = vim.api
local colors = require 'lib/config'.colors local colors = require 'lib/config'.colors
local cmd = vim.api.nvim_command local M = {}
local HIGHLIGHTS = { local HIGHLIGHTS = {
Symlink = { gui = 'bold', fg = colors.cyan }, Symlink = { gui = 'bold', fg = colors.cyan },
@@ -43,17 +44,15 @@ local LINKS = {
CursorColumn = 'CursorColumn' CursorColumn = 'CursorColumn'
} }
local function init_colors() function M.init_colors()
for k, d in pairs(HIGHLIGHTS) do for k, d in pairs(HIGHLIGHTS) do
local gui = d.gui or 'NONE' local gui = d.gui or 'NONE'
vim.api.nvim_command('hi def LuaTree'..k..' gui='..gui..' guifg='..d.fg) api.nvim_command('hi def LuaTree'..k..' gui='..gui..' guifg='..d.fg)
end end
for k, d in pairs(LINKS) do for k, d in pairs(LINKS) do
vim.api.nvim_command('hi def link LuaTree'..k..' '..d) api.nvim_command('hi def link LuaTree'..k..' '..d)
end end
end end
return { return M
init_colors = init_colors
}

View File

@@ -1,8 +1,10 @@
local api = vim.api local api = vim.api
local M = {}
local function get(var, fallback) local function get(var, fallback)
if vim.api.nvim_call_function('exists', { var }) == 1 then if api.nvim_call_function('exists', { var }) == 1 then
return vim.api.nvim_get_var(var) return api.nvim_get_var(var)
else else
return fallback return fallback
end end
@@ -12,11 +14,11 @@ local HAS_DEV_ICONS = api.nvim_call_function('exists', { "*WebDevIconsGetFileTyp
local show_icons = get('lua_tree_show_icons', { git = 1, folders = 1, files = 1 }) local show_icons = get('lua_tree_show_icons', { git = 1, folders = 1, files = 1 })
local SHOW_FILE_ICON = HAS_DEV_ICONS and show_icons.files == 1 M.SHOW_FILE_ICON = HAS_DEV_ICONS and show_icons.files == 1
local SHOW_FOLDER_ICON = show_icons.folders == 1 M.SHOW_FOLDER_ICON = show_icons.folders == 1
local SHOW_GIT_ICON = show_icons.git == 1 M.SHOW_GIT_ICON = show_icons.git == 1
local colors = { M.colors = {
red = get('terminal_color_1', 'Red'), red = get('terminal_color_1', 'Red'),
green = get('terminal_color_2', 'Green'), green = get('terminal_color_2', 'Green'),
yellow = get('terminal_color_3', 'Yellow'), yellow = get('terminal_color_3', 'Yellow'),
@@ -29,7 +31,7 @@ local colors = {
local keybindings = get('lua_tree_bindings', {}); local keybindings = get('lua_tree_bindings', {});
local bindings = { M.bindings = {
edit = keybindings.edit or '<CR>', edit = keybindings.edit or '<CR>',
edit_vsplit = keybindings.edit_vsplit or '<C-v>', edit_vsplit = keybindings.edit_vsplit or '<C-v>',
edit_split = keybindings.edit_split or '<C-x>', edit_split = keybindings.edit_split or '<C-x>',
@@ -40,11 +42,4 @@ local bindings = {
rename = keybindings.rename or 'r', rename = keybindings.rename or 'r',
} }
return { return M
SHOW_FOLDER_ICON = SHOW_FOLDER_ICON,
SHOW_FILE_ICON = SHOW_FILE_ICON,
SHOW_GIT_ICON = SHOW_GIT_ICON,
colors = colors,
bindings = bindings
}

View File

@@ -1,6 +1,7 @@
local api = vim.api
local config = require 'lib/config' local config = require 'lib/config'
local api = vim.api local M = {}
local function get_padding(depth) local function get_padding(depth)
local str = "" local str = ""
@@ -68,7 +69,7 @@ end
local get_icon = get_icon_func_gen() local get_icon = get_icon_func_gen()
local function format_tree(tree) function M.format_tree(tree)
local dirs = {} local dirs = {}
for i, node in pairs(tree) do for i, node in pairs(tree) do
@@ -113,7 +114,7 @@ local HIGHLIGHT_ICON_GROUPS = {
local function highlight_line(buffer) local function highlight_line(buffer)
local function highlight(group, line, from, to) local function highlight(group, line, from, to)
vim.api.nvim_buf_add_highlight(buffer, -1, group, line, from, to) api.nvim_buf_add_highlight(buffer, -1, group, line, from, to)
end end
return function(line, node) return function(line, node)
local text_start = node.depth * 2 local text_start = node.depth * 2
@@ -172,14 +173,11 @@ local function highlight_line(buffer)
end end
end end
local function highlight_buffer(buffer, tree) function M.highlight_buffer(buffer, tree)
local highlight = highlight_line(buffer) local highlight = highlight_line(buffer)
for i, node in pairs(tree) do for i, node in pairs(tree) do
highlight(i - 1, node) highlight(i - 1, node)
end end
end end
return { return M
format_tree = format_tree;
highlight_buffer = highlight_buffer;
}

View File

@@ -1,63 +1,77 @@
local api = vim.api local api = vim.api
local luv = vim.loop local luv = vim.loop
local function get_cwd() return luv.cwd() end local M = {}
local function is_dir(path) function M.get_cwd() return luv.cwd() end
function M.is_dir(path)
local stat = luv.fs_lstat(path) local stat = luv.fs_lstat(path)
return stat and stat.type == 'directory' or false return stat and stat.type == 'directory' or false
end end
local function is_symlink(path) function M.is_symlink(path)
local stat = luv.fs_lstat(path) local stat = luv.fs_lstat(path)
return stat and stat.type == 'link' or false return stat and stat.type == 'link' or false
end end
local function link_to(path) function M.link_to(path)
return luv.fs_readlink(path) or '' return luv.fs_readlink(path) or ''
end end
local function print_err(err) function M.check_dir_access(path)
if err then
api.nvim_err_writeln(err)
end
end
local function system(v)
print_err(api.nvim_call_function('system', { v }))
end
local function check_dir_access(path)
if luv.fs_access(path, 'R') == true then if luv.fs_access(path, 'R') == true then
return true return true
else else
print_err('Permission denied: ' .. path) api.nvim_err_writeln('Permission denied: ' .. path)
return false return false
end end
end end
-- TODO: better handling of path removal, rename and file creation with luv calls local handle = nil
-- it will take some time so leave it for a dedicated PR
local function rm(path) local function run_process(opt, err, cb)
system('rm -rf ' ..path) 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)
end
cb()
end))
end end
local function rename(file, new_path) function M.rm(path, cb)
system('mv '..file..' '..new_path) local opt = { cmd='rm', args = {'-rf', path } };
run_process(opt, 'Error removing '..path, cb)
end end
local function create(path, file, folders)
if folders ~= "" then system('mkdir -p '..path..folders) end function M.rename(file, new_path, cb)
if file ~= nil then system('touch '..path..folders..file) end local opt = { cmd='mv', args = {file, new_path } };
run_process(opt, 'Error renaming '..file..' to '..new_path, cb)
end end
return { function M.create(path, file, folders, cb)
check_dir_access = check_dir_access; local opt_file = nil
is_symlink = is_symlink; local file_path = nil
link_to = link_to; if file ~= nil then
get_cwd = get_cwd; file_path = path..folders..file
is_dir = is_dir; opt_file = { cmd='touch', args = {file_path} }
rename = rename; end
rm = rm;
create = create; 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()
end
end)
elseif opt_file then
run_process(opt_file, 'Error creating file '..file_path, cb)
end
end
return M

View File

@@ -1,14 +1,11 @@
local api = vim.api local api = vim.api
local fs = require 'lib/fs'
local update_view = require 'lib/winutils'.update_view local update_view = require 'lib/winutils'.update_view
local refresh_tree = require 'lib/state'.refresh_tree local refresh_tree = require 'lib/state'.refresh_tree
local refresh_git = require 'lib/git'.refresh_git local refresh_git = require 'lib/git'.refresh_git
local utils = require'lib/utils'
local fs = require 'lib/fs' local M = {}
local rm = fs.rm
local rename = fs.rename
local create = fs.create
local function input(v) local function input(v)
local param local param
@@ -20,7 +17,7 @@ local function clear_prompt()
api.nvim_command('normal :<esc>') api.nvim_command('normal :<esc>')
end end
local function create_file(path) function M.create_file(path)
local new_file = input("Create file: " .. path) local new_file = input("Create file: " .. path)
local file = nil local file = nil
@@ -39,34 +36,33 @@ local function create_file(path)
end end
clear_prompt() clear_prompt()
create(path, file, folders) fs.create(path, file, folders, function()
refresh_git() refresh_git()
refresh_tree() refresh_tree()
update_view() update_view()
end)
end end
local function remove_file(filename, path) function M.remove_file(filename, path)
local ans = input("Remove " .. filename .. " ? y/n: ") local ans = input("Remove " .. filename .. " ? y/n: ")
clear_prompt() clear_prompt()
if ans == "y" then if ans == "y" then
rm(path .. filename) fs.rm(path .. filename, function()
refresh_git() refresh_git()
refresh_tree() refresh_tree()
update_view() update_view()
end)
end end
end end
local function rename_file(filename, path) function M.rename_file(filename, path)
local new_path = input({"Rename file " .. filename .. ": ", path .. filename}) local new_path = input({"Rename file " .. filename .. ": ", path .. filename})
clear_prompt() clear_prompt()
rename(path .. filename, new_path) fs.rename(path .. filename, new_path, function()
refresh_git() refresh_git()
refresh_tree() refresh_tree()
update_view() update_view()
end)
end end
return { return M
create_file = create_file;
remove_file = remove_file;
rename_file = rename_file;
}

View File

@@ -1,8 +1,11 @@
local api = vim.api
local config = require 'lib/config' local config = require 'lib/config'
local utils = require'lib.utils' local utils = require'lib.utils'
local function system(v) return vim.api.nvim_call_function('system', { v }) end local M = {}
local function systemlist(v) return vim.api.nvim_call_function('systemlist', { v }) end
local function system(v) return api.nvim_call_function('system', { v }) end
local function systemlist(v) return api.nvim_call_function('systemlist', { v }) end
local function is_git_repo() local function is_git_repo()
local is_git = system('git rev-parse') local is_git = system('git rev-parse')
@@ -18,15 +21,15 @@ end
local GIT_STATUS = set_git_status() local GIT_STATUS = set_git_status()
local function refresh_git() function M.refresh_git()
if IS_GIT_REPO == false then return false end if IS_GIT_REPO == false then return false end
GIT_STATUS = set_git_status() GIT_STATUS = set_git_status()
return true return true
end end
local function force_refresh_git() function M.force_refresh_git()
IS_GIT_REPO = is_git_repo() IS_GIT_REPO = is_git_repo()
refresh_git() M.refresh_git()
end end
local function is_folder_dirty(relpath) local function is_folder_dirty(relpath)
@@ -55,7 +58,7 @@ local unmerged = create_git_checker('^[U ][U ]')
local renamed = create_git_checker('^R') local renamed = create_git_checker('^R')
local untracked = create_git_checker('^%?%?') local untracked = create_git_checker('^%?%?')
local function get_git_attr(path, is_dir) function M.get_git_attr(path, is_dir)
if IS_GIT_REPO == false or not config.SHOW_GIT_ICON then return '' end if IS_GIT_REPO == false or not config.SHOW_GIT_ICON then return '' end
if is_dir then if is_dir then
if is_folder_dirty(path) == true then return '' end if is_folder_dirty(path) == true then return '' end
@@ -73,8 +76,4 @@ local function get_git_attr(path, is_dir)
return '' return ''
end end
return { return M
get_git_attr = get_git_attr;
refresh_git = refresh_git;
force_refresh_git = force_refresh_git;
}

View File

@@ -1,18 +1,13 @@
local api = vim.api local api = vim.api
local utils = require'lib.utils' local utils = require'lib.utils'
local gitutils = require 'lib.git'
local fs = require 'lib.fs'
local function syslist(v) return api.nvim_call_function('systemlist', { v }) end local M = {}
local get_git_attr = require 'lib/git'.get_git_attr local ROOT_PATH = fs.get_cwd() .. '/'
local fs = require 'lib/fs'
local is_dir = fs.is_dir
local is_symlink = fs.is_symlink
local get_cwd = fs.get_cwd
local link_to = fs.link_to
local ROOT_PATH = get_cwd() .. '/' function M.set_root_path(path)
local function set_root_path(path)
ROOT_PATH = path ROOT_PATH = path
end end
@@ -33,8 +28,7 @@ if not MACOS and api.nvim_call_function('exists', { 'g:lua_tree_ignore' }) == 1
end end
local function list_dirs(path) local function list_dirs(path)
local ls_cmd = 'ls -A '..IGNORE_LIST..path return api.nvim_call_function('systemlist', { 'ls -A '..IGNORE_LIST..path })
return syslist(ls_cmd)
end end
local function sort_dirs(dirs) local function sort_dirs(dirs)
@@ -58,9 +52,9 @@ local function create_nodes(path, relpath, depth, dirs)
for i, name in pairs(dirs) do for i, name in pairs(dirs) do
local full_path = path..name local full_path = path..name
local dir = is_dir(full_path) local dir = fs.is_dir(full_path)
local link = is_symlink(full_path) local link = fs.is_symlink(full_path)
local linkto = link == true and link_to(full_path) or nil local linkto = link == true and fs.link_to(full_path) or nil
local rel_path = relpath ..name local rel_path = relpath ..name
tree[i] = { tree[i] = {
path = path, path = path,
@@ -72,14 +66,14 @@ local function create_nodes(path, relpath, depth, dirs)
dir = dir, dir = dir,
open = false, open = false,
icon = true, icon = true,
git = get_git_attr(rel_path, dir) git = gitutils.get_git_attr(rel_path, dir)
} }
end end
return sort_dirs(tree) return sort_dirs(tree)
end end
local function init_tree() function M.init_tree()
Tree = create_nodes(ROOT_PATH, '', 0, list_dirs(ROOT_PATH)) Tree = create_nodes(ROOT_PATH, '', 0, list_dirs(ROOT_PATH))
if ROOT_PATH ~= '/' then if ROOT_PATH ~= '/' then
table.insert(Tree, 1, { table.insert(Tree, 1, {
@@ -94,7 +88,7 @@ local function init_tree()
end end
end end
local function refresh_tree() function M.refresh_tree()
local cache = {} local cache = {}
for _, v in pairs(Tree) do for _, v in pairs(Tree) do
@@ -103,7 +97,7 @@ local function refresh_tree()
end end
end end
init_tree() M.init_tree()
for i, node in pairs(Tree) do for i, node in pairs(Tree) do
if node.dir == true then if node.dir == true then
@@ -127,7 +121,7 @@ local function clone(obj)
return res return res
end end
local function find_file(path) function M.find_file(path)
local relpath = string.sub(path, #ROOT_PATH + 1, -1) local relpath = string.sub(path, #ROOT_PATH + 1, -1)
local tree_copy = clone(Tree) local tree_copy = clone(Tree)
@@ -139,10 +133,10 @@ local function find_file(path)
return i return i
end end
if node.dir and not node.open then if node.dir and not node.open then
local path = node.path .. node.name local dirpath = node.path .. node.name
node.open = true node.open = true
local dirs = list_dirs(path) local dirs = list_dirs(dirpath)
for j, n in pairs(create_nodes(path, node.relpath, node.depth + 1, dirs)) do for j, n in pairs(create_nodes(dirpath, node.relpath, node.depth + 1, dirs)) do
table.insert(tree_copy, i + j, n) table.insert(tree_copy, i + j, n)
end end
end end
@@ -152,7 +146,7 @@ local function find_file(path)
return nil return nil
end end
local function open_dir(tree_index) function M.open_dir(tree_index)
local node = Tree[tree_index]; local node = Tree[tree_index];
node.open = not node.open node.open = not node.open
@@ -165,7 +159,7 @@ local function open_dir(tree_index)
next_node = Tree[next_index] next_node = Tree[next_index]
end end
else else
local dirlist = list_dirs('"' .. node.path .. node.name ..'"') local dirlist = list_dirs(tostring(node.path .. node.name))
local child_dirs = create_nodes(node.path .. node.name .. '/', node.relpath, node.depth + 1, dirlist) local child_dirs = create_nodes(node.path .. node.name .. '/', node.relpath, node.depth + 1, dirlist)
for i, n in pairs(child_dirs) do for i, n in pairs(child_dirs) do
@@ -174,16 +168,8 @@ local function open_dir(tree_index)
end end
end end
local function get_tree() function M.get_tree()
return Tree return Tree
end end
return { return M
init_tree = init_tree;
get_tree = get_tree;
refresh_tree = refresh_tree;
open_dir = open_dir;
set_root_path = set_root_path;
get_cwd = get_cwd;
find_file = find_file;
}

View File

@@ -1,4 +1,3 @@
local BUF_NAME = 'LuaTree'
local api = vim.api local api = vim.api
local libformat = require 'lib/format' local libformat = require 'lib/format'
@@ -10,8 +9,13 @@ local get_tree = stateutils.get_tree
local bindings = require 'lib/config'.bindings local bindings = require 'lib/config'.bindings
local function get_buf() local M = {
local regex = '.*'..BUF_NAME..'$'; BUF_NAME = 'LuaTree'
}
function M.get_buf()
local regex = '.*'..M.BUF_NAME..'$';
for _, win in pairs(api.nvim_list_wins()) do for _, win in pairs(api.nvim_list_wins()) do
local buf = api.nvim_win_get_buf(win) local buf = api.nvim_win_get_buf(win)
@@ -23,8 +27,8 @@ local function get_buf()
return nil return nil
end end
local function get_win() function M.get_win()
local regex = '.*'..BUF_NAME..'$'; local regex = '.*'..M.BUF_NAME..'$';
for _, win in pairs(api.nvim_list_wins()) do for _, win in pairs(api.nvim_list_wins()) do
local buf_name = api.nvim_buf_get_name(api.nvim_win_get_buf(win)) local buf_name = api.nvim_buf_get_name(api.nvim_win_get_buf(win))
@@ -54,7 +58,7 @@ if api.nvim_call_function('exists', { 'g:lua_tree_side' }) == 1 then
end end
end end
local function open() function M.open()
local options = { local options = {
bufhidden = 'wipe'; bufhidden = 'wipe';
buftype = 'nowrite'; buftype = 'nowrite';
@@ -62,7 +66,7 @@ local function open()
} }
local buf = api.nvim_create_buf(false, true) local buf = api.nvim_create_buf(false, true)
api.nvim_buf_set_name(buf, BUF_NAME) api.nvim_buf_set_name(buf, M.BUF_NAME)
for opt, val in pairs(options) do for opt, val in pairs(options) do
api.nvim_buf_set_option(buf, opt, val) api.nvim_buf_set_option(buf, opt, val)
@@ -84,8 +88,8 @@ local function open()
end end
end end
local function replace_tree() function M.replace_tree()
local win = get_win() local win = M.get_win()
if not win then return end if not win then return end
local tree_position = api.nvim_win_get_position(win) local tree_position = api.nvim_win_get_position(win)
@@ -105,15 +109,15 @@ local function replace_tree()
api.nvim_set_current_win(current_win) api.nvim_set_current_win(current_win)
end end
local function close() function M.close()
local win = get_win() local win = M.get_win()
if not win then return end if not win then return end
api.nvim_win_close(win, true) api.nvim_win_close(win, true)
end end
local function update_view(update_cursor) function M.update_view(update_cursor)
local buf = get_buf(); local buf = M.get_buf();
if not buf then return end if not buf then return end
local cursor = api.nvim_win_get_cursor(0) local cursor = api.nvim_win_get_cursor(0)
@@ -129,8 +133,8 @@ local function update_view(update_cursor)
end end
end end
local function set_mappings() function M.set_mappings()
local buf = get_buf() local buf = M.get_buf()
if not buf then return end if not buf then return end
local mappings = { local mappings = {
@@ -153,17 +157,8 @@ local function set_mappings()
end end
end end
local function is_win_open() function M.is_win_open()
return get_buf() ~= nil return M.get_buf() ~= nil
end end
return { return M
open = open;
close = close;
is_win_open = is_win_open;
update_view = update_view;
get_buf = get_buf;
get_win = get_win;
set_mappings = set_mappings;
replace_tree = replace_tree;
}

View File

@@ -24,7 +24,6 @@ local is_win_open = winutils.is_win_open
local close = winutils.close local close = winutils.close
local open = winutils.open local open = winutils.open
local set_mappings = winutils.set_mappings local set_mappings = winutils.set_mappings
local replace_tree = winutils.replace_tree
local get_win = winutils.get_win local get_win = winutils.get_win
local git = require 'lib/git' local git = require 'lib/git'
@@ -33,9 +32,13 @@ local force_refresh_git = git.force_refresh_git
require 'lib/colors'.init_colors() require 'lib/colors'.init_colors()
local M = {}
M.replace_tree = winutils.replace_tree
init_tree() init_tree()
local function toggle() function M.toggle()
if is_win_open() == true then if is_win_open() == true then
local wins = api.nvim_list_wins() local wins = api.nvim_list_wins()
if #wins > 1 then close() end if #wins > 1 then close() end
@@ -64,7 +67,7 @@ local function create_new_buf(open_type, bufname)
end end
end end
local function open_file(open_type) function M.open_file(open_type)
local tree_index = api.nvim_win_get_cursor(0)[1] local tree_index = api.nvim_win_get_cursor(0)[1]
local tree = get_tree() local tree = get_tree()
local node = tree[tree_index] local node = tree[tree_index]
@@ -116,7 +119,7 @@ local function open_file(open_type)
end end
end end
local function edit_file(edit_type) function M.edit_file(edit_type)
local tree = get_tree() local tree = get_tree()
local tree_index = api.nvim_win_get_cursor(0)[1] local tree_index = api.nvim_win_get_cursor(0)[1]
local node = tree[tree_index] local node = tree[tree_index]
@@ -134,14 +137,14 @@ local function edit_file(edit_type)
end end
end end
local function refresh() function M.refresh()
if refresh_git() == true then if refresh_git() == true then
refresh_tree() refresh_tree()
update_view() update_view()
end end
end end
local function check_windows_and_close() function M.check_windows_and_close()
local wins = api.nvim_list_wins() local wins = api.nvim_list_wins()
if #wins == 1 and is_win_open() then if #wins == 1 and is_win_open() then
@@ -149,10 +152,10 @@ local function check_windows_and_close()
end end
end end
local function check_buffer_and_open() function M.check_buffer_and_open()
local bufname = api.nvim_buf_get_name(0) local bufname = api.nvim_buf_get_name(0)
if bufname == '' then if bufname == '' then
toggle() M.toggle()
elseif is_dir(bufname) then elseif is_dir(bufname) then
api.nvim_command('cd ' .. bufname) api.nvim_command('cd ' .. bufname)
@@ -163,11 +166,11 @@ local function check_buffer_and_open()
set_root_path(new_path) set_root_path(new_path)
init_tree() init_tree()
toggle() M.toggle()
end end
end end
local function find() function M.find()
local line = find_file(api.nvim_buf_get_name(0)) local line = find_file(api.nvim_buf_get_name(0))
if not line then return end if not line then return end
@@ -180,14 +183,4 @@ local function find()
end end
return { return M
toggle = toggle;
open_file = open_file;
edit_file = edit_file;
refresh = refresh;
check_windows_and_close = check_windows_and_close;
check_buffer_and_open = check_buffer_and_open;
replace_tree = replace_tree;
find = find;
}