feat(view): add view.width.min/max replacing adaptive_size, allowing upper bound (#1915)
* feat: max_width for adaptive_size * view grow calculates size correctly based on sign column visibility * limit width to a minimum of 20 * adaptive_size -> min/max table * harden view size calculations against bad user input * style * add back an extra column of padding to adaptive resizing * back out: limit width to a minimum of 20 * revert unnecessary change * backout: view grow calculates size correctly based on sign column visibility * remove adaptive_size from help * backout unnecessary change M.View.config Co-authored-by: Alexander Courtis <alex@courtis.org>
This commit is contained in:
parent
13adc94e8e
commit
96506fee49
@ -81,7 +81,6 @@ Setup should be run in a lua file or in a |lua-heredoc| if using in a vim file.
|
||||
require("nvim-tree").setup({
|
||||
sort_by = "case_sensitive",
|
||||
view = {
|
||||
adaptive_size = true,
|
||||
mappings = {
|
||||
list = {
|
||||
{ key = "u", action = "dir_up" },
|
||||
@ -188,7 +187,6 @@ Subsequent calls to setup will replace the previous configuration.
|
||||
remove_keymaps = false,
|
||||
select_prompts = false,
|
||||
view = {
|
||||
adaptive_size = false,
|
||||
centralize_selection = false,
|
||||
cursorline = true,
|
||||
debounce_delay = 15,
|
||||
@ -709,10 +707,6 @@ Type: `boolean`, Default: `false`
|
||||
*nvim-tree.view*
|
||||
Window / buffer setup.
|
||||
|
||||
*nvim-tree.view.adaptive_size*
|
||||
Resize the window on each draw based on the longest line.
|
||||
Type: `boolean`, Default: `false`
|
||||
|
||||
*nvim-tree.view.centralize_selection*
|
||||
When entering nvim-tree, reposition the view so that the current node is
|
||||
initially centralized, see |zz|.
|
||||
@ -732,10 +726,20 @@ Window / buffer setup.
|
||||
Type: `boolean`, Default: `false`
|
||||
|
||||
*nvim-tree.view.width*
|
||||
Width of the window, can be a `%` string, a number representing columns or
|
||||
a function.
|
||||
Width of the window: can be a `%` string, a number representing columns, a
|
||||
function or a table.
|
||||
A table indicates that the view should be dynamically sized based on the
|
||||
longest line (previously `view.adaptive_size`).
|
||||
Type: `string | number | function | table`, Default: `30`
|
||||
|
||||
*nvim-tree.view.width.min*
|
||||
Minimum dynamic width.
|
||||
Type: `string | number | function`, Default: `30`
|
||||
|
||||
*nvim-tree.view.width.max*
|
||||
Maximum dynamic width, -1 for unbounded.
|
||||
Type: `string | number | function`, Default: `-1`
|
||||
|
||||
*nvim-tree.view.side*
|
||||
Side of the tree, can be `"left"`, `"right"`.
|
||||
Type: `string`, Default: `"left"`
|
||||
|
||||
@ -513,7 +513,6 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
|
||||
remove_keymaps = false,
|
||||
select_prompts = false,
|
||||
view = {
|
||||
adaptive_size = false,
|
||||
centralize_selection = false,
|
||||
cursorline = true,
|
||||
debounce_delay = 15,
|
||||
@ -744,7 +743,9 @@ local FIELD_SKIP_VALIDATE = {
|
||||
}
|
||||
|
||||
local FIELD_OVERRIDE_TYPECHECK = {
|
||||
width = { string = true, ["function"] = true, number = true },
|
||||
width = { string = true, ["function"] = true, number = true, ["table"] = true },
|
||||
max = { string = true, ["function"] = true, number = true },
|
||||
min = { string = true, ["function"] = true, number = true },
|
||||
remove_keymaps = { boolean = true, table = true },
|
||||
on_attach = { ["function"] = true, string = true },
|
||||
sort_by = { ["function"] = true, string = true },
|
||||
|
||||
@ -30,6 +30,17 @@ local function refactored(opts)
|
||||
|
||||
-- 2023/01/08
|
||||
utils.move_missing_val(opts, "trash", "require_confirm", opts, "ui.confirm", "trash", true)
|
||||
|
||||
-- 2023/01/15
|
||||
if opts.view and opts.view.adaptive_size ~= nil then
|
||||
if opts.view.adaptive_size and type(opts.view.width) ~= "table" then
|
||||
local width = opts.view.width
|
||||
opts.view.width = {
|
||||
min = width,
|
||||
}
|
||||
end
|
||||
opts.view.adaptive_size = nil
|
||||
end
|
||||
end
|
||||
|
||||
local function removed(opts)
|
||||
|
||||
@ -4,6 +4,9 @@ local events = require "nvim-tree.events"
|
||||
local utils = require "nvim-tree.utils"
|
||||
local log = require "nvim-tree.log"
|
||||
|
||||
local DEFAULT_MIN_WIDTH = 30
|
||||
local DEFAULT_MAX_WIDTH = -1
|
||||
|
||||
M.View = {
|
||||
adaptive_size = false,
|
||||
centralize_selection = false,
|
||||
@ -98,8 +101,8 @@ local function create_buffer(bufnr)
|
||||
events._dispatch_tree_attached_post(M.get_bufnr())
|
||||
end
|
||||
|
||||
local function get_size()
|
||||
local size = M.View.width
|
||||
local function get_size(size)
|
||||
size = size or M.View.width
|
||||
if type(size) == "number" then
|
||||
return size
|
||||
elseif type(size) == "function" then
|
||||
@ -246,14 +249,29 @@ end
|
||||
local function grow()
|
||||
local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0
|
||||
local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false)
|
||||
local max_length = M.View.initial_width
|
||||
-- 1 column of right-padding to indicate end of path
|
||||
local padding = 3
|
||||
local resizing_width = M.View.initial_width - padding
|
||||
local max_width
|
||||
|
||||
-- maybe bound max
|
||||
if M.View.max_width == -1 then
|
||||
max_width = -1
|
||||
else
|
||||
max_width = get_size(M.View.max_width) - padding
|
||||
end
|
||||
|
||||
for _, l in pairs(lines) do
|
||||
local count = vim.fn.strchars(l) + 3 -- plus some padding
|
||||
if max_length < count then
|
||||
max_length = count
|
||||
local count = vim.fn.strchars(l)
|
||||
if resizing_width < count then
|
||||
resizing_width = count
|
||||
end
|
||||
if M.View.adaptive_size and max_width >= 0 and resizing_width >= max_width then
|
||||
resizing_width = max_width
|
||||
break
|
||||
end
|
||||
end
|
||||
M.resize(max_length)
|
||||
M.resize(resizing_width + padding)
|
||||
end
|
||||
|
||||
function M.grow_from_content()
|
||||
@ -482,12 +500,9 @@ end
|
||||
|
||||
function M.setup(opts)
|
||||
local options = opts.view or {}
|
||||
M.View.adaptive_size = options.adaptive_size
|
||||
M.View.centralize_selection = options.centralize_selection
|
||||
M.View.side = (options.side == "right") and "right" or "left"
|
||||
M.View.width = options.width
|
||||
M.View.height = options.height
|
||||
M.View.initial_width = get_size()
|
||||
M.View.hide_root_folder = options.hide_root_folder
|
||||
M.View.tab = opts.tab
|
||||
M.View.preserve_window_proportions = options.preserve_window_proportions
|
||||
@ -497,6 +512,17 @@ function M.setup(opts)
|
||||
M.View.winopts.signcolumn = options.signcolumn
|
||||
M.View.float = options.float
|
||||
M.on_attach = opts.on_attach
|
||||
|
||||
if type(options.width) == "table" then
|
||||
M.View.adaptive_size = true
|
||||
M.View.width = options.width.min or DEFAULT_MIN_WIDTH
|
||||
M.View.max_width = options.width.max or DEFAULT_MAX_WIDTH
|
||||
else
|
||||
M.View.adaptive_size = false
|
||||
M.View.width = options.width
|
||||
end
|
||||
|
||||
M.View.initial_width = get_size()
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Loading…
Reference in New Issue
Block a user