Skip to content

feat: add winid parameter for api.tree.open, toggle, find_file #2213

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 3 commits into from
May 21, 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
8 changes: 7 additions & 1 deletion doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,8 @@ tree.open({opts}) *nvim-tree-api.tree.open()*
Options: ~
• {path} (string) root directory for the tree
• {current_window} (boolean) open the tree in the current window
• {winid} (number) open the tree in the specified |winid|,
overrides {current_window}
• {find_file} (boolean) find the current buffer
• {update_root} (boolean) requires {find_file}, see
|nvim-tree.update_focused_file.update_root|
Expand All @@ -1371,6 +1373,8 @@ tree.toggle({opts}) *nvim-tree-api.tree.toggle()*
Options: ~
• {path} (string) root directory for the tree
• {current_window} (boolean) open the tree in the current window
• {winid} (number) open the tree in the specified |winid|,
overrides {current_window}
• {find_file} (boolean) find the current buffer
• {update_root} (boolean) requires {find_file}, see
|nvim-tree.update_focused_file.update_root|
Expand Down Expand Up @@ -1433,8 +1437,10 @@ tree.find_file({opts}) *nvim-tree-api.tree.find_file()*

Options: ~
• {buf} (string|number) absolute/relative path OR bufnr to find
• {open} (boolean) open the tree
• {open} (boolean) open the tree if necessary
• {current_window} (boolean) requires {open}, open in the current window
• {winid} (number) open the tree in the specified |winid|,
overrides {current_window}
• {update_root} (boolean) see |nvim-tree.update_focused_file.update_root|
• {focus} (boolean) focus the tree

Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function M.open_replacing_current_buffer(cwd)
if not core.get_explorer() or cwd ~= core.get_cwd() then
core.init(cwd)
end
view.open_in_current_win { hijack_current_buf = false, resize = false }
view.open_in_win { hijack_current_buf = false, resize = false }
require("nvim-tree.renderer").draw()
require("nvim-tree.actions.finders.find-file").fn(bufname)
end
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree/actions/tree/find-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function M.fn(opts)
end
elseif opts.open then
-- open
lib.open { current_window = opts.current_window }
lib.open { current_window = opts.current_window, winid = opts.winid }
if not opts.focus then
vim.cmd "noautocmd wincmd p"
end
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree/actions/tree/open.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function M.fn(opts)
view.focus()
else
-- open
lib.open { path = opts.path, current_window = opts.current_window }
lib.open { path = opts.path, current_window = opts.current_window, winid = opts.winid }
end

-- find file
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree/actions/tree/toggle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function M.fn(opts, no_focus, cwd, bang)
view.close()
else
-- open
lib.open { path = opts.path, current_window = opts.current_window }
lib.open { path = opts.path, current_window = opts.current_window, winid = opts.winid }

-- find file
if M.config.update_focused_file.enable or opts.find_file then
Expand Down
3 changes: 3 additions & 0 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ end
---@class ApiTreeOpenOpts
---@field path string|nil path
---@field current_window boolean|nil default false
---@field winid number|nil
---@field find_file boolean|nil default false
---@field update_root boolean|nil default false

Expand All @@ -44,6 +45,7 @@ Api.tree.open = wrap(require("nvim-tree.actions.tree.open").fn)
---@class ApiTreeToggleOpts
---@field path string|nil
---@field current_window boolean|nil default false
---@field winid number|nil
---@field find_file boolean|nil default false
---@field update_root boolean|nil default false
---@field focus boolean|nil default true
Expand Down Expand Up @@ -84,6 +86,7 @@ Api.tree.get_nodes = wrap(require("nvim-tree.lib").get_nodes)
---@field buf string|number|nil
---@field open boolean|nil default false
---@field current_window boolean|nil default false
---@field winid number|nil
---@field update_root boolean|nil default false
---@field focus boolean|nil default false

Expand Down
8 changes: 6 additions & 2 deletions lua/nvim-tree/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local events = require "nvim-tree.events"
---@class LibOpenOpts
---@field path string|nil path
---@field current_window boolean|nil default false
---@field winid number|nil

local M = {
target_winid = nil,
Expand Down Expand Up @@ -163,10 +164,13 @@ function M.open(opts)
end
if should_hijack_current_buf() then
view.close_this_tab_only()
view.open_in_current_win()
view.open_in_win()
renderer.draw()
elseif opts.winid then
view.open_in_win { hijack_current_buf = false, resize = false, winid = opts.winid }
renderer.draw()
elseif opts.current_window then
view.open_in_current_win { hijack_current_buf = false, resize = false }
view.open_in_win { hijack_current_buf = false, resize = false }
renderer.draw()
else
open_view_and_draw()
Expand Down
16 changes: 13 additions & 3 deletions lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
local M = {}

local events = require "nvim-tree.events"
local utils = require "nvim-tree.utils"
local log = require "nvim-tree.log"

---@class OpenInWinOpts
---@field hijack_current_buf boolean|nil default true
---@field resize boolean|nil default true
---@field winid number|nil 0 or nil for current

local M = {}

local DEFAULT_MIN_WIDTH = 30
local DEFAULT_MAX_WIDTH = -1
local DEFAULT_PADDING = 1
Expand Down Expand Up @@ -340,8 +345,13 @@ local function set_current_win()
M.View.tabpages[current_tab].winnr = vim.api.nvim_get_current_win()
end

function M.open_in_current_win(opts)
---Open the tree in the a window
---@param opts OpenInWinOpts|nil
function M.open_in_win(opts)
opts = opts or { hijack_current_buf = true, resize = true }
if opts.winid and vim.api.nvim_win_is_valid(opts.winid) then
vim.api.nvim_set_current_win(opts.winid)
end
create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf())
setup_tabpage(vim.api.nvim_get_current_tabpage())
set_current_win()
Expand Down