Skip to content

feat: max_width for adaptive_size #1915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,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" },
Expand Down Expand Up @@ -189,7 +188,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,
Expand Down Expand Up @@ -703,10 +701,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|.
Expand All @@ -726,9 +720,19 @@ 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.
Type: `string | number | function`, Default: `30`
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"`.
Expand Down
5 changes: 3 additions & 2 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 },
Expand Down
11 changes: 11 additions & 0 deletions lua/nvim-tree/legacy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 36 additions & 10 deletions lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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