diff --git a/lua/user/check/exists.lua b/lua/user/check/exists.lua index a6bdd2ce..1bce8a45 100644 --- a/lua/user/check/exists.lua +++ b/lua/user/check/exists.lua @@ -14,26 +14,22 @@ local is_num = Value.is_num local is_fun = Value.is_fun local empty = Value.empty ----@type User.Check.Existance -M = { - module = function(mod, return_mod) - return_mod = is_bool(return_mod) and return_mod or false - - ---@type boolean - local res - ---@type unknown - local m - res, m = pcall(require, mod) - - if return_mod then - return not is_nil(m) and m or nil - else - return res - end - end, -} +local function module(mod, return_mod) + return_mod = is_bool(return_mod) and return_mod or false + + ---@type boolean + local res + ---@type unknown + local m + res, m = pcall(require, mod) -function M.vim_has(expr) + if return_mod then + return not is_nil(m) and m or nil + else + return res + end +end +local function vim_has(expr) if is_str(expr) then return vim.fn.has(expr) == 1 end @@ -42,7 +38,7 @@ function M.vim_has(expr) local res = false for _, v in next, expr do - if not M.vim_has(v) then + if not vim_has(v) then return false end end @@ -52,8 +48,7 @@ function M.vim_has(expr) return false end - -function M.vim_exists(expr) +local function vim_exists(expr) local exists = vim.fn.exists if is_str(expr) then @@ -63,7 +58,7 @@ function M.vim_exists(expr) if is_tbl(expr) and not empty(expr) then local res = false for _, v in next, expr do - res = M.vim_exists(v) + res = vim_exists(v) if not res then break @@ -75,11 +70,6 @@ function M.vim_exists(expr) return false end - -function M.vim_isdir(path) - return (is_str(path) and not empty(path)) and (vim.fn.isdirectory(path) == 1) or false -end - local function env_vars(vars, fallback) local environment = vim.fn.environ() @@ -109,12 +99,7 @@ local function env_vars(vars, fallback) return res end - -M.env_vars = env_vars - -function M.executable(exe, fallback) - local executable = vim.fn.executable - +local function executable(exe, fallback) if not (is_str(exe) or is_tbl(exe)) then error('(user.check.exists.executable): Argument type is neither string nor table') end @@ -124,10 +109,10 @@ function M.executable(exe, fallback) local res = false if is_str(exe) then - res = executable(exe) == 1 + res = vim.fn.executable(exe) == 1 elseif is_tbl(exe) then for _, v in next, exe do - res = M.executable(v) + res = executable(v) if not res then break @@ -141,9 +126,8 @@ function M.executable(exe, fallback) return res end - -function M.modules(mod, need_all) - local exists = M.module +local function modules(mod, need_all) + local exists = module if not (is_str(mod) or is_tbl(mod)) or empty(mod) then error('`(user.check.exists.modules)`: Input is neither a string nor a table.') @@ -177,5 +161,19 @@ function M.modules(mod, need_all) return res end +local function vim_isdir(path) + return (is_str(path) and not empty(path)) and (vim.fn.isdirectory(path) == 1) or false +end + +---@type User.Check.Existance +M = { + module = module, + vim_has = vim_has, + vim_exists = vim_exists, + env_vars = env_vars, + executable = executable, + modules = modules, + vim_isdir = vim_isdir, +} return M diff --git a/lua/user/check/init.lua b/lua/user/check/init.lua index f288edfd..437a7a61 100644 --- a/lua/user/check/init.lua +++ b/lua/user/check/init.lua @@ -10,27 +10,27 @@ local Exists = require('user.check.exists') --- Checking Utilities ---@type User.Check local M = { - value = Value, - exists = Exists, + value = require('user.check.value'), + exists = require('user.check.exists'), + + --- Check whether Nvim is running in a Linux Console rather than a `pty`. + --- --- + --- ## Description + --- This function can be useful for (un)loading certain elements that conflict with the Linux console, for example. + --- --- + --- ## Return + --- A boolean that confirms whether the environment is a Linux Console. + --- --- + in_console = function() + local env = vim.fn.environ() + ---@type string + local TERM = env['TERM'] + + --- TODO: This is not a good enough check. Must find a better solution. + return vim.tbl_contains({ 'screen', 'linux' }, TERM) + end, } ---- Check whether Nvim is running in a Linux Console rather than a `pty`. ---- --- ---- ## Description ---- This function can be useful for (un)loading certain elements that conflict with the Linux console, for example. ---- --- ---- ## Return ---- A boolean that confirms whether the environment is a Linux Console. ---- --- -function M.in_console() - local env = vim.fn.environ() - ---@type string - local TERM = env['TERM'] - - --- TODO: This is not a good enough check. Must find a better solution. - return vim.tbl_contains({ 'screen', 'linux' }, TERM) -end - function M.new() local self = setmetatable({}, { __index = M }) diff --git a/lua/user/check/value.lua b/lua/user/check/value.lua index 13fafc1a..f5fa4bc6 100644 --- a/lua/user/check/value.lua +++ b/lua/user/check/value.lua @@ -5,6 +5,41 @@ require('user.types.user.check') +--- Checks whether a value is `nil`, i.e. non existant or explicitly set as nil. +--- ## Parameters +--- +--- * `var`: Any data type to be checked if it's nil. +--- **Keep in mind that if `multiple` is set to `true`, this _MUST_ be a _non-empty_ table**. +--- Otherwise it will be flagged as non-existant and the function will return `true`. +--- +--- * `multiple`: Tell the function you're checking for multiple values. (Default: `false`). +--- If set to `true`, every element of the table will be checked. +--- If **any** element doesn't exist or is `nil`, the function automatically returns false. +--- +--- ## Return +--- A boolean value indicating whether the data is `nil` or doesn't exist. +--- --- +local function is_nil(var, multiple) + multiple = (multiple ~= nil and type(multiple) == 'boolean') and multiple or false + + if not multiple then + return var == nil + end + + --- Treat `var` as a table from here on + if type(var) ~= 'table' or vim.tbl_isempty(var) then + return false + end + + for _, v in next, var do + if v ~= nil then + return false + end + end + + return true +end + ---@type User.Check.Value local M = { -- NOTE: We define `is_nil` first as it's used by the other checkers. @@ -23,33 +58,12 @@ local M = { --- ## Return --- A boolean value indicating whether the data is `nil` or doesn't exist. --- --- - is_nil = function(var, multiple) - multiple = (multiple ~= nil and type(multiple) == 'boolean') and multiple or false - - if not multiple then - return var == nil - end - - --- Treat `var` as a table from here on - if type(var) ~= 'table' or vim.tbl_isempty(var) then - return false - end - - for _, v in next, var do - if v ~= nil then - return false - end - end - - return true - end, + is_nil = is_nil, } ---@type fun(t: Types): ValueFunc local function type_fun(t) return function(var, multiple) - local is_nil = M.is_nil - multiple = (not is_nil(multiple) and type(multiple) == 'boolean') and multiple or false if not multiple then @@ -164,7 +178,6 @@ M.is_tbl = type_fun('table') --- A boolean value indicating whether the data is an integer or not. --- --- function M.is_int(var, multiple) - local is_nil = M.is_nil local is_tbl = M.is_tbl local is_bool = M.is_bool local is_num = M.is_num @@ -222,8 +235,7 @@ function M.empty(v) return true end -function M.fields(fields, T) - local is_nil = M.is_nil +local function fields(fields, T) local is_tbl = M.is_tbl local is_str = M.is_str local is_num = M.is_num @@ -249,6 +261,7 @@ function M.fields(fields, T) return true end +M.fields = fields function M.tbl_values(values, T, return_keys) local is_tbl = M.is_tbl diff --git a/lua/user/highlight.lua b/lua/user/highlight.lua index b1cd448d..cf282673 100644 --- a/lua/user/highlight.lua +++ b/lua/user/highlight.lua @@ -3,25 +3,28 @@ require('user.types.user.highlight') -local Check = require('user.check') +local Value = require('user.check.value') -local is_nil = Check.value.is_nil -local is_str = Check.value.is_str -local is_tbl = Check.value.is_tbl -local is_int = Check.value.is_int -local empty = Check.value.empty +local is_nil = Value.is_nil +local is_str = Value.is_str +local is_tbl = Value.is_tbl +local is_int = Value.is_int +local empty = Value.empty ----@type User.Hl -local M = { - hl = function(name, opts, bufnr) - if not (is_str(name) and is_tbl(opts)) or empty(name) then - error('(user.highlight.hl): A highlight value is not permitted!') - end +local function hl(name, opts, bufnr) + if not (is_str(name) and is_tbl(opts)) or empty(name) then + error('(user.highlight.hl): A highlight value is not permitted!') + end - bufnr = is_int(bufnr) and bufnr or 0 + bufnr = is_int(bufnr) and bufnr or 0 - vim.api.nvim_set_hl(bufnr, name, opts) - end, + vim.api.nvim_set_hl(bufnr, name, opts) +end + +---@type User.Hl +---@diagnostic disable-next-line:missing-fields +local M = { + hl = hl, } function M.hl_from_arr(arr) @@ -34,7 +37,7 @@ function M.hl_from_arr(arr) error('(user.highlight.hl_from_arr): A highlight value is not permitted!') end - M.hl(T.name, T.opts) + hl(T.name, T.opts) end end @@ -61,7 +64,7 @@ function M.hl_from_dict(dict) for k, v in next, dict do if (is_str(k) and is_tbl(v)) and not empty(k) then - M.hl(k, v) + hl(k, v) else error('(user.highlight.hl_from_dict): A highlight value is not permitted!') end diff --git a/lua/user/util/init.lua b/lua/user/util/init.lua index f54b1002..677ed2dc 100644 --- a/lua/user/util/init.lua +++ b/lua/user/util/init.lua @@ -3,11 +3,7 @@ require('user.types.user.util') ----@type User.Util ----@diagnostic disable-next-line:missing-fields -local M = {} - -function M.xor(x, y) +local function xor(x, y) if not require('user.check.value').is_bool({ x, y }, true) then error('(user.util.xor): An argument is not of boolean type') end @@ -15,6 +11,12 @@ function M.xor(x, y) return (x and not y) or (not x and y) end +---@type User.Util +---@diagnostic disable-next-line:missing-fields +local M = { + xor = xor, +} + function M.strip_fields(T, fields) local Value = require('user.check.value') @@ -76,12 +78,9 @@ function M.strip_values(T, values, max_instances) error('(user.util.strip_values): No values given') end - local xor = M.xor - max_instances = is_int(max_instances) and max_instances or 0 local res = {} - local count = 0 for k, v in next, T do @@ -103,7 +102,7 @@ function M.strip_values(T, values, max_instances) return res end -function M.ft_set(s, bufnr) +local function ft_set(s, bufnr) local Value = require('user.check.value') local is_int = Value.is_int @@ -117,6 +116,7 @@ function M.ft_set(s, bufnr) end end end +M.ft_set = ft_set function M.ft_get(bufnr) bufnr = require('user.check.value').is_int(bufnr) and bufnr or 0 @@ -135,7 +135,7 @@ function M.assoc() local is_str = Value.is_str local empty = Value.empty - local ft = M.ft_set + local ft = ft_set local au = vim.api.nvim_create_autocmd @@ -209,8 +209,8 @@ function M.assoc() pattern = 'lua', callback = function() local map_dict = require('user.maps').map_dict - local WK = require('user.maps').wk - local desc = require('user.maps').kmap.desc + local WK = require('user.maps.wk') + local desc = require('user.maps.kmap').desc if require('user.check.exists').executable('stylua') then map_dict({ @@ -232,30 +232,32 @@ function M.assoc() table.insert(aus[1].opts_tbl, { pattern = '*.org', callback = ft('org'), group = group }) end + local notify = require('user.util.notify').notify + for _, v in next, aus do if not (is_str(v.events) or is_tbl(v.events)) or empty(v.events) then - M.notify.notify('(user.assoc): Event type `' .. type(v.events) .. '` is neither string nor table', 'error') + notify('(user.assoc): Event type `' .. type(v.events) .. '` is neither string nor table', 'error') goto continue end if not is_tbl(v.opts_tbl) or empty(v.opts_tbl) then - M.notify.notify('(user.assoc): Event options are not in a table or it is empty', 'error') + notify('(user.assoc): Event options are not in a table or it is empty', 'error') goto continue end for _, o in next, v.opts_tbl do if not is_tbl(o) or empty(o) then - M.notify.notify('(user.assoc): Event option is not a table or an empty one', 'error') + notify('(user.assoc): Event option is not a table or an empty one', 'error') goto continue end if not is_nil(o.pattern) and (not is_str(o.pattern) or empty(o.pattern)) then - M.notify.notify('(user.assoc): Pattern is not a string or is an empty one', 'error') + notify('(user.assoc): Pattern is not a string or is an empty one', 'error') goto continue end if not is_fun(o.callback) then - M.notify.notify('(user.assoc): Callback is not a function', 'error') + notify('(user.assoc): Callback is not a function', 'error') goto continue end