make code faster and cleaner in file.lua and winutils.lua

This commit is contained in:
kyazdani42 2020-02-19 19:43:03 +01:00
parent bb38758960
commit 3945771fb5
4 changed files with 44 additions and 34 deletions

View File

@ -23,3 +23,4 @@
- open automatically when opening neovim with `nvim` or `nvim .` - open automatically when opening neovim with `nvim` or `nvim .`
- cd command to move faster accross the fs if needed - cd command to move faster accross the fs if needed
- quickly find file in the directory structure - quickly find file in the directory structure
- use libuv functions instead of `touch` and `mkdir` in `add_file()`

View File

@ -1,4 +1,5 @@
local api = vim.api local api = vim.api
local luv = vim.loop
local system = function(v) api.nvim_call_function('system', { v }) end local system = function(v) api.nvim_call_function('system', { v }) end
local update_tree_view = require 'lib/winutils'.update_view local update_tree_view = require 'lib/winutils'.update_view
local scratch_wrapper = require 'lib/winutils'.scratch_wrapper local scratch_wrapper = require 'lib/winutils'.scratch_wrapper
@ -13,7 +14,7 @@ end
local function edit_remove(filename, path) local function edit_remove(filename, path)
EDIT_FILE = path .. filename EDIT_FILE = path .. filename
scratch_wrapper("delete", { "Remove " .. filename .. " ?", "y/n: " }) scratch_wrapper("remove", { "Remove " .. filename .. " ?", "y/n: " })
end end
local function edit_rename(filename, path) local function edit_rename(filename, path)
@ -21,39 +22,47 @@ local function edit_rename(filename, path)
scratch_wrapper("rename", { "Rename " .. path, path .. filename }) scratch_wrapper("rename", { "Rename " .. path, path .. filename })
end end
local function add_file(path) local function add_file()
local path = api.nvim_get_current_line()
if string.match(path, '.*/$') then if string.match(path, '.*/$') then
system('mkdir -p ' .. path) system('mkdir -p ' .. path)
refresh_git() refresh_git()
update_tree_state() update_tree_state()
update_tree_view(true) update_tree_view()
else else
system('touch ' .. path) system('touch ' .. path)
refresh_git() refresh_git()
update_tree_state() update_tree_state()
update_tree_view(true) update_tree_view()
end end
api.nvim_command("q!") api.nvim_command("q!")
end 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 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() refresh_git()
update_tree_state() update_tree_state()
update_tree_view(true) update_tree_view()
end end
EDIT_FILE = nil EDIT_FILE = nil
api.nvim_command("q!") api.nvim_command("q!")
end end
local function rename_file(path) local function rename_file()
system('mv '..EDIT_FILE..' '..path) local path = api.nvim_get_current_line()
EDIT_FILE = nil luv.fs_rename(EDIT_FILE, path)
api.nvim_command("q!")
refresh_git() refresh_git()
update_tree_state() update_tree_state()
update_tree_view(true) update_tree_view()
EDIT_FILE = nil
api.nvim_command("q!")
end end
return { return {

View File

@ -34,11 +34,6 @@ local function get_win()
return nil return nil
end 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() local function scratch_buffer()
scratch_buf = api.nvim_create_buf(false, true) scratch_buf = api.nvim_create_buf(false, true)
api.nvim_buf_set_option(scratch_buf, 'bufhidden', 'wipe') api.nvim_buf_set_option(scratch_buf, 'bufhidden', 'wipe')
@ -91,24 +86,22 @@ local function set_scratch_mappings(edit_type)
local chars = { 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' '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 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, '', options)
api.nvim_buf_set_keymap(scratch_buf, 'n', v:upper(), '', { nowait = true, noremap = true, silent = true }) api.nvim_buf_set_keymap(scratch_buf, 'n', v:upper(), '', options)
api.nvim_buf_set_keymap(scratch_buf, 'n', '<c-'..v..'>', '', { nowait = true, noremap = true, silent = true }) api.nvim_buf_set_keymap(scratch_buf, 'n', '<c-'..v..'>', '', options)
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..'>', '', options)
api.nvim_buf_set_keymap(scratch_buf, 'i', '<c-' ..v:upper()..'>', '', { nowait = true, noremap = true, silent = true }) api.nvim_buf_set_keymap(scratch_buf, 'i', '<c-' ..v:upper()..'>', '', options)
end end
if edit_type == 'add' then api.nvim_buf_set_keymap(scratch_buf, 'i', '<CR>', "<esc>:lua require'lib/file'."..edit_type.."_file()<CR>", options)
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 local ikeys = { '<esc>', '<C-c>', '<C-[' }
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 }) for _, map in pairs(ikeys) do
elseif edit_type == 'delete' then api.nvim_buf_set_keymap(scratch_buf, 'i', map, "<esc>:q!<CR>", options)
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 })
end 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 end
local function update_scratch_view(...) local function update_scratch_view(...)
@ -123,6 +116,11 @@ local function scratch_wrapper(edit_type, ...)
set_scratch_mappings(edit_type) set_scratch_mappings(edit_type)
end end
local BUF_OPTIONS = {
'nonumber', 'norelativenumber', 'winfixwidth', 'winfixheight', 'winhighlight=EndOfBuffer:LuaTreeEndOfBuffer',
}
local function open() local function open()
local win_width = 30 local win_width = 30
local options = { local options = {
@ -140,7 +138,9 @@ local function open()
api.nvim_command('topleft '..win_width..'vnew') api.nvim_command('topleft '..win_width..'vnew')
api.nvim_win_set_buf(0, buf) api.nvim_win_set_buf(0, buf)
buf_setup() for _, opt in pairs(BUF_OPTIONS) do
api.nvim_command('setlocal '..opt)
end
end end
local function close() local function close()
@ -183,7 +183,7 @@ local function set_mappings()
['<C-x>'] = 'open_file("split")'; ['<C-x>'] = 'open_file("split")';
['<C-[>'] = 'open_file("chdir")'; ['<C-[>'] = 'open_file("chdir")';
a = 'edit_file("add")'; a = 'edit_file("add")';
d = 'edit_file("delete")'; d = 'edit_file("remove")';
r = 'edit_file("rename")'; r = 'edit_file("rename")';
} }

View File

@ -105,7 +105,7 @@ local function edit_file(edit_type)
else else
edit_add(node.path) edit_add(node.path)
end end
elseif edit_type == 'delete' then elseif edit_type == 'remove' then
edit_remove(node.name, node.path, node.dir) edit_remove(node.name, node.path, node.dir)
elseif edit_type == 'rename' then elseif edit_type == 'rename' then
edit_rename(node.name, node.path, node.dir) edit_rename(node.name, node.path, node.dir)