autocmds.lua 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 = [[
  73. let _auto_resize_current_tab = tabpagenr()
  74. tabdo wincmd =
  75. execute 'tabnext' _auto_resize_current_tab
  76. ]],
  77. },
  78. },
  79. {
  80. "FileType",
  81. {
  82. group = "_filetype_settings",
  83. pattern = "alpha",
  84. callback = function()
  85. vim.cmd [[
  86. set nobuflisted
  87. ]]
  88. end,
  89. },
  90. },
  91. {
  92. "FileType",
  93. {
  94. group = "_filetype_settings",
  95. pattern = "lir",
  96. callback = function()
  97. vim.opt_local.number = false
  98. vim.opt_local.relativenumber = false
  99. end,
  100. },
  101. },
  102. {
  103. "ColorScheme",
  104. {
  105. group = "_lvim_colorscheme",
  106. callback = function()
  107. if lvim.builtin.breadcrumbs.active then
  108. require("lvim.core.breadcrumbs").get_winbar()
  109. end
  110. local statusline_hl = vim.api.nvim_get_hl(0, { name = "StatusLine" })
  111. local cursorline_hl = vim.api.nvim_get_hl(0, { name = "CursorLine" })
  112. local normal_hl = vim.api.nvim_get_hl(0, { name = "Normal" })
  113. vim.api.nvim_set_hl(0, "CmpItemKindCopilot", { fg = "#6CC644" })
  114. vim.api.nvim_set_hl(0, "CmpItemKindTabnine", { fg = "#CA42F0" })
  115. vim.api.nvim_set_hl(0, "CmpItemKindCrate", { fg = "#F64D00" })
  116. vim.api.nvim_set_hl(0, "CmpItemKindEmoji", { fg = "#FDE030" })
  117. vim.api.nvim_set_hl(0, "SLCopilot", { fg = "#6CC644", bg = statusline_hl.bg })
  118. vim.api.nvim_set_hl(0, "SLGitIcon", { fg = "#E8AB53", bg = cursorline_hl.bg })
  119. vim.api.nvim_set_hl(0, "SLBranchName", { fg = normal_hl.fg, bg = cursorline_hl.bg })
  120. vim.api.nvim_set_hl(0, "SLSeparator", { fg = cursorline_hl.fg, bg = statusline_hl.bg })
  121. end,
  122. },
  123. },
  124. { -- taken from AstroNvim
  125. "BufEnter",
  126. {
  127. group = "_dir_opened",
  128. nested = true,
  129. callback = function(args)
  130. local bufname = vim.api.nvim_buf_get_name(args.buf)
  131. if require("lvim.utils").is_directory(bufname) then
  132. vim.api.nvim_del_augroup_by_name "_dir_opened"
  133. vim.cmd "do User DirOpened"
  134. vim.api.nvim_exec_autocmds(args.event, { buffer = args.buf, data = args.data })
  135. end
  136. end,
  137. },
  138. },
  139. { -- taken from AstroNvim
  140. { "BufRead", "BufWinEnter", "BufNewFile" },
  141. {
  142. group = "_file_opened",
  143. nested = true,
  144. callback = function(args)
  145. local buftype = vim.api.nvim_get_option_value("buftype", { buf = args.buf })
  146. if not (vim.fn.expand "%" == "" or buftype == "nofile") then
  147. vim.api.nvim_del_augroup_by_name "_file_opened"
  148. vim.api.nvim_exec_autocmds("User", { pattern = "FileOpened" })
  149. require("lvim.lsp").setup()
  150. end
  151. end,
  152. },
  153. },
  154. }
  155. M.define_autocmds(definitions)
  156. end
  157. local get_format_on_save_opts = function()
  158. local defaults = require("lvim.config.defaults").format_on_save
  159. -- accept a basic boolean `lvim.format_on_save=true`
  160. if type(lvim.format_on_save) ~= "table" then
  161. return defaults
  162. end
  163. return {
  164. pattern = lvim.format_on_save.pattern or defaults.pattern,
  165. timeout = lvim.format_on_save.timeout or defaults.timeout,
  166. }
  167. end
  168. function M.enable_format_on_save()
  169. local opts = get_format_on_save_opts()
  170. vim.api.nvim_create_augroup("lsp_format_on_save", {})
  171. vim.api.nvim_create_autocmd("BufWritePre", {
  172. group = "lsp_format_on_save",
  173. pattern = opts.pattern,
  174. callback = function()
  175. require("lvim.lsp.utils").format { timeout_ms = opts.timeout, filter = opts.filter }
  176. end,
  177. })
  178. Log:debug "enabled format-on-save"
  179. end
  180. function M.disable_format_on_save()
  181. M.clear_augroup "lsp_format_on_save"
  182. Log:debug "disabled format-on-save"
  183. end
  184. function M.configure_format_on_save()
  185. if type(lvim.format_on_save) == "table" and lvim.format_on_save.enabled then
  186. M.enable_format_on_save()
  187. elseif lvim.format_on_save == true then
  188. M.enable_format_on_save()
  189. else
  190. M.disable_format_on_save()
  191. end
  192. end
  193. function M.toggle_format_on_save()
  194. local exists, autocmds = pcall(vim.api.nvim_get_autocmds, {
  195. group = "lsp_format_on_save",
  196. event = "BufWritePre",
  197. })
  198. if not exists or #autocmds == 0 then
  199. M.enable_format_on_save()
  200. else
  201. M.disable_format_on_save()
  202. end
  203. end
  204. function M.enable_reload_config_on_save()
  205. -- autocmds require forward slashes (even on windows)
  206. local pattern = get_config_dir():gsub("\\", "/") .. "/*.lua"
  207. vim.api.nvim_create_augroup("lvim_reload_config_on_save", {})
  208. vim.api.nvim_create_autocmd("BufWritePost", {
  209. group = "lvim_reload_config_on_save",
  210. pattern = pattern,
  211. desc = "Trigger LvimReload on saving config.lua",
  212. callback = function()
  213. require("lvim.config"):reload()
  214. end,
  215. })
  216. end
  217. function M.enable_transparent_mode()
  218. vim.api.nvim_create_autocmd("ColorScheme", {
  219. pattern = "*",
  220. callback = function()
  221. local hl_groups = {
  222. "Normal",
  223. "SignColumn",
  224. "NormalNC",
  225. "TelescopeBorder",
  226. "NvimTreeNormal",
  227. "NvimTreeNormalNC",
  228. "EndOfBuffer",
  229. "MsgArea",
  230. }
  231. for _, name in ipairs(hl_groups) do
  232. vim.cmd(string.format("highlight %s ctermbg=none guibg=none", name))
  233. end
  234. end,
  235. })
  236. vim.opt.fillchars = "eob: "
  237. end
  238. --- Clean autocommand in a group if it exists
  239. --- This is safer than trying to delete the augroup itself
  240. ---@param name string the augroup name
  241. function M.clear_augroup(name)
  242. -- defer the function in case the autocommand is still in-use
  243. Log:debug("request to clear autocmds " .. name)
  244. vim.schedule(function()
  245. pcall(function()
  246. vim.api.nvim_clear_autocmds { group = name }
  247. end)
  248. end)
  249. end
  250. --- Create autocommand groups based on the passed definitions
  251. --- Also creates the augroup automatically if it doesn't exist
  252. ---@param definitions table contains a tuple of event, opts, see `:h nvim_create_autocmd`
  253. function M.define_autocmds(definitions)
  254. for _, entry in ipairs(definitions) do
  255. local event = entry[1]
  256. local opts = entry[2]
  257. if type(opts.group) == "string" and opts.group ~= "" then
  258. local exists, _ = pcall(vim.api.nvim_get_autocmds, { group = opts.group })
  259. if not exists then
  260. vim.api.nvim_create_augroup(opts.group, {})
  261. end
  262. end
  263. vim.api.nvim_create_autocmd(event, opts)
  264. end
  265. end
  266. return M