diff --git a/lua/astrocore/init.lua b/lua/astrocore/init.lua index b18c5de..91847b4 100644 --- a/lua/astrocore/init.lua +++ b/lua/astrocore/init.lua @@ -27,22 +27,6 @@ function M.extend_tbl(default, opts) return default and vim.tbl_deep_extend("force", default, opts) or opts end ---- Sync Lazy and then update Mason -function M.update_packages() - require("lazy").sync { wait = true } - require("astrocore.mason").update_all() -end - ---- Partially reload AstroNvim user settings. Includes core vim options, mappings, and highlights. This is an experimental feature and may lead to instabilities until restart. -function M.reload() - local lazy, was_modifiable = require "lazy", vim.opt.modifiable:get() - if not was_modifiable then vim.opt.modifiable = true end - lazy.reload { plugins = { M.get_plugin "astrocore" } } - if not was_modifiable then vim.opt.modifiable = false end - if M.is_available "astroui" then lazy.reload { plugins = { M.get_plugin "astroui" } } end - vim.cmd.doautocmd "ColorScheme" -end - --- Insert one or more values into a list like table and maintain that you do not insert non-unique values (THIS MODIFIES `dst`) ---@param dst any[]|nil The list like table that you want to insert into ---@param src any[] Values to be inserted @@ -176,71 +160,52 @@ function M.toggle_term_cmd(opts) terms[opts.cmd][num]:toggle() end ---- Get a plugin spec from lazy ----@param plugin string The plugin to search for ----@return LazyPlugin? available # The found plugin spec from Lazy -function M.get_plugin(plugin) - local lazy_config_avail, lazy_config = pcall(require, "lazy.core.config") - return lazy_config_avail and lazy_config.spec.plugins[plugin] or nil -end - ---- Check if a plugin is defined in lazy. Useful with lazy loading when a plugin is not necessarily loaded yet +--- Check if a plugin is loaded ---@param plugin string The plugin to search for ---@return boolean available # Whether the plugin is available -function M.is_available(plugin) return M.get_plugin(plugin) ~= nil end - ---- Resolve the options table for a given plugin with lazy ----@param plugin string The plugin to search for ----@return table opts # The plugin options -function M.plugin_opts(plugin) - local spec = M.get_plugin(plugin) - return spec and require("lazy.core.plugin").values(spec, "opts") or {} -end - ---- A helper function to wrap a module function to require a plugin before running ----@param plugin string The plugin to call `require("lazy").load` with ----@param module table The system module where the functions live (e.g. `vim.ui`) ----@param funcs string|string[] The functions to wrap in the given module (e.g. `"ui", "select"`) -function M.load_plugin_with_func(plugin, module, funcs) - if type(funcs) == "string" then funcs = { funcs } end - for _, func in ipairs(funcs) do - local old_func = module[func] - module[func] = function(...) - module[func] = old_func - require("lazy").load { plugins = { plugin } } - module[func](...) - end - end -end +function M.is_available(plugin) return package.loaded[plugin] ~= nil end --- Execute a function when a specified plugin is loaded with Lazy.nvim, or immediately if already loaded ---@param plugins string|string[] the name of the plugin or a list of plugins to defer the function execution on. If a list is provided, only one needs to be loaded to execute the provided function ---@param load_op fun()|string|string[] the function to execute when the plugin is loaded, a plugin name to load, or a list of plugin names to load function M.on_load(plugins, load_op) - local lazy_config_avail, lazy_config = pcall(require, "lazy.core.config") - if lazy_config_avail then - if type(plugins) == "string" then plugins = { plugins } end - if type(load_op) ~= "function" then - local to_load = type(load_op) == "string" and { load_op } or load_op --[=[@as string[]]=] - load_op = function() require("lazy").load { plugins = to_load } end - end + -- local lazy_config_avail, lazy_config = pcall(require, "lazy.core.config") + -- if lazy_config_avail then + -- if type(plugins) == "string" then plugins = { plugins } end + -- if type(load_op) ~= "function" then + -- local to_load = type(load_op) == "string" and { load_op } or load_op --[=[@as string[]]=] + -- load_op = function() require("lazy").load { plugins = to_load } end + -- end + -- + -- for _, plugin in ipairs(plugins) do + -- if vim.tbl_get(lazy_config.plugins, plugin, "_", "loaded") then + -- vim.schedule(load_op) + -- return + -- end + -- end + -- vim.api.nvim_create_autocmd("User", { + -- pattern = "LazyLoad", + -- desc = ("A function to be ran when one of these plugins runs: %s"):format(vim.inspect(plugins)), + -- callback = function(args) + -- if vim.tbl_contains(plugins, args.data) then + -- load_op() + -- return true + -- end + -- end, + -- }) + -- end + + if type(plugins) == "string" then plugins = { plugins } end + if type(load_op) ~= "function" then + M.notify "astrocore.on_load cannot load plugins, they must be registered through nixvim" + load_op = function() end + end - for _, plugin in ipairs(plugins) do - if vim.tbl_get(lazy_config.plugins, plugin, "_", "loaded") then - vim.schedule(load_op) - return - end + for _, plugin in ipairs(plugins) do + if M.is_available(plugin) then + vim.schedule(load_op) + return end - vim.api.nvim_create_autocmd("User", { - pattern = "LazyLoad", - desc = ("A function to be ran when one of these plugins runs: %s"):format(vim.inspect(plugins)), - callback = function(args) - if vim.tbl_contains(plugins, args.data) then - load_op() - return true - end - end, - }) end end