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
This commit is contained in:
Alexander Courtis
2023-05-21 17:37:22 +10:00
committed by GitHub
parent 736c7ff590
commit b1e074d2b5
8 changed files with 33 additions and 10 deletions

View File

@@ -1358,6 +1358,8 @@ 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) open the tree in the current window • {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 • {find_file} (boolean) find the current buffer
• {update_root} (boolean) requires {find_file}, see • {update_root} (boolean) requires {find_file}, see
|nvim-tree.update_focused_file.update_root| |nvim-tree.update_focused_file.update_root|
@@ -1371,6 +1373,8 @@ 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) open the tree in the current window • {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 • {find_file} (boolean) find the current buffer
• {update_root} (boolean) requires {find_file}, see • {update_root} (boolean) requires {find_file}, see
|nvim-tree.update_focused_file.update_root| |nvim-tree.update_focused_file.update_root|
@@ -1433,8 +1437,10 @@ tree.find_file({opts}) *nvim-tree-api.tree.find_file()*
Options: ~ Options: ~
• {buf} (string|number) absolute/relative path OR bufnr to find • {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 • {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| • {update_root} (boolean) see |nvim-tree.update_focused_file.update_root|
• {focus} (boolean) focus the tree • {focus} (boolean) focus the tree

View File

@@ -96,7 +96,7 @@ function M.open_replacing_current_buffer(cwd)
if not core.get_explorer() or cwd ~= core.get_cwd() then if not core.get_explorer() or cwd ~= core.get_cwd() then
core.init(cwd) core.init(cwd)
end 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.renderer").draw()
require("nvim-tree.actions.finders.find-file").fn(bufname) require("nvim-tree.actions.finders.find-file").fn(bufname)
end end

View File

@@ -48,7 +48,7 @@ function M.fn(opts)
end end
elseif opts.open then elseif opts.open then
-- open -- open
lib.open { current_window = opts.current_window } lib.open { current_window = opts.current_window, winid = opts.winid }
if not opts.focus then if not opts.focus then
vim.cmd "noautocmd wincmd p" vim.cmd "noautocmd wincmd p"
end end

View File

@@ -29,7 +29,7 @@ function M.fn(opts)
view.focus() view.focus()
else else
-- open -- 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 end
-- find file -- find file

View File

@@ -45,7 +45,7 @@ function M.fn(opts, no_focus, cwd, bang)
view.close() view.close()
else else
-- open -- 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 -- find file
if M.config.update_focused_file.enable or opts.find_file then if M.config.update_focused_file.enable or opts.find_file then

View File

@@ -36,6 +36,7 @@ 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 winid number|nil
---@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
@@ -44,6 +45,7 @@ Api.tree.open = wrap(require("nvim-tree.actions.tree.open").fn)
---@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 winid number|nil
---@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 ---@field focus boolean|nil default true
@@ -84,6 +86,7 @@ Api.tree.get_nodes = wrap(require("nvim-tree.lib").get_nodes)
---@field buf string|number|nil ---@field buf string|number|nil
---@field open boolean|nil default false ---@field open boolean|nil default false
---@field current_window boolean|nil default false ---@field current_window boolean|nil default false
---@field winid number|nil
---@field update_root boolean|nil default false ---@field update_root boolean|nil default false
---@field focus boolean|nil default false ---@field focus boolean|nil default false

View File

@@ -7,6 +7,7 @@ local events = require "nvim-tree.events"
---@class LibOpenOpts ---@class LibOpenOpts
---@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 winid number|nil
local M = { local M = {
target_winid = nil, target_winid = nil,
@@ -163,10 +164,13 @@ function M.open(opts)
end end
if should_hijack_current_buf() then if should_hijack_current_buf() then
view.close_this_tab_only() 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() renderer.draw()
elseif opts.current_window then 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() renderer.draw()
else else
open_view_and_draw() open_view_and_draw()

View File

@@ -1,9 +1,14 @@
local M = {}
local events = require "nvim-tree.events" local events = require "nvim-tree.events"
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local log = require "nvim-tree.log" 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_MIN_WIDTH = 30
local DEFAULT_MAX_WIDTH = -1 local DEFAULT_MAX_WIDTH = -1
local DEFAULT_PADDING = 1 local DEFAULT_PADDING = 1
@@ -340,8 +345,13 @@ local function set_current_win()
M.View.tabpages[current_tab].winnr = vim.api.nvim_get_current_win() M.View.tabpages[current_tab].winnr = vim.api.nvim_get_current_win()
end 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 } 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()) create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf())
setup_tabpage(vim.api.nvim_get_current_tabpage()) setup_tabpage(vim.api.nvim_get_current_tabpage())
set_current_win() set_current_win()