Skip to content

Commit

Permalink
fix(api): Multiple corrections.
Browse files Browse the repository at this point in the history
Signed-off-by: Guennadi Maximov C <[email protected]>
  • Loading branch information
DrKJeff16 committed Sep 3, 2024
1 parent cf64eeb commit 6894bb7
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 91 deletions.
135 changes: 75 additions & 60 deletions lua/user_api/maps/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,34 @@ end

function M.map_dict(T, map_func, dict_has_modes, mode, bufnr)
if not (is_tbl(T) and not empty(T)) then
error("(user_api.maps.map_dict): Keys either aren't table or table is empty")
vim.notify("Keys either aren't table or table is empty", 'error', {
timeout = 700,
title = '(user_api.maps.map_dict)',
})
return
end
if not is_str(map_func) then
error('(user_api.maps.map_dict): `map_func` is not a string')

if is_str(mode) and not vim.tbl_contains(MODES, mode) then
mode = 'n'
elseif is_tbl(mode) and not empty(mode) then
for _, v in next, mode do
if not vim.tbl_contains(MODES, v) then
error('(user_api.maps.map_dict): Bad modes table', vim.log.levels.ERROR)
end
end
elseif is_tbl(mode) and empty(mode) then
mode = 'n'
end

local map_choices = { 'kmap', 'wk.register' }

map_func = (is_str(map_func) and vim.tbl_contains(map_choices)) and map_func or 'wk.register'
map_func = M.wk.available() and map_func or 'kmap'
mode = (is_str(mode) and vim.tbl_contains(MODES, mode)) and mode or 'n'
dict_has_modes = is_bool(dict_has_modes) and dict_has_modes or false
bufnr = is_int(bufnr) and bufnr or nil

local map_choices = {
'kmap',
'wk.register',
}

if not vim.tbl_contains(map_choices, map_func) or not M.wk.available() then
map_func = 'kmap'
end

---@type fun(...)
---@type KeyMapFunction
local func

if dict_has_modes then
Expand All @@ -94,50 +102,54 @@ function M.map_dict(T, map_func, dict_has_modes, mode, bufnr)

func(lhs, v[1], v[2])
end
else
for lhs, v in next, t do
local tbl = {}
if is_str(lhs) then
table.insert(tbl, lhs)
else
goto continue
end

if not is_nil(v[1]) then
table.insert(tbl, v[1])
end

tbl.mode = mode_choice

if is_int(bufnr) then
tbl.buffer = bufnr
end

if is_str(v.group) then
tbl.group = v.group
end

if is_tbl(v[2]) and is_str(v[2].desc) then
tbl.desc = v[2].desc
end
if is_tbl(v[2]) and v[2].expr then
tbl.expr = v[2].expr
end
if is_tbl(v[2]) and v[2].expr then
tbl.noremap = v[2].noremap
end
if is_tbl(v[2]) and v[2].expr then
tbl.nowait = v[2].nowait
end

if not is_nil(v.hidden) then
tbl.hidden = v.hidden
end

require('which-key').add(tbl)

::continue::
goto continue
end

for lhs, v in next, t do
local tbl = {}
if is_str(lhs) then
table.insert(tbl, lhs)
else
goto continue
end

if not is_nil(v[1]) then
table.insert(tbl, v[1])
end

tbl.mode = mode_choice

if is_int(bufnr) then
tbl.buffer = bufnr
end

if is_str(v.group) then
tbl.group = v.group
end

if is_tbl(v[2]) and is_str(v[2].desc) then
tbl.desc = v[2].desc
end
if is_tbl(v[2]) and is_bool(v[2].expr) then
tbl.expr = v[2].expr
end
if is_tbl(v[2]) and is_bool(v[2].noremap) then
tbl.noremap = v[2].noremap
end
if is_tbl(v[2]) and is_bool(v[2].nowait) then
tbl.nowait = v[2].nowait
end
if is_tbl(v[2]) and is_bool(v[2].silent) then
tbl.silent = v[2].silent
end

if is_bool(v.hidden) then
tbl.hidden = v.hidden
end

require('which-key').add(tbl)

::continue::
end

::continue::
Expand Down Expand Up @@ -177,17 +189,20 @@ function M.map_dict(T, map_func, dict_has_modes, mode, bufnr)
if is_tbl(v[2]) and is_str(v[2].desc) then
tbl.desc = v[2].desc
end
if is_tbl(v[2]) and v[2].expr then
if is_tbl(v[2]) and is_bool(v[2].expr) then
tbl.expr = v[2].expr
end
if is_tbl(v[2]) and v[2].expr then
if is_tbl(v[2]) and is_bool(v[2].noremap) then
tbl.noremap = v[2].noremap
end
if is_tbl(v[2]) and v[2].expr then
if is_tbl(v[2]) and is_bool(v[2].nowait) then
tbl.nowait = v[2].nowait
end
if is_tbl(v[2]) and is_bool(v[2].silent) then
tbl.silent = v[2].silent
end

if not is_nil(v.hidden) then
if is_bool(v.hidden) then
tbl.hidden = v.hidden
end

Expand Down
10 changes: 5 additions & 5 deletions lua/user_api/maps/wk.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require('user_api.types.user.maps')

local Check = require('user_api.check')
local Util = require('user_api.util')

local is_tbl = Check.value.is_tbl
local is_fun = Check.value.is_fun
Expand All @@ -17,9 +16,10 @@ local MODES = { 'n', 'i', 'v', 't', 'o', 'x' }

--- `which_key` API entrypoints
---@type User.Maps.WK
M = {
available = function() return require('user_api.check.exists').module('which-key') end,
}
---@diagnostic disable-next-line:missing-fields
M = {}

function M.available() return require('user_api.check.exists').module('which-key') end

function M.convert(lhs, rhs, opts)
if not M.available() then
Expand Down Expand Up @@ -65,7 +65,7 @@ end

function M.register(T, opts)
if not M.available() then
Util.notify.notify('(user.maps.wk.register): `which_key` unavailable')
require('user_api.util.notify').notify('(user.maps.wk.register): `which_key` unavailable')
return false
end

Expand Down
2 changes: 1 addition & 1 deletion lua/user_api/types/user/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ require('user_api.types.user.autocmd')
---@field ft_get fun(bufnr: integer?): string
---@field assoc fun()
---@field displace_letter fun(c: string, direction: ('next'|'prev')?, cycle: boolean?): string
---@field mv_tbl_values? fun(T: table|table<string|integer, any>, steps: integer?, direction: ('r'|'l')?): (res: table<string|integer, any>)
---@field mv_tbl_values? fun(T: table|table<string|integer, any>, steps: integer?, direction: ('r'|'l')?): res: table<string|integer, any>
---@field inspect fun(data: any): string

--- vim:ts=4:sts=4:sw=4:et:ai:si:sta:noci:nopi:
39 changes: 14 additions & 25 deletions lua/user_api/util/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require('user_api.types.user.util')

local curr_buf = vim.api.nvim_get_current_buf
local optset = vim.api.nvim_set_option_value
local in_tbl = vim.tbl_contains

---@type User.Util
---@diagnostic disable-next-line:missing-fields
Expand All @@ -24,16 +25,15 @@ function M.mv_tbl_values(T, steps, direction)
local empty = Value.empty

if not is_tbl(T) then
error("(user_api.util.mv_tbl_values): Input isn't a table")
error("(user_api.util.mv_tbl_values): Input isn't a table", vim.log.levels.ERROR)
end

if empty(T) then
return T
end

steps = (is_int(steps) and steps > 0) and steps or 1
direction = (is_str(direction) and vim.tbl_contains({ 'l', 'r' }, direction)) and direction
or 'r'
direction = (is_str(direction) and in_tbl({ 'l', 'r' }, direction)) and direction or 'r'

---@class DirectionFuns
---@field r fun(t: table<string|integer, any>): res: table<string|integer, any>
Expand Down Expand Up @@ -102,7 +102,12 @@ end
---@return boolean
function M.xor(x, y)
if not require('user_api.check.value').is_bool({ x, y }, true) then
error('(user_api.util.xor): An argument is not of boolean type')
M.notify.notify('An argument is not of boolean type', 'error', {
hide_from_history = false,
timeout = 800,
title = '(user_api.util.xor)',
})
return false
end

return (x and not y) or (not x and y)
Expand All @@ -116,7 +121,6 @@ function M.strip_fields(T, fields)

local is_tbl = Value.is_tbl
local is_str = Value.is_str
local is_int = Value.is_int
local empty = Value.empty
local field = Value.fields

Expand Down Expand Up @@ -147,7 +151,7 @@ function M.strip_fields(T, fields)
end
else
for k, v in next, T do
if not vim.tbl_contains(fields, k) then
if not in_tbl(fields, k) then
res[k] = v
end
end
Expand All @@ -164,8 +168,6 @@ function M.strip_values(T, values, max_instances)
local Value = require('user_api.check.value')

local is_tbl = Value.is_tbl
local is_nil = Value.is_nil
local is_str = Value.is_str
local is_int = Value.is_int
local empty = Value.empty

Expand All @@ -184,9 +186,9 @@ function M.strip_values(T, values, max_instances)

for k, v in next, T do
if M.xor(max_instances == 0, max_instances ~= 0 and max_instances > count) then
if not vim.tbl_contains(values, v) and is_int(k) then
if not in_tbl(values, v) and is_int(k) then
table.insert(res, v)
elseif not vim.tbl_contains(values, v) then
elseif not in_tbl(values, v) then
res[k] = v
else
count = count + 1
Expand Down Expand Up @@ -236,13 +238,6 @@ function M.ft_get(bufnr)
end

function M.assoc()
local Value = require('user_api.check.value')

local is_nil = Value.is_nil
local is_fun = Value.is_fun
local is_tbl = Value.is_tbl
local is_str = Value.is_str
local empty = Value.empty
local au_repeated_events = M.au.au_repeated_events

local group = vim.api.nvim_create_augroup('UserAssocs', { clear = true })
Expand All @@ -265,7 +260,7 @@ function M.assoc()
group = group,
callback = function()
if
not vim.tbl_contains({
not in_tbl({
M.ft_get(curr_buf()),
M.bt_get(curr_buf()),
}, 'help')
Expand Down Expand Up @@ -362,11 +357,6 @@ function M.assoc()
callback = function()
local buf = curr_buf()

-- Make sure the buffer is modifiable
if not vim.api.nvim_get_option_value('modifiable', { buf = buf }) then
return
end

if require('user_api.check.exists').executable('isort') then
local map_dict = require('user_api.maps').map_dict
local desc = require('user_api.maps.kmap').desc
Expand Down Expand Up @@ -412,8 +402,7 @@ function M.displace_letter(c, direction, cycle)
local is_bool = Value.is_bool
local mv_tbl_values = M.mv_tbl_values

direction = (is_str(direction) and vim.tbl_contains({ 'next', 'prev' }, direction))
and direction
direction = (is_str(direction) and in_tbl({ 'next', 'prev' }, direction)) and direction
or 'next'
cycle = is_bool(cycle) and cycle or false

Expand Down

0 comments on commit 6894bb7

Please sign in to comment.