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 end
---@deprecated ---@deprecated
function M.on_keypress(...) M.on_keypress = require'nvim-tree.actions'.on_keypress
require'nvim-tree.actions'.on_keypress(...)
end
function M.toggle(find_file) function M.toggle(find_file)
if view.win_open() then if view.win_open() then
@@ -37,13 +35,6 @@ function M.toggle(find_file)
end end
end end
function M.close()
if view.win_open() then
view.close()
return true
end
end
function M.open() function M.open()
if not view.win_open() then if not view.win_open() then
lib.open() lib.open()
@@ -64,6 +55,22 @@ function M.tab_change()
end) end)
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() function M.hijack_current_window()
local View = require'nvim-tree.view'.View local View = require'nvim-tree.view'.View
if not View.bufnr then if not View.bufnr then
@@ -77,6 +84,7 @@ function M.hijack_current_window()
else else
View.tabpages[current_tab] = { winnr = api.nvim_get_current_win() } View.tabpages[current_tab] = { winnr = api.nvim_get_current_win() }
end end
vim.schedule(remove_empty_buffer)
end end
function M.on_enter(opts) function M.on_enter(opts)
@@ -237,7 +245,7 @@ end
local function setup_vim_commands() local function setup_vim_commands()
vim.cmd [[ vim.cmd [[
command! NvimTreeOpen lua require'nvim-tree'.open() 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! NvimTreeToggle lua require'nvim-tree'.toggle(false)
command! NvimTreeFocus lua require'nvim-tree'.focus() command! NvimTreeFocus lua require'nvim-tree'.focus()
command! NvimTreeRefresh lua require'nvim-tree.actions.reloaders'.reload_explorer() command! NvimTreeRefresh lua require'nvim-tree.actions.reloaders'.reload_explorer()

View File

@@ -54,40 +54,31 @@ local function go_to(mode)
end end
local keypress_funcs = { local keypress_funcs = {
create = require'nvim-tree.actions.create-file'.fn, close = view.close,
remove = require'nvim-tree.actions.remove-file'.fn, close_node = require'nvim-tree.actions.movements'.parent_node(true),
rename = require'nvim-tree.actions.rename-file'.fn(false), copy_absolute_path = require'nvim-tree.actions.copy-paste'.copy_absolute_path,
full_rename = require'nvim-tree.actions.rename-file'.fn(true),
copy = require'nvim-tree.actions.copy-paste'.copy,
copy_name = require'nvim-tree.actions.copy-paste'.copy_filename, copy_name = require'nvim-tree.actions.copy-paste'.copy_filename,
copy_path = require'nvim-tree.actions.copy-paste'.copy_path, 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, 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, paste = require'nvim-tree.actions.copy-paste'.paste,
close_node = lib.close_node, prev_git_item = go_to('prev_git_item'),
parent_node = require'nvim-tree.actions.movements'.parent_node, prev_sibling = require'nvim-tree.actions.movements'.sibling(-1),
toggle_ignored = lib.toggle_ignored, 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_dotfiles = lib.toggle_dotfiles,
toggle_help = lib.toggle_help, toggle_help = lib.toggle_help,
refresh = require'nvim-tree.actions.reloaders'.reload_explorer, toggle_ignored = lib.toggle_ignored,
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,
trash = require'nvim-tree.actions.trash'.fn, trash = require'nvim-tree.actions.trash'.fn,
} }
@@ -105,7 +96,12 @@ function M.on_keypress(action)
return default_function(node) return default_function(node)
end 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("..") return require'nvim-tree.actions.change-dir'.fn("..")
elseif action == "cd" and node.nodes ~= nil then elseif action == "cd" and node.nodes ~= nil then
return require'nvim-tree.actions.change-dir'.fn(lib.get_last_group_node(node).absolute_path) 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 end
function M.parent_node(node, should_close) function M.parent_node(should_close)
if node.name == '..' then return end return function(node)
if node.name == '..' then return end
should_close = should_close or false should_close = should_close or false
local altered_tree = false local altered_tree = false
local iter = get_line_from_node(node, true) local iter = get_line_from_node(node, true)
if node.open == true and should_close then if node.open == true and should_close then
node.open = false 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 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 end
line = view.View.hide_root_folder and line - 1 or line
view.set_cursor({line, 0})
end
if altered_tree then if altered_tree then
diagnostics.update() diagnostics.update()
lib().redraw() lib().redraw()
end
end end
end end

View File

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