doc: clarify open/toggle defaults, more robust legacy argument handling

This commit is contained in:
Alexander Courtis
2023-01-30 11:49:21 +11:00
parent fb775b3353
commit 9fcd50d3e1
3 changed files with 31 additions and 20 deletions

View File

@@ -1251,24 +1251,24 @@ api.tree.open({opts}) *nvim-tree.api.tree.open()*
Open the tree, focusing it if already open. Open the tree, focusing it if already open.
Parameters: ~ Parameters: ~
• {opts} (table) optional parameters • {opts} (table) optional parameters with boolean defaults
Options: ~ Options: ~
• {path} (string) root directory for the tree • {path} (string) root directory for the tree
• {current_window} (boolean) open the tree in the current window • {current_window} (boolean, false) open the tree in the current window
api.tree.toggle({opts}) *nvim-tree.api.tree.toggle()* api.tree.toggle({opts}) *nvim-tree.api.tree.toggle()*
Open or close the tree. Open or close the tree.
Parameters: ~ Parameters: ~
• {opts} (table) optional parameters • {opts} (table) optional parameters with boolean defaults
Options: ~ Options: ~
• {path} (string) root directory for the tree • {path} (string) root directory for the tree
• {current_window} (boolean) open the tree in the current window • {current_window} (boolean, false) open the tree in the current window
• {focus} (boolean) focus the tree when opening • {focus} (boolean, true) focus the tree when opening
• {find_file} (boolean) find the current buffer • {find_file} (boolean, false) find the current buffer
• {update_root} (boolean) see |nvim-tree.update_focused_file.update_root| • {update_root} (boolean, false) see |nvim-tree.update_focused_file.update_root|
api.tree.close() *nvim-tree.api.tree.close()* api.tree.close() *nvim-tree.api.tree.close()*
Close the tree, affecting all tabs as per |nvim-tree.tab.sync.close| Close the tree, affecting all tabs as per |nvim-tree.tab.sync.close|

View File

@@ -75,7 +75,7 @@ M.on_keypress = require("nvim-tree.actions.dispatch").dispatch
---@param opts ApiTreeOpenOpts|nil|string ---@param opts ApiTreeOpenOpts|nil|string
function M.open(opts) function M.open(opts)
-- legacy arguments -- legacy arguments
if type(opts) ~= "table" then if type(opts) == "string" then
opts = { opts = {
path = opts, path = opts,
} }
@@ -184,17 +184,28 @@ end
---@param opts ApiTreeToggleOpts|nil|boolean ---@param opts ApiTreeToggleOpts|nil|boolean
function M.toggle(opts, no_focus, cwd, bang) function M.toggle(opts, no_focus, cwd, bang)
-- legacy arguments -- legacy arguments
if type(opts) ~= "table" then if type(opts) == "boolean" then
opts = { opts = {
path = cwd, find_file = opts
focus = not no_focus,
find_file = opts,
update_root = bang,
} }
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 end
opts = opts or {} opts = opts or {}
-- defaults
if opts.focus == nil then
opts.focus = true
end
-- sanitise path -- sanitise path
if type(opts.path) ~= "string" or vim.fn.isdirectory(opts.path) == 0 then if type(opts.path) ~= "string" or vim.fn.isdirectory(opts.path) == 0 then
opts.path = nil opts.path = nil

View File

@@ -18,16 +18,16 @@ end
---@class ApiTreeOpenOpts ---@class ApiTreeOpenOpts
---@field path string|nil path ---@field path string|nil path
---@field current_window boolean|nil ---@field current_window boolean|nil default false
Api.tree.open = require("nvim-tree").open Api.tree.open = require("nvim-tree").open
---@class ApiTreeToggleOpts ---@class ApiTreeToggleOpts
---@field path string|nil ---@field path string|nil
---@field current_window boolean|nil ---@field current_window boolean|nil default false
---@field focus boolean|nil ---@field focus boolean|nil default true
---@field find_file boolean|nil ---@field find_file boolean|nil default false
---@field update_root boolean|nil ---@field update_root boolean|nil default false
Api.tree.toggle = require("nvim-tree").toggle Api.tree.toggle = require("nvim-tree").toggle