Skip to content

Commit

Permalink
fix(api): Improve performance, annotations accomodated.
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 7, 2024
1 parent 59117ac commit 61c1ce5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
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 @@ -70,7 +70,7 @@ require('user_api.types.user.autocmd')
---@field xor fun(x: boolean, y: boolean): boolean
---@field strip_fields fun(T: table<string|integer, any>, values: string|string[]): table
---@field strip_values fun(T: table<string|integer, any>, values: any[], max_instances: integer?): table
---@field ft_set fun(s: string, bufnr: integer?): fun()
---@field ft_set fun(s: string?, bufnr: integer?): fun()
---@field bt_get fun(bufnr: integer?): string
---@field ft_get fun(bufnr: integer?): string
---@field assoc fun()
Expand Down
41 changes: 22 additions & 19 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 optget = vim.api.nvim_get_option_value
local in_tbl = vim.tbl_contains

local ERROR = vim.log.levels.ERROR
Expand Down Expand Up @@ -127,7 +128,7 @@ function M.strip_fields(T, fields)
local field = Value.fields

if not is_tbl(T) then
error('(user_api.util.strip_fields): Argument is not a table')
error('(user_api.util.strip_fields): Argument is not a table', ERROR)
end

if empty(T) then
Expand Down Expand Up @@ -173,11 +174,11 @@ function M.strip_values(T, values, max_instances)
local is_int = Value.is_int
local empty = Value.empty

if not is_tbl(T) then
error('(user_api.util.strip_values): Not a table')
if not is_tbl({ T, values }, true) then
error('(user_api.util.strip_values): Not a table', ERROR)
end
if not is_tbl(values) or empty(values) then
error('(user_api.util.strip_values): No values given')
if empty(values) then
error('(user_api.util.strip_values): No values given', ERROR)
end

max_instances = is_int(max_instances) and max_instances or 0
Expand All @@ -187,7 +188,8 @@ function M.strip_values(T, values, max_instances)
local count = 0

for k, v in next, T do
if M.xor(max_instances == 0, max_instances ~= 0 and max_instances > count) then
-- Both arguments can't be true simultaneously
if M.xor((max_instances == 0), (max_instances ~= 0 and max_instances > count)) then
if not in_tbl(values, v) and is_int(k) then
table.insert(res, v)
elseif not in_tbl(values, v) then
Expand All @@ -205,7 +207,7 @@ function M.strip_values(T, values, max_instances)
return res
end

---@param s string
---@param s? string
---@param bufnr? integer
---@return fun()
function M.ft_set(s, bufnr)
Expand All @@ -214,31 +216,26 @@ function M.ft_set(s, bufnr)
local is_int = Value.is_int
local is_str = Value.is_str

s = is_str(s) and s or ''
bufnr = is_int(bufnr) and bufnr or curr_buf()

return function()
if not is_str(s) then
optset('ft', '', { buf = bufnr })
else
optset('ft', s, { buf = bufnr })
end
end
return function() optset('ft', s, { buf = bufnr }) end
end

---@param bufnr? integer
---@return string
function M.bt_get(bufnr)
bufnr = require('user_api.check.value').is_int(bufnr) and bufnr or curr_buf()

return vim.api.nvim_get_option_value('bt', { buf = bufnr })
return optget('bt', { buf = bufnr })
end

---@param bufnr? integer
---@return string
function M.ft_get(bufnr)
bufnr = require('user_api.check.value').is_int(bufnr) and bufnr or curr_buf()

return vim.api.nvim_get_option_value('ft', { buf = bufnr })
return optget('ft', { buf = bufnr })
end

function M.assoc()
Expand Down Expand Up @@ -338,7 +335,7 @@ function M.assoc()
local buf = curr_buf()

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

Expand All @@ -361,6 +358,11 @@ function M.assoc()
callback = function()
local buf = curr_buf()

-- Make sure the buffer is modifiable
if not optget('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 @@ -438,12 +440,13 @@ function M.discard_dups(data)
local is_tbl = Value.is_tbl
local is_str = Value.is_str
local empty = Value.empty
local notify = M.notify.notify

---@type string|table
local res

if not (is_str(data) or is_tbl(data)) then
M.notify.notify('Input is neither a string nor a table', 'error', {
notify('Input is neither a string nor a table', 'error', {
hide_from_history = false,
timeout = 750,
title = '(user_api.util.discard_dups)',
Expand All @@ -455,7 +458,7 @@ function M.discard_dups(data)
end

if empty(data) then
M.notify.notify('Input is empty', 'error', {
notify('Input is empty', 'error', {
hide_from_history = false,
timeout = 750,
title = '(user_api.util.discard_dups)',
Expand Down

0 comments on commit 61c1ce5

Please sign in to comment.