|
|
|
@ -1,36 +1,36 @@
|
|
|
|
|
--- @class libmodal.utils.Vars
|
|
|
|
|
--- @param str_with_spaces string
|
|
|
|
|
--- @param first_letter_modifier fun(s: string): string
|
|
|
|
|
local function no_spaces(str_with_spaces, first_letter_modifier)
|
|
|
|
|
local split_str = vim.split(str_with_spaces:gsub(vim.pesc '_', vim.pesc ' '), ' ')
|
|
|
|
|
|
|
|
|
|
--- @param str string
|
|
|
|
|
--- @param func fun(s: string): string
|
|
|
|
|
local function camel_case(str, func)
|
|
|
|
|
return func(str:sub(0, 1) or '') .. (str:sub(2) or ''):lower()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
split_str[1] = camel_case(split_str[1], first_letter_modifier)
|
|
|
|
|
|
|
|
|
|
for i = 2, #split_str do split_str[i] =
|
|
|
|
|
camel_case(split_str[i], string.upper)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return table.concat(split_str)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- @class libmodal.utils.Var
|
|
|
|
|
--- @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()
|
|
|
|
|
local Var = require('libmodal.utils.classes').new()
|
|
|
|
|
|
|
|
|
|
--- create a new set of variables
|
|
|
|
|
--- @param mode_name string the name of the mode
|
|
|
|
|
--- @param var_name string the name of the key used to refer to this variable in `Vars`.
|
|
|
|
|
--- @param var_name string the name of the key used to refer to this variable in `Var`.
|
|
|
|
|
--- @param default_global? unknown the default global value
|
|
|
|
|
--- @return libmodal.utils.Vars
|
|
|
|
|
function Vars.new(mode_name, var_name, default_global)
|
|
|
|
|
local self = setmetatable({}, Vars)
|
|
|
|
|
|
|
|
|
|
--- @param str_with_spaces string
|
|
|
|
|
--- @param first_letter_modifier fun(s: string): string
|
|
|
|
|
local function no_spaces(str_with_spaces, first_letter_modifier)
|
|
|
|
|
local split_str = vim.split(str_with_spaces:gsub(vim.pesc '_', vim.pesc ' '), ' ')
|
|
|
|
|
|
|
|
|
|
--- @param str string
|
|
|
|
|
--- @param func fun(s: string): string
|
|
|
|
|
local function camel_case(str, func)
|
|
|
|
|
return func(str:sub(0, 1) or '') .. (str:sub(2) or ''):lower()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
split_str[1] = camel_case(split_str[1], first_letter_modifier)
|
|
|
|
|
|
|
|
|
|
for i = 2, #split_str do split_str[i] =
|
|
|
|
|
camel_case(split_str[i], string.upper)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return table.concat(split_str)
|
|
|
|
|
end
|
|
|
|
|
--- @return libmodal.utils.Var
|
|
|
|
|
function Var.new(mode_name, var_name, default_global)
|
|
|
|
|
local self = setmetatable({}, Var)
|
|
|
|
|
|
|
|
|
|
self.mode_name = no_spaces(mode_name, string.lower)
|
|
|
|
|
self.var_name = 'Mode' .. no_spaces(var_name, string.upper)
|
|
|
|
@ -45,7 +45,7 @@ end
|
|
|
|
|
|
|
|
|
|
--- @generic T
|
|
|
|
|
--- @return T value the local value if it exists, or the global value
|
|
|
|
|
function Vars:get()
|
|
|
|
|
function Var:get()
|
|
|
|
|
local local_value = self:get_local()
|
|
|
|
|
if local_value == nil then
|
|
|
|
|
return self:get_global()
|
|
|
|
@ -56,18 +56,18 @@ end
|
|
|
|
|
|
|
|
|
|
--- @generic T
|
|
|
|
|
--- @return T global_value the global value
|
|
|
|
|
function Vars:get_local()
|
|
|
|
|
function Var:get_local()
|
|
|
|
|
return self.value
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- @generic T
|
|
|
|
|
--- @return T global_value the global value
|
|
|
|
|
function Vars:get_global()
|
|
|
|
|
function Var:get_global()
|
|
|
|
|
return vim.g[self:name()]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- @return string name the global Neovim setting
|
|
|
|
|
function Vars:name()
|
|
|
|
|
function Var:name()
|
|
|
|
|
return self.mode_name .. self.var_name
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
@ -76,7 +76,7 @@ end
|
|
|
|
|
--- 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)
|
|
|
|
|
function Var:set(val)
|
|
|
|
|
if self:get_local() == nil then
|
|
|
|
|
self:set_global(val)
|
|
|
|
|
else
|
|
|
|
@ -86,13 +86,13 @@ end
|
|
|
|
|
|
|
|
|
|
--- @param val unknown set the local value equal to this
|
|
|
|
|
--- @return nil
|
|
|
|
|
function Vars:set_local(val)
|
|
|
|
|
function Var:set_local(val)
|
|
|
|
|
self.value = val
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- @param val unknown set the global value equal to this
|
|
|
|
|
--- @return nil
|
|
|
|
|
function Vars:set_global(val)
|
|
|
|
|
function Var: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
|
|
|
|
@ -100,4 +100,4 @@ function Vars:set_global(val)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return Vars
|
|
|
|
|
return Var
|