refacto: move some code into actions

(non breaking, old assignments link to new assignments)
- move lib.collapse-all into actions/collapse-all
- move lib.dir-up into actions/dir-up
- move lib.change-dir into actions/change-dir
- use setup option for change-dir global (and use the old option for
  setup)
This commit is contained in:
kiyan
2022-02-05 17:42:40 +01:00
parent 95e3aacc01
commit f74dd24c58
8 changed files with 107 additions and 57 deletions

View File

@@ -0,0 +1,37 @@
local a = vim.api
local lib = function() return require'nvim-tree.lib' end
local M = {
current_tab = a.nvim_get_current_tabpage(),
options = {
global = false,
}
}
function M.fn(name)
local foldername = name == '..' and vim.fn.fnamemodify(lib().Tree.cwd, ':h') or name
local no_cwd_change = vim.fn.expand(foldername) == lib().Tree.cwd
local new_tab = a.nvim_get_current_tabpage()
local is_window = vim.v.event.scope == "window" and new_tab == M.current_tab
if no_cwd_change or is_window then
return
end
M.current_tab = new_tab
if M.options.global then
vim.cmd('cd '..vim.fn.fnameescape(foldername))
else
vim.cmd('lcd '..vim.fn.fnameescape(foldername))
end
lib().init(false, foldername)
end
function M.setup(options)
if options.actions.change_dir.global ~= nil then
M.options.global = options.actions.change_dir.global
else
M.options.global = vim.g.nvim_tree_change_dir_global == 1
end
end
return M

View File

@@ -0,0 +1,19 @@
local M = {}
function M.fn()
local function iter(nodes)
for _, node in pairs(nodes) do
if node.open then
node.open = false
end
if node.entries then
iter(node.entries)
end
end
end
iter(require'nvim-tree.lib'.Tree.entries)
require'nvim-tree.lib'.redraw()
end
return M

View File

@@ -0,0 +1,13 @@
local M = {}
function M.fn(node)
if not node or node.name == ".." then
return require'nvim-tree.actions.change-dir'.fn('..')
else
local newdir = vim.fn.fnamemodify(require'nvim-tree.lib'.Tree.cwd, ':h')
require'nvim-tree.actions.change-dir'.fn(newdir)
return require'nvim-tree.lib'.set_index_and_redraw(node.absolute_path)
end
end
return M

View File

@@ -76,7 +76,7 @@ local keypress_funcs = {
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 = lib.dir_up,
dir_up = require'nvim-tree.actions.dir-up'.fn,
close = function() require'nvim-tree'.close() end,
preview = function(node)
if node.name == '..' then
@@ -106,9 +106,9 @@ function M.on_keypress(action)
end
if node.name == ".." then
return lib.change_dir("..")
return require'nvim-tree.actions.change-dir'.fn("..")
elseif action == "cd" and node.entries ~= nil then
return lib.change_dir(lib.get_last_group_node(node).absolute_path)
return require'nvim-tree.actions.change-dir'.fn(lib.get_last_group_node(node).absolute_path)
elseif action == "cd" then
return
end