hooks.lua 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. local M = {}
  2. local Log = require "lvim.core.log"
  3. local in_headless = #vim.api.nvim_list_uis() == 0
  4. function M.run_pre_update()
  5. Log:debug "Starting pre-update hook"
  6. if package.loaded["lspconfig"] then
  7. vim.cmd [[ LspStop ]]
  8. end
  9. end
  10. function M.run_pre_reload()
  11. Log:debug "Starting pre-reload hook"
  12. if package.loaded["lspconfig"] then
  13. vim.cmd [[ LspStop ]]
  14. end
  15. end
  16. function M.run_on_packer_complete()
  17. require("lvim.plugin-loader").recompile()
  18. -- forcefully activate nvim-web-devicons
  19. require("nvim-web-devicons").set_up_highlights()
  20. Log:info "Reloaded configuration"
  21. end
  22. function M.run_post_reload()
  23. Log:debug "Starting post-reload hook"
  24. if package.loaded["lspconfig"] then
  25. vim.cmd [[ LspRestart ]]
  26. end
  27. M.reset_cache()
  28. require("lvim.plugin-loader").ensure_installed()
  29. end
  30. ---Reset any startup cache files used by Packer and Impatient
  31. ---It also forces regenerating any template ftplugin files
  32. ---Tip: Useful for clearing any outdated settings
  33. function M.reset_cache()
  34. local impatient = _G.__luacache
  35. if impatient then
  36. impatient.clear_cache()
  37. end
  38. local lvim_modules = {}
  39. for module, _ in pairs(package.loaded) do
  40. if module:match "lvim.core" or module:match "lvim.lsp" then
  41. package.loaded[module] = nil
  42. table.insert(lvim_modules, module)
  43. end
  44. end
  45. Log:trace(string.format("Cache invalidated for core modules: { %s }", table.concat(lvim_modules, ", ")))
  46. require("lvim.lsp.templates").generate_templates()
  47. end
  48. function M.run_post_update()
  49. Log:debug "Starting post-update hook"
  50. M.reset_cache()
  51. Log:debug "Updating core plugins"
  52. require("lvim.plugin-loader").ensure_installed()
  53. if not in_headless then
  54. vim.schedule(function()
  55. if package.loaded["nvim-treesitter"] then
  56. vim.cmd [[ TSUpdateSync ]]
  57. end
  58. -- TODO: add a changelog
  59. vim.notify("Update complete", vim.log.levels.INFO)
  60. if package.loaded["lspconfig"] then
  61. vim.cmd [[ LspRestart ]]
  62. end
  63. end)
  64. end
  65. end
  66. return M