Skip to content

Commit 761967c

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

File tree

3 files changed

+69
-41
lines changed

3 files changed

+69
-41
lines changed

doc/nvim-tree-lua.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,15 @@ 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+
Supported values: `"root".
828+
Type: `table`
829+
Default:
830+
`{`
831+
`"root"`
832+
`}`
833+
825834
*nvim-tree.view.width.padding*
826835
Extra padding to the right.
827836
Type: `number | fun(): number|string`
@@ -3323,6 +3332,7 @@ highlight group is not, hard linking as follows: >
33233332
|nvim-tree.view.side|
33243333
|nvim-tree.view.signcolumn|
33253334
|nvim-tree.view.width|
3335+
|nvim-tree.view.width.lines_excluded|
33263336
|nvim-tree.view.width.max|
33273337
|nvim-tree.view.width.min|
33283338
|nvim-tree.view.width.padding|

lua/nvim-tree.lua

Lines changed: 43 additions & 24 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 = { "table" },
556557
padding = { "function", "number" },
557558
},
558559
},
@@ -612,6 +613,14 @@ local ACCEPTED_STRINGS = {
612613
},
613614
}
614615

616+
local ACCEPTED_ENUMS = {
617+
view = {
618+
width = {
619+
lines_excluded = { "root", },
620+
},
621+
},
622+
}
623+
615624
---@param conf table|nil
616625
local function validate_options(conf)
617626
local msg
@@ -620,15 +629,19 @@ local function validate_options(conf)
620629
---@param def any
621630
---@param strs table
622631
---@param types table
632+
---@param enums table
623633
---@param prefix string
624-
local function validate(user, def, strs, types, prefix)
634+
local function validate(user, def, strs, types, enums, prefix)
625635
-- if user's option is not a table there is nothing to do
626636
if type(user) ~= "table" then
627637
return
628638
end
629639

630-
-- only compare tables with contents that are not integer indexed
631-
if type(def) ~= "table" or not next(def) or type(next(def)) == "number" then
640+
-- we have hit a leaf enum to validate against - it's an integer indexed table
641+
local enum_value = type(enums) == "table" and next(enums) and type(next(enums)) == "number"
642+
643+
-- only compare tables with contents that are not integer indexed nor enums
644+
if not enum_value and (type(def) ~= "table" or not next(def) or type(next(def)) == "number") then
632645
-- unless the field can be a table (and is not a table in default config)
633646
if vim.tbl_contains(types, "table") then
634647
-- use a dummy default to allow all checks
@@ -642,27 +655,33 @@ local function validate_options(conf)
642655
if not FIELD_SKIP_VALIDATE[k] then
643656
local invalid
644657

645-
if def[k] == nil and types[k] == nil then
646-
-- option does not exist
647-
invalid = string.format("Unknown option: %s%s", prefix, k)
648-
elseif type(v) ~= type(def[k]) then
649-
local expected
650-
651-
if types[k] and #types[k] > 0 then
652-
if not vim.tbl_contains(types[k], type(v)) then
653-
expected = table.concat(types[k], "|")
654-
end
655-
else
656-
expected = type(def[k])
658+
if enum_value then
659+
if not vim.tbl_contains(enums, v) then
660+
invalid = string.format("Invalid value for field %s%s: Expected one of enum '%s', got '%s'", prefix, k, table.concat(enums, "'|'"), tostring(v))
657661
end
662+
else
663+
if def[k] == nil and types[k] == nil then
664+
-- option does not exist
665+
invalid = string.format("Unknown option: %s%s", prefix, k)
666+
elseif type(v) ~= type(def[k]) then
667+
local expected
668+
669+
if types[k] and #types[k] > 0 then
670+
if not vim.tbl_contains(types[k], type(v)) then
671+
expected = table.concat(types[k], "|")
672+
end
673+
else
674+
expected = type(def[k])
675+
end
658676

659-
if expected then
660-
-- option is of the wrong type
661-
invalid = string.format("Invalid option: %s%s. Expected %s, got %s", prefix, k, expected, type(v))
677+
if expected then
678+
-- option is of the wrong type
679+
invalid = string.format("Invalid option: %s%s. Expected %s, got %s", prefix, k, expected, type(v))
680+
end
681+
elseif type(v) == "string" and strs[k] and not vim.tbl_contains(strs[k], v) then
682+
-- option has type `string` but value is not accepted
683+
invalid = string.format("Invalid value for field %s%s: '%s'", prefix, k, v)
662684
end
663-
elseif type(v) == "string" and strs[k] and not vim.tbl_contains(strs[k], v) then
664-
-- option has type `string` but value is not accepted
665-
invalid = string.format("Invalid value for field %s%s: '%s'", prefix, k, v)
666685
end
667686

668687
if invalid then
@@ -672,14 +691,14 @@ local function validate_options(conf)
672691
msg = invalid
673692
end
674693
user[k] = nil
675-
else
676-
validate(v, def[k], strs[k] or {}, types[k] or {}, prefix .. k .. ".")
694+
elseif not enum_value then
695+
validate(v, def[k], strs[k] or {}, types[k] or {}, enums[k] or {}, prefix .. k .. ".")
677696
end
678697
end
679698
end
680699
end
681700

682-
validate(conf, DEFAULT_OPTS, ACCEPTED_STRINGS, ACCEPTED_TYPES, "")
701+
validate(conf, DEFAULT_OPTS, ACCEPTED_STRINGS, ACCEPTED_TYPES, ACCEPTED_ENUMS, "")
683702

684703
if msg then
685704
notify.warn(msg .. "\n\nsee :help nvim-tree-opts for available configuration options")

lua/nvim-tree/view.lua

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

1313
local DEFAULT_MIN_WIDTH = 30
1414
local DEFAULT_MAX_WIDTH = -1
15+
local DEFAULT_LINES_EXCLUDED = {
16+
"root",
17+
}
1518
local DEFAULT_PADDING = 1
1619

1720
M.View = {
@@ -303,7 +306,7 @@ function M.open(options)
303306
end
304307

305308
local function grow()
306-
local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0
309+
local starts_at = (M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and M.View.root_excluded) and 1 or 0
307310
local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false)
308311
-- number of columns of right-padding to indicate end of path
309312
local padding = get_size(M.View.padding)
@@ -314,31 +317,25 @@ local function grow()
314317
padding = padding + wininfo[1].textoff
315318
end
316319

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
320+
local final_width = M.View.initial_width
321+
local max_width = math.huge
322+
if M.View.max_width ~= -1 then
323+
max_width = get_width(M.View.max_width)
325324
end
326325

327326
local ns_id = vim.api.nvim_get_namespaces()["NvimTreeExtmarks"]
328327
for line_nr, l in pairs(lines) do
329-
local count = vim.fn.strchars(l)
328+
local line_width = vim.fn.strchars(l)
330329
-- also add space for right-aligned icons
331330
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
331+
line_width = line_width + utils.extmarks_length(extmarks) + padding
332+
final_width = math.max(final_width, line_width)
333+
if final_width >= max_width then
334+
final_width = max_width
338335
break
339336
end
340337
end
341-
M.resize(resizing_width + padding)
338+
M.resize(final_width)
342339
end
343340

344341
function M.grow_from_content()
@@ -600,6 +597,8 @@ function M.configure_width(width)
600597
M.View.adaptive_size = true
601598
M.View.width = width.min or DEFAULT_MIN_WIDTH
602599
M.View.max_width = width.max or DEFAULT_MAX_WIDTH
600+
local lines_excluded = width.lines_excluded or DEFAULT_LINES_EXCLUDED
601+
M.View.root_excluded = vim.tbl_contains(lines_excluded, "root")
603602
M.View.padding = width.padding or DEFAULT_PADDING
604603
elseif width == nil then
605604
if M.config.width ~= nil then

0 commit comments

Comments
 (0)