refactor(vim.opt): unify vim.bo/wo building

This commit is contained in:
Lewis Russell
2022-09-08 15:56:35 +01:00
parent 164752b380
commit 7533ceec13
2 changed files with 31 additions and 65 deletions

View File

@@ -54,75 +54,41 @@ vim.env = setmetatable({}, {
end,
})
do -- buffer option accessor
local function buf_opt_validate(k)
local scope = options_info[k].scope
if scope == 'win' then
local function opt_validate(option_name, target_scope)
local scope = options_info[option_name].scope
if scope ~= target_scope then
local scope_to_string = { buf = 'buffer', win = 'window' }
error(
string.format([['%s' is a window option, not a buffer option. See ":help %s"]], k, k)
string.format(
[['%s' is a %s option, not a %s option. See ":help %s"]],
option_name,
scope_to_string[scope] or scope,
scope_to_string[target_scope] or target_scope,
option_name
)
elseif scope == 'global' then
error(
string.format([['%s' is a global option, not a buffer option. See ":help %s"]], k, k)
)
end
end
local function new_buf_opt_accessor(bufnr)
local function new_opt_accessor(handle, scope)
return setmetatable({}, {
__index = function(_, k)
if bufnr == nil and type(k) == 'number' then
return new_buf_opt_accessor(k)
if handle == nil and type(k) == 'number' then
return new_opt_accessor(k, scope)
end
buf_opt_validate(k)
return a.nvim_get_option_value(k, { buf = bufnr or 0 })
opt_validate(k, scope)
return a.nvim_get_option_value(k, { [scope] = handle or 0 })
end,
__newindex = function(_, k, v)
buf_opt_validate(k)
return a.nvim_set_option_value(k, v, { buf = bufnr or 0 })
end,
})
end
vim.bo = new_buf_opt_accessor(nil)
end
do -- window option accessor
local function win_opt_validate(k)
local scope = options_info[k].scope
if scope == 'buf' then
error(
string.format([['%s' is a buffer option, not a window option. See ":help %s"]], k, k)
)
elseif scope == 'global' then
error(
string.format([['%s' is a global option, not a window option. See ":help %s"]], k, k)
)
end
end
local function new_win_opt_accessor(winnr)
return setmetatable({}, {
__index = function(_, k)
if winnr == nil and type(k) == 'number' then
return new_win_opt_accessor(k)
end
win_opt_validate(k)
return a.nvim_get_option_value(k, { win = winnr or 0 })
end,
__newindex = function(_, k, v)
win_opt_validate(k)
return a.nvim_set_option_value(k, v, { win = winnr or 0 })
opt_validate(k, scope)
return a.nvim_set_option_value(k, v, { [scope] = handle or 0 })
end,
})
end
vim.wo = new_win_opt_accessor(nil)
end
vim.bo = new_opt_accessor(nil, 'buf')
vim.wo = new_opt_accessor(nil, 'win')
-- vim global option
-- this ONLY sets the global option. like `setglobal`

View File

@@ -1396,7 +1396,7 @@ describe('lua stdlib', function()
]]
eq('', funcs.luaeval "vim.bo.filetype")
eq(true, funcs.luaeval "vim.bo[BUF].modifiable")
matches("unknown option 'nosuchopt'$",
matches("no such option: 'nosuchopt'$",
pcall_err(exec_lua, 'return vim.bo.nosuchopt'))
matches("Expected lua string$",
pcall_err(exec_lua, 'return vim.bo[0][0].autoread'))
@@ -1417,7 +1417,7 @@ describe('lua stdlib', function()
eq(0, funcs.luaeval "vim.wo.cole")
eq(0, funcs.luaeval "vim.wo[0].cole")
eq(0, funcs.luaeval "vim.wo[1001].cole")
matches("unknown option 'notanopt'$",
matches("no such option: 'notanopt'$",
pcall_err(exec_lua, 'return vim.wo.notanopt'))
matches("Expected lua string$",
pcall_err(exec_lua, 'return vim.wo[0][0].list'))