plugin-loader.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.fn.delete(plugins_dir, "rf")
  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.api.nvim_create_autocmd("User", { pattern = "LazyDone", callback = require("lvim.lsp").setup })
  36. end
  37. vim.opt.runtimepath:append(lazy_install_dir)
  38. vim.opt.runtimepath:append(join_paths(plugins_dir, "*"))
  39. pcall(function()
  40. -- set a custom path for lazy's cache
  41. local lazy_cache = require "lazy.core.cache"
  42. lazy_cache.path = join_paths(get_cache_dir(), "lazy", "luac")
  43. end)
  44. end
  45. function plugin_loader.reload(spec)
  46. local Config = require "lazy.core.config"
  47. local lazy = require "lazy"
  48. -- TODO: reset cache? and unload plugins?
  49. Config.spec = spec
  50. require("lazy.core.plugin").load(true)
  51. require("lazy.core.plugin").update_state()
  52. local not_installed_plugins = vim.tbl_filter(function(plugin)
  53. return not plugin._.installed
  54. end, Config.plugins)
  55. require("lazy.manage").clear()
  56. if #not_installed_plugins > 0 then
  57. lazy.install { wait = true }
  58. end
  59. if #Config.to_clean > 0 then
  60. -- TODO: set show to true when lazy shows something useful on clean
  61. lazy.clean { wait = true, show = false }
  62. end
  63. end
  64. function plugin_loader.load(configurations)
  65. Log:debug "loading plugins configuration"
  66. local lazy_available, lazy = pcall(require, "lazy")
  67. if not lazy_available then
  68. Log:warn "skipping loading plugins until lazy.nvim is installed"
  69. return
  70. end
  71. -- remove plugins from rtp before loading lazy, so that all plugins won't be loaded on startup
  72. vim.opt.runtimepath:remove(join_paths(plugins_dir, "*"))
  73. local status_ok = xpcall(function()
  74. table.insert(lvim.lazy.opts.install.colorscheme, 1, lvim.colorscheme)
  75. lazy.setup(configurations, lvim.lazy.opts)
  76. end, debug.traceback)
  77. if not status_ok then
  78. Log:warn "problems detected while loading plugins' configurations"
  79. Log:trace(debug.traceback())
  80. end
  81. end
  82. function plugin_loader.get_core_plugins()
  83. local names = {}
  84. local plugins = require "lvim.plugins"
  85. local get_name = require("lazy.core.plugin").Spec.get_name
  86. for _, spec in pairs(plugins) do
  87. if spec.enabled == true or spec.enabled == nil then
  88. table.insert(names, get_name(spec[1]))
  89. end
  90. end
  91. return names
  92. end
  93. function plugin_loader.sync_core_plugins()
  94. local core_plugins = plugin_loader.get_core_plugins()
  95. Log:trace(string.format("Syncing core plugins: [%q]", table.concat(core_plugins, ", ")))
  96. require("lazy").update { wait = true, plugins = core_plugins }
  97. end
  98. function plugin_loader.ensure_plugins()
  99. Log:debug "calling lazy.install()"
  100. require("lazy").install { wait = true }
  101. end
  102. return plugin_loader