autocmds.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. local M = {}
  2. local Log = require "lvim.core.log"
  3. --- Load the default set of autogroups and autocommands.
  4. function M.load_augroups()
  5. local user_config_file = require("lvim.config"):get_user_config_path()
  6. if vim.loop.os_uname().version:match "Windows" then
  7. -- autocmds require forward slashes even on windows
  8. user_config_file = user_config_file:gsub("\\", "/")
  9. end
  10. return {
  11. _general_settings = {
  12. { "FileType", "qf,help,man", "nnoremap <silent> <buffer> q :close<CR>" },
  13. {
  14. "TextYankPost",
  15. "*",
  16. "lua require('vim.highlight').on_yank({higroup = 'Search', timeout = 200})",
  17. },
  18. {
  19. "BufWinEnter",
  20. "dashboard",
  21. "setlocal cursorline signcolumn=yes cursorcolumn number",
  22. },
  23. { "BufWritePost", user_config_file, "lua require('lvim.config'):reload()" },
  24. { "FileType", "qf", "set nobuflisted" },
  25. -- { "VimLeavePre", "*", "set title set titleold=" },
  26. },
  27. _formatoptions = {
  28. {
  29. "BufWinEnter,BufRead,BufNewFile",
  30. "*",
  31. "setlocal formatoptions-=c formatoptions-=r formatoptions-=o",
  32. },
  33. },
  34. _filetypechanges = {
  35. { "BufWinEnter", ".tf", "setlocal filetype=terraform" },
  36. { "BufRead", "*.tf", "setlocal filetype=terraform" },
  37. { "BufNewFile", "*.tf", "setlocal filetype=terraform" },
  38. { "BufWinEnter", ".zsh", "setlocal filetype=sh" },
  39. { "BufRead", "*.zsh", "setlocal filetype=sh" },
  40. { "BufNewFile", "*.zsh", "setlocal filetype=sh" },
  41. },
  42. _git = {
  43. { "FileType", "gitcommit", "setlocal wrap" },
  44. { "FileType", "gitcommit", "setlocal spell" },
  45. },
  46. _markdown = {
  47. { "FileType", "markdown", "setlocal wrap" },
  48. { "FileType", "markdown", "setlocal spell" },
  49. },
  50. _buffer_bindings = {
  51. { "FileType", "floaterm", "nnoremap <silent> <buffer> q :q<CR>" },
  52. },
  53. _auto_resize = {
  54. -- will cause split windows to be resized evenly if main window is resized
  55. { "VimResized", "*", "tabdo wincmd =" },
  56. },
  57. _general_lsp = {
  58. { "FileType", "lspinfo,lsp-installer,null-ls-info", "nnoremap <silent> <buffer> q :close<CR>" },
  59. },
  60. custom_groups = {},
  61. }
  62. end
  63. local get_format_on_save_opts = function()
  64. local defaults = require("lvim.config.defaults").format_on_save
  65. -- accept a basic boolean `lvim.format_on_save=true`
  66. if type(lvim.format_on_save) ~= "table" then
  67. return defaults
  68. end
  69. return {
  70. pattern = lvim.format_on_save.pattern or defaults.pattern,
  71. timeout = lvim.format_on_save.timeout or defaults.timeout,
  72. }
  73. end
  74. function M.enable_format_on_save(opts)
  75. local fmd_cmd = string.format(":silent lua vim.lsp.buf.formatting_sync({}, %s)", opts.timeout)
  76. M.define_augroups {
  77. format_on_save = { { "BufWritePre", opts.pattern, fmd_cmd } },
  78. }
  79. Log:debug "enabled format-on-save"
  80. end
  81. function M.disable_format_on_save()
  82. M.disable_augroup "format_on_save"
  83. Log:debug "disabled format-on-save"
  84. end
  85. function M.configure_format_on_save()
  86. if lvim.format_on_save then
  87. local opts = get_format_on_save_opts()
  88. M.enable_format_on_save(opts)
  89. else
  90. M.disable_format_on_save()
  91. end
  92. end
  93. function M.toggle_format_on_save()
  94. if vim.fn.exists "#format_on_save#BufWritePre" == 0 then
  95. local opts = get_format_on_save_opts()
  96. M.enable_format_on_save(opts)
  97. else
  98. M.disable_format_on_save()
  99. end
  100. end
  101. function M.enable_lsp_document_highlight(client_id)
  102. M.define_augroups({
  103. lsp_document_highlight = {
  104. {
  105. "CursorHold",
  106. "<buffer>",
  107. string.format("lua require('lvim.lsp.utils').conditional_document_highlight(%d)", client_id),
  108. },
  109. {
  110. "CursorMoved",
  111. "<buffer>",
  112. "lua vim.lsp.buf.clear_references()",
  113. },
  114. },
  115. }, true)
  116. end
  117. function M.disable_lsp_document_highlight()
  118. M.disable_augroup "lsp_document_highlight"
  119. end
  120. function M.enable_code_lens_refresh()
  121. M.define_augroups({
  122. lsp_code_lens_refresh = {
  123. {
  124. "InsertLeave ",
  125. "<buffer>",
  126. "lua vim.lsp.codelens.refresh()",
  127. },
  128. {
  129. "InsertLeave ",
  130. "<buffer>",
  131. "lua vim.lsp.codelens.display()",
  132. },
  133. },
  134. }, true)
  135. end
  136. function M.disable_code_lens_refresh()
  137. M.disable_augroup "lsp_code_lens_refresh"
  138. end
  139. function M.enable_transparent_mode()
  140. vim.cmd "au ColorScheme * hi Normal ctermbg=none guibg=none"
  141. vim.cmd "au ColorScheme * hi SignColumn ctermbg=none guibg=none"
  142. vim.cmd "au ColorScheme * hi NormalNC ctermbg=none guibg=none"
  143. vim.cmd "au ColorScheme * hi MsgArea ctermbg=none guibg=none"
  144. vim.cmd "au ColorScheme * hi TelescopeBorder ctermbg=none guibg=none"
  145. vim.cmd "au ColorScheme * hi NvimTreeNormal ctermbg=none guibg=none"
  146. vim.cmd "au ColorScheme * hi EndOfBuffer ctermbg=none guibg=none"
  147. vim.cmd "let &fcs='eob: '"
  148. end
  149. --- Disable autocommand groups if it exists
  150. --- This is more reliable than trying to delete the augroup itself
  151. ---@param name string the augroup name
  152. function M.disable_augroup(name)
  153. -- defer the function in case the autocommand is still in-use
  154. vim.schedule(function()
  155. if vim.fn.exists("#" .. name) == 1 then
  156. vim.cmd("augroup " .. name)
  157. vim.cmd "autocmd!"
  158. vim.cmd "augroup END"
  159. end
  160. end)
  161. end
  162. --- Create autocommand groups based on the passed definitions
  163. ---@param definitions table contains trigger, pattern and text. The key will be used as a group name
  164. ---@param buffer boolean indicate if the augroup should be local to the buffer
  165. function M.define_augroups(definitions, buffer)
  166. for group_name, definition in pairs(definitions) do
  167. vim.cmd("augroup " .. group_name)
  168. if buffer then
  169. vim.cmd [[autocmd! * <buffer>]]
  170. else
  171. vim.cmd [[autocmd!]]
  172. end
  173. for _, def in pairs(definition) do
  174. local command = table.concat(vim.tbl_flatten { "autocmd", def }, " ")
  175. vim.cmd(command)
  176. end
  177. vim.cmd "augroup END"
  178. end
  179. end
  180. return M