Skip to content

Commit 51f0236

Browse files
feat: validate some option string values (#2404)
* Add check for accepted strings in user opts * option failures point to :help nvim-tree-opts --------- Co-authored-by: Alexander Courtis <[email protected]>
1 parent ec33d4b commit 51f0236

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

lua/nvim-tree.lua

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -630,10 +630,29 @@ local FIELD_OVERRIDE_TYPECHECK = {
630630
picker = { ["function"] = true, string = true },
631631
}
632632

633+
local ACCEPTED_STRINGS = {
634+
sort = {
635+
sorter = { "name", "case_sensitive", "modification_time", "extension", "suffix", "filetype" },
636+
},
637+
view = {
638+
side = { "left", "right" },
639+
signcolumn = { "yes", "no", "auto" },
640+
},
641+
renderer = {
642+
highlight_opened_files = { "none", "icon", "name", "all" },
643+
highlight_modified = { "none", "icon", "name", "all" },
644+
icons = {
645+
git_placement = { "before", "after", "signcolumn" },
646+
diagnostics_placement = { "before", "after", "signcolumn" },
647+
modified_placement = { "before", "after", "signcolumn" },
648+
},
649+
},
650+
}
651+
633652
local function validate_options(conf)
634653
local msg
635654

636-
local function validate(user, def, prefix)
655+
local function validate(user, def, strs, prefix)
637656
-- only compare tables with contents that are not integer indexed
638657
if type(user) ~= "table" or type(def) ~= "table" or not next(def) or type(next(def)) == "number" then
639658
return
@@ -650,6 +669,9 @@ local function validate_options(conf)
650669
-- option is of the wrong type and is not a function
651670
invalid =
652671
string.format("[NvimTree] invalid option: %s%s expected: %s actual: %s", prefix, k, type(def[k]), type(v))
672+
elseif type(v) == "string" and strs[k] and not vim.tbl_contains(strs[k], v) then
673+
-- option has type `string` but value is not accepted
674+
invalid = string.format("[NvimTree] invalid value for field %s%s: '%s'", prefix, k, v)
653675
end
654676

655677
if invalid then
@@ -660,16 +682,16 @@ local function validate_options(conf)
660682
end
661683
user[k] = nil
662684
else
663-
validate(v, def[k], prefix .. k .. ".")
685+
validate(v, def[k], strs[k] or {}, prefix .. k .. ".")
664686
end
665687
end
666688
end
667689
end
668690

669-
validate(conf, DEFAULT_OPTS, "")
691+
validate(conf, DEFAULT_OPTS, ACCEPTED_STRINGS, "")
670692

671693
if msg then
672-
vim.notify_once(msg .. " | see :help nvim-tree-setup for available configuration options\n", vim.log.levels.WARN)
694+
vim.notify_once(msg .. " | see :help nvim-tree-opts for available configuration options\n", vim.log.levels.WARN)
673695
end
674696
end
675697

0 commit comments

Comments
 (0)