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:
37
lua/nvim-tree/actions/change-dir.lua
Normal file
37
lua/nvim-tree/actions/change-dir.lua
Normal 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
|
||||
19
lua/nvim-tree/actions/collapse-all.lua
Normal file
19
lua/nvim-tree/actions/collapse-all.lua
Normal 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
|
||||
13
lua/nvim-tree/actions/dir-up.lua
Normal file
13
lua/nvim-tree/actions/dir-up.lua
Normal 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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user