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

@@ -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,161 +32,155 @@ 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()
if is_win_open() == true then
local wins = api.nvim_list_wins()
if #wins > 1 then close() end
else
open()
update_view()
set_mappings()
end
function M.toggle()
if is_win_open() == true then
local wins = api.nvim_list_wins()
if #wins > 1 then close() end
else
open()
update_view()
set_mappings()
end
end
local MOVE_TO = 'l'
if api.nvim_call_function('exists', { 'g:lua_tree_side' }) == 1 then
if api.nvim_get_var('lua_tree_side') == 'right' then
MOVE_TO = 'h'
end
if api.nvim_get_var('lua_tree_side') == 'right' then
MOVE_TO = 'h'
end
end
local function create_new_buf(open_type, bufname)
if open_type == 'edit' or open_type == 'split' then
api.nvim_command('wincmd '..MOVE_TO..' | '..open_type..' '..bufname)
elseif open_type == 'vsplit' then
local windows = api.nvim_list_wins();
api.nvim_command(#windows..'wincmd '..MOVE_TO..' | vsplit '..bufname)
elseif open_type == 'tabnew' then
api.nvim_command('tabnew '..bufname)
end
if open_type == 'edit' or open_type == 'split' then
api.nvim_command('wincmd '..MOVE_TO..' | '..open_type..' '..bufname)
elseif open_type == 'vsplit' then
local windows = api.nvim_list_wins();
api.nvim_command(#windows..'wincmd '..MOVE_TO..' | vsplit '..bufname)
elseif open_type == 'tabnew' then
api.nvim_command('tabnew '..bufname)
end
end
local function open_file(open_type)
local tree_index = api.nvim_win_get_cursor(0)[1]
local tree = get_tree()
local node = tree[tree_index]
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]
if node.name == '..' then
api.nvim_command('cd ..')
if node.name == '..' then
api.nvim_command('cd ..')
local new_path = get_cwd()
if new_path ~= '/' then
new_path = new_path .. '/'
end
set_root_path(new_path)
force_refresh_git()
init_tree(new_path)
update_view()
elseif open_type == 'chdir' then
if node.dir == false or check_dir_access(node.path .. node.name) == false then return end
api.nvim_command('cd ' .. node.path .. node.name)
local new_path = get_cwd() .. '/'
set_root_path(new_path)
force_refresh_git()
init_tree(new_path)
update_view()
elseif node.link == true then
local link_to_dir = is_dir(node.linkto)
if link_to_dir == true and check_dir_access(node.linkto) == false then return end
if link_to_dir == true then
api.nvim_command('cd ' .. node.linkto)
local new_path = get_cwd() .. '/'
set_root_path(new_path)
force_refresh_git()
init_tree(new_path)
update_view()
else
create_new_buf(open_type, node.link_to);
end
elseif node.dir == true then
if check_dir_access(node.path .. node.name) == false then return end
open_dir(tree_index)
update_view(true)
else
create_new_buf(open_type, node.path .. node.name);
local new_path = get_cwd()
if new_path ~= '/' then
new_path = new_path .. '/'
end
end
local function edit_file(edit_type)
local tree = get_tree()
local tree_index = api.nvim_win_get_cursor(0)[1]
local node = tree[tree_index]
if edit_type == 'create' then
if node.dir == true then
create_file(node.path .. node.name .. '/')
else
create_file(node.path)
end
elseif edit_type == 'remove' then
remove_file(node.name, node.path)
elseif edit_type == 'rename' then
rename_file(node.name, node.path)
end
end
local function refresh()
if refresh_git() == true then
refresh_tree()
update_view()
end
end
local function check_windows_and_close()
local wins = api.nvim_list_wins()
if #wins == 1 and is_win_open() then
api.nvim_command('q!')
end
end
local function check_buffer_and_open()
local bufname = api.nvim_buf_get_name(0)
if bufname == '' then
toggle()
elseif is_dir(bufname) then
api.nvim_command('cd ' .. bufname)
local new_path = get_cwd()
if new_path ~= '/' then
new_path = new_path .. '/'
end
set_root_path(new_path)
init_tree()
toggle()
end
end
local function find()
local line = find_file(api.nvim_buf_get_name(0))
if not line then return end
set_root_path(new_path)
force_refresh_git()
init_tree(new_path)
update_view()
local win = get_win()
if win then
api.nvim_win_set_cursor(win, { line, 0 })
elseif open_type == 'chdir' then
if node.dir == false or check_dir_access(node.path .. node.name) == false then return end
api.nvim_command('cd ' .. node.path .. node.name)
local new_path = get_cwd() .. '/'
set_root_path(new_path)
force_refresh_git()
init_tree(new_path)
update_view()
elseif node.link == true then
local link_to_dir = is_dir(node.linkto)
if link_to_dir == true and check_dir_access(node.linkto) == false then return end
if link_to_dir == true then
api.nvim_command('cd ' .. node.linkto)
local new_path = get_cwd() .. '/'
set_root_path(new_path)
force_refresh_git()
init_tree(new_path)
update_view()
else
create_new_buf(open_type, node.link_to);
end
elseif node.dir == true then
if check_dir_access(node.path .. node.name) == false then return end
open_dir(tree_index)
update_view(true)
else
create_new_buf(open_type, node.path .. node.name);
end
end
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]
if edit_type == 'create' then
if node.dir == true then
create_file(node.path .. node.name .. '/')
else
create_file(node.path)
end
elseif edit_type == 'remove' then
remove_file(node.name, node.path)
elseif edit_type == 'rename' then
rename_file(node.name, node.path)
end
end
function M.refresh()
if refresh_git() == true then
refresh_tree()
update_view()
end
end
function M.check_windows_and_close()
local wins = api.nvim_list_wins()
if #wins == 1 and is_win_open() then
api.nvim_command('q!')
end
end
function M.check_buffer_and_open()
local bufname = api.nvim_buf_get_name(0)
if bufname == '' then
M.toggle()
elseif is_dir(bufname) then
api.nvim_command('cd ' .. bufname)
local new_path = get_cwd()
if new_path ~= '/' then
new_path = new_path .. '/'
end
set_root_path(new_path)
init_tree()
M.toggle()
end
end
function M.find()
local line = find_file(api.nvim_buf_get_name(0))
if not line then return end
update_view()
local win = get_win()
if win then
api.nvim_win_set_cursor(win, { line, 0 })
end
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