autocmds.lua 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. local M = {}
  2. local Log = require "lvim.core.log"
  3. --- Load the default set of autogroups and autocommands.
  4. function M.load_defaults()
  5. local definitions = {
  6. {
  7. "TextYankPost",
  8. {
  9. group = "_general_settings",
  10. pattern = "*",
  11. desc = "Highlight text on yank",
  12. callback = function()
  13. vim.highlight.on_yank { higroup = "Search", timeout = 100 }
  14. end,
  15. },
  16. },
  17. {
  18. "FileType",
  19. {
  20. group = "_hide_dap_repl",
  21. pattern = "dap-repl",
  22. command = "set nobuflisted",
  23. },
  24. },
  25. {
  26. "FileType",
  27. {
  28. group = "_filetype_settings",
  29. pattern = { "lua" },
  30. desc = "fix gf functionality inside .lua files",
  31. callback = function()
  32. ---@diagnostic disable: assign-type-mismatch
  33. -- credit: https://github.com/sam4llis/nvim-lua-gf
  34. vim.opt_local.include = [[\v<((do|load)file|require|reload)[^''"]*[''"]\zs[^''"]+]]
  35. vim.opt_local.includeexpr = "substitute(v:fname,'\\.','/','g')"
  36. vim.opt_local.suffixesadd:prepend ".lua"
  37. vim.opt_local.suffixesadd:prepend "init.lua"
  38. for _, path in pairs(vim.api.nvim_list_runtime_paths()) do
  39. vim.opt_local.path:append(path .. "/lua")
  40. end
  41. end,
  42. },
  43. },
  44. {
  45. "FileType",
  46. {
  47. group = "_buffer_mappings",
  48. pattern = {
  49. "qf",
  50. "help",
  51. "man",
  52. "floaterm",
  53. "lspinfo",
  54. "lir",
  55. "lsp-installer",
  56. "null-ls-info",
  57. "tsplayground",
  58. "DressingSelect",
  59. "Jaq",
  60. },
  61. callback = function()
  62. vim.keymap.set("n", "q", "<cmd>close<cr>", { buffer = true })
  63. vim.opt_local.buflisted = false
  64. end,
  65. },
  66. },
  67. {
  68. "VimResized",
  69. {
  70. group = "_auto_resize",
  71. pattern = "*",
  72. command = "tabdo wincmd =",
  73. },
  74. },
  75. {
  76. "FileType",
  77. {
  78. group = "_filetype_settings",
  79. pattern = "alpha",
  80. callback = function()
  81. vim.cmd [[
  82. set nobuflisted
  83. ]]
  84. end,
  85. },
  86. },
  87. {
  88. "FileType",
  89. {
  90. group = "_filetype_settings",
  91. pattern = "lir",
  92. callback = function()
  93. vim.opt_local.number = false
  94. vim.opt_local.relativenumber = false
  95. end,
  96. },
  97. },
  98. {
  99. "ColorScheme",
  100. {
  101. group = "_lvim_colorscheme",
  102. callback = function()
  103. local statusline_hl = vim.api.nvim_get_hl_by_name("StatusLine", true)
  104. local cursorline_hl = vim.api.nvim_get_hl_by_name("CursorLine", true)
  105. local normal_hl = vim.api.nvim_get_hl_by_name("Normal", true)
  106. vim.api.nvim_set_hl(0, "CmpItemKindCopilot", { fg = "#6CC644" })
  107. vim.api.nvim_set_hl(0, "CmpItemKindTabnine", { fg = "#CA42F0" })
  108. vim.api.nvim_set_hl(0, "CmpItemKindCrate", { fg = "#F64D00" })
  109. vim.api.nvim_set_hl(0, "CmpItemKindEmoji", { fg = "#FDE030" })
  110. vim.api.nvim_set_hl(0, "SLCopilot", { fg = "#6CC644", bg = statusline_hl.background })
  111. vim.api.nvim_set_hl(0, "SLGitIcon", { fg = "#E8AB53", bg = cursorline_hl.background })
  112. vim.api.nvim_set_hl(0, "SLBranchName", { fg = normal_hl.foreground, bg = cursorline_hl.background })
  113. vim.api.nvim_set_hl(0, "SLSeparator", { fg = cursorline_hl.background, bg = statusline_hl.background })
  114. end,
  115. },
  116. },
  117. { -- taken from AstroNvim
  118. "BufEnter",
  119. {
  120. group = "_dir_opened",
  121. nested = true,
  122. callback = function(args)
  123. local bufname = vim.api.nvim_buf_get_name(args.buf)
  124. if require("lvim.utils").is_directory(bufname) then
  125. vim.api.nvim_del_augroup_by_name "_dir_opened"
  126. vim.cmd "do User DirOpened"
  127. vim.api.nvim_exec_autocmds(args.event, { buffer = args.buf, data = args.data })
  128. end
  129. end,
  130. },
  131. },
  132. { -- taken from AstroNvim
  133. { "BufRead", "BufWinEnter", "BufNewFile" },
  134. {
  135. group = "_file_opened",
  136. nested = true,
  137. callback = function(args)
  138. local buftype = vim.api.nvim_get_option_value("buftype", { buf = args.buf })
  139. if not (vim.fn.expand "%" == "" or buftype == "nofile") then
  140. vim.api.nvim_del_augroup_by_name "_file_opened"
  141. vim.api.nvim_exec_autocmds("User", { pattern = "FileOpened" })
  142. require("lvim.lsp").setup()
  143. end
  144. end,
  145. },
  146. },
  147. }
  148. M.define_autocmds(definitions)
  149. end
  150. local get_format_on_save_opts = function()
  151. local defaults = require("lvim.config.defaults").format_on_save
  152. -- accept a basic boolean `lvim.format_on_save=true`
  153. if type(lvim.format_on_save) ~= "table" then
  154. return defaults
  155. end
  156. return {
  157. pattern = lvim.format_on_save.pattern or defaults.pattern,
  158. timeout = lvim.format_on_save.timeout or defaults.timeout,
  159. }
  160. end
  161. function M.enable_format_on_save()
  162. local opts = get_format_on_save_opts()
  163. vim.api.nvim_create_augroup("lsp_format_on_save", {})
  164. vim.api.nvim_create_autocmd("BufWritePre", {
  165. group = "lsp_format_on_save",
  166. pattern = opts.pattern,
  167. callback = function()
  168. require("lvim.lsp.utils").format { timeout_ms = opts.timeout, filter = opts.filter }
  169. end,
  170. })
  171. Log:debug "enabled format-on-save"
  172. end
  173. function M.disable_format_on_save()
  174. M.clear_augroup "lsp_format_on_save"
  175. Log:debug "disabled format-on-save"
  176. end
  177. function M.configure_format_on_save()
  178. if type(lvim.format_on_save) == "table" and lvim.format_on_save.enabled then
  179. M.enable_format_on_save()
  180. elseif lvim.format_on_save == true then
  181. M.enable_format_on_save()
  182. else
  183. M.disable_format_on_save()
  184. end
  185. end
  186. function M.toggle_format_on_save()
  187. local exists, autocmds = pcall(vim.api.nvim_get_autocmds, {
  188. group = "lsp_format_on_save",
  189. event = "BufWritePre",
  190. })
  191. if not exists or #autocmds == 0 then
  192. M.enable_format_on_save()
  193. else
  194. M.disable_format_on_save()
  195. end
  196. end
  197. function M.enable_reload_config_on_save()
  198. -- autocmds require forward slashes (even on windows)
  199. local pattern = get_config_dir():gsub("\\", "/") .. "/*.lua"
  200. vim.api.nvim_create_augroup("lvim_reload_config_on_save", {})
  201. vim.api.nvim_create_autocmd("BufWritePost", {
  202. group = "lvim_reload_config_on_save",
  203. pattern = pattern,
  204. desc = "Trigger LvimReload on saving config.lua",
  205. callback = function()
  206. require("lvim.config"):reload()
  207. end,
  208. })
  209. end
  210. function M.enable_transparent_mode()
  211. vim.api.nvim_create_autocmd("ColorScheme", {
  212. pattern = "*",
  213. callback = function()
  214. local hl_groups = {
  215. "Normal",
  216. "SignColumn",
  217. "NormalNC",
  218. "TelescopeBorder",
  219. "NvimTreeNormal",
  220. "NvimTreeNormalNC",
  221. "EndOfBuffer",
  222. "MsgArea",
  223. }
  224. for _, name in ipairs(hl_groups) do
  225. vim.cmd(string.format("highlight %s ctermbg=none guibg=none", name))
  226. end
  227. end,
  228. })
  229. vim.opt.fillchars = "eob: "
  230. end
  231. --- Clean autocommand in a group if it exists
  232. --- This is safer than trying to delete the augroup itself
  233. ---@param name string the augroup name
  234. function M.clear_augroup(name)
  235. -- defer the function in case the autocommand is still in-use
  236. Log:debug("request to clear autocmds " .. name)
  237. vim.schedule(function()
  238. pcall(function()
  239. vim.api.nvim_clear_autocmds { group = name }
  240. end)
  241. end)
  242. end
  243. --- Create autocommand groups based on the passed definitions
  244. --- Also creates the augroup automatically if it doesn't exist
  245. ---@param definitions table contains a tuple of event, opts, see `:h nvim_create_autocmd`
  246. function M.define_autocmds(definitions)
  247. for _, entry in ipairs(definitions) do
  248. local event = entry[1]
  249. local opts = entry[2]
  250. if type(opts.group) == "string" and opts.group ~= "" then
  251. local exists, _ = pcall(vim.api.nvim_get_autocmds, { group = opts.group })
  252. if not exists then
  253. vim.api.nvim_create_augroup(opts.group, {})
  254. end
  255. end
  256. vim.api.nvim_create_autocmd(event, opts)
  257. end
  258. end
  259. return M