refacto: sort actions declaration, remove close from main file

fixes #921
- use view.close instead of nvim-tree.close
- put preview code inside the execution logic on keypress
- sort keys in keypress_funcs
- remove empty buffer when hijacking window
This commit is contained in:
kiyan
2022-02-06 19:25:23 +01:00
parent 0573c68fd7
commit 923e034668
4 changed files with 66 additions and 64 deletions

View File

@@ -20,9 +20,7 @@ function M.focus()
end
---@deprecated
function M.on_keypress(...)
require'nvim-tree.actions'.on_keypress(...)
end
M.on_keypress = require'nvim-tree.actions'.on_keypress
function M.toggle(find_file)
if view.win_open() then
@@ -37,13 +35,6 @@ function M.toggle(find_file)
end
end
function M.close()
if view.win_open() then
view.close()
return true
end
end
function M.open()
if not view.win_open() then
lib.open()
@@ -64,6 +55,22 @@ function M.tab_change()
end)
end
local function remove_empty_buffer()
if not view.win_open() or #api.nvim_list_wins() ~= 1 then
return
end
local bufs = vim.api.nvim_list_bufs()
for _, buf in ipairs(bufs) do
if api.nvim_buf_is_valid(buf) and api.nvim_buf_is_loaded(buf) then
local name = api.nvim_buf_get_name(buf)
if name == "" then
return api.nvim_buf_delete(buf, {})
end
end
end
end
function M.hijack_current_window()
local View = require'nvim-tree.view'.View
if not View.bufnr then
@@ -77,6 +84,7 @@ function M.hijack_current_window()
else
View.tabpages[current_tab] = { winnr = api.nvim_get_current_win() }
end
vim.schedule(remove_empty_buffer)
end
function M.on_enter(opts)
@@ -237,7 +245,7 @@ end
local function setup_vim_commands()
vim.cmd [[
command! NvimTreeOpen lua require'nvim-tree'.open()
command! NvimTreeClose lua require'nvim-tree'.close()
command! NvimTreeClose lua require'nvim-tree.view'.close()
command! NvimTreeToggle lua require'nvim-tree'.toggle(false)
command! NvimTreeFocus lua require'nvim-tree'.focus()
command! NvimTreeRefresh lua require'nvim-tree.actions.reloaders'.reload_explorer()

View File

@@ -54,40 +54,31 @@ local function go_to(mode)
end
local keypress_funcs = {
create = require'nvim-tree.actions.create-file'.fn,
remove = require'nvim-tree.actions.remove-file'.fn,
rename = require'nvim-tree.actions.rename-file'.fn(false),
full_rename = require'nvim-tree.actions.rename-file'.fn(true),
copy = require'nvim-tree.actions.copy-paste'.copy,
close = view.close,
close_node = require'nvim-tree.actions.movements'.parent_node(true),
copy_absolute_path = require'nvim-tree.actions.copy-paste'.copy_absolute_path,
copy_name = require'nvim-tree.actions.copy-paste'.copy_filename,
copy_path = require'nvim-tree.actions.copy-paste'.copy_path,
copy_absolute_path = require'nvim-tree.actions.copy-paste'.copy_absolute_path,
copy = require'nvim-tree.actions.copy-paste'.copy,
create = require'nvim-tree.actions.create-file'.fn,
cut = require'nvim-tree.actions.copy-paste'.cut,
dir_up = require'nvim-tree.actions.dir-up'.fn,
first_sibling = require'nvim-tree.actions.movements'.sibling(-math.huge),
full_rename = require'nvim-tree.actions.rename-file'.fn(true),
last_sibling = require'nvim-tree.actions.movements'.sibling(math.huge),
next_git_item = go_to('next_git_item'),
next_sibling = require'nvim-tree.actions.movements'.sibling(1),
parent_node = require'nvim-tree.actions.movements'.parent_node(false),
paste = require'nvim-tree.actions.copy-paste'.paste,
close_node = lib.close_node,
parent_node = require'nvim-tree.actions.movements'.parent_node,
toggle_ignored = lib.toggle_ignored,
prev_git_item = go_to('prev_git_item'),
prev_sibling = require'nvim-tree.actions.movements'.sibling(-1),
refresh = require'nvim-tree.actions.reloaders'.reload_explorer,
remove = require'nvim-tree.actions.remove-file'.fn,
rename = require'nvim-tree.actions.rename-file'.fn(false),
system_open = require'nvim-tree.actions.system-open'.fn,
toggle_dotfiles = lib.toggle_dotfiles,
toggle_help = lib.toggle_help,
refresh = require'nvim-tree.actions.reloaders'.reload_explorer,
first_sibling = require'nvim-tree.actions.movements'.sibling(-math.huge),
last_sibling = require'nvim-tree.actions.movements'.sibling(math.huge),
prev_sibling = require'nvim-tree.actions.movements'.sibling(-1),
next_sibling = require'nvim-tree.actions.movements'.sibling(1),
prev_git_item = go_to('prev_git_item'),
next_git_item = go_to('next_git_item'),
dir_up = require'nvim-tree.actions.dir-up'.fn,
close = function() require'nvim-tree'.close() end,
preview = function(node)
if node.name == '..' then
return
end
if node.nodes ~= nil then
return lib.expand_or_collapse(node)
end
return require'nvim-tree.actions.open-file'.fn('preview', node.absolute_path)
end,
system_open = require'nvim-tree.actions.system-open'.fn,
toggle_ignored = lib.toggle_ignored,
trash = require'nvim-tree.actions.trash'.fn,
}
@@ -105,7 +96,12 @@ function M.on_keypress(action)
return default_function(node)
end
if node.name == ".." then
if action == "preview" then
if node.name == '..' then return end
if not node.nodes then
return require'nvim-tree.actions.open-file'.fn('preview', node.absolute_path)
end
elseif node.name == ".." then
return require'nvim-tree.actions.change-dir'.fn("..")
elseif action == "cd" and node.nodes ~= nil then
return require'nvim-tree.actions.change-dir'.fn(lib.get_last_group_node(node).absolute_path)

View File

@@ -31,31 +31,33 @@ local function get_line_from_node(node, find_parent)
end
function M.parent_node(node, should_close)
if node.name == '..' then return end
function M.parent_node(should_close)
return function(node)
if node.name == '..' then return end
should_close = should_close or false
local altered_tree = false
should_close = should_close or false
local altered_tree = false
local iter = get_line_from_node(node, true)
if node.open == true and should_close then
node.open = false
altered_tree = true
else
local line, parent = iter(lib().Tree.nodes, true)
if parent == nil then
line = 1
elseif should_close then
parent.open = false
local iter = get_line_from_node(node, true)
if node.open == true and should_close then
node.open = false
altered_tree = true
else
local line, parent = iter(lib().Tree.nodes, true)
if parent == nil then
line = 1
elseif should_close then
parent.open = false
altered_tree = true
end
line = view.View.hide_root_folder and line - 1 or line
view.set_cursor({line, 0})
end
line = view.View.hide_root_folder and line - 1 or line
view.set_cursor({line, 0})
end
if altered_tree then
diagnostics.update()
lib().redraw()
if altered_tree then
diagnostics.update()
lib().redraw()
end
end
end

View File

@@ -119,10 +119,6 @@ function M.open()
end
end
function M.close_node(node)
require'nvim-tree.actions.movements'.parent_node(node, true)
end
function M.toggle_ignored()
local config = require"nvim-tree.explorer.utils".config
config.filter_ignored = not config.filter_ignored