autocmds.lua 6.7 KB

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