Skip to content

Commit

Permalink
feat: allow false to disable autocmds, commands, or on_key functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mehalter committed Dec 4, 2023
1 parent 249f134 commit 38991f9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
8 changes: 4 additions & 4 deletions lua/astrocore/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
--- }
---}
---```
---@field autocmds table<string,table[]>?
---@field autocmds table<string,table[]|false>?
---Configuration of user commands
---The key into the table is the name of the user command and the value is a table of command options
---Example:
Expand All @@ -101,7 +101,7 @@
--- }
---}
---```
---@field commands table<string,table>?
---@field commands table<string,table|false>?
---Configuration of vim mappings to create.
---The first key into the table is the vim map mode (`:h map-modes`), and the value is a table of entries to be passed to `vim.keymap.set` (`:h vim.keymap.set`):
--- - The key is the first parameter or the vim mode (only a single mode supported) and the value is a table of keymaps within that mode:
Expand Down Expand Up @@ -133,7 +133,7 @@
--- }
---}
---```
---@field mappings table<string,table<string,(table|boolean|string)?>?>?
---@field mappings table<string,table<string,(table|string|false)?>?>?
---Configuration of vim `on_key` functions.
---The key into the table is the namespace of the function and the value is a list like table of `on_key` functions
---Example:
Expand All @@ -152,7 +152,7 @@
--- },
---},
---```
---@field on_keys table<string,fun(key:string)[]>?
---@field on_keys table<string,fun(key:string)[]|false>?
---Configuration table of features provided by AstroCore
---Example:
--
Expand Down
34 changes: 20 additions & 14 deletions lua/astrocore/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -334,29 +334,35 @@ function M.setup(opts)

-- autocmds
for augroup, autocmds in pairs(M.config.autocmds) do
local augroup_id = vim.api.nvim_create_augroup(augroup, { clear = true })
for _, autocmd in ipairs(autocmds) do
local event = autocmd.event
autocmd.event = nil
autocmd.group = augroup_id
vim.api.nvim_create_autocmd(event, autocmd)
autocmd.event = event
if autocmds then
local augroup_id = vim.api.nvim_create_augroup(augroup, { clear = true })
for _, autocmd in ipairs(autocmds) do
local event = autocmd.event
autocmd.event = nil
autocmd.group = augroup_id
vim.api.nvim_create_autocmd(event, autocmd)
autocmd.event = event
end
end
end

-- user commands
for cmd, spec in pairs(M.config.commands) do
local action = spec[1]
spec[1] = nil
vim.api.nvim_create_user_command(cmd, action, spec)
spec[1] = action
if spec then
local action = spec[1]
spec[1] = nil
vim.api.nvim_create_user_command(cmd, action, spec)
spec[1] = action
end
end

-- on_key hooks
for namespace, funcs in pairs(M.config.on_keys) do
local ns = vim.api.nvim_create_namespace(namespace)
for _, func in ipairs(funcs) do
vim.on_key(func, ns)
if funcs then
local ns = vim.api.nvim_create_namespace(namespace)
for _, func in ipairs(funcs) do
vim.on_key(func, ns)
end
end
end
end
Expand Down

0 comments on commit 38991f9

Please sign in to comment.