autocmds.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 type(lvim.format_on_save) == "table" and lvim.format_on_save.enabled then
  155. M.enable_format_on_save()
  156. elseif lvim.format_on_save == true then
  157. M.enable_format_on_save()
  158. else
  159. M.disable_format_on_save()
  160. end
  161. end
  162. function M.toggle_format_on_save()
  163. local exists, autocmds = pcall(vim.api.nvim_get_autocmds, {
  164. group = "lsp_format_on_save",
  165. event = "BufWritePre",
  166. })
  167. if not exists or #autocmds == 0 then
  168. M.enable_format_on_save()
  169. else
  170. M.disable_format_on_save()
  171. end
  172. end
  173. function M.enable_reload_config_on_save()
  174. local user_config_file = require("lvim.config"):get_user_config_path()
  175. if vim.loop.os_uname().version:match "Windows" then
  176. -- autocmds require forward slashes even on windows
  177. user_config_file = user_config_file:gsub("\\", "/")
  178. end
  179. vim.api.nvim_create_autocmd("BufWritePost", {
  180. group = "_general_settings",
  181. pattern = user_config_file,
  182. desc = "Trigger LvimReload on saving config.lua",
  183. callback = function()
  184. require("lvim.config"):reload()
  185. end,
  186. })
  187. end
  188. function M.enable_transparent_mode()
  189. vim.api.nvim_create_autocmd("ColorScheme", {
  190. pattern = "*",
  191. callback = function()
  192. local hl_groups = {
  193. "Normal",
  194. "SignColumn",
  195. "NormalNC",
  196. "TelescopeBorder",
  197. "NvimTreeNormal",
  198. "EndOfBuffer",
  199. "MsgArea",
  200. }
  201. for _, name in ipairs(hl_groups) do
  202. vim.cmd(string.format("highlight %s ctermbg=none guibg=none", name))
  203. end
  204. end,
  205. })
  206. vim.opt.fillchars = "eob: "
  207. end
  208. --- Clean autocommand in a group if it exists
  209. --- This is safer than trying to delete the augroup itself
  210. ---@param name string the augroup name
  211. function M.clear_augroup(name)
  212. -- defer the function in case the autocommand is still in-use
  213. Log:debug("request to clear autocmds " .. name)
  214. vim.schedule(function()
  215. pcall(function()
  216. vim.api.nvim_clear_autocmds { group = name }
  217. end)
  218. end)
  219. end
  220. --- Create autocommand groups based on the passed definitions
  221. --- Also creates the augroup automatically if it doesn't exist
  222. ---@param definitions table contains a tuple of event, opts, see `:h nvim_create_autocmd`
  223. function M.define_autocmds(definitions)
  224. for _, entry in ipairs(definitions) do
  225. local event = entry[1]
  226. local opts = entry[2]
  227. if type(opts.group) == "string" and opts.group ~= "" then
  228. local exists, _ = pcall(vim.api.nvim_get_autocmds, { group = opts.group })
  229. if not exists then
  230. vim.api.nvim_create_augroup(opts.group, {})
  231. end
  232. end
  233. vim.api.nvim_create_autocmd(event, opts)
  234. end
  235. end
  236. return M