Skip to content

Commit

Permalink
feat(api): New module util. Integrated first fun
Browse files Browse the repository at this point in the history
  • Loading branch information
DrKJeff16 committed Jun 1, 2024
1 parent 2e19c26 commit e121a37
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 39 deletions.
2 changes: 2 additions & 0 deletions lua/user/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

local Types = require('user.types') -- Source all API annotations
local Check = require('user.check') -- Checker utilities
local Util = require('user.util') -- Coding utilities

local is_nil = Check.value.is_nil
local is_tbl = Check.value.is_tbl
Expand All @@ -14,6 +15,7 @@ local empty = Check.value.empty
---@type UserMod
local M = {
types = Types,
util = Util,
check = Check,
maps = require('user.maps'),
highlight = require('user.highlight'),
Expand Down
43 changes: 4 additions & 39 deletions lua/user/maps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require('user.types.user.maps')

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

local is_nil = Check.value.is_nil
local is_tbl = Check.value.is_tbl
Expand All @@ -15,6 +16,7 @@ local is_int = Check.value.is_int
local is_bool = Check.value.is_bool
local empty = Check.value.empty
local field = Check.value.field
local strip_fields = Util.strip_fields

local kmap = vim.keymap.set
local map = vim.api.nvim_set_keymap
Expand All @@ -23,44 +25,6 @@ local bufmap = vim.api.nvim_buf_set_keymap
---@type Modes
local MODES = { 'n', 'i', 'v', 't', 'o', 'x' }

---@type fun(T: UserMaps.Api.Opts|UserMaps.Keymap.Opts|UserMaps.Buf.Opts, fields: string|string[]): UserMaps.Api.Opts|UserMaps.Keymap.Opts|UserMaps.Buf.Opts
local strip_options = function(T, fields)
if not is_tbl(T) then
error('(maps:strip_options): Empty table')
end

if empty(T) then
return T
end

if not (is_str(fields) or is_tbl(fields)) or empty(fields) then
return T
end

---@type UserMaps.Keymap.Opts
local res = {}

if is_str(fields) then
if not field(fields, T) then
return T
end

for k, v in next, T do
if k ~= fields then
res[k] = v
end
end
else
for k, v in next, T do
if not vim.tbl_contains(fields, k) then
res[k] = v
end
end
end

return res
end

---@type fun(mode: string, func: MapFuncs, with_buf: boolean?): KeyMapFunction|ApiMapFunction|BufMapFunction
local function variant(mode, func, with_buf)
if not (is_fun(func) and is_str(mode) and vim.tbl_contains(MODES, mode)) then
Expand Down Expand Up @@ -177,7 +141,8 @@ function M.nop(T, opts, mode)
opts.silent = is_bool(opts.silent) and opts.silent or true

if is_int(opts.buffer) then
opts = strip_options(vim.deepcopy(opts), 'buffer')
---@type UserMaps.Keymap.Opts
opts = strip_fields(opts, 'buffer')
end

if is_str(T) then
Expand Down
1 change: 1 addition & 0 deletions lua/user/types/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
---@field opts UserOpts
---@field types UserTypes
---@field ft_get fun(scope: ('local'|'global')?): string
---@field util UserUtils

---@type UserTypes
local M = {
Expand Down
2 changes: 2 additions & 0 deletions lua/user/types/user/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
---@field autocmd table
---@field opts table
---@field check table
---@field util table

---@type UserSubTypes
local M = {
Expand All @@ -15,6 +16,7 @@ local M = {
highlight = require('user.types.user.highlight'),
maps = require('user.types.user.maps'),
opts = require('user.types.user.opts'),
util = require('user.types.user.util'),
}

return M
5 changes: 5 additions & 0 deletions lua/user/types/user/util.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---@diagnostic disable:unused-local
---@diagnostic disable:unused-function

---@class UserUtils
---@field strip_fields fun(T: table, fields: string|string[]): table
52 changes: 52 additions & 0 deletions lua/user/util/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---@diagnostic disable:unused-function
---@diagnostic disable:unused-local

require('user.types.user.util')

local Value = require('user.check.value')
local is_tbl = Value.is_tbl
local is_str = Value.is_str
local empty = Value.empty
local field = Value.field

---@type UserUtils
local M = {}

function M.strip_fields(T, fields)
if not is_tbl(T) then
error('(maps:strip_options): Empty table')
end

if empty(T) then
return T
end

if not (is_str(fields) or is_tbl(fields)) or empty(fields) then
return T
end

---@type UserMaps.Keymap.Opts
local res = {}

if is_str(fields) then
if not field(fields, T) then
return T
end

for k, v in next, T do
if k ~= fields then
res[k] = v
end
end
else
for k, v in next, T do
if not vim.tbl_contains(fields, k) then
res[k] = v
end
end
end

return res
end

return M

0 comments on commit e121a37

Please sign in to comment.