Skip to content

Commit 3ac4432

Browse files
authored
#1166 validate config (#1195)
1 parent a94f5bf commit 3ac4432

File tree

5 files changed

+81
-31
lines changed

5 files changed

+81
-31
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ require'nvim-tree'.setup {
117117
require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS
118118
auto_reload_on_write = true,
119119
disable_netrw = false,
120-
hide_root_folder = false,
121120
hijack_cursor = false,
122121
hijack_netrw = true,
123122
hijack_unnamed_buffer_when_opening = false,
@@ -130,6 +129,7 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS
130129
view = {
131130
width = 30,
132131
height = 30,
132+
hide_root_folder = false,
133133
side = "left",
134134
preserve_window_proportions = false,
135135
number = false,
@@ -166,7 +166,7 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS
166166
},
167167
ignore_ft_on_setup = {},
168168
system_open = {
169-
cmd = nil,
169+
cmd = "",
170170
args = {},
171171
},
172172
diagnostics = {

doc/nvim-tree-lua.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ SETUP *nvim-tree.setup*
8282
To configure the tree (and make it runnable), you should call the setup
8383
function.
8484

85+
Values may be functions. Warning: this may result in unexpected behaviour.
8586
>
8687
require("nvim-tree").setup { -- BEGIN_DEFAULT_OPTS
8788
auto_reload_on_write = true,
8889
disable_netrw = false,
89-
hide_root_folder = false,
9090
hijack_cursor = false,
9191
hijack_netrw = true,
9292
hijack_unnamed_buffer_when_opening = false,
@@ -99,6 +99,7 @@ function.
9999
view = {
100100
width = 30,
101101
height = 30,
102+
hide_root_folder = false,
102103
side = "left",
103104
preserve_window_proportions = false,
104105
number = false,
@@ -135,7 +136,7 @@ function.
135136
},
136137
ignore_ft_on_setup = {},
137138
system_open = {
138-
cmd = nil,
139+
cmd = "",
139140
args = {},
140141
},
141142
diagnostics = {
@@ -311,10 +312,10 @@ Here is a list of the options available in the setup call:
311312
*nvim-tree.system_open*
312313
- |system_open|: configuration options for the system open command
313314

314-
- |system_open.cmd|: the command to run, leaving nil should work but
315+
- |system_open.cmd|: the command to run, leaving empty should work but
315316
useful if you want to override the default command with another one.
316317
type: `string`
317-
default: `nil`
318+
default: `""`
318319

319320
- |system_open.args|: the command arguments as a list
320321
type: `{string}`

lua/nvim-tree.lua

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,6 @@ end
311311
local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
312312
auto_reload_on_write = true,
313313
disable_netrw = false,
314-
hide_root_folder = false,
315314
hijack_cursor = false,
316315
hijack_netrw = true,
317316
hijack_unnamed_buffer_when_opening = false,
@@ -324,6 +323,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
324323
view = {
325324
width = 30,
326325
height = 30,
326+
hide_root_folder = false,
327327
side = "left",
328328
preserve_window_proportions = false,
329329
number = false,
@@ -360,7 +360,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
360360
},
361361
ignore_ft_on_setup = {},
362362
system_open = {
363-
cmd = nil,
363+
cmd = "",
364364
args = {},
365365
},
366366
diagnostics = {
@@ -429,16 +429,53 @@ local function merge_options(conf)
429429
return vim.tbl_deep_extend("force", DEFAULT_OPTS, conf or {})
430430
end
431431

432+
local function validate_options(conf)
433+
local msg
434+
435+
local function validate(user, def, prefix)
436+
-- only compare tables with contents that are not integer indexed
437+
if type(user) ~= "table" or type(def) ~= "table" or not next(def) or type(next(def)) == "number" then
438+
return
439+
end
440+
441+
for k, v in pairs(user) do
442+
local invalid
443+
if def[k] == nil then
444+
-- option does not exist
445+
invalid = string.format("unknown option: %s%s", prefix, k)
446+
elseif type(v) ~= type(def[k]) and type(v) ~= "function" then
447+
-- option is of the wrong type and is not a function
448+
invalid = string.format("invalid option: %s%s expected: %s actual: %s", prefix, k, type(def[k]), type(v))
449+
end
450+
451+
if invalid then
452+
if msg then
453+
msg = string.format("%s | %s", msg, invalid)
454+
else
455+
msg = string.format("%s", invalid)
456+
end
457+
user[k] = nil
458+
else
459+
validate(v, def[k], prefix .. k .. ".")
460+
end
461+
end
462+
end
463+
464+
validate(conf, DEFAULT_OPTS, "")
465+
466+
if msg then
467+
utils.warn(msg)
468+
end
469+
end
470+
432471
function M.setup(conf)
433472
legacy.migrate_legacy_options(conf or {})
434473

474+
validate_options(conf)
475+
435476
local opts = merge_options(conf)
436477
local netrw_disabled = opts.disable_netrw or opts.hijack_netrw
437478

438-
if opts.auto_close then
439-
utils.warn "auto close feature has been removed, see note in the README (tips & reminder section)"
440-
end
441-
442479
_config.update_focused_file = opts.update_focused_file
443480
_config.open_on_setup = opts.open_on_setup
444481
_config.open_on_setup_file = opts.open_on_setup_file

lua/nvim-tree/actions/system-open.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ local M = {
99
}
1010

1111
function M.fn(node)
12-
if not M.config.system_open.cmd then
12+
if #M.config.system_open.cmd == 0 then
1313
require("nvim-tree.utils").warn "Cannot open file with system application. Unrecognized platform."
1414
return
1515
end
@@ -53,7 +53,7 @@ end
5353
function M.setup(opts)
5454
M.config.system_open = opts or {}
5555

56-
if not M.config.system_open.cmd then
56+
if #M.config.system_open.cmd == 0 then
5757
if M.config.is_windows then
5858
M.config.system_open = {
5959
cmd = "cmd",

lua/nvim-tree/legacy.lua

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local M = {}
55
-- TODO update git.io/JPhyt when adding a migration
66

77
-- migrate the g: to o if the user has not specified that when calling setup
8-
local migrations = {
8+
local g_migrations = {
99
nvim_tree_disable_netrw = function(o)
1010
if o.disable_netrw == nil then
1111
o.disable_netrw = vim.g.nvim_tree_disable_netrw ~= 0
@@ -178,22 +178,7 @@ local migrations = {
178178
end,
179179
}
180180

181-
function M.migrate_legacy_options(opts)
182-
local msg = nil
183-
184-
-- g: options
185-
for g, m in pairs(migrations) do
186-
if vim.fn.exists("g:" .. g) ~= 0 then
187-
m(opts)
188-
msg = (msg and msg .. ", " or "Following options were moved to setup, see git.io/JPhyt: ") .. g
189-
end
190-
end
191-
192-
if msg then
193-
require("nvim-tree.utils").warn(msg)
194-
end
195-
196-
-- regular opts
181+
local function refactored(opts)
197182
if opts.view then
198183
if opts.view.mappings then
199184
if opts.view.mappings.list then
@@ -207,4 +192,31 @@ function M.migrate_legacy_options(opts)
207192
end
208193
end
209194

195+
local function removed(opts)
196+
if opts.auto_close then
197+
utils.warn "auto close feature has been removed, see note in the README (tips & reminder section)"
198+
opts.auto_close = nil
199+
end
200+
end
201+
202+
function M.migrate_legacy_options(opts)
203+
-- g: options
204+
local msg
205+
for g, m in pairs(g_migrations) do
206+
if vim.fn.exists("g:" .. g) ~= 0 then
207+
m(opts)
208+
msg = (msg and msg .. ", " or "Following options were moved to setup, see git.io/JPhyt: ") .. g
209+
end
210+
end
211+
if msg then
212+
utils.warn(msg)
213+
end
214+
215+
-- silently move
216+
refactored(opts)
217+
218+
-- warn and delete
219+
removed(opts)
220+
end
221+
210222
return M

0 commit comments

Comments
 (0)