nvim-tree.lua/lua/nvim-tree/actions/tree/toggle.lua
Alexander Courtis a9156c0139
refactor(#2826): remove view debug/cc, enable new codepaths for get_winid and get_bufnr (#3169)
* refactor(#2826): fuller error messages

* refactor(#2826): winnr->winid in view/globals, remove redundant get_winid and get_bufnr calls

* refactor(#2826): winnr->winid consistently

* refactor(#2826): consistent use of buffer registry, tidy, add todos

* refactor(#2826): remove unnecessary view members float, hide_root_folder; use explorer opts

* refactor(#2826): remove unused view members centralize_selection and preserve_window_proportions

* refactor(#2826): remove unused view member height

* refactor(#2826): temporarily reuse BUFNR_PER_TAB in view constructor

* refactor(#2826): get_winid returns new after consistency check

* refactor(#2826): globals.TABPAGES -> WINID_PER_TAB

* refactor(#2826): consistent naming of tabid

* refactor(#2826): more consistency checking

* refactor(#2826): more consistency checking

* refactor(#2826): move global CURSORS to view member

* Revert "refactor(#2826): move global CURSORS to view member"

This reverts commit d84dfad1c3.

* refactor(#2826): move global CURSORS to view member

* refactor(#2826): consistency check returns new

* refactor(#2826): remove consistency checks, enabling new path for view get_winid and get_bufnr

* refactor(#2826): restore CURSORS global
2025-07-28 12:44:17 +10:00

79 lines
1.8 KiB
Lua

local core = require("nvim-tree.core")
local lib = require("nvim-tree.lib")
local finders_find_file = require("nvim-tree.actions.finders.find-file")
local M = {}
---Toggle the tree.
---@param opts ApiTreeToggleOpts|nil|boolean legacy -> opts.find_file
---@param no_focus string|nil legacy -> opts.focus
---@param cwd boolean|nil legacy -> opts.path
---@param bang boolean|nil legacy -> opts.update_root
function M.fn(opts, no_focus, cwd, bang)
local explorer = core.get_explorer()
-- legacy arguments
if type(opts) == "boolean" then
opts = {
find_file = opts,
}
if type(cwd) == "string" then
opts.path = cwd
end
if type(no_focus) == "boolean" then
opts.focus = not no_focus
end
if type(bang) == "boolean" then
opts.update_root = bang
end
end
opts = opts or {}
-- defaults
if opts.focus == nil then
opts.focus = true
end
local previous_buf = vim.api.nvim_get_current_buf()
local previous_path = vim.api.nvim_buf_get_name(previous_buf)
-- sanitise path
if type(opts.path) ~= "string" or vim.fn.isdirectory(opts.path) == 0 then
opts.path = nil
end
if explorer and explorer.view:is_visible() then
-- close
explorer.view:close()
else
-- open
lib.open({
path = opts.path,
current_window = opts.current_window,
winid = opts.winid,
})
-- find file
if M.config.update_focused_file.enable or opts.find_file then
-- update root
if opts.update_root then
require("nvim-tree").change_root(previous_path, previous_buf)
end
-- find
finders_find_file.fn(previous_path)
end
-- restore focus
if not opts.focus then
vim.cmd("noautocmd wincmd p")
end
end
end
function M.setup(opts)
M.config = opts or {}
end
return M