diff --git a/README.md b/README.md index e2ec651a..379ad74c 100644 --- a/README.md +++ b/README.md @@ -23,3 +23,4 @@ - open automatically when opening neovim with `nvim` or `nvim .` - cd command to move faster accross the fs if needed - quickly find file in the directory structure +- use libuv functions instead of `touch` and `mkdir` in `add_file()` diff --git a/lua/lib/file.lua b/lua/lib/file.lua index 4e807a7b..d5e4cac0 100644 --- a/lua/lib/file.lua +++ b/lua/lib/file.lua @@ -1,4 +1,5 @@ local api = vim.api +local luv = vim.loop local system = function(v) api.nvim_call_function('system', { v }) end local update_tree_view = require 'lib/winutils'.update_view local scratch_wrapper = require 'lib/winutils'.scratch_wrapper @@ -13,7 +14,7 @@ end local function edit_remove(filename, path) EDIT_FILE = path .. filename - scratch_wrapper("delete", { "Remove " .. filename .. " ?", "y/n: " }) + scratch_wrapper("remove", { "Remove " .. filename .. " ?", "y/n: " }) end local function edit_rename(filename, path) @@ -21,39 +22,47 @@ local function edit_rename(filename, path) scratch_wrapper("rename", { "Rename " .. path, path .. filename }) end -local function add_file(path) +local function add_file() + local path = api.nvim_get_current_line() if string.match(path, '.*/$') then system('mkdir -p ' .. path) refresh_git() update_tree_state() - update_tree_view(true) + update_tree_view() else system('touch ' .. path) refresh_git() update_tree_state() - update_tree_view(true) + update_tree_view() end api.nvim_command("q!") end -local function remove_file(confirmation) +local function remove_file() + local confirmation = api.nvim_get_current_line() if string.match(confirmation, '^y/n: y.*$') ~= nil then - system('rm -rf ' .. EDIT_FILE) + local stat = luv.fs_lstat(EDIT_FILE) + if stat.type == 'directory' then + luv.fs_rmdir(EDIT_FILE) + else + luv.fs_unlink(EDIT_FILE) + end refresh_git() update_tree_state() - update_tree_view(true) + update_tree_view() end EDIT_FILE = nil api.nvim_command("q!") end -local function rename_file(path) - system('mv '..EDIT_FILE..' '..path) - EDIT_FILE = nil - api.nvim_command("q!") +local function rename_file() + local path = api.nvim_get_current_line() + luv.fs_rename(EDIT_FILE, path) refresh_git() update_tree_state() - update_tree_view(true) + update_tree_view() + EDIT_FILE = nil + api.nvim_command("q!") end return { diff --git a/lua/lib/winutils.lua b/lua/lib/winutils.lua index 036ec6fd..4ac67a31 100644 --- a/lua/lib/winutils.lua +++ b/lua/lib/winutils.lua @@ -34,11 +34,6 @@ local function get_win() return nil end -local function buf_setup() - api.nvim_command('setlocal nonumber norelativenumber winfixwidth winfixheight') - api.nvim_command('setlocal winhighlight=EndOfBuffer:LuaTreeEndOfBuffer') -end - local function scratch_buffer() scratch_buf = api.nvim_create_buf(false, true) api.nvim_buf_set_option(scratch_buf, 'bufhidden', 'wipe') @@ -91,24 +86,22 @@ local function set_scratch_mappings(edit_type) local chars = { 'a', 'b', 'd', 'e', 'f', 'h', 'l', 'j', 'q', 'k', 'g', 'i', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' } + local options = { nowait = true, noremap = true, silent = true } + for _,v in ipairs(chars) do - api.nvim_buf_set_keymap(scratch_buf, 'n', v, '', { nowait = true, noremap = true, silent = true }) - api.nvim_buf_set_keymap(scratch_buf, 'n', v:upper(), '', { nowait = true, noremap = true, silent = true }) - api.nvim_buf_set_keymap(scratch_buf, 'n', '', '', { nowait = true, noremap = true, silent = true }) - api.nvim_buf_set_keymap(scratch_buf, 'i', '', '', { nowait = true, noremap = true, silent = true }) - api.nvim_buf_set_keymap(scratch_buf, 'i', '', '', { nowait = true, noremap = true, silent = true }) + api.nvim_buf_set_keymap(scratch_buf, 'n', v, '', options) + api.nvim_buf_set_keymap(scratch_buf, 'n', v:upper(), '', options) + api.nvim_buf_set_keymap(scratch_buf, 'n', '', '', options) + api.nvim_buf_set_keymap(scratch_buf, 'i', '', '', options) + api.nvim_buf_set_keymap(scratch_buf, 'i', '', '', options) end - if edit_type == 'add' then - api.nvim_buf_set_keymap(scratch_buf, 'i', '', ":lua require'lib/file'.add_file(vim.api.nvim_get_current_line())", { nowait = true, noremap = true, silent = true }) - elseif edit_type == 'rename' then - api.nvim_buf_set_keymap(scratch_buf, 'i', '', ":lua require'lib/file'.rename_file(vim.api.nvim_get_current_line())", { nowait = true, noremap = true, silent = true }) - elseif edit_type == 'delete' then - api.nvim_buf_set_keymap(scratch_buf, 'i', '', ":lua require'lib/file'.remove_file(vim.api.nvim_get_current_line())", { nowait = true, noremap = true, silent = true }) + api.nvim_buf_set_keymap(scratch_buf, 'i', '', ":lua require'lib/file'."..edit_type.."_file()", options) + + local ikeys = { '', '', ':q!", options) end - api.nvim_buf_set_keymap(scratch_buf, 'i', '', ":q!", { nowait = true, noremap = true, silent = true }) - api.nvim_buf_set_keymap(scratch_buf, 'i', '', ":q!", { nowait = true, noremap = true, silent = true }) - api.nvim_buf_set_keymap(scratch_buf, 'i', '', ":q!", { nowait = true, noremap = true, silent = true }) end local function update_scratch_view(...) @@ -123,6 +116,11 @@ local function scratch_wrapper(edit_type, ...) set_scratch_mappings(edit_type) end + +local BUF_OPTIONS = { + 'nonumber', 'norelativenumber', 'winfixwidth', 'winfixheight', 'winhighlight=EndOfBuffer:LuaTreeEndOfBuffer', +} + local function open() local win_width = 30 local options = { @@ -140,7 +138,9 @@ local function open() api.nvim_command('topleft '..win_width..'vnew') api.nvim_win_set_buf(0, buf) - buf_setup() + for _, opt in pairs(BUF_OPTIONS) do + api.nvim_command('setlocal '..opt) + end end local function close() @@ -183,7 +183,7 @@ local function set_mappings() [''] = 'open_file("split")'; [''] = 'open_file("chdir")'; a = 'edit_file("add")'; - d = 'edit_file("delete")'; + d = 'edit_file("remove")'; r = 'edit_file("rename")'; } diff --git a/lua/tree.lua b/lua/tree.lua index 150cfbb3..9ed7d004 100644 --- a/lua/tree.lua +++ b/lua/tree.lua @@ -105,7 +105,7 @@ local function edit_file(edit_type) else edit_add(node.path) end - elseif edit_type == 'delete' then + elseif edit_type == 'remove' then edit_remove(node.name, node.path, node.dir) elseif edit_type == 'rename' then edit_rename(node.name, node.path, node.dir)