Browse Source

feat(modules): better autocmd syntax

Now supports lua functions. The ideas come from @caligian and his repo, though
modified to use a unique index instead of requiring a name.
my-config
Luigi Sartor Piucco 3 years ago
parent
commit
d1f76041fe
  1. 3
      docs/modules.md
  2. 14
      lua/doom/modules/core/autocmds.lua
  3. 4
      lua/doom/modules/dashboard/autocmds.lua
  4. 4
      lua/doom/modules/init.lua
  5. 8
      lua/doom/modules/linter/autocmds.lua
  6. 13
      lua/doom/modules/lsp/autocmds.lua
  7. 6
      lua/doom/modules/lua/autocmds.lua
  8. 60
      lua/doom/utils/init.lua
  9. 4
      lua/doom/utils/reloader.lua

3
docs/modules.md

@ -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 }
}
```

14
lua/doom/modules/core/autocmds.lua

@ -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,
},
}
@ -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
@ -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

4
lua/doom/modules/dashboard/autocmds.lua

@ -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,
},
}

4
lua/doom/modules/init.lua

@ -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
lua/doom/modules/linter/autocmds.lua

@ -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
lua/doom/modules/lsp/autocmds.lua

@ -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

6
lua/doom/modules/lua/autocmds.lua

@ -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,
},
}

60
lua/doom/utils/init.lua

@ -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

4
lua/doom/utils/reloader.lua

@ -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)

Loading…
Cancel
Save