autocomands.lua 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. -- Create autogroups first
  2. local MZuninoGroup = vim.api.nvim_create_augroup("mzunino", {})
  3. local yank_group = vim.api.nvim_create_augroup("HighlightYank", {})
  4. -- Set bigfile size threshold
  5. vim.g.bigfile_size = 1024 * 1024 * 1.5 -- 1.5 MB
  6. -- Netrw diagnostic disable
  7. vim.api.nvim_create_autocmd("FileType", {
  8. pattern = "netrw",
  9. callback = function()
  10. vim.diagnostic.enable(false)
  11. end,
  12. })
  13. -- Template files
  14. vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
  15. pattern = "*.templ",
  16. command = "set filetype=templ",
  17. })
  18. -- Highlight on yank
  19. vim.api.nvim_create_autocmd("TextYankPost", {
  20. group = yank_group,
  21. pattern = "*",
  22. callback = function()
  23. vim.highlight.on_yank({
  24. higroup = "IncSearch",
  25. timeout = 40,
  26. })
  27. end,
  28. })
  29. -- Remove trailing whitespace on save
  30. vim.api.nvim_create_autocmd("BufWritePre", {
  31. group = MZuninoGroup,
  32. pattern = "*",
  33. command = [[%s/\s\+$//e]],
  34. })
  35. -- Bigfile detection
  36. vim.filetype.add({
  37. pattern = {
  38. [".*"] = {
  39. function(path, buf)
  40. return vim.bo[buf].filetype ~= "bigfile" and path and vim.fn.getfsize(path) > vim.g.bigfile_size and "bigfile"
  41. or nil
  42. end,
  43. },
  44. },
  45. })
  46. vim.api.nvim_create_autocmd("LspAttach", {
  47. group = vim.api.nvim_create_augroup("lsp", { clear = true }),
  48. callback = function(ev)
  49. local opts = { buffer = ev.buf, silent = true }
  50. nmap("K", vim.lsp.buf.hover, vim.tbl_extend("force", opts, { desc = "Hover Doc" }))
  51. nmap("<C-h>", vim.lsp.buf.signature_help, vim.tbl_extend("force", opts, { desc = "Signature Help" }))
  52. nmap("<leader>r", vim.lsp.buf.rename, vim.tbl_extend("force", opts, { desc = "Rename" }))
  53. nmap("<leader>ca", vim.lsp.buf.code_action, vim.tbl_extend("force", opts, { desc = "Code Action" }))
  54. nmap("vd", vim.diagnostic.open_float, vim.tbl_extend("force", opts, { desc = "View Diagnostics" }))
  55. nmap("<leader>lr", "<cmd>LspRestart<CR>", vim.tbl_extend("force", opts, { desc = "Restart LSP" }))
  56. end,
  57. })