feat(#2598): add api.tree.resize (#2811)

* feat(#2598): Implemented API `tree.resize`

* rely on  when resize

* Fix docs

---------

Co-authored-by: Alexander Courtis <alex@courtis.org>
This commit is contained in:
Vladimir Levin 2024-07-11 14:15:40 +09:00 committed by GitHub
parent 12a9a995a4
commit 2ede0de67b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 107 additions and 9 deletions

View File

@ -1670,6 +1670,22 @@ tree.focus() *nvim-tree-api.tree.focus()*
tree.reload() *nvim-tree-api.tree.reload()* tree.reload() *nvim-tree-api.tree.reload()*
Refresh the tree. Does nothing if closed. Refresh the tree. Does nothing if closed.
tree.resize({opts}) *nvim-tree-api.tree.resize()*
Resize the tree, persisting the new size.
Resets to |nvim-tree.view.width| when no {opts} provided.
See |:NvimTreeResize|
Parameters: ~
• {opts} (table) optional parameters
Options: ~
• {width} (table) new |nvim-tree.view.width| value
• {absolute} (number) set the width
• {relative} (number) increase or decrease the width
Only one option is supported, in the priority order above.
{absolute} and {relative} do nothing when {width} is a function.
tree.change_root({path}) *nvim-tree-api.tree.change_root()* tree.change_root({path}) *nvim-tree-api.tree.change_root()*
Change the tree's root to a path. Change the tree's root to a path.
@ -3001,6 +3017,7 @@ highlight group is not, hard linking as follows: >
|nvim-tree-api.tree.is_visible()| |nvim-tree-api.tree.is_visible()|
|nvim-tree-api.tree.open()| |nvim-tree-api.tree.open()|
|nvim-tree-api.tree.reload()| |nvim-tree-api.tree.reload()|
|nvim-tree-api.tree.resize()|
|nvim-tree-api.tree.search_node()| |nvim-tree-api.tree.search_node()|
|nvim-tree-api.tree.toggle()| |nvim-tree-api.tree.toggle()|
|nvim-tree-api.tree.toggle_custom_filter()| |nvim-tree-api.tree.toggle_custom_filter()|

View File

@ -4,12 +4,14 @@ M.find_file = require "nvim-tree.actions.tree.find-file"
M.modifiers = require "nvim-tree.actions.tree.modifiers" M.modifiers = require "nvim-tree.actions.tree.modifiers"
M.open = require "nvim-tree.actions.tree.open" M.open = require "nvim-tree.actions.tree.open"
M.toggle = require "nvim-tree.actions.tree.toggle" M.toggle = require "nvim-tree.actions.tree.toggle"
M.resize = require "nvim-tree.actions.tree.resize"
function M.setup(opts) function M.setup(opts)
M.find_file.setup(opts) M.find_file.setup(opts)
M.modifiers.setup(opts) M.modifiers.setup(opts)
M.open.setup(opts) M.open.setup(opts)
M.toggle.setup(opts) M.toggle.setup(opts)
M.resize.setup(opts)
end end
return M return M

View File

@ -0,0 +1,51 @@
local view = require "nvim-tree.view"
local M = {}
---Resize the tree, persisting the new size.
---@param opts ApiTreeResizeOpts|nil
function M.fn(opts)
if opts == nil then
-- reset to config values
view.configure_width()
view.resize()
return
end
local options = opts or {}
local width_cfg = options.width
if width_cfg ~= nil then
view.configure_width(width_cfg)
view.resize()
return
end
if not view.is_width_determined() then
-- {absolute} and {relative} do nothing when {width} is a function.
return
end
local absolute = options.absolute
if type(absolute) == "number" then
view.resize(absolute)
return
end
local relative = options.relative
if type(relative) == "number" then
local relative_size = tostring(relative)
if relative > 0 then
relative_size = "+" .. relative_size
end
view.resize(relative_size)
return
end
end
function M.setup(opts)
M.config = opts or {}
end
return M

View File

@ -100,6 +100,13 @@ Api.tree.close_in_this_tab = wrap(view.close_this_tab_only)
Api.tree.close_in_all_tabs = wrap(view.close_all_tabs) Api.tree.close_in_all_tabs = wrap(view.close_all_tabs)
Api.tree.reload = wrap(actions.reloaders.reload_explorer) Api.tree.reload = wrap(actions.reloaders.reload_explorer)
---@class ApiTreeResizeOpts
---@field width string|function|number|table|nil
---@field absolute number|nil
---@field relative number|nil
Api.tree.resize = wrap(actions.tree.resize.fn)
Api.tree.change_root = wrap(function(...) Api.tree.change_root = wrap(function(...)
require("nvim-tree").change_dir(...) require("nvim-tree").change_dir(...)
end) end)

View File

@ -548,6 +548,34 @@ function M.reset_winhl()
end end
end end
---Check if width determined or calculated on-fly
---@return boolean
function M.is_width_determined()
return type(M.View.width) ~= "function"
end
---Configure width-related config
---@param width string|function|number|table|nil
function M.configure_width(width)
if type(width) == "table" then
M.View.adaptive_size = true
M.View.width = width.min or DEFAULT_MIN_WIDTH
M.View.max_width = width.max or DEFAULT_MAX_WIDTH
M.View.padding = width.padding or DEFAULT_PADDING
elseif width == nil then
if M.config.width ~= nil then
-- if we had input config - fallback to it
M.configure_width(M.config.width)
else
-- otherwise - restore initial width
M.View.width = M.View.initial_width
end
else
M.View.adaptive_size = false
M.View.width = width
end
end
function M.setup(opts) function M.setup(opts)
local options = opts.view or {} local options = opts.view or {}
M.View.centralize_selection = options.centralize_selection M.View.centralize_selection = options.centralize_selection
@ -563,15 +591,8 @@ function M.setup(opts)
M.View.float = options.float M.View.float = options.float
M.on_attach = opts.on_attach M.on_attach = opts.on_attach
if type(options.width) == "table" then M.config = options
M.View.adaptive_size = true M.configure_width(options.width)
M.View.width = options.width.min or DEFAULT_MIN_WIDTH
M.View.max_width = options.width.max or DEFAULT_MAX_WIDTH
M.View.padding = options.width.padding or DEFAULT_PADDING
else
M.View.adaptive_size = false
M.View.width = options.width
end
M.View.initial_width = get_width() M.View.initial_width = get_width()
end end