autocmds.lua 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. vim.api.nvim_create_autocmd({ "FileType" }, {
  6. pattern = {
  7. "Jaq",
  8. "qf",
  9. "help",
  10. "man",
  11. "lspinfo",
  12. "spectre_panel",
  13. "lir",
  14. "DressingSelect",
  15. "tsplayground",
  16. },
  17. callback = function()
  18. vim.cmd [[
  19. nnoremap <silent> <buffer> q :close<CR>
  20. set nobuflisted
  21. ]]
  22. end,
  23. })
  24. local definitions = {
  25. {
  26. "TextYankPost",
  27. {
  28. group = "_general_settings",
  29. pattern = "*",
  30. desc = "Highlight text on yank",
  31. callback = function()
  32. require("vim.highlight").on_yank { higroup = "Search", timeout = 100 }
  33. end,
  34. },
  35. },
  36. {
  37. "FileType",
  38. {
  39. group = "_hide_dap_repl",
  40. pattern = "dap-repl",
  41. command = "set nobuflisted",
  42. },
  43. },
  44. {
  45. "FileType",
  46. {
  47. group = "_filetype_settings",
  48. pattern = "qf",
  49. command = "set nobuflisted",
  50. },
  51. },
  52. {
  53. "FileType",
  54. {
  55. group = "_filetype_settings",
  56. pattern = { "gitcommit", "markdown" },
  57. command = "setlocal wrap spell",
  58. },
  59. },
  60. {
  61. "FileType",
  62. {
  63. group = "_buffer_mappings",
  64. pattern = { "qf", "help", "man", "floaterm", "lspinfo", "lsp-installer", "null-ls-info" },
  65. command = "nnoremap <silent> <buffer> q :close<CR>",
  66. },
  67. },
  68. {
  69. "VimResized",
  70. {
  71. group = "_auto_resize",
  72. pattern = "*",
  73. command = "tabdo wincmd =",
  74. },
  75. },
  76. {
  77. "FileType",
  78. {
  79. group = "_filetype_settings",
  80. pattern = "alpha",
  81. callback = function()
  82. vim.cmd [[
  83. nnoremap <silent> <buffer> q :qa<CR>
  84. nnoremap <silent> <buffer> <esc> :qa<CR>
  85. set nobuflisted
  86. ]]
  87. end,
  88. },
  89. },
  90. {
  91. "FileType",
  92. {
  93. group = "_filetype_settings",
  94. pattern = "lir",
  95. callback = function()
  96. vim.opt_local.number = false
  97. vim.opt_local.relativenumber = false
  98. end,
  99. },
  100. },
  101. {
  102. "ColorScheme",
  103. {
  104. group = "_lvim_colorscheme",
  105. callback = function()
  106. if lvim.builtin.breadcrumbs.active then
  107. require("lvim.core.breadcrumbs").get_winbar()
  108. end
  109. local statusline_hl = vim.api.nvim_get_hl_by_name("StatusLine", true)
  110. local cursorline_hl = vim.api.nvim_get_hl_by_name("CursorLine", true)
  111. local normal_hl = vim.api.nvim_get_hl_by_name("Normal", true)
  112. vim.api.nvim_set_hl(0, "CmpItemKindCopilot", { fg = "#6CC644" })
  113. vim.api.nvim_set_hl(0, "CmpItemKindTabnine", { fg = "#CA42F0" })
  114. vim.api.nvim_set_hl(0, "CmpItemKindCrate", { fg = "#F64D00" })
  115. vim.api.nvim_set_hl(0, "CmpItemKindEmoji", { fg = "#FDE030" })
  116. vim.api.nvim_set_hl(0, "SLCopilot", { fg = "#6CC644", bg = statusline_hl.background })
  117. vim.api.nvim_set_hl(0, "SLGitIcon", { fg = "#E8AB53", bg = cursorline_hl.background })
  118. vim.api.nvim_set_hl(0, "SLBranchName", { fg = normal_hl.foreground, bg = cursorline_hl.background })
  119. vim.api.nvim_set_hl(0, "SLSeparator", { fg = cursorline_hl.background, bg = statusline_hl.background })
  120. end,
  121. },
  122. },
  123. }
  124. M.define_autocmds(definitions)
  125. end
  126. local get_format_on_save_opts = function()
  127. local defaults = require("lvim.config.defaults").format_on_save
  128. -- accept a basic boolean `lvim.format_on_save=true`
  129. if type(lvim.format_on_save) ~= "table" then
  130. return defaults
  131. end
  132. return {
  133. pattern = lvim.format_on_save.pattern or defaults.pattern,
  134. timeout = lvim.format_on_save.timeout or defaults.timeout,
  135. }
  136. end
  137. function M.enable_format_on_save()
  138. local opts = get_format_on_save_opts()
  139. vim.api.nvim_create_augroup("lsp_format_on_save", {})
  140. vim.api.nvim_create_autocmd("BufWritePre", {
  141. group = "lsp_format_on_save",
  142. pattern = opts.pattern,
  143. callback = function()
  144. require("lvim.lsp.utils").format { timeout_ms = opts.timeout, filter = opts.filter }
  145. end,
  146. })
  147. Log:debug "enabled format-on-save"
  148. end
  149. function M.disable_format_on_save()
  150. M.clear_augroup "lsp_format_on_save"
  151. Log:debug "disabled format-on-save"
  152. end
  153. function M.configure_format_on_save()
  154. if lvim.format_on_save then
  155. M.enable_format_on_save()
  156. else
  157. M.disable_format_on_save()
  158. end
  159. end
  160. function M.toggle_format_on_save()
  161. local exists, autocmds = pcall(vim.api.nvim_get_autocmds, {
  162. group = "lsp_format_on_save",
  163. event = "BufWritePre",
  164. })
  165. if not exists or #autocmds == 0 then
  166. M.enable_format_on_save()
  167. else
  168. M.disable_format_on_save()
  169. end
  170. end
  171. function M.enable_reload_config_on_save()
  172. local user_config_file = require("lvim.config"):get_user_config_path()
  173. if vim.loop.os_uname().version:match "Windows" then
  174. -- autocmds require forward slashes even on windows
  175. user_config_file = user_config_file:gsub("\\", "/")
  176. end
  177. vim.api.nvim_create_autocmd("BufWritePost", {
  178. group = "_general_settings",
  179. pattern = user_config_file,
  180. desc = "Trigger LvimReload on saving config.lua",
  181. callback = function()
  182. require("lvim.config"):reload()
  183. end,
  184. })
  185. end
  186. function M.enable_transparent_mode()
  187. vim.api.nvim_create_autocmd("ColorScheme", {
  188. pattern = "*",
  189. callback = function()
  190. local hl_groups = {
  191. "Normal",
  192. "SignColumn",
  193. "NormalNC",
  194. "TelescopeBorder",
  195. "NvimTreeNormal",
  196. "EndOfBuffer",
  197. "MsgArea",
  198. }
  199. for _, name in ipairs(hl_groups) do
  200. vim.cmd(string.format("highlight %s ctermbg=none guibg=none", name))
  201. end
  202. end,
  203. })
  204. vim.opt.fillchars = "eob: "
  205. end
  206. --- Clean autocommand in a group if it exists
  207. --- This is safer than trying to delete the augroup itself
  208. ---@param name string the augroup name
  209. function M.clear_augroup(name)
  210. -- defer the function in case the autocommand is still in-use
  211. Log:debug("request to clear autocmds " .. name)
  212. vim.schedule(function()
  213. pcall(function()
  214. vim.api.nvim_clear_autocmds { group = name }
  215. end)
  216. end)
  217. end
  218. --- Create autocommand groups based on the passed definitions
  219. --- Also creates the augroup automatically if it doesn't exist
  220. ---@param definitions table contains a tuple of event, opts, see `:h nvim_create_autocmd`
  221. function M.define_autocmds(definitions)
  222. for _, entry in ipairs(definitions) do
  223. local event = entry[1]
  224. local opts = entry[2]
  225. if type(opts.group) == "string" and opts.group ~= "" then
  226. local exists, _ = pcall(vim.api.nvim_get_autocmds, { group = opts.group })
  227. if not exists then
  228. vim.api.nvim_create_augroup(opts.group, {})
  229. end
  230. end
  231. vim.api.nvim_create_autocmd(event, opts)
  232. end
  233. end
  234. return M