Skip to content

Commit 8c53482

Browse files
j-huialex-courtis
andauthored
feat(#2544): add api.tree.winid (#2545)
* feat(#2544): add API for querying win ID, api.tree.winid() * Document winid() opts Co-authored-by: Alexander Courtis <alex@courtis.org> * Fix winid() docs Co-authored-by: Alexander Courtis <alex@courtis.org> * Handle case where tabpage = 0 --------- Co-authored-by: Alexander Courtis <alex@courtis.org>
1 parent 28cf0cd commit 8c53482

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

doc/nvim-tree-lua.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,18 @@ tree.is_visible({opts}) *nvim-tree-api.tree.is_visible()*
16981698
Return: ~
16991699
(boolean) nvim-tree is visible
17001700

1701+
tree.winid({opts}) *nvim-tree-api.tree.winid()*
1702+
Retrieve the winid of the open tree.
1703+
1704+
Parameters: ~
1705+
{opts} (table) optional parameters
1706+
1707+
Options: ~
1708+
{tabpage} (number|nil) tabpage, 0 or nil for current, default nil
1709+
1710+
Return: ~
1711+
(number) winid or nil if tree is not visible
1712+
17011713
==============================================================================
17021714
6.2 API FILE SYSTEM *nvim-tree-api.fs*
17031715

lua/nvim-tree/api.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ Api.tree.is_tree_buf = wrap(require("nvim-tree.utils").is_nvim_tree_buf)
145145

146146
Api.tree.is_visible = wrap(require("nvim-tree.view").is_visible)
147147

148+
---@class ApiTreeWinIdOpts
149+
---@field tabpage number|nil default nil
150+
151+
Api.tree.winid = wrap(require("nvim-tree.view").winid)
152+
148153
Api.fs.create = wrap_node_or_nil(require("nvim-tree.actions.fs.create-file").fn)
149154
Api.fs.remove = wrap_node(require("nvim-tree.actions.fs.remove-file").fn)
150155
Api.fs.trash = wrap_node(require("nvim-tree.actions.fs.trash").fn)

lua/nvim-tree/view.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,21 @@ function M.focus(winnr, open_if_closed)
427427
vim.api.nvim_set_current_win(wnr)
428428
end
429429

430+
--- Retrieve the winid of the open tree.
431+
--- @param opts ApiTreeWinIdOpts|nil
432+
--- @return number|nil winid unlike get_winnr(), this returns nil if the nvim-tree window is not visible
433+
function M.winid(opts)
434+
local tabpage = opts and opts.tabpage
435+
if tabpage == 0 then
436+
tabpage = vim.api.nvim_get_current_tabpage()
437+
end
438+
if M.is_visible { tabpage = tabpage } then
439+
return M.get_winnr(tabpage)
440+
else
441+
return nil
442+
end
443+
end
444+
430445
--- Restores the state of a NvimTree window if it was initialized before.
431446
function M.restore_tab_state()
432447
local tabpage = vim.api.nvim_get_current_tabpage()

0 commit comments

Comments
 (0)