|
|
|
@ -1,23 +1,15 @@
|
|
|
|
|
--- @class libmodal.utils.Vars
|
|
|
|
|
--- @field private mode_name string the highlight group to use when printing `str`
|
|
|
|
|
--- @field private value? unknown the local value of the variable
|
|
|
|
|
--- @field private var_name string the highlight group to use when printing `str`
|
|
|
|
|
local Vars = require('libmodal.utils.classes').new()
|
|
|
|
|
|
|
|
|
|
--- @return unknown `vim.g[self:name()])`
|
|
|
|
|
function Vars:get()
|
|
|
|
|
return vim.g[self:name()]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- @return string name the global Neovim setting
|
|
|
|
|
function Vars:name()
|
|
|
|
|
return self.mode_name .. self.var_name
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- create a new set of variables
|
|
|
|
|
--- @param var_name string the name of the key used to refer to this variable in `Vars`.
|
|
|
|
|
--- @param mode_name string the name of the mode
|
|
|
|
|
--- @param default_global? unknown the default global value
|
|
|
|
|
--- @return libmodal.utils.Vars
|
|
|
|
|
function Vars.new(var_name, mode_name)
|
|
|
|
|
function Vars.new(var_name, mode_name, default_global)
|
|
|
|
|
local self = setmetatable({}, Vars)
|
|
|
|
|
|
|
|
|
|
--- @param str_with_spaces string
|
|
|
|
@ -42,15 +34,70 @@ function Vars.new(var_name, mode_name)
|
|
|
|
|
|
|
|
|
|
self.mode_name = no_spaces(mode_name, string.lower)
|
|
|
|
|
self.var_name = 'Mode' .. no_spaces(var_name, string.upper)
|
|
|
|
|
self.value = nil
|
|
|
|
|
|
|
|
|
|
if default_global ~= nil and self:get_global() == nil then
|
|
|
|
|
self:set_global(default_global)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- @generic T
|
|
|
|
|
--- @param val T set `g:{self:name()})` equal to this value
|
|
|
|
|
--- @return T value the local value if it exists, or the global value
|
|
|
|
|
function Vars:get()
|
|
|
|
|
local local_value = self:get_local()
|
|
|
|
|
if local_value == nil then
|
|
|
|
|
return self:get_global()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return local_value
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- @generic T
|
|
|
|
|
--- @return T global_value the global value
|
|
|
|
|
function Vars:get_local()
|
|
|
|
|
return self.value
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- @generic T
|
|
|
|
|
--- @return T global_value the global value
|
|
|
|
|
function Vars:get_global()
|
|
|
|
|
return vim.g[self:name()]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- @return string name the global Neovim setting
|
|
|
|
|
function Vars:name()
|
|
|
|
|
return self.mode_name .. self.var_name
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- NOTE: the local value is only set if not `nil`, for backwards compatibility purposes.
|
|
|
|
|
--- local values did not always exist, and since `get` prefers local values, it may
|
|
|
|
|
--- too-eagerly shadow the global variable.
|
|
|
|
|
--- @param val unknown set local value if it exists, or the global value
|
|
|
|
|
--- @return nil
|
|
|
|
|
function Vars:set(val)
|
|
|
|
|
vim.api.nvim_set_var(self:name(), val)
|
|
|
|
|
if self:get_local() == nil then
|
|
|
|
|
self:set_global(val)
|
|
|
|
|
else
|
|
|
|
|
self:set_local(val)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- @param val unknown set the local value equal to this
|
|
|
|
|
--- @return nil
|
|
|
|
|
function Vars:set_local(val)
|
|
|
|
|
self.value = val
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- @param val unknown set the global value equal to this
|
|
|
|
|
--- @return nil
|
|
|
|
|
function Vars:set_global(val)
|
|
|
|
|
if val == nil then
|
|
|
|
|
vim.api.nvim_del_var(self:name()) -- because `nvim_set_var('foo', nil)` actually sets 'foo' to `vim.NIL`
|
|
|
|
|
else
|
|
|
|
|
vim.api.nvim_set_var(self:name(), val)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return Vars
|
|
|
|
|