Skip to content

Commit

Permalink
fix(api): Created util function, assoc fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
DrKJeff16 committed Jun 1, 2024
1 parent 79b366a commit 5683cf4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
33 changes: 6 additions & 27 deletions lua/user/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,20 @@ local M = {
opts = require('user.opts'),

assoc = function()
---@type fun(s: string): fun()
local function ft(s)
local set_option = vim.api.nvim_set_option_value
local curr_buf = vim.api.nvim_get_current_buf

return function()
if is_str(s) then
set_option('ft', s, {
buf = curr_buf(),
scope = 'local',
})
end
end
end
local ft = Util.ft_set

local au = vim.api.nvim_create_autocmd

vim.api.nvim_create_augroup('UserAssocs', { clear = true })
local group = vim.api.nvim_create_augroup('UserAssocs', { clear = false })

---@type AuRepeatEvents[]
local aus = {
{
events = { 'BufNewFile', 'BufReadPre' },
opts_tbl = {
{ pattern = '*.org', callback = ft('org'), group = 'UserAssocs' },
{ pattern = '.spacemacs', callback = ft('lisp'), group = 'UserAssocs' },
{ pattern = '.clangd', callback = ft('yaml'), group = 'UserAssocs' },
{ pattern = '*.org', callback = ft('org'), group = group },
{ pattern = '.spacemacs', callback = ft('lisp'), group = group },
{ pattern = '.clangd', callback = ft('yaml'), group = group },
},
},
}
Expand All @@ -59,8 +46,6 @@ local M = {
goto continue
end

local events = v.events

if not is_tbl(v.opts_tbl) or empty(v.opts_tbl) then
vim.notify('(user.assoc): Event options in a non-table or an empty one')
goto continue
Expand All @@ -82,13 +67,7 @@ local M = {
goto continue
end

o.group = (is_str(o.group) and o.group == 'UserAssocs') and o.group or 'UserAssocs'

if not is_nil(o.buffer) then
o.buffer = is_int(o.buffer) and o.buffer or 0
end

au(events, o)
au(v.events, o)
end

::continue::
Expand Down
1 change: 1 addition & 0 deletions lua/user/types/user/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

---@class UserUtils
---@field strip_fields fun(T: table, fields: string|string[]): table
---@field ft_set fun(s: string): fun()
13 changes: 13 additions & 0 deletions lua/user/util/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,17 @@ function M.strip_fields(T, fields)
return res
end

function M.ft_set(s)
local set_option = vim.api.nvim_set_option_value
local curr_buf = vim.api.nvim_get_current_buf

return function()
if is_str(s) then
set_option('ft', s, {
buf = curr_buf(),
})
end
end
end

return M

0 comments on commit 5683cf4

Please sign in to comment.