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

@ -96,6 +96,11 @@ require'nvim-tree'.setup {
trash = { trash = {
cmd = "trash", cmd = "trash",
require_confirm = true require_confirm = true
},
actions = {
change_dir = {
global = false,
}
} }
} }
``` ```
@ -110,7 +115,6 @@ let g:nvim_tree_highlight_opened_files = 1 "0 by default, will enable folder and
let g:nvim_tree_root_folder_modifier = ':~' "This is the default. See :help filename-modifiers for more options let g:nvim_tree_root_folder_modifier = ':~' "This is the default. See :help filename-modifiers for more options
let g:nvim_tree_add_trailing = 1 "0 by default, append a trailing slash to folder names let g:nvim_tree_add_trailing = 1 "0 by default, append a trailing slash to folder names
let g:nvim_tree_group_empty = 1 " 0 by default, compact folders that only contain a single folder into one node in the file tree let g:nvim_tree_group_empty = 1 " 0 by default, compact folders that only contain a single folder into one node in the file tree
let g:nvim_tree_change_dir_global = 1 "0 by default, use :cd when changing directories.
let g:nvim_tree_disable_window_picker = 1 "0 by default, will disable the window picker. let g:nvim_tree_disable_window_picker = 1 "0 by default, will disable the window picker.
let g:nvim_tree_icon_padding = ' ' "one space by default, used for rendering the space between the icon and the filename. Use with caution, it could break rendering if you set an empty string depending on your font. let g:nvim_tree_icon_padding = ' ' "one space by default, used for rendering the space between the icon and the filename. Use with caution, it could break rendering if you set an empty string depending on your font.
let g:nvim_tree_symlink_arrow = ' >> ' " defaults to ' ➛ '. used as a separator between symlinks' source and target. let g:nvim_tree_symlink_arrow = ' >> ' " defaults to ' ➛ '. used as a separator between symlinks' source and target.

View File

@ -126,6 +126,11 @@ function.
trash = { trash = {
cmd = "trash", cmd = "trash",
require_confirm = true, require_confirm = true,
},
actions = {
change_dir = {
global = false,
}
} }
} }
< <
@ -354,6 +359,15 @@ Here is a list of the options available in the setup call:
type: `boolean` type: `boolean`
default: `true` default: `true`
*nvim-tree.actions*
|actions|: configuration for various actions
- |actions.change_dir.global|: use `:cd` instead of `:lcd` when changing
directories. Consider that this might cause issues with
the |update_cwd| options.
type: `boolean`
default: `false`
============================================================================== ==============================================================================
OPTIONS *nvim-tree-options* OPTIONS *nvim-tree-options*
@ -485,10 +499,6 @@ default table is
["readme.md"] = true, ["readme.md"] = true,
} }
|g:nvim_tree_change_dir_global| *g:nvim_tree_change_dir_global*
Can be 0 or 1. When 1, use :cd instead of :lcd when changing directories.
|g:nvim_tree_disable_window_picker| *g:nvim_tree_disable_window_picker* |g:nvim_tree_disable_window_picker| *g:nvim_tree_disable_window_picker*
Can be 0 or 1. When 1, will disable the window picker. Files will open in the Can be 0 or 1. When 1, will disable the window picker. Files will open in the

View File

@ -6,6 +6,7 @@ local colors = require'nvim-tree.colors'
local renderer = require'nvim-tree.renderer' local renderer = require'nvim-tree.renderer'
local view = require'nvim-tree.view' local view = require'nvim-tree.view'
local utils = require'nvim-tree.utils' local utils = require'nvim-tree.utils'
local ChangeDir = require'nvim-tree.actions.change-dir'
local _config = {} local _config = {}
@ -124,7 +125,7 @@ local function update_base_dir_with_filepath(filepath, bufnr)
end end
if not vim.startswith(filepath, lib.Tree.cwd or vim.loop.cwd()) then if not vim.startswith(filepath, lib.Tree.cwd or vim.loop.cwd()) then
lib.change_dir(vim.fn.fnamemodify(filepath, ':p:h')) ChangeDir.fn(vim.fn.fnamemodify(filepath, ':p:h'))
end end
end end
@ -183,7 +184,7 @@ function M.open_on_directory()
view.close() view.close()
if bufname ~= lib.Tree.cwd then if bufname ~= lib.Tree.cwd then
lib.change_dir(bufname) ChangeDir.fn(bufname)
end end
M.hijack_current_window() M.hijack_current_window()
@ -247,7 +248,7 @@ local function setup_vim_commands()
end end
function M.change_dir(name) function M.change_dir(name)
lib.change_dir(name) ChangeDir.fn(name)
if _config.update_focused_file.enable then if _config.update_focused_file.enable then
M.find_file(false) M.find_file(false)
@ -326,6 +327,11 @@ local DEFAULT_OPTS = {
enable = true, enable = true,
ignore = true, ignore = true,
timeout = 400, timeout = 400,
},
actions = {
change_dir = {
global = false,
}
} }
} }

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), next_sibling = require'nvim-tree.actions.movements'.sibling(1),
prev_git_item = go_to('prev_git_item'), prev_git_item = go_to('prev_git_item'),
next_git_item = go_to('next_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, close = function() require'nvim-tree'.close() end,
preview = function(node) preview = function(node)
if node.name == '..' then if node.name == '..' then
@ -106,9 +106,9 @@ function M.on_keypress(action)
end end
if node.name == ".." then if node.name == ".." then
return lib.change_dir("..") return require'nvim-tree.actions.change-dir'.fn("..")
elseif action == "cd" and node.entries ~= nil then 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 elseif action == "cd" then
return return
end end

View File

@ -222,42 +222,6 @@ function M.set_index_and_redraw(fname)
end end
end end
function M.collapse_all()
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(M.Tree.entries)
M.redraw()
end
local current_tab = api.nvim_get_current_tabpage()
function M.change_dir(name)
local foldername = name == '..' and vim.fn.fnamemodify(M.Tree.cwd, ':h') or name
local no_cwd_change = vim.fn.expand(foldername) == M.Tree.cwd
local new_tab = api.nvim_get_current_tabpage()
local is_window = vim.v.event.scope == "window" and new_tab == current_tab
if no_cwd_change or is_window then
return
end
current_tab = new_tab
if vim.g.nvim_tree_change_dir_global == 1 then
vim.cmd('cd '..vim.fn.fnameescape(foldername))
else
vim.cmd('lcd '..vim.fn.fnameescape(foldername))
end
M.init(false, foldername)
end
function M.set_target_win() function M.set_target_win()
local id = api.nvim_get_current_win() local id = api.nvim_get_current_win()
local tree_id = view.get_winnr() local tree_id = view.get_winnr()
@ -277,7 +241,7 @@ function M.open()
local respect_buf_cwd = vim.g.nvim_tree_respect_buf_cwd or 0 local respect_buf_cwd = vim.g.nvim_tree_respect_buf_cwd or 0
if respect_buf_cwd == 1 and cwd ~= M.Tree.cwd then if respect_buf_cwd == 1 and cwd ~= M.Tree.cwd then
M.change_dir(cwd) require'nvim-tree.actions.change-dir'.fn(cwd)
end end
if should_redraw then if should_redraw then
M.redraw() M.redraw()
@ -303,14 +267,11 @@ function M.toggle_help()
return M.redraw() return M.redraw()
end end
function M.dir_up(node) -- @deprecated: use nvim-tree.actions.collapse-all.fn
if not node or node.name == ".." then M.collapse_all = require'nvim-tree.actions.collapse-all'.fn
return M.change_dir('..') -- @deprecated: use nvim-tree.actions.dir-up.fn
else M.dir_up = require'nvim-tree.actions.dir-up'.fn
local newdir = vim.fn.fnamemodify(M.Tree.cwd, ':h') -- @deprecated: use nvim-tree.actions.change-dir.fn
M.change_dir(newdir) M.change_dir = require'nvim-tree.actions.change-dir'.fn
return M.set_index_and_redraw(node.absolute_path)
end
end
return M return M