plugin-loader.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. local plugin_loader = {}
  2. local utils = require "lvim.utils"
  3. local Log = require "lvim.core.log"
  4. local join_paths = utils.join_paths
  5. local plugins_dir = join_paths(get_runtime_dir(), "site", "pack", "lazy", "opt")
  6. function plugin_loader.init(opts)
  7. opts = opts or {}
  8. local lazy_install_dir = opts.install_path
  9. or join_paths(vim.fn.stdpath "data", "site", "pack", "lazy", "opt", "lazy.nvim")
  10. if not utils.is_directory(lazy_install_dir) then
  11. print "Initializing first time setup"
  12. local core_plugins_dir = join_paths(get_lvim_base_dir(), "plugins")
  13. if utils.is_directory(core_plugins_dir) then
  14. vim.fn.mkdir(plugins_dir, "p")
  15. vim.loop.fs_rmdir(plugins_dir)
  16. require("lvim.utils").fs_copy(core_plugins_dir, plugins_dir)
  17. else
  18. vim.fn.system {
  19. "git",
  20. "clone",
  21. "--branch=stable",
  22. "https://github.com/folke/lazy.nvim.git",
  23. lazy_install_dir,
  24. }
  25. local default_snapshot_path = join_paths(get_lvim_base_dir(), "snapshots", "default.json")
  26. local snapshot = assert(vim.fn.json_decode(vim.fn.readfile(default_snapshot_path)))
  27. vim.fn.system {
  28. "git",
  29. "-C",
  30. lazy_install_dir,
  31. "checkout",
  32. snapshot["lazy.nvim"].commit,
  33. }
  34. end
  35. vim.schedule(function()
  36. require("lvim.lsp").setup()
  37. end)
  38. end
  39. vim.opt.runtimepath:append(lazy_install_dir)
  40. vim.opt.runtimepath:append(join_paths(plugins_dir, "*"))
  41. local lazy_cache = require "lazy.core.cache"
  42. lazy_cache.setup {
  43. performance = {
  44. cache = {
  45. enabled = true,
  46. path = join_paths(get_cache_dir(), "lazy", "cache"),
  47. },
  48. },
  49. }
  50. -- HACK: Don't allow lazy to call setup second time
  51. lazy_cache.setup = function() end
  52. end
  53. function plugin_loader.reset_cache()
  54. os.remove(require("lazy.core.cache").config.path)
  55. end
  56. function plugin_loader.reload(spec)
  57. local Config = require "lazy.core.config"
  58. local lazy = require "lazy"
  59. -- TODO: reset cache? and unload plugins?
  60. Config.spec = spec
  61. require("lazy.core.plugin").load(true)
  62. require("lazy.core.plugin").update_state()
  63. local not_installed_plugins = vim.tbl_filter(function(plugin)
  64. return not plugin._.installed
  65. end, Config.plugins)
  66. require("lazy.manage").clear()
  67. if #not_installed_plugins > 0 then
  68. lazy.install { wait = true }
  69. end
  70. if #Config.to_clean > 0 then
  71. -- TODO: set show to true when lazy shows something useful on clean
  72. lazy.clean { wait = true, show = false }
  73. end
  74. end
  75. function plugin_loader.load(configurations)
  76. Log:debug "loading plugins configuration"
  77. local lazy_available, lazy = pcall(require, "lazy")
  78. if not lazy_available then
  79. Log:warn "skipping loading plugins until lazy.nvim is installed"
  80. return
  81. end
  82. -- remove plugins from rtp before loading lazy, so that all plugins won't be loaded on startup
  83. vim.opt.runtimepath:remove(join_paths(plugins_dir, "*"))
  84. local status_ok = xpcall(function()
  85. local opts = {
  86. install = {
  87. missing = true,
  88. colorscheme = { lvim.colorscheme, "lunar", "habamax" },
  89. },
  90. ui = {
  91. border = "rounded",
  92. },
  93. root = plugins_dir,
  94. git = {
  95. timeout = 120,
  96. },
  97. lockfile = join_paths(get_config_dir(), "lazy-lock.json"),
  98. performance = {
  99. rtp = {
  100. reset = false,
  101. },
  102. },
  103. defaults = {
  104. lazy = false,
  105. version = nil,
  106. },
  107. readme = {
  108. root = join_paths(get_runtime_dir(), "lazy", "readme"),
  109. },
  110. }
  111. lazy.setup(configurations, opts)
  112. end, debug.traceback)
  113. if not status_ok then
  114. Log:warn "problems detected while loading plugins' configurations"
  115. Log:trace(debug.traceback())
  116. end
  117. end
  118. function plugin_loader.get_core_plugins()
  119. local names = {}
  120. local plugins = require "lvim.plugins"
  121. local get_name = require("lazy.core.plugin").Spec.get_name
  122. for _, spec in pairs(plugins) do
  123. if spec.enabled == true or spec.enabled == nil then
  124. table.insert(names, get_name(spec[1]))
  125. end
  126. end
  127. return names
  128. end
  129. function plugin_loader.sync_core_plugins()
  130. local core_plugins = plugin_loader.get_core_plugins()
  131. Log:trace(string.format("Syncing core plugins: [%q]", table.concat(core_plugins, ", ")))
  132. require("lazy").update { wait = true, plugins = core_plugins }
  133. end
  134. function plugin_loader.ensure_plugins()
  135. Log:debug "calling lazy.install()"
  136. require("lazy").install { wait = true }
  137. end
  138. return plugin_loader