autocmds.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. _git = {
  36. { "FileType", "gitcommit", "setlocal wrap" },
  37. { "FileType", "gitcommit", "setlocal spell" },
  38. },
  39. _markdown = {
  40. { "FileType", "markdown", "setlocal wrap" },
  41. { "FileType", "markdown", "setlocal spell" },
  42. },
  43. _buffer_bindings = {
  44. { "FileType", "floaterm", "nnoremap <silent> <buffer> q :q<CR>" },
  45. },
  46. _auto_resize = {
  47. -- will cause split windows to be resized evenly if main window is resized
  48. { "VimResized", "*", "tabdo wincmd =" },
  49. },
  50. _general_lsp = {
  51. { "FileType", "lspinfo,lsp-installer,null-ls-info", "nnoremap <silent> <buffer> q :close<CR>" },
  52. },
  53. custom_groups = {},
  54. }
  55. end
  56. local get_format_on_save_opts = function()
  57. local defaults = require("lvim.config.defaults").format_on_save
  58. -- accept a basic boolean `lvim.format_on_save=true`
  59. if type(lvim.format_on_save) ~= "table" then
  60. return defaults
  61. end
  62. return {
  63. pattern = lvim.format_on_save.pattern or defaults.pattern,
  64. timeout = lvim.format_on_save.timeout or defaults.timeout,
  65. }
  66. end
  67. function M.enable_format_on_save(opts)
  68. local fmd_cmd = string.format(":silent lua vim.lsp.buf.formatting_sync({}, %s)", opts.timeout)
  69. M.define_augroups {
  70. format_on_save = { { "BufWritePre", opts.pattern, fmd_cmd } },
  71. }
  72. Log:debug "enabled format-on-save"
  73. end
  74. function M.disable_format_on_save()
  75. M.disable_augroup "format_on_save"
  76. Log:debug "disabled format-on-save"
  77. end
  78. function M.configure_format_on_save()
  79. if lvim.format_on_save then
  80. local opts = get_format_on_save_opts()
  81. M.enable_format_on_save(opts)
  82. else
  83. M.disable_format_on_save()
  84. end
  85. end
  86. function M.toggle_format_on_save()
  87. if vim.fn.exists "#format_on_save#BufWritePre" == 0 then
  88. local opts = get_format_on_save_opts()
  89. M.enable_format_on_save(opts)
  90. else
  91. M.disable_format_on_save()
  92. end
  93. end
  94. function M.enable_lsp_document_highlight(client_id)
  95. M.define_augroups({
  96. lsp_document_highlight = {
  97. {
  98. "CursorHold",
  99. "<buffer>",
  100. string.format("lua require('lvim.lsp.utils').conditional_document_highlight(%d)", client_id),
  101. },
  102. {
  103. "CursorMoved",
  104. "<buffer>",
  105. "lua vim.lsp.buf.clear_references()",
  106. },
  107. },
  108. }, true)
  109. end
  110. function M.disable_lsp_document_highlight()
  111. M.disable_augroup "lsp_document_highlight"
  112. end
  113. function M.enable_code_lens_refresh()
  114. M.define_augroups({
  115. lsp_code_lens_refresh = {
  116. {
  117. "InsertLeave ",
  118. "<buffer>",
  119. "lua vim.lsp.codelens.refresh()",
  120. },
  121. {
  122. "InsertLeave ",
  123. "<buffer>",
  124. "lua vim.lsp.codelens.display()",
  125. },
  126. },
  127. }, true)
  128. end
  129. function M.disable_code_lens_refresh()
  130. M.disable_augroup "lsp_code_lens_refresh"
  131. end
  132. function M.enable_transparent_mode()
  133. vim.cmd "au ColorScheme * hi Normal ctermbg=none guibg=none"
  134. vim.cmd "au ColorScheme * hi SignColumn ctermbg=none guibg=none"
  135. vim.cmd "au ColorScheme * hi NormalNC ctermbg=none guibg=none"
  136. vim.cmd "au ColorScheme * hi MsgArea ctermbg=none guibg=none"
  137. vim.cmd "au ColorScheme * hi TelescopeBorder ctermbg=none guibg=none"
  138. vim.cmd "au ColorScheme * hi NvimTreeNormal ctermbg=none guibg=none"
  139. vim.cmd "au ColorScheme * hi EndOfBuffer ctermbg=none guibg=none"
  140. vim.cmd "let &fcs='eob: '"
  141. end
  142. --- Disable autocommand groups if it exists
  143. --- This is more reliable than trying to delete the augroup itself
  144. ---@param name string the augroup name
  145. function M.disable_augroup(name)
  146. -- defer the function in case the autocommand is still in-use
  147. vim.schedule(function()
  148. if vim.fn.exists("#" .. name) == 1 then
  149. vim.cmd("augroup " .. name)
  150. vim.cmd "autocmd!"
  151. vim.cmd "augroup END"
  152. end
  153. end)
  154. end
  155. --- Create autocommand groups based on the passed definitions
  156. ---@param definitions table contains trigger, pattern and text. The key will be used as a group name
  157. ---@param buffer boolean indicate if the augroup should be local to the buffer
  158. function M.define_augroups(definitions, buffer)
  159. for group_name, definition in pairs(definitions) do
  160. vim.cmd("augroup " .. group_name)
  161. if buffer then
  162. vim.cmd [[autocmd! * <buffer>]]
  163. else
  164. vim.cmd [[autocmd!]]
  165. end
  166. for _, def in pairs(definition) do
  167. local command = table.concat(vim.tbl_flatten { "autocmd", def }, " ")
  168. vim.cmd(command)
  169. end
  170. vim.cmd "augroup END"
  171. end
  172. end
  173. return M