Skip to content

Commit

Permalink
fix(api): Docs and code fixed; implemented new check.value.empty()
Browse files Browse the repository at this point in the history
…usage.

Signed-off-by: Guennadi Maximov C <[email protected]>
  • Loading branch information
DrKJeff16 committed Sep 5, 2024
1 parent c1a1913 commit 34c69be
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
43 changes: 20 additions & 23 deletions lua/user_api/check/exists.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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<string, boolean>
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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions lua/user_api/check/init.lua
Original file line number Diff line number Diff line change
@@ -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')

Expand Down
5 changes: 3 additions & 2 deletions lua/user_api/check/value.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions lua/user_api/highlight.lua
Original file line number Diff line number Diff line change
@@ -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 = {}
Expand All @@ -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

Expand All @@ -34,15 +36,15 @@ 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

for _, t in next, A do
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
Expand Down Expand Up @@ -77,18 +79,15 @@ 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

for k, v in next, D do
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
Expand Down
2 changes: 1 addition & 1 deletion lua/user_api/types/user/check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<string|integer, any>): 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
Expand Down
2 changes: 1 addition & 1 deletion lua/user_api/util/autocmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 34c69be

Please sign in to comment.