Skip to content

Commit 5bbd3a0

Browse files
authored
#1166 validate user's options (#1177)
1 parent 74ae970 commit 5bbd3a0

File tree

5 files changed

+77
-31
lines changed

5 files changed

+77
-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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ function.
8686
require("nvim-tree").setup { -- BEGIN_DEFAULT_OPTS
8787
auto_reload_on_write = true,
8888
disable_netrw = false,
89-
hide_root_folder = false,
9089
hijack_cursor = false,
9190
hijack_netrw = true,
9291
hijack_unnamed_buffer_when_opening = false,
@@ -99,6 +98,7 @@ function.
9998
view = {
10099
width = 30,
101100
height = 30,
101+
hide_root_folder = false,
102102
side = "left",
103103
preserve_window_proportions = false,
104104
number = false,
@@ -135,7 +135,7 @@ function.
135135
},
136136
ignore_ft_on_setup = {},
137137
system_open = {
138-
cmd = nil,
138+
cmd = "",
139139
args = {},
140140
},
141141
diagnostics = {
@@ -311,10 +311,10 @@ Here is a list of the options available in the setup call:
311311
*nvim-tree.system_open*
312312
- |system_open|: configuration options for the system open command
313313

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

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

lua/nvim-tree.lua

Lines changed: 40 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,50 @@ 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+
if type(user) ~= "table" or type(def) ~= "table" or not next(def) then
437+
return
438+
end
439+
440+
for k, v in pairs(user) do
441+
local invalid
442+
if def[k] == nil then
443+
invalid = string.format("unknown option: %s%s", prefix, k)
444+
elseif type(v) ~= type(def[k]) then
445+
invalid = string.format("invalid option: %s%s expected: %s actual: %s", prefix, k, type(def[k]), type(v))
446+
end
447+
448+
if invalid then
449+
if msg then
450+
msg = string.format("%s | %s", msg, invalid)
451+
else
452+
msg = string.format("%s", invalid)
453+
end
454+
user[k] = nil
455+
else
456+
validate(v, def[k], prefix .. k .. ".")
457+
end
458+
end
459+
end
460+
461+
validate(conf, DEFAULT_OPTS, "")
462+
463+
if msg then
464+
utils.warn(msg)
465+
end
466+
end
467+
432468
function M.setup(conf)
433469
legacy.migrate_legacy_options(conf or {})
434470

471+
validate_options(conf)
472+
435473
local opts = merge_options(conf)
436474
local netrw_disabled = opts.disable_netrw or opts.hijack_netrw
437475

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-
442476
_config.update_focused_file = opts.update_focused_file
443477
_config.open_on_setup = opts.open_on_setup
444478
_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)