plugin-loader.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. local rtp = vim.opt.rtp:get()
  38. local base_dir = (vim.env.LUNARVIM_BASE_DIR or get_runtime_dir() .. "/lvim"):gsub("\\", "/")
  39. local idx_base = #rtp + 1
  40. for i, path in ipairs(rtp) do
  41. path = path:gsub("\\", "/")
  42. if path == base_dir then
  43. idx_base = i + 1
  44. break
  45. end
  46. end
  47. table.insert(rtp, idx_base, lazy_install_dir)
  48. table.insert(rtp, idx_base + 1, join_paths(plugins_dir, "*"))
  49. vim.opt.rtp = rtp
  50. pcall(function()
  51. -- set a custom path for lazy's cache
  52. local lazy_cache = require "lazy.core.cache"
  53. lazy_cache.path = join_paths(get_cache_dir(), "lazy", "luac")
  54. end)
  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. table.insert(lvim.lazy.opts.install.colorscheme, 1, lvim.colorscheme)
  86. lazy.setup(configurations, lvim.lazy.opts)
  87. end, debug.traceback)
  88. if not status_ok then
  89. Log:warn "problems detected while loading plugins' configurations"
  90. Log:trace(debug.traceback())
  91. end
  92. end
  93. function plugin_loader.get_core_plugins()
  94. local names = {}
  95. local plugins = require "lvim.plugins"
  96. local get_name = require("lazy.core.plugin").Spec.get_name
  97. for _, spec in pairs(plugins) do
  98. if spec.enabled == true or spec.enabled == nil then
  99. table.insert(names, get_name(spec[1]))
  100. end
  101. end
  102. return names
  103. end
  104. function plugin_loader.sync_core_plugins()
  105. local core_plugins = plugin_loader.get_core_plugins()
  106. Log:trace(string.format("Syncing core plugins: [%q]", table.concat(core_plugins, ", ")))
  107. require("lazy").update { wait = true, plugins = core_plugins }
  108. end
  109. function plugin_loader.ensure_plugins()
  110. Log:debug "calling lazy.install()"
  111. require("lazy").install { wait = true }
  112. end
  113. return plugin_loader