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

This commit is contained in:
Alexander Courtis 2023-01-31 12:27:10 +11:00 committed by GitHub
parent f3b73725c5
commit 215b29bfad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 27 deletions

View File

@ -1256,6 +1256,8 @@ api.tree.open({opts}) *nvim-tree.api.tree.open()*
Options: ~ Options: ~
• {path} (string) root directory for the tree • {path} (string) root directory for the tree
• {current_window} (boolean, false) open the tree in the current window • {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()* api.tree.toggle({opts}) *nvim-tree.api.tree.toggle()*
Open or close the tree. Open or close the tree.
@ -1266,9 +1268,9 @@ api.tree.toggle({opts}) *nvim-tree.api.tree.toggle()*
Options: ~ Options: ~
• {path} (string) root directory for the tree • {path} (string) root directory for the tree
• {current_window} (boolean, false) open the tree in the current window • {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 • {find_file} (boolean, false) find the current buffer
• {update_root} (boolean, false) see |nvim-tree.update_focused_file.update_root| • {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()* 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

@ -71,31 +71,6 @@ end
---@deprecated ---@deprecated
M.on_keypress = require("nvim-tree.actions.dispatch").dispatch 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) function M.open_replacing_current_buffer(cwd)
if view.is_visible() then if view.is_visible() then
return return
@ -180,6 +155,37 @@ function M.find_file(with_open, bufnr, bang)
find_file(with_open, bufnr, bang) find_file(with_open, bufnr, bang)
end 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. ---Toggle the tree.
---@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)

View File

@ -19,15 +19,17 @@ end
---@class ApiTreeOpenOpts ---@class ApiTreeOpenOpts
---@field path string|nil path ---@field path string|nil path
---@field current_window boolean|nil default false ---@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 Api.tree.open = require("nvim-tree").open
---@class ApiTreeToggleOpts ---@class ApiTreeToggleOpts
---@field path string|nil ---@field path string|nil
---@field current_window boolean|nil default false ---@field current_window boolean|nil default false
---@field focus boolean|nil default true
---@field find_file boolean|nil default false ---@field find_file boolean|nil default false
---@field update_root boolean|nil default false ---@field update_root boolean|nil default false
---@field focus boolean|nil default true
Api.tree.toggle = require("nvim-tree").toggle Api.tree.toggle = require("nvim-tree").toggle