From 3d234325d1ed5411a41260ff92718b4f41540717 Mon Sep 17 00:00:00 2001 From: "simon.mandlik" Date: Sat, 15 Nov 2025 21:11:07 +0100 Subject: [PATCH 1/2] feat: `include_root` option --- doc/nvim-tree-lua.txt | 5 +++++ lua/nvim-tree.lua | 1 + lua/nvim-tree/view.lua | 4 +++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 75f29dbb067..68f86a3fca9 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -822,6 +822,10 @@ longest line. Type: `string | number | fun(): number|string` Default: `-1` + *nvim-tree.view.width.include_root* + Include root folder when computing width. + Type: `boolean`, Default: `false` + *nvim-tree.view.width.padding* Extra padding to the right. Type: `number | fun(): number|string` @@ -3323,6 +3327,7 @@ highlight group is not, hard linking as follows: > |nvim-tree.view.side| |nvim-tree.view.signcolumn| |nvim-tree.view.width| +|nvim-tree.view.width.include_root| |nvim-tree.view.width.max| |nvim-tree.view.width.min| |nvim-tree.view.width.padding| diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 18858af14f3..55444078895 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -553,6 +553,7 @@ local ACCEPTED_TYPES = { "table", min = { "string", "function", "number" }, max = { "string", "function", "number" }, + include_root = { "boolean" }, padding = { "function", "number" }, }, }, diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 90f2b033f3e..db5faece4ff 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -12,6 +12,7 @@ local M = {} local DEFAULT_MIN_WIDTH = 30 local DEFAULT_MAX_WIDTH = -1 +local DEFAULT_INCLUDE_ROOT = false local DEFAULT_PADDING = 1 M.View = { @@ -303,7 +304,7 @@ function M.open(options) end 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 M.View.include_root) and 1 or 0 local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false) -- number of columns of right-padding to indicate end of path local padding = get_size(M.View.padding) @@ -600,6 +601,7 @@ function M.configure_width(width) M.View.adaptive_size = true M.View.width = width.min or DEFAULT_MIN_WIDTH M.View.max_width = width.max or DEFAULT_MAX_WIDTH + M.View.include_root = width.include_root or DEFAULT_INCLUDE_ROOT M.View.padding = width.padding or DEFAULT_PADDING elseif width == nil then if M.config.width ~= nil then From 45aa35780ef2fe67efe41df5244bb53be9c42681 Mon Sep 17 00:00:00 2001 From: "simon.mandlik" Date: Wed, 19 Nov 2025 16:54:34 +0100 Subject: [PATCH 2/2] feat: add early exit to the `grow()` algorithm --- lua/nvim-tree/view.lua | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index db5faece4ff..2ea7bc9c876 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -315,31 +315,25 @@ local function grow() padding = padding + wininfo[1].textoff end - 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_width(M.View.max_width) - padding + local final_width = M.View.initial_width + local max_width = math.huge + if M.View.max_width ~= -1 then + max_width = get_width(M.View.max_width) end local ns_id = vim.api.nvim_get_namespaces()["NvimTreeExtmarks"] for line_nr, l in pairs(lines) do - local count = vim.fn.strchars(l) + local line_width = vim.fn.strchars(l) -- also add space for right-aligned icons local extmarks = vim.api.nvim_buf_get_extmarks(M.get_bufnr(), ns_id, { line_nr, 0 }, { line_nr, -1 }, { details = true }) - count = count + utils.extmarks_length(extmarks) - 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 + line_width = line_width + utils.extmarks_length(extmarks) + padding + final_width = math.max(final_width, line_width) + if final_width >= max_width then + final_width = max_width break end end - M.resize(resizing_width + padding) + M.resize(final_width) end function M.grow_from_content()