From 34c69be1d8ddc2d70184dad820713f61e426111d Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Wed, 4 Sep 2024 20:44:00 -0600 Subject: [PATCH] fix(api): Docs and code fixed; implemented new `check.value.empty()` usage. Signed-off-by: Guennadi Maximov C --- lua/user_api/check/exists.lua | 43 ++++++++++++++----------------- lua/user_api/check/init.lua | 11 +++++--- lua/user_api/check/value.lua | 5 ++-- lua/user_api/highlight.lua | 15 +++++------ lua/user_api/types/user/check.lua | 2 +- lua/user_api/util/autocmd.lua | 2 +- 6 files changed, 40 insertions(+), 38 deletions(-) diff --git a/lua/user_api/check/exists.lua b/lua/user_api/check/exists.lua index be89363..168396d 100644 --- a/lua/user_api/check/exists.lua +++ b/lua/user_api/check/exists.lua @@ -6,10 +6,11 @@ local is_nil = Value.is_nil local is_bool = Value.is_bool local is_str = Value.is_str local is_tbl = Value.is_tbl -local is_num = Value.is_num local is_fun = Value.is_fun local empty = Value.empty +local ERROR = vim.log.levels.ERROR + ---@type User.Check.Existance ---@diagnostic disable-next-line:missing-fields local M = {} @@ -52,26 +53,24 @@ function M.modules(mod, need_all) need_all = is_bool(need_all) and need_all or false + if is_str(mod) then + return not need_all and exists(mod) or { [mod] = exists(mod) } + end + ---@type boolean|table - local res = false + local res = {} - if is_str(mod) then - res = not need_all and exists(mod) or { [mod] = exists(mod) } - else - res = {} - - for _, v in next, mod do - local r = exists(v) - - if need_all then - res[v] = r - else - res = r - - -- Break when a module is not found - if not r then - break - end + for _, v in next, mod do + local r = exists(v) + + if need_all then + res[v] = r + else + res = r + + -- Break when a module is not found + if not res then + break end end end @@ -87,8 +86,6 @@ function M.vim_has(expr) end if is_tbl(expr) and not empty(expr) then - local res = false - for _, v in next, expr do if not M.vim_has(v) then return false @@ -135,7 +132,7 @@ function M.env_vars(vars, fallback) if not (is_str(vars) or is_tbl(vars)) then vim.notify( '(user_api.check.exists.env_vars): Argument type is neither string nor table', - vim.log.levels.ERROR + ERROR ) return false end @@ -170,7 +167,7 @@ function M.executable(exe, fallback) if not (is_str(exe) or is_tbl(exe)) then vim.notify( '(user_api.check.exists.executable): Argument type is neither string nor table', - vim.log.levels.ERROR + ERROR ) return false end diff --git a/lua/user_api/check/init.lua b/lua/user_api/check/init.lua index b772fd5..b287591 100644 --- a/lua/user_api/check/init.lua +++ b/lua/user_api/check/init.lua @@ -1,22 +1,27 @@ require('user_api.types.user.check') --- Checking Utilities +--- --- ---@type User.Check ---@diagnostic disable-next-line:missing-fields local M = {} --- Value checking utilities ---- +--- --- --- ## Description +--- --- Pretty much reserved for data checking, type checking and conditional operations +--- --- ---@type User.Check.Value M.value = require('user_api.check.value') --- Exitstance checks ---- +--- --- --- ## Description ---- This contains many environment, module, namespace, etc. checkers. +--- +--- This contains many checkers for environment, modules, namespaces, etc. --- Also, simplified Vim functions can be found here +--- --- ---@type User.Check.Existance M.exists = require('user_api.check.exists') diff --git a/lua/user_api/check/value.lua b/lua/user_api/check/value.lua index 6080f1e..f6a05ac 100644 --- a/lua/user_api/check/value.lua +++ b/lua/user_api/check/value.lua @@ -266,7 +266,7 @@ end --- --- A boolean indicatin whether input data is empty or not. --- --- ----@param v string|table|number|(string|table|number)[] +---@param v string|table|number|integer|(string|table|number|integer)[] ---@param multiple? boolean ---@return boolean function M.empty(v, multiple) @@ -299,7 +299,8 @@ function M.empty(v, multiple) end for _, val in next, v do - if M.empty(val) then + -- NOTE: NO RECURSIVE CHECKING + if M.empty(val, false) then return true end end diff --git a/lua/user_api/highlight.lua b/lua/user_api/highlight.lua index 4b4fe08..8cc692f 100644 --- a/lua/user_api/highlight.lua +++ b/lua/user_api/highlight.lua @@ -1,5 +1,7 @@ require('user_api.types.user.highlight') +local ERROR = vim.log.levels.ERROR + ---@type User.Hl ---@diagnostic disable-next-line:missing-fields local M = {} @@ -16,7 +18,7 @@ function M.hl(name, opts, bufnr) local empty = Value.empty if not (is_str(name) and is_tbl(opts)) or empty(name) then - vim.notify('(user_api.highlight.hl): Bad arguments', vim.log.levels.ERROR) + vim.notify('(user_api.highlight.hl): Bad arguments', ERROR) return end @@ -34,7 +36,7 @@ function M.hl_from_arr(A) local empty = Value.empty if not is_tbl(A) or empty(A) then - vim.notify('(user_api.highlight.hl_from_arr): Bad argument', vim.log.levels.ERROR) + vim.notify('(user_api.highlight.hl_from_arr): Bad argument', ERROR) return end @@ -42,7 +44,7 @@ function M.hl_from_arr(A) if not (is_str(t.name) and is_tbl(t.opts)) or empty(t.name) then vim.notify( '(user_api.highlight.hl_from_arr): A highlight value is not permitted, skipping', - vim.log.levels.ERROR + ERROR ) goto continue end @@ -77,10 +79,7 @@ function M.hl_from_dict(D) local empty = Value.empty if not is_tbl(D) or empty(D) then - vim.notify( - '(user_api.highlight.hl_from_dict): Unable to parse argument', - vim.log.levels.ERROR - ) + vim.notify('(user_api.highlight.hl_from_dict): Unable to parse argument', ERROR) return end @@ -88,7 +87,7 @@ function M.hl_from_dict(D) if not (is_str(k) and is_tbl(v)) or empty(k) then vim.notify( '(user_api.highlight.hl_from_dict): A highlight value is not permitted, skipping', - vim.log.levels.ERROR + ERROR ) goto continue diff --git a/lua/user_api/types/user/check.lua b/lua/user_api/types/user/check.lua index 485c6d7..5e79159 100644 --- a/lua/user_api/types/user/check.lua +++ b/lua/user_api/types/user/check.lua @@ -155,7 +155,7 @@ --- --- A boolean indicatin whether input data is empty or not. --- --- ----@field empty fun(v: string|table|number|(string|table|number)[], multiple: boolean?): boolean +---@field empty fun(v: string|table|number|integer|(string|table|number|integer)[], multiple: boolean?): boolean ---@field fields fun(fields: string|integer|(string|integer)[], T: table): boolean ---@field tbl_values fun(values: any[], T: table, return_keys: boolean?): boolean|string|integer|(string|integer)[] ---@field single_type_tbl fun(type_str: Types, T: table): boolean diff --git a/lua/user_api/util/autocmd.lua b/lua/user_api/util/autocmd.lua index f786ba4..7913246 100644 --- a/lua/user_api/util/autocmd.lua +++ b/lua/user_api/util/autocmd.lua @@ -212,7 +212,7 @@ function M.au_repeated_events(T) vim.notify('(user_api.util.au.au_repeated_events): Not a valid table', vim.log.levels.ERROR) return end - if empty(T) or empty(T.events) or empty(T.opts_tbl) then + if empty({ T, T.events, T.opts_tbl }, true) then vim.notify('(user_api.util.au.au_repeated_events): Empty table(s)', vim.log.levels.WARN) return end