hooks.lua 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. end
  7. function M.run_pre_reload()
  8. Log:debug "Starting pre-reload hook"
  9. end
  10. function M.run_on_packer_complete()
  11. -- manually trigger event to fix colors
  12. vim.cmd [[ doautocmd ColorScheme ]]
  13. Log:info "Reloaded configuration"
  14. end
  15. function M.run_post_reload()
  16. Log:debug "Starting post-reload hook"
  17. require("lvim.plugin-loader").ensure_installed()
  18. M.reset_cache()
  19. if package.loaded["lspconfig"] then
  20. pcall(vim.cmd, "LspRestart")
  21. end
  22. end
  23. ---Reset any startup cache files used by Packer and Impatient
  24. ---It also forces regenerating any template ftplugin files
  25. ---Tip: Useful for clearing any outdated settings
  26. function M.reset_cache()
  27. local impatient = _G.__luacache
  28. if impatient then
  29. impatient.clear_cache()
  30. end
  31. local lvim_modules = {}
  32. for module, _ in pairs(package.loaded) do
  33. if module:match "lvim.core" or module:match "lvim.lsp" then
  34. package.loaded[module] = nil
  35. table.insert(lvim_modules, module)
  36. end
  37. end
  38. Log:trace(string.format("Cache invalidated for core modules: { %s }", table.concat(lvim_modules, ", ")))
  39. require("lvim.lsp.templates").generate_templates()
  40. end
  41. function M.run_post_update()
  42. Log:debug "Starting post-update hook"
  43. M.reset_cache()
  44. Log:debug "Updating core plugins"
  45. require("lvim.plugin-loader").ensure_installed()
  46. if not in_headless then
  47. vim.schedule(function()
  48. if package.loaded["nvim-treesitter"] then
  49. vim.cmd [[ TSUpdateSync ]]
  50. end
  51. -- TODO: add a changelog
  52. vim.notify("Update complete", vim.log.levels.INFO)
  53. end)
  54. end
  55. end
  56. return M