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 cmd = vim.api.nvim_command
local M = {}
local HIGHLIGHTS = {
Symlink = { gui = 'bold', fg = colors.cyan },
@@ -43,17 +44,15 @@ local LINKS = {
CursorColumn = 'CursorColumn'
}
local function init_colors()
function M.init_colors()
for k, d in pairs(HIGHLIGHTS) do
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
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
return {
init_colors = init_colors
}
return M

View File

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

View File

@@ -1,6 +1,7 @@
local api = vim.api
local config = require 'lib/config'
local api = vim.api
local M = {}
local function get_padding(depth)
local str = ""
@@ -36,13 +37,13 @@ local is_special = create_matcher({
'readme',
'Makefile',
'Cargo%.toml',
})
})
local is_pic = create_matcher({
'%.jpe?g$',
'%.png',
'%.gif'
})
})
local function is_executable(name)
return api.nvim_call_function('executable', { name }) == 1
@@ -68,7 +69,7 @@ end
local get_icon = get_icon_func_gen()
local function format_tree(tree)
function M.format_tree(tree)
local dirs = {}
for i, node in pairs(tree) do
@@ -113,7 +114,7 @@ local HIGHLIGHT_ICON_GROUPS = {
local function highlight_line(buffer)
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
return function(line, node)
local text_start = node.depth * 2
@@ -172,14 +173,11 @@ local function highlight_line(buffer)
end
end
local function highlight_buffer(buffer, tree)
function M.highlight_buffer(buffer, tree)
local highlight = highlight_line(buffer)
for i, node in pairs(tree) do
highlight(i - 1, node)
end
end
return {
format_tree = format_tree;
highlight_buffer = highlight_buffer;
}
return M

View File

@@ -1,63 +1,77 @@
local api = vim.api
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)
return stat and stat.type == 'directory' or false
end
local function is_symlink(path)
function M.is_symlink(path)
local stat = luv.fs_lstat(path)
return stat and stat.type == 'link' or false
end
local function link_to(path)
function M.link_to(path)
return luv.fs_readlink(path) or ''
end
local function print_err(err)
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)
function M.check_dir_access(path)
if luv.fs_access(path, 'R') == true then
return true
else
print_err('Permission denied: ' .. path)
api.nvim_err_writeln('Permission denied: ' .. path)
return false
end
end
-- TODO: better handling of path removal, rename and file creation with luv calls
-- it will take some time so leave it for a dedicated PR
local function rm(path)
system('rm -rf ' ..path)
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)
end
cb()
end))
end
local function rename(file, new_path)
system('mv '..file..' '..new_path)
function M.rm(path, cb)
local opt = { cmd='rm', args = {'-rf', path } };
run_process(opt, 'Error removing '..path, cb)
end
local function create(path, file, folders)
if folders ~= "" then system('mkdir -p '..path..folders) end
if file ~= nil then system('touch '..path..folders..file) 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
return {
check_dir_access = check_dir_access;
is_symlink = is_symlink;
link_to = link_to;
get_cwd = get_cwd;
is_dir = is_dir;
rename = rename;
rm = rm;
create = create;
}
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} }
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()
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 fs = require 'lib/fs'
local update_view = require 'lib/winutils'.update_view
local refresh_tree = require 'lib/state'.refresh_tree
local refresh_git = require 'lib/git'.refresh_git
local utils = require'lib/utils'
local fs = require 'lib/fs'
local rm = fs.rm
local rename = fs.rename
local create = fs.create
local M = {}
local function input(v)
local param
@@ -20,7 +17,7 @@ local function clear_prompt()
api.nvim_command('normal :<esc>')
end
local function create_file(path)
function M.create_file(path)
local new_file = input("Create file: " .. path)
local file = nil
@@ -39,34 +36,33 @@ local function create_file(path)
end
clear_prompt()
create(path, file, folders)
fs.create(path, file, folders, function()
refresh_git()
refresh_tree()
update_view()
end)
end
local function remove_file(filename, path)
function M.remove_file(filename, path)
local ans = input("Remove " .. filename .. " ? y/n: ")
clear_prompt()
if ans == "y" then
rm(path .. filename)
fs.rm(path .. filename, function()
refresh_git()
refresh_tree()
update_view()
end)
end
end
local function rename_file(filename, path)
function M.rename_file(filename, path)
local new_path = input({"Rename file " .. filename .. ": ", path .. filename})
clear_prompt()
rename(path .. filename, new_path)
fs.rename(path .. filename, new_path, function()
refresh_git()
refresh_tree()
update_view()
end)
end
return {
create_file = create_file;
remove_file = remove_file;
rename_file = rename_file;
}
return M

View File

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

View File

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

View File

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

View File

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