Skip to content

Commit 85542bc

Browse files
committed
feat(#3213): add view.width.lines_excluded option
1 parent 3fb91e1 commit 85542bc

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

doc/nvim-tree-lua.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,12 @@ longest line.
822822
Type: `string | number | fun(): number|string`
823823
Default: `-1`
824824

825+
*nvim-tree.view.width.lines_excluded*
826+
Exclude these lines when computing width.
827+
Value can be `"root"` or `"none"`.
828+
Type: `string`
829+
Default: `"root"`
830+
825831
*nvim-tree.view.width.padding*
826832
Extra padding to the right.
827833
Type: `number | fun(): number|string`
@@ -3323,6 +3329,7 @@ highlight group is not, hard linking as follows: >
33233329
|nvim-tree.view.side|
33243330
|nvim-tree.view.signcolumn|
33253331
|nvim-tree.view.width|
3332+
|nvim-tree.view.width.lines_excluded|
33263333
|nvim-tree.view.width.max|
33273334
|nvim-tree.view.width.min|
33283335
|nvim-tree.view.width.padding|

lua/nvim-tree.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ local ACCEPTED_TYPES = {
553553
"table",
554554
min = { "string", "function", "number" },
555555
max = { "string", "function", "number" },
556+
lines_excluded = { "string" },
556557
padding = { "function", "number" },
557558
},
558559
},
@@ -589,6 +590,9 @@ local ACCEPTED_STRINGS = {
589590
view = {
590591
side = { "left", "right" },
591592
signcolumn = { "yes", "no", "auto" },
593+
width = {
594+
lines_excluded = { "root", "none" },
595+
},
592596
},
593597
renderer = {
594598
hidden_display = { "none", "simple", "all" },

lua/nvim-tree/view.lua

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ local M = {}
1212

1313
local DEFAULT_MIN_WIDTH = 30
1414
local DEFAULT_MAX_WIDTH = -1
15+
local DEFAULT_LINES_EXCLUDED = "root"
1516
local DEFAULT_PADDING = 1
1617

1718
M.View = {
@@ -303,7 +304,7 @@ function M.open(options)
303304
end
304305

305306
local function grow()
306-
local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0
307+
local starts_at = (M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and M.View.lines_excluded == "root") and 1 or 0
307308
local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false)
308309
-- number of columns of right-padding to indicate end of path
309310
local padding = get_size(M.View.padding)
@@ -314,31 +315,25 @@ local function grow()
314315
padding = padding + wininfo[1].textoff
315316
end
316317

317-
local resizing_width = M.View.initial_width - padding
318-
local max_width
319-
320-
-- maybe bound max
321-
if M.View.max_width == -1 then
322-
max_width = -1
323-
else
324-
max_width = get_width(M.View.max_width) - padding
318+
local final_width = M.View.initial_width
319+
local max_width = math.huge
320+
if M.View.max_width ~= -1 then
321+
max_width = get_width(M.View.max_width)
325322
end
326323

327324
local ns_id = vim.api.nvim_get_namespaces()["NvimTreeExtmarks"]
328325
for line_nr, l in pairs(lines) do
329-
local count = vim.fn.strchars(l)
326+
local line_width = vim.fn.strchars(l)
330327
-- also add space for right-aligned icons
331328
local extmarks = vim.api.nvim_buf_get_extmarks(M.get_bufnr(), ns_id, { line_nr, 0 }, { line_nr, -1 }, { details = true })
332-
count = count + utils.extmarks_length(extmarks)
333-
if resizing_width < count then
334-
resizing_width = count
335-
end
336-
if M.View.adaptive_size and max_width >= 0 and resizing_width >= max_width then
337-
resizing_width = max_width
329+
line_width = line_width + utils.extmarks_length(extmarks) + padding
330+
final_width = math.max(final_width, line_width)
331+
if final_width >= max_width then
332+
final_width = max_width
338333
break
339334
end
340335
end
341-
M.resize(resizing_width + padding)
336+
M.resize(final_width)
342337
end
343338

344339
function M.grow_from_content()
@@ -600,6 +595,7 @@ function M.configure_width(width)
600595
M.View.adaptive_size = true
601596
M.View.width = width.min or DEFAULT_MIN_WIDTH
602597
M.View.max_width = width.max or DEFAULT_MAX_WIDTH
598+
M.View.lines_excluded = width.lines_excluded or DEFAULT_LINES_EXCLUDED
603599
M.View.padding = width.padding or DEFAULT_PADDING
604600
elseif width == nil then
605601
if M.config.width ~= nil then

0 commit comments

Comments
 (0)