autocmd.lua 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. -- apply default terminal settings
  2. vim.api.nvim_create_autocmd('TermOpen', {
  3. callback = function()
  4. vim.bo.scrollback = 10000
  5. vim.opt_local.number = false
  6. vim.opt_local.relativenumber = false
  7. vim.b.miniindentscope_disable = true
  8. end
  9. })
  10. -- preserve window structure when exiting terminal via C-d
  11. vim.api.nvim_create_autocmd('TermClose', {
  12. callback = function(opts)
  13. -- don't trigger when force deleting
  14. if vim.api.nvim_buf_get_option(opts.buf, 'modified') then
  15. return
  16. end
  17. MiniBufremove.delete(opts.buf)
  18. end,
  19. -- needed so statusline properly updates
  20. nested = true,
  21. })
  22. -- automatically enter/leave terminal mode
  23. vim.api.nvim_create_autocmd('TermOpen', { command = 'startinsert' })
  24. vim.api.nvim_create_autocmd({'WinEnter','BufWinEnter'}, {
  25. pattern = 'term://*',
  26. command = 'startinsert',
  27. })
  28. vim.api.nvim_create_autocmd('BufLeave', {
  29. pattern = 'term://*',
  30. command = 'stopinsert',
  31. })
  32. -- rename
  33. vim.api.nvim_create_autocmd('User', {
  34. pattern = 'MiniFilesActionRename',
  35. callback = function(opts)
  36. local params = {
  37. files = {{
  38. oldUri = vim.uri_from_fname(opts.data.from),
  39. newUri = vim.uri_from_fname(opts.data.to),
  40. }}
  41. }
  42. local bufnr = vim.fn.bufadd(opts.data.to)
  43. local clients = vim.lsp.get_clients({ bufnr = bufnr })
  44. for _, client in ipairs(clients) do
  45. if client:supports_method('workspace/willRenameFiles') then
  46. local resp = client:request_sync('workspace/willRenameFiles', params, 5000, bufnr)
  47. if resp and resp.result ~= nil then
  48. vim.lsp.util.apply_workspace_edit(resp.result, client.offset_encoding)
  49. end
  50. end
  51. end
  52. for _, client in ipairs(clients) do
  53. if client:supports_method('workspace/didRenameFiles') then
  54. client:notify('workspace/didRenameFiles', params)
  55. end
  56. end
  57. end,
  58. nested = true,
  59. })
  60. -- formatting
  61. vim.api.nvim_create_autocmd('BufWritePre', {
  62. callback = function(opts)
  63. if vim.bo.filetype == 'diff' then
  64. return
  65. end
  66. if not vim.g.no_lsp_format then
  67. -- check if can LSP format
  68. local clients = vim.lsp.get_clients({ bufnr = opts.buf, method = 'textDocument/formatting' })
  69. if #clients > 0 then
  70. vim.lsp.buf.format({ bufnr = opts.buf })
  71. end
  72. end
  73. -- otherwise strip trailing whitespace
  74. vim.cmd('%s/\\s\\+$//e')
  75. end,
  76. })
  77. -- filetype specific options
  78. vim.api.nvim_create_autocmd('FileType', {
  79. pattern = 'markdown',
  80. callback = function()
  81. vim.bo.textwidth = 80
  82. end,
  83. })