Skip to content

feat(api): api.tree.open feature parity with api.tree.toggle #1955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,8 @@ api.tree.open({opts}) *nvim-tree.api.tree.open()*
Options: ~
• {path} (string) root directory for the tree
• {current_window} (boolean, false) open the tree in the current window
• {find_file} (boolean, false) find the current buffer
• {update_root} (boolean, false) see |nvim-tree.update_focused_file.update_root|

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

api.tree.close() *nvim-tree.api.tree.close()*
Close the tree, affecting all tabs as per |nvim-tree.tab.sync.close|
Expand Down
56 changes: 31 additions & 25 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,6 @@ end
---@deprecated
M.on_keypress = require("nvim-tree.actions.dispatch").dispatch

---Open the tree, focusing if already open.
---@param opts ApiTreeOpenOpts|nil|string
function M.open(opts)
-- legacy arguments
if type(opts) == "string" then
opts = {
path = opts,
}
end

opts = opts or {}

-- sanitise path
if type(opts.path) ~= "string" or vim.fn.isdirectory(opts.path) == 0 then
opts.path = nil
end

if view.is_visible() then
lib.set_target_win()
view.focus()
else
lib.open(opts)
end
end

function M.open_replacing_current_buffer(cwd)
if view.is_visible() then
return
Expand Down Expand Up @@ -180,6 +155,37 @@ function M.find_file(with_open, bufnr, bang)
find_file(with_open, bufnr, bang)
end

---Open the tree, focusing if already open.
---@param opts ApiTreeOpenOpts|nil|string
function M.open(opts)
-- legacy arguments
if type(opts) == "string" then
opts = {
path = opts,
}
end

opts = opts or {}

local previous_buf = vim.api.nvim_get_current_buf()

-- sanitise path
if type(opts.path) ~= "string" or vim.fn.isdirectory(opts.path) == 0 then
opts.path = nil
end

if view.is_visible() then
lib.set_target_win()
view.focus()
else
lib.open(opts)
end

if _config.update_focused_file.enable or opts.find_file then
find_file(false, previous_buf, opts.update_root)
end
end

---Toggle the tree.
---@param opts ApiTreeToggleOpts|nil|boolean
function M.toggle(opts, no_focus, cwd, bang)
Expand Down
4 changes: 3 additions & 1 deletion lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ end
---@class ApiTreeOpenOpts
---@field path string|nil path
---@field current_window boolean|nil default false
---@field find_file boolean|nil default false
---@field update_root boolean|nil default false

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

---@class ApiTreeToggleOpts
---@field path string|nil
---@field current_window boolean|nil default false
---@field focus boolean|nil default true
---@field find_file boolean|nil default false
---@field update_root boolean|nil default false
---@field focus boolean|nil default true

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

Expand Down