autocmds.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 = vim.fn.resolve(require("lvim.config"):get_user_config_path())
  6. return {
  7. _general_settings = {
  8. { "FileType", "qf,help,man", "nnoremap <silent> <buffer> q :close<CR>" },
  9. {
  10. "TextYankPost",
  11. "*",
  12. "lua require('vim.highlight').on_yank({higroup = 'Search', timeout = 200})",
  13. },
  14. {
  15. "BufWinEnter",
  16. "dashboard",
  17. "setlocal cursorline signcolumn=yes cursorcolumn number",
  18. },
  19. { "BufWritePost", user_config_file, "lua require('lvim.config'):reload()" },
  20. { "FileType", "qf", "set nobuflisted" },
  21. -- { "VimLeavePre", "*", "set title set titleold=" },
  22. },
  23. _formatoptions = {
  24. {
  25. "BufWinEnter,BufRead,BufNewFile",
  26. "*",
  27. "setlocal formatoptions-=c formatoptions-=r formatoptions-=o",
  28. },
  29. },
  30. _filetypechanges = {
  31. { "BufWinEnter", ".tf", "setlocal filetype=terraform" },
  32. { "BufRead", "*.tf", "setlocal filetype=terraform" },
  33. { "BufNewFile", "*.tf", "setlocal filetype=terraform" },
  34. { "BufWinEnter", ".zsh", "setlocal filetype=sh" },
  35. { "BufRead", "*.zsh", "setlocal filetype=sh" },
  36. { "BufNewFile", "*.zsh", "setlocal filetype=sh" },
  37. },
  38. _git = {
  39. { "FileType", "gitcommit", "setlocal wrap" },
  40. { "FileType", "gitcommit", "setlocal spell" },
  41. },
  42. _markdown = {
  43. { "FileType", "markdown", "setlocal wrap" },
  44. { "FileType", "markdown", "setlocal spell" },
  45. },
  46. _buffer_bindings = {
  47. { "FileType", "floaterm", "nnoremap <silent> <buffer> q :q<CR>" },
  48. },
  49. _auto_resize = {
  50. -- will cause split windows to be resized evenly if main window is resized
  51. { "VimResized", "*", "tabdo wincmd =" },
  52. },
  53. _general_lsp = {
  54. { "FileType", "lspinfo,lsp-installer,null-ls-info", "nnoremap <silent> <buffer> q :close<CR>" },
  55. },
  56. custom_groups = {},
  57. }
  58. end
  59. local get_format_on_save_opts = function()
  60. local defaults = require("lvim.config.defaults").format_on_save
  61. -- accept a basic boolean `lvim.format_on_save=true`
  62. if type(lvim.format_on_save) ~= "table" then
  63. return defaults
  64. end
  65. return {
  66. pattern = lvim.format_on_save.pattern or defaults.pattern,
  67. timeout = lvim.format_on_save.timeout or defaults.timeout,
  68. }
  69. end
  70. function M.enable_format_on_save(opts)
  71. local fmd_cmd = string.format(":silent lua vim.lsp.buf.formatting_sync({}, %s)", opts.timeout_ms)
  72. M.define_augroups {
  73. format_on_save = { { "BufWritePre", opts.pattern, fmd_cmd } },
  74. }
  75. Log:debug "enabled format-on-save"
  76. end
  77. function M.disable_format_on_save()
  78. M.remove_augroup "format_on_save"
  79. Log:debug "disabled format-on-save"
  80. end
  81. function M.configure_format_on_save()
  82. if lvim.format_on_save then
  83. if vim.fn.exists "#format_on_save#BufWritePre" == 1 then
  84. M.remove_augroup "format_on_save"
  85. Log:debug "reloading format-on-save configuration"
  86. end
  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.remove_augroup(name)
  102. if vim.fn.exists("#" .. name) == 1 then
  103. vim.cmd("au! " .. name)
  104. end
  105. end
  106. function M.define_augroups(definitions) -- {{{1
  107. -- Create autocommand groups based on the passed definitions
  108. --
  109. -- The key will be the name of the group, and each definition
  110. -- within the group should have:
  111. -- 1. Trigger
  112. -- 2. Pattern
  113. -- 3. Text
  114. -- just like how they would normally be defined from Vim itself
  115. for group_name, definition in pairs(definitions) do
  116. vim.cmd("augroup " .. group_name)
  117. vim.cmd "autocmd!"
  118. for _, def in pairs(definition) do
  119. local command = table.concat(vim.tbl_flatten { "autocmd", def }, " ")
  120. vim.cmd(command)
  121. end
  122. vim.cmd "augroup END"
  123. end
  124. end
  125. return M