Skip to content

#1166 validate config #1195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ require'nvim-tree'.setup {
require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS
auto_reload_on_write = true,
disable_netrw = false,
hide_root_folder = false,
hijack_cursor = false,
hijack_netrw = true,
hijack_unnamed_buffer_when_opening = false,
Expand All @@ -130,6 +129,7 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS
view = {
width = 30,
height = 30,
hide_root_folder = false,
side = "left",
preserve_window_proportions = false,
number = false,
Expand Down Expand Up @@ -166,7 +166,7 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS
},
ignore_ft_on_setup = {},
system_open = {
cmd = nil,
cmd = "",
args = {},
},
diagnostics = {
Expand Down
9 changes: 5 additions & 4 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ SETUP *nvim-tree.setup*
To configure the tree (and make it runnable), you should call the setup
function.

Values may be functions. Warning: this may result in unexpected behaviour.
>
require("nvim-tree").setup { -- BEGIN_DEFAULT_OPTS
auto_reload_on_write = true,
disable_netrw = false,
hide_root_folder = false,
hijack_cursor = false,
hijack_netrw = true,
hijack_unnamed_buffer_when_opening = false,
Expand All @@ -99,6 +99,7 @@ function.
view = {
width = 30,
height = 30,
hide_root_folder = false,
side = "left",
preserve_window_proportions = false,
number = false,
Expand Down Expand Up @@ -135,7 +136,7 @@ function.
},
ignore_ft_on_setup = {},
system_open = {
cmd = nil,
cmd = "",
args = {},
},
diagnostics = {
Expand Down Expand Up @@ -311,10 +312,10 @@ Here is a list of the options available in the setup call:
*nvim-tree.system_open*
- |system_open|: configuration options for the system open command

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

- |system_open.args|: the command arguments as a list
type: `{string}`
Expand Down
49 changes: 43 additions & 6 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ end
local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
auto_reload_on_write = true,
disable_netrw = false,
hide_root_folder = false,
hijack_cursor = false,
hijack_netrw = true,
hijack_unnamed_buffer_when_opening = false,
Expand All @@ -324,6 +323,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
view = {
width = 30,
height = 30,
hide_root_folder = false,
side = "left",
preserve_window_proportions = false,
number = false,
Expand Down Expand Up @@ -360,7 +360,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
},
ignore_ft_on_setup = {},
system_open = {
cmd = nil,
cmd = "",
args = {},
},
diagnostics = {
Expand Down Expand Up @@ -429,16 +429,53 @@ local function merge_options(conf)
return vim.tbl_deep_extend("force", DEFAULT_OPTS, conf or {})
end

local function validate_options(conf)
local msg

local function validate(user, def, 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
end

for k, v in pairs(user) do
local invalid
if def[k] == nil then
-- option does not exist
invalid = string.format("unknown option: %s%s", prefix, k)
elseif type(v) ~= type(def[k]) and type(v) ~= "function" then
-- option is of the wrong type and is not a function
invalid = string.format("invalid option: %s%s expected: %s actual: %s", prefix, k, type(def[k]), type(v))
end

if invalid then
if msg then
msg = string.format("%s | %s", msg, invalid)
else
msg = string.format("%s", invalid)
end
user[k] = nil
else
validate(v, def[k], prefix .. k .. ".")
end
end
end

validate(conf, DEFAULT_OPTS, "")

if msg then
utils.warn(msg)
end
end

function M.setup(conf)
legacy.migrate_legacy_options(conf or {})

validate_options(conf)

local opts = merge_options(conf)
local netrw_disabled = opts.disable_netrw or opts.hijack_netrw

if opts.auto_close then
utils.warn "auto close feature has been removed, see note in the README (tips & reminder section)"
end

_config.update_focused_file = opts.update_focused_file
_config.open_on_setup = opts.open_on_setup
_config.open_on_setup_file = opts.open_on_setup_file
Expand Down
4 changes: 2 additions & 2 deletions lua/nvim-tree/actions/system-open.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local M = {
}

function M.fn(node)
if not M.config.system_open.cmd then
if #M.config.system_open.cmd == 0 then
require("nvim-tree.utils").warn "Cannot open file with system application. Unrecognized platform."
return
end
Expand Down Expand Up @@ -53,7 +53,7 @@ end
function M.setup(opts)
M.config.system_open = opts or {}

if not M.config.system_open.cmd then
if #M.config.system_open.cmd == 0 then
if M.config.is_windows then
M.config.system_open = {
cmd = "cmd",
Expand Down
46 changes: 29 additions & 17 deletions lua/nvim-tree/legacy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local M = {}
-- TODO update git.io/JPhyt when adding a migration

-- migrate the g: to o if the user has not specified that when calling setup
local migrations = {
local g_migrations = {
nvim_tree_disable_netrw = function(o)
if o.disable_netrw == nil then
o.disable_netrw = vim.g.nvim_tree_disable_netrw ~= 0
Expand Down Expand Up @@ -178,22 +178,7 @@ local migrations = {
end,
}

function M.migrate_legacy_options(opts)
local msg = nil

-- g: options
for g, m in pairs(migrations) do
if vim.fn.exists("g:" .. g) ~= 0 then
m(opts)
msg = (msg and msg .. ", " or "Following options were moved to setup, see git.io/JPhyt: ") .. g
end
end

if msg then
require("nvim-tree.utils").warn(msg)
end

-- regular opts
local function refactored(opts)
if opts.view then
if opts.view.mappings then
if opts.view.mappings.list then
Expand All @@ -207,4 +192,31 @@ function M.migrate_legacy_options(opts)
end
end

local function removed(opts)
if opts.auto_close then
utils.warn "auto close feature has been removed, see note in the README (tips & reminder section)"
opts.auto_close = nil
end
end

function M.migrate_legacy_options(opts)
-- g: options
local msg
for g, m in pairs(g_migrations) do
if vim.fn.exists("g:" .. g) ~= 0 then
m(opts)
msg = (msg and msg .. ", " or "Following options were moved to setup, see git.io/JPhyt: ") .. g
end
end
if msg then
utils.warn(msg)
end

-- silently move
refactored(opts)

-- warn and delete
removed(opts)
end

return M