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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user