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({
|
require("nvim-tree").setup({
|
||||||
sort_by = "case_sensitive",
|
sort_by = "case_sensitive",
|
||||||
view = {
|
view = {
|
||||||
adaptive_size = true,
|
|
||||||
mappings = {
|
mappings = {
|
||||||
list = {
|
list = {
|
||||||
{ key = "u", action = "dir_up" },
|
{ key = "u", action = "dir_up" },
|
||||||
@ -188,7 +187,6 @@ Subsequent calls to setup will replace the previous configuration.
|
|||||||
remove_keymaps = false,
|
remove_keymaps = false,
|
||||||
select_prompts = false,
|
select_prompts = false,
|
||||||
view = {
|
view = {
|
||||||
adaptive_size = false,
|
|
||||||
centralize_selection = false,
|
centralize_selection = false,
|
||||||
cursorline = true,
|
cursorline = true,
|
||||||
debounce_delay = 15,
|
debounce_delay = 15,
|
||||||
@ -709,10 +707,6 @@ Type: `boolean`, Default: `false`
|
|||||||
*nvim-tree.view*
|
*nvim-tree.view*
|
||||||
Window / buffer setup.
|
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*
|
*nvim-tree.view.centralize_selection*
|
||||||
When entering nvim-tree, reposition the view so that the current node is
|
When entering nvim-tree, reposition the view so that the current node is
|
||||||
initially centralized, see |zz|.
|
initially centralized, see |zz|.
|
||||||
@ -732,9 +726,19 @@ Window / buffer setup.
|
|||||||
Type: `boolean`, Default: `false`
|
Type: `boolean`, Default: `false`
|
||||||
|
|
||||||
*nvim-tree.view.width*
|
*nvim-tree.view.width*
|
||||||
Width of the window, can be a `%` string, a number representing columns or
|
Width of the window: can be a `%` string, a number representing columns, a
|
||||||
a function.
|
function or a table.
|
||||||
Type: `string | number | function`, Default: `30`
|
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*
|
*nvim-tree.view.side*
|
||||||
Side of the tree, can be `"left"`, `"right"`.
|
Side of the tree, can be `"left"`, `"right"`.
|
||||||
|
|||||||
@ -513,7 +513,6 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
|
|||||||
remove_keymaps = false,
|
remove_keymaps = false,
|
||||||
select_prompts = false,
|
select_prompts = false,
|
||||||
view = {
|
view = {
|
||||||
adaptive_size = false,
|
|
||||||
centralize_selection = false,
|
centralize_selection = false,
|
||||||
cursorline = true,
|
cursorline = true,
|
||||||
debounce_delay = 15,
|
debounce_delay = 15,
|
||||||
@ -744,7 +743,9 @@ local FIELD_SKIP_VALIDATE = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
local FIELD_OVERRIDE_TYPECHECK = {
|
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 },
|
remove_keymaps = { boolean = true, table = true },
|
||||||
on_attach = { ["function"] = true, string = true },
|
on_attach = { ["function"] = true, string = true },
|
||||||
sort_by = { ["function"] = true, string = true },
|
sort_by = { ["function"] = true, string = true },
|
||||||
|
|||||||
@ -30,6 +30,17 @@ local function refactored(opts)
|
|||||||
|
|
||||||
-- 2023/01/08
|
-- 2023/01/08
|
||||||
utils.move_missing_val(opts, "trash", "require_confirm", opts, "ui.confirm", "trash", true)
|
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
|
end
|
||||||
|
|
||||||
local function removed(opts)
|
local function removed(opts)
|
||||||
|
|||||||
@ -4,6 +4,9 @@ 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"
|
||||||
|
|
||||||
|
local DEFAULT_MIN_WIDTH = 30
|
||||||
|
local DEFAULT_MAX_WIDTH = -1
|
||||||
|
|
||||||
M.View = {
|
M.View = {
|
||||||
adaptive_size = false,
|
adaptive_size = false,
|
||||||
centralize_selection = false,
|
centralize_selection = false,
|
||||||
@ -98,8 +101,8 @@ local function create_buffer(bufnr)
|
|||||||
events._dispatch_tree_attached_post(M.get_bufnr())
|
events._dispatch_tree_attached_post(M.get_bufnr())
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_size()
|
local function get_size(size)
|
||||||
local size = M.View.width
|
size = size or M.View.width
|
||||||
if type(size) == "number" then
|
if type(size) == "number" then
|
||||||
return size
|
return size
|
||||||
elseif type(size) == "function" then
|
elseif type(size) == "function" then
|
||||||
@ -246,14 +249,29 @@ end
|
|||||||
local function grow()
|
local function grow()
|
||||||
local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0
|
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 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
|
for _, l in pairs(lines) do
|
||||||
local count = vim.fn.strchars(l) + 3 -- plus some padding
|
local count = vim.fn.strchars(l)
|
||||||
if max_length < count then
|
if resizing_width < count then
|
||||||
max_length = count
|
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
|
||||||
end
|
end
|
||||||
M.resize(max_length)
|
M.resize(resizing_width + padding)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.grow_from_content()
|
function M.grow_from_content()
|
||||||
@ -482,12 +500,9 @@ end
|
|||||||
|
|
||||||
function M.setup(opts)
|
function M.setup(opts)
|
||||||
local options = opts.view or {}
|
local options = opts.view or {}
|
||||||
M.View.adaptive_size = options.adaptive_size
|
|
||||||
M.View.centralize_selection = options.centralize_selection
|
M.View.centralize_selection = options.centralize_selection
|
||||||
M.View.side = (options.side == "right") and "right" or "left"
|
M.View.side = (options.side == "right") and "right" or "left"
|
||||||
M.View.width = options.width
|
|
||||||
M.View.height = options.height
|
M.View.height = options.height
|
||||||
M.View.initial_width = get_size()
|
|
||||||
M.View.hide_root_folder = options.hide_root_folder
|
M.View.hide_root_folder = options.hide_root_folder
|
||||||
M.View.tab = opts.tab
|
M.View.tab = opts.tab
|
||||||
M.View.preserve_window_proportions = options.preserve_window_proportions
|
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.winopts.signcolumn = options.signcolumn
|
||||||
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.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
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user