Skip to content

Commit 9fcd50d

Browse files
committed
doc: clarify open/toggle defaults, more robust legacy argument handling
1 parent fb775b3 commit 9fcd50d

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

doc/nvim-tree-lua.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,24 +1251,24 @@ api.tree.open({opts}) *nvim-tree.api.tree.open()*
12511251
Open the tree, focusing it if already open.
12521252

12531253
Parameters: ~
1254-
{opts} (table) optional parameters
1254+
{opts} (table) optional parameters with boolean defaults
12551255

12561256
Options: ~
1257-
{path} (string) root directory for the tree
1258-
• {current_window} (boolean) open the tree in the current window
1257+
{path} (string) root directory for the tree
1258+
• {current_window} (boolean, false) open the tree in the current window
12591259

12601260
api.tree.toggle({opts}) *nvim-tree.api.tree.toggle()*
12611261
Open or close the tree.
12621262

12631263
Parameters: ~
1264-
{opts} (table) optional parameters
1264+
{opts} (table) optional parameters with boolean defaults
12651265

12661266
Options: ~
1267-
{path} (string) root directory for the tree
1268-
• {current_window} (boolean) open the tree in the current window
1269-
{focus} (boolean) focus the tree when opening
1270-
• {find_file} (boolean) find the current buffer
1271-
• {update_root} (boolean) see |nvim-tree.update_focused_file.update_root|
1267+
{path} (string) root directory for the tree
1268+
• {current_window} (boolean, false) open the tree in the current window
1269+
{focus} (boolean, true) focus the tree when opening
1270+
• {find_file} (boolean, false) find the current buffer
1271+
• {update_root} (boolean, false) see |nvim-tree.update_focused_file.update_root|
12721272

12731273
api.tree.close() *nvim-tree.api.tree.close()*
12741274
Close the tree, affecting all tabs as per |nvim-tree.tab.sync.close|

lua/nvim-tree.lua

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ M.on_keypress = require("nvim-tree.actions.dispatch").dispatch
7575
---@param opts ApiTreeOpenOpts|nil|string
7676
function M.open(opts)
7777
-- legacy arguments
78-
if type(opts) ~= "table" then
78+
if type(opts) == "string" then
7979
opts = {
8080
path = opts,
8181
}
@@ -184,17 +184,28 @@ end
184184
---@param opts ApiTreeToggleOpts|nil|boolean
185185
function M.toggle(opts, no_focus, cwd, bang)
186186
-- legacy arguments
187-
if type(opts) ~= "table" then
187+
if type(opts) == "boolean" then
188188
opts = {
189-
path = cwd,
190-
focus = not no_focus,
191-
find_file = opts,
192-
update_root = bang,
189+
find_file = opts
193190
}
191+
if type(cwd) == "string" then
192+
opts.path = cwd
193+
end
194+
if type(no_focus) == "boolean" then
195+
opts.focus = not no_focus
196+
end
197+
if type(bang) == "boolean" then
198+
opts.update_root = bang
199+
end
194200
end
195201

196202
opts = opts or {}
197203

204+
-- defaults
205+
if opts.focus == nil then
206+
opts.focus = true
207+
end
208+
198209
-- sanitise path
199210
if type(opts.path) ~= "string" or vim.fn.isdirectory(opts.path) == 0 then
200211
opts.path = nil

lua/nvim-tree/api.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ end
1818

1919
---@class ApiTreeOpenOpts
2020
---@field path string|nil path
21-
---@field current_window boolean|nil
21+
---@field current_window boolean|nil default false
2222

2323
Api.tree.open = require("nvim-tree").open
2424

2525
---@class ApiTreeToggleOpts
2626
---@field path string|nil
27-
---@field current_window boolean|nil
28-
---@field focus boolean|nil
29-
---@field find_file boolean|nil
30-
---@field update_root boolean|nil
27+
---@field current_window boolean|nil default false
28+
---@field focus boolean|nil default true
29+
---@field find_file boolean|nil default false
30+
---@field update_root boolean|nil default false
3131

3232
Api.tree.toggle = require("nvim-tree").toggle
3333

0 commit comments

Comments
 (0)