@@ -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
616625local 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\n see :help nvim-tree-opts for available configuration options" )
0 commit comments