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 2 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
7 changes: 7 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ Subsequent calls to setup will replace the previous configuration.
cursorline = true,
debounce_delay = 15,
width = 30,
max_width = -1,
hide_root_folder = false,
side = "left",
preserve_window_proportions = false,
Expand Down Expand Up @@ -730,6 +731,12 @@ Window / buffer setup.
a function.
Type: `string | number | function`, Default: `30`

*nvim-tree.view.max_width*
Maximum width of the window when `nvim-tree.view.adaptive_size` is `true`, can be
a `%` string, a number representing columns or a function. If set to `-1`, the
width is unbounded.
Type: `string | number | function`, Default: `-1`

*nvim-tree.view.side*
Side of the tree, can be `"left"`, `"right"`.
Type: `string`, Default: `"left"`
Expand Down
2 changes: 2 additions & 0 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
cursorline = true,
debounce_delay = 15,
width = 30,
max_width = -1,
hide_root_folder = false,
side = "left",
preserve_window_proportions = false,
Expand Down Expand Up @@ -745,6 +746,7 @@ local FIELD_SKIP_VALIDATE = {

local FIELD_OVERRIDE_TYPECHECK = {
width = { string = true, ["function"] = true, number = true },
max_width = { 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
21 changes: 14 additions & 7 deletions lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,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 +246,20 @@ 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
local padding = M.View.winopts.signcolumn and 2 or 0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to add an extra character (I.e. 3 instead of 3) as it was previously? I personally find the extra character useful as it indicates that I haven't reached the maximum width yet (i.e. the filename is fully shown and not clipped)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I think you got it... I didn't realise that the extra space was useful.

Yes, let's put that back and ensure that it works for max = -1 and max = 30

local resizing_width = M.View.initial_width - padding
local max_width = get_size(M.View.max_width) - padding
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 @@ -486,6 +492,7 @@ function M.setup(opts)
M.View.centralize_selection = options.centralize_selection
M.View.side = (options.side == "right") and "right" or "left"
M.View.width = options.width
M.View.max_width = options.max_width
M.View.height = options.height
M.View.initial_width = get_size()
M.View.hide_root_folder = options.hide_root_folder
Expand Down