Skip to content

Commit 215b29b

Browse files
authored
feat(api): api.tree.open feature parity with api.tree.toggle (#1955)
1 parent f3b7372 commit 215b29b

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

doc/nvim-tree-lua.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,8 @@ api.tree.open({opts}) *nvim-tree.api.tree.open()*
12561256
Options: ~
12571257
{path} (string) root directory for the tree
12581258
• {current_window} (boolean, false) open the tree in the current window
1259+
• {find_file} (boolean, false) find the current buffer
1260+
• {update_root} (boolean, false) see |nvim-tree.update_focused_file.update_root|
12591261

12601262
api.tree.toggle({opts}) *nvim-tree.api.tree.toggle()*
12611263
Open or close the tree.
@@ -1266,9 +1268,9 @@ api.tree.toggle({opts}) *nvim-tree.api.tree.toggle()*
12661268
Options: ~
12671269
{path} (string) root directory for the tree
12681270
• {current_window} (boolean, false) open the tree in the current window
1269-
{focus} (boolean, true) focus the tree when opening
12701271
• {find_file} (boolean, false) find the current buffer
12711272
• {update_root} (boolean, false) see |nvim-tree.update_focused_file.update_root|
1273+
{focus} (boolean, true) focus the tree when opening
12721274

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

lua/nvim-tree.lua

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,31 +71,6 @@ end
7171
---@deprecated
7272
M.on_keypress = require("nvim-tree.actions.dispatch").dispatch
7373

74-
---Open the tree, focusing if already open.
75-
---@param opts ApiTreeOpenOpts|nil|string
76-
function M.open(opts)
77-
-- legacy arguments
78-
if type(opts) == "string" then
79-
opts = {
80-
path = opts,
81-
}
82-
end
83-
84-
opts = opts or {}
85-
86-
-- sanitise path
87-
if type(opts.path) ~= "string" or vim.fn.isdirectory(opts.path) == 0 then
88-
opts.path = nil
89-
end
90-
91-
if view.is_visible() then
92-
lib.set_target_win()
93-
view.focus()
94-
else
95-
lib.open(opts)
96-
end
97-
end
98-
9974
function M.open_replacing_current_buffer(cwd)
10075
if view.is_visible() then
10176
return
@@ -180,6 +155,37 @@ function M.find_file(with_open, bufnr, bang)
180155
find_file(with_open, bufnr, bang)
181156
end
182157

158+
---Open the tree, focusing if already open.
159+
---@param opts ApiTreeOpenOpts|nil|string
160+
function M.open(opts)
161+
-- legacy arguments
162+
if type(opts) == "string" then
163+
opts = {
164+
path = opts,
165+
}
166+
end
167+
168+
opts = opts or {}
169+
170+
local previous_buf = vim.api.nvim_get_current_buf()
171+
172+
-- sanitise path
173+
if type(opts.path) ~= "string" or vim.fn.isdirectory(opts.path) == 0 then
174+
opts.path = nil
175+
end
176+
177+
if view.is_visible() then
178+
lib.set_target_win()
179+
view.focus()
180+
else
181+
lib.open(opts)
182+
end
183+
184+
if _config.update_focused_file.enable or opts.find_file then
185+
find_file(false, previous_buf, opts.update_root)
186+
end
187+
end
188+
183189
---Toggle the tree.
184190
---@param opts ApiTreeToggleOpts|nil|boolean
185191
function M.toggle(opts, no_focus, cwd, bang)

lua/nvim-tree/api.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ end
1919
---@class ApiTreeOpenOpts
2020
---@field path string|nil path
2121
---@field current_window boolean|nil default false
22+
---@field find_file boolean|nil default false
23+
---@field update_root boolean|nil default false
2224

2325
Api.tree.open = require("nvim-tree").open
2426

2527
---@class ApiTreeToggleOpts
2628
---@field path string|nil
2729
---@field current_window boolean|nil default false
28-
---@field focus boolean|nil default true
2930
---@field find_file boolean|nil default false
3031
---@field update_root boolean|nil default false
32+
---@field focus boolean|nil default true
3133

3234
Api.tree.toggle = require("nvim-tree").toggle
3335

0 commit comments

Comments
 (0)