make code faster and cleaner in file.lua and winutils.lua
This commit is contained in:
parent
bb38758960
commit
3945771fb5
@ -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()`
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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', '<c-'..v..'>', '', { nowait = true, noremap = true, silent = true })
|
||||
api.nvim_buf_set_keymap(scratch_buf, 'i', '<c-'..v..'>', '', { nowait = true, noremap = true, silent = true })
|
||||
api.nvim_buf_set_keymap(scratch_buf, 'i', '<c-' ..v:upper()..'>', '', { 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', '<c-'..v..'>', '', options)
|
||||
api.nvim_buf_set_keymap(scratch_buf, 'i', '<c-'..v..'>', '', options)
|
||||
api.nvim_buf_set_keymap(scratch_buf, 'i', '<c-' ..v:upper()..'>', '', options)
|
||||
end
|
||||
|
||||
if edit_type == 'add' then
|
||||
api.nvim_buf_set_keymap(scratch_buf, 'i', '<CR>', "<esc>:lua require'lib/file'.add_file(vim.api.nvim_get_current_line())<CR>", { nowait = true, noremap = true, silent = true })
|
||||
elseif edit_type == 'rename' then
|
||||
api.nvim_buf_set_keymap(scratch_buf, 'i', '<CR>', "<esc>:lua require'lib/file'.rename_file(vim.api.nvim_get_current_line())<CR>", { nowait = true, noremap = true, silent = true })
|
||||
elseif edit_type == 'delete' then
|
||||
api.nvim_buf_set_keymap(scratch_buf, 'i', '<CR>', "<esc>:lua require'lib/file'.remove_file(vim.api.nvim_get_current_line())<CR>", { nowait = true, noremap = true, silent = true })
|
||||
api.nvim_buf_set_keymap(scratch_buf, 'i', '<CR>', "<esc>:lua require'lib/file'."..edit_type.."_file()<CR>", options)
|
||||
|
||||
local ikeys = { '<esc>', '<C-c>', '<C-[' }
|
||||
for _, map in pairs(ikeys) do
|
||||
api.nvim_buf_set_keymap(scratch_buf, 'i', map, "<esc>:q!<CR>", options)
|
||||
end
|
||||
api.nvim_buf_set_keymap(scratch_buf, 'i', '<esc>', "<esc>:q!<CR>", { nowait = true, noremap = true, silent = true })
|
||||
api.nvim_buf_set_keymap(scratch_buf, 'i', '<C-c>', "<esc>:q!<CR>", { nowait = true, noremap = true, silent = true })
|
||||
api.nvim_buf_set_keymap(scratch_buf, 'i', '<C-[>', "<esc>:q!<CR>", { 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()
|
||||
['<C-x>'] = 'open_file("split")';
|
||||
['<C-[>'] = 'open_file("chdir")';
|
||||
a = 'edit_file("add")';
|
||||
d = 'edit_file("delete")';
|
||||
d = 'edit_file("remove")';
|
||||
r = 'edit_file("rename")';
|
||||
}
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user