Skip to content

Commit b1e074d

Browse files
authored
feat: add winid parameter for api.tree.open, toggle, find_file (#2213)
* feat: add winid parameter for api.tree.open, toggle, find_file * feat: add winid parameter for api.tree.open, toggle, find_file
1 parent 736c7ff commit b1e074d

File tree

8 files changed

+33
-10
lines changed

8 files changed

+33
-10
lines changed

doc/nvim-tree-lua.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,8 @@ tree.open({opts}) *nvim-tree-api.tree.open()*
13581358
Options: ~
13591359
{path} (string) root directory for the tree
13601360
• {current_window} (boolean) open the tree in the current window
1361+
{winid} (number) open the tree in the specified |winid|,
1362+
overrides {current_window}
13611363
• {find_file} (boolean) find the current buffer
13621364
• {update_root} (boolean) requires {find_file}, see
13631365
|nvim-tree.update_focused_file.update_root|
@@ -1371,6 +1373,8 @@ tree.toggle({opts}) *nvim-tree-api.tree.toggle()*
13711373
Options: ~
13721374
{path} (string) root directory for the tree
13731375
• {current_window} (boolean) open the tree in the current window
1376+
{winid} (number) open the tree in the specified |winid|,
1377+
overrides {current_window}
13741378
• {find_file} (boolean) find the current buffer
13751379
• {update_root} (boolean) requires {find_file}, see
13761380
|nvim-tree.update_focused_file.update_root|
@@ -1433,8 +1437,10 @@ tree.find_file({opts}) *nvim-tree-api.tree.find_file()*
14331437

14341438
Options: ~
14351439
{buf} (string|number) absolute/relative path OR bufnr to find
1436-
{open} (boolean) open the tree
1440+
{open} (boolean) open the tree if necessary
14371441
• {current_window} (boolean) requires {open}, open in the current window
1442+
{winid} (number) open the tree in the specified |winid|,
1443+
overrides {current_window}
14381444
• {update_root} (boolean) see |nvim-tree.update_focused_file.update_root|
14391445
{focus} (boolean) focus the tree
14401446

lua/nvim-tree.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function M.open_replacing_current_buffer(cwd)
9696
if not core.get_explorer() or cwd ~= core.get_cwd() then
9797
core.init(cwd)
9898
end
99-
view.open_in_current_win { hijack_current_buf = false, resize = false }
99+
view.open_in_win { hijack_current_buf = false, resize = false }
100100
require("nvim-tree.renderer").draw()
101101
require("nvim-tree.actions.finders.find-file").fn(bufname)
102102
end

lua/nvim-tree/actions/tree/find-file.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function M.fn(opts)
4848
end
4949
elseif opts.open then
5050
-- open
51-
lib.open { current_window = opts.current_window }
51+
lib.open { current_window = opts.current_window, winid = opts.winid }
5252
if not opts.focus then
5353
vim.cmd "noautocmd wincmd p"
5454
end

lua/nvim-tree/actions/tree/open.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function M.fn(opts)
2929
view.focus()
3030
else
3131
-- open
32-
lib.open { path = opts.path, current_window = opts.current_window }
32+
lib.open { path = opts.path, current_window = opts.current_window, winid = opts.winid }
3333
end
3434

3535
-- find file

lua/nvim-tree/actions/tree/toggle.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function M.fn(opts, no_focus, cwd, bang)
4545
view.close()
4646
else
4747
-- open
48-
lib.open { path = opts.path, current_window = opts.current_window }
48+
lib.open { path = opts.path, current_window = opts.current_window, winid = opts.winid }
4949

5050
-- find file
5151
if M.config.update_focused_file.enable or opts.find_file then

lua/nvim-tree/api.lua

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ end
3636
---@class ApiTreeOpenOpts
3737
---@field path string|nil path
3838
---@field current_window boolean|nil default false
39+
---@field winid number|nil
3940
---@field find_file boolean|nil default false
4041
---@field update_root boolean|nil default false
4142

@@ -44,6 +45,7 @@ Api.tree.open = wrap(require("nvim-tree.actions.tree.open").fn)
4445
---@class ApiTreeToggleOpts
4546
---@field path string|nil
4647
---@field current_window boolean|nil default false
48+
---@field winid number|nil
4749
---@field find_file boolean|nil default false
4850
---@field update_root boolean|nil default false
4951
---@field focus boolean|nil default true
@@ -84,6 +86,7 @@ Api.tree.get_nodes = wrap(require("nvim-tree.lib").get_nodes)
8486
---@field buf string|number|nil
8587
---@field open boolean|nil default false
8688
---@field current_window boolean|nil default false
89+
---@field winid number|nil
8790
---@field update_root boolean|nil default false
8891
---@field focus boolean|nil default false
8992

lua/nvim-tree/lib.lua

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local events = require "nvim-tree.events"
77
---@class LibOpenOpts
88
---@field path string|nil path
99
---@field current_window boolean|nil default false
10+
---@field winid number|nil
1011

1112
local M = {
1213
target_winid = nil,
@@ -163,10 +164,13 @@ function M.open(opts)
163164
end
164165
if should_hijack_current_buf() then
165166
view.close_this_tab_only()
166-
view.open_in_current_win()
167+
view.open_in_win()
168+
renderer.draw()
169+
elseif opts.winid then
170+
view.open_in_win { hijack_current_buf = false, resize = false, winid = opts.winid }
167171
renderer.draw()
168172
elseif opts.current_window then
169-
view.open_in_current_win { hijack_current_buf = false, resize = false }
173+
view.open_in_win { hijack_current_buf = false, resize = false }
170174
renderer.draw()
171175
else
172176
open_view_and_draw()

lua/nvim-tree/view.lua

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
local M = {}
2-
31
local events = require "nvim-tree.events"
42
local utils = require "nvim-tree.utils"
53
local log = require "nvim-tree.log"
64

5+
---@class OpenInWinOpts
6+
---@field hijack_current_buf boolean|nil default true
7+
---@field resize boolean|nil default true
8+
---@field winid number|nil 0 or nil for current
9+
10+
local M = {}
11+
712
local DEFAULT_MIN_WIDTH = 30
813
local DEFAULT_MAX_WIDTH = -1
914
local DEFAULT_PADDING = 1
@@ -340,8 +345,13 @@ local function set_current_win()
340345
M.View.tabpages[current_tab].winnr = vim.api.nvim_get_current_win()
341346
end
342347

343-
function M.open_in_current_win(opts)
348+
---Open the tree in the a window
349+
---@param opts OpenInWinOpts|nil
350+
function M.open_in_win(opts)
344351
opts = opts or { hijack_current_buf = true, resize = true }
352+
if opts.winid and vim.api.nvim_win_is_valid(opts.winid) then
353+
vim.api.nvim_set_current_win(opts.winid)
354+
end
345355
create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf())
346356
setup_tabpage(vim.api.nvim_get_current_tabpage())
347357
set_current_win()

0 commit comments

Comments
 (0)