From 51f02366deed1f21153c564b546fed813acfd6d8 Mon Sep 17 00:00:00 2001 From: Azad <49314270+Akmadan23@users.noreply.github.com> Date: Sat, 9 Sep 2023 03:07:06 +0200 Subject: [PATCH] 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 --- lua/nvim-tree.lua | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index a11ed71a..006a104f 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -630,10 +630,29 @@ local FIELD_OVERRIDE_TYPECHECK = { picker = { ["function"] = true, string = true }, } +local ACCEPTED_STRINGS = { + sort = { + sorter = { "name", "case_sensitive", "modification_time", "extension", "suffix", "filetype" }, + }, + view = { + side = { "left", "right" }, + signcolumn = { "yes", "no", "auto" }, + }, + renderer = { + highlight_opened_files = { "none", "icon", "name", "all" }, + highlight_modified = { "none", "icon", "name", "all" }, + icons = { + git_placement = { "before", "after", "signcolumn" }, + diagnostics_placement = { "before", "after", "signcolumn" }, + modified_placement = { "before", "after", "signcolumn" }, + }, + }, +} + local function validate_options(conf) local msg - local function validate(user, def, prefix) + local function validate(user, def, strs, prefix) -- only compare tables with contents that are not integer indexed if type(user) ~= "table" or type(def) ~= "table" or not next(def) or type(next(def)) == "number" then return @@ -650,6 +669,9 @@ local function validate_options(conf) -- option is of the wrong type and is not a function invalid = string.format("[NvimTree] invalid option: %s%s expected: %s actual: %s", prefix, k, type(def[k]), type(v)) + elseif type(v) == "string" and strs[k] and not vim.tbl_contains(strs[k], v) then + -- option has type `string` but value is not accepted + invalid = string.format("[NvimTree] invalid value for field %s%s: '%s'", prefix, k, v) end if invalid then @@ -660,16 +682,16 @@ local function validate_options(conf) end user[k] = nil else - validate(v, def[k], prefix .. k .. ".") + validate(v, def[k], strs[k] or {}, prefix .. k .. ".") end end end end - validate(conf, DEFAULT_OPTS, "") + validate(conf, DEFAULT_OPTS, ACCEPTED_STRINGS, "") if msg then - vim.notify_once(msg .. " | see :help nvim-tree-setup for available configuration options\n", vim.log.levels.WARN) + vim.notify_once(msg .. " | see :help nvim-tree-opts for available configuration options\n", vim.log.levels.WARN) end end