Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
feat(modules): better autocmd syntax
Browse files Browse the repository at this point in the history
Now supports lua functions. The ideas come from @caligian and his repo, though
modified to use a unique index instead of requiring a name.
  • Loading branch information
LuigiPiucco committed Dec 31, 2021
1 parent b38659b commit d1f7604
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 29 deletions.
3 changes: 2 additions & 1 deletion docs/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ Common patterns:
- Add autocmds (`doom_` will be prefixed to the name):
```lua
doom.autocmds[augroup_name] = {
{ "BufReadPre", "*.lua", --[[once and nested go here if needed]] "setlocal sw=2" },
{ "BufReadPre", "*.lua", "setlocal sw=2", --[[once and nested are boolean keys here]] },
{ "InsertEnter", "*", function() print("Lua functions are valid!") end, once = true }
}
```

Expand Down
14 changes: 8 additions & 6 deletions lua/doom/modules/core/autocmds.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
local is_plugin_disabled = require("doom.utils").is_plugin_disabled

local autocmds = {
{ "BufWritePost", "*/doom/**/*.lua", [[lua require("doom.utils.reloader").full_reload()]] },
{ "BufWritePost", "*/doom/**/*.lua", require("doom.utils.reloader").full_reload },
{
"BufWritePost",
"*/doom-nvim/modules.lua,*/doom-nvim/config.lua",
[[lua require("doom.utils.reloader").full_reload()]],
require("doom.utils.reloader").full_reload,
},
}

Expand All @@ -17,7 +17,9 @@ if doom.highlight_yank then
table.insert(autocmds, {
"TextYankPost",
"*",
"lua require('vim.highlight').on_yank({higroup = 'Search', timeout = 200})",
function()
require("vim.highlight").on_yank({ higroup = "Search", timeout = 200 })
end,
})
end

Expand All @@ -33,17 +35,17 @@ if is_plugin_disabled("explorer") then
table.insert(autocmds, {
"FileType",
"netrw",
"lua require('doom.core.settings.netrw').set_maps()",
require("doom.core.settings.netrw").set_maps,
})
table.insert(autocmds, {
"FileType",
"netrw",
"lua require('doom.core.settings.netrw').draw_icons()",
require("doom.core.settings.netrw").draw_icons(),
})
table.insert(autocmds, {
"TextChanged",
"*",
"lua require('doom.core.settings.netrw').draw_icons()",
require("doom.core.settings.netrw").draw_icons(),
})
end

Expand Down
4 changes: 3 additions & 1 deletion lua/doom/modules/dashboard/autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ local autocmds = {
{
"FileType",
"dashboard",
[[lua require("nest").applyKeymaps({ "q", "<cmd>q<CR>", buffer = true })]],
function()
require("nest").applyKeymaps({ "q", "<cmd>q<CR>", buffer = true })
end,
},
}

Expand Down
4 changes: 1 addition & 3 deletions lua/doom/modules/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,5 @@ end
-- user.
for module, cmds in pairs(doom.autocmds) do
local augroup_name = ("doom_%s"):format(module)
utils.create_augroups({
[augroup_name] = cmds,
})
utils.make_augroup(augroup_name, cmds)
end
8 changes: 7 additions & 1 deletion lua/doom/modules/linter/autocmds.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
local autocmds = {
{ "BufWinEnter,BufWritePost", "<buffer>", [[lua require("lint").try_lint()]] },
{
"BufWinEnter,BufWritePost",
"<buffer>",
function()
require("lint").try_lint()
end,
},
}

return autocmds
13 changes: 7 additions & 6 deletions lua/doom/modules/lsp/autocmds.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
local autocmds = {}

if doom.lsp.hint_enable then
local show_diagnostics_function = (
"vim.diagnostic.open_float(nil, { focus = false, border = %s })"
):format(doom.border_style)
local show_diagnostics_function = function()
vim.diagnostic.open_float(nil, { focus = false, border = doom.border_style })
end
if vim.fn.has("nvim-0.6.0") ~= 1 then
show_diagnostics_function =
'vim.lsp.diagnostic.show_line_diagnostics({ focusable = false, border = "single" })'
show_diagnostics_function = function()
vim.lsp.diagnostic.show_line_diagnostics({ focusable = false, border = doom.border_style })
end
end
table.insert(autocmds, {
"CursorHold,CursorHoldI",
"<buffer>",
("lua %s"):format(show_diagnostics_function),
show_diagnostics_function,
})
end

Expand Down
6 changes: 4 additions & 2 deletions lua/doom/modules/lua/autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ local autocmds = {
{
"BufNewFile,BufRead",
"*.lua",
"++once",
[[lua dofile(vim.api.nvim_get_runtime_file("*/doom/modules/lua/config.lua", false)[1])]],
function()
dofile(vim.api.nvim_get_runtime_file("*/doom/modules/lua/config.lua", false)[1])
end,
once = true,
},
}

Expand Down
60 changes: 51 additions & 9 deletions lua/doom/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,63 @@ utils.get_capabilities = function()
return capabilities
end

--- For autocommands, extracted from
--- https://github.com/norcalli/nvim_utils
--- @param definitions table<string, table<number, string>>
utils.create_augroups = function(definitions)
for group_name, definition in pairs(definitions) do
utils.make_autocmd = function(event, pattern, action, group, nested, once)
local cmd = "autocmd "

if group then
cmd = cmd .. group .. " "
end

cmd = cmd .. event .. " "
cmd = cmd .. pattern .. " "

if nested then
cmd = cmd .. "++nested "
end
if once then
cmd = cmd .. "++once "
end

if type(action) == "function" then
if not _G._doom then
_G._doom = {}
end
if not _doom.autocmd_funcs then
_doom.autocmd_funcs = {}
end
-- Nobody is going to need more than a million of these, right?
local unique_number = utils.unique_index()
_doom.autocmd_funcs[unique_number] = action
cmd = cmd .. ("lua _doom.autocmd_funcs[%d]()"):format(unique_number)
else
cmd = cmd .. action
end

vim.cmd(cmd)
end

utils.make_augroup = function(group_name, cmds, existing_group)
if not existing_group then
vim.cmd("augroup " .. group_name)
vim.cmd("autocmd!")
for _, def in ipairs(definition) do
local command = table.concat(vim.tbl_flatten({ "autocmd", def }), " ")
vim.cmd(command)
end
end

for _, cmd in ipairs(cmds) do
utils.make_autocmd(cmd[1], cmd[2], cmd[3], existing_group and group_name, cmd.nested, cmd.once)
end

if not existing_group then
vim.cmd("augroup END")
end
end

local index = 0
utils.unique_index = function()
local ret = index
index = index + 1
return ret
end

--- Wraps nvim_replace_termcodes
--- @param str string
--- @return string
Expand Down
4 changes: 4 additions & 0 deletions lua/doom/utils/reloader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ end
reloader.reload_plugins_definitions = function()
local old_plugins = vim.deepcopy(packer_plugins)

if _doom and _doom.autocmd_funcs then
_doom.autocmd_funcs = {}
end

-- Silently reload plugins modules
reloader.reload_lua_module("doom.core.config", true)
reloader.reload_lua_module("doom.core.config.modules", true)
Expand Down

0 comments on commit d1f7604

Please sign in to comment.