feat(plugins): add astrocore

This commit is contained in:
Nicolas Goudry 2024-06-03 23:45:15 +02:00
parent 61d8dd64fe
commit 8a438a9449
No known key found for this signature in database
GPG key ID: 5FC434D9FFD1DF44
4 changed files with 278 additions and 0 deletions

View file

@ -0,0 +1,22 @@
{ lib, pkgs, ... }:
let
version = "1.5.0";
in
pkgs.vimUtils.buildVimPlugin {
inherit version;
name = "astrocore";
src = pkgs.fetchFromGitHub {
owner = "astronvim";
repo = "astrocore";
rev = "v${version}";
hash = "sha256-KKNglNd3S8E11CMAS6E3vhN4oZoRh0u3rjkgHiIGozI=";
};
patches = [
./init.lua.patch
./mason.lua.patch
];
}

View file

@ -0,0 +1,136 @@
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

View file

@ -0,0 +1,113 @@
diff --git a/lua/astrocore/mason.lua b/lua/astrocore/mason.lua
index a286251..7edbeeb 100644
--- a/lua/astrocore/mason.lua
+++ b/lua/astrocore/mason.lua
@@ -12,105 +12,12 @@ local M = {}
local astro = require "astrocore"
local function mason_notify(msg, type) astro.notify(msg, type, { title = "Mason" }) end
---- Update specified mason packages, or just update the registries if no packages are listed
----@param pkg_names? string|string[] The package names as defined in Mason (Not mason-lspconfig or mason-null-ls) if the value is nil then it will just update the registries
----@param auto_install? boolean whether or not to install a package that is not currently installed (default: True)
-function M.update(pkg_names, auto_install)
- pkg_names = pkg_names or {}
- if type(pkg_names) == "string" then pkg_names = { pkg_names } end
- if auto_install == nil then auto_install = true end
- local registry_avail, registry = pcall(require, "mason-registry")
- if not registry_avail then
- vim.api.nvim_err_writeln "Unable to access mason registry"
- return
- end
-
- registry.update(vim.schedule_wrap(function(success, updated_registries)
- if success then
- local count = #updated_registries
- if vim.tbl_count(pkg_names) == 0 then
- mason_notify(("Successfully updated %d %s."):format(count, count == 1 and "registry" or "registries"))
- end
- for _, pkg_name in ipairs(pkg_names) do
- local pkg_avail, pkg = pcall(registry.get_package, pkg_name)
- if not pkg_avail then
- mason_notify(("`%s` is not available"):format(pkg_name), vim.log.levels.ERROR)
- else
- if not pkg:is_installed() then
- if auto_install then
- mason_notify(("Installing `%s`"):format(pkg.name))
- pkg:install()
- else
- mason_notify(("`%s` not installed"):format(pkg.name), vim.log.levels.WARN)
- end
- else
- pkg:check_new_version(function(update_available, version)
- if update_available then
- mason_notify(("Updating `%s` to %s"):format(pkg.name, version.latest_version))
- pkg:install():on("closed", function() mason_notify(("Updated %s"):format(pkg.name)) end)
- else
- mason_notify(("No updates available for `%s`"):format(pkg.name))
- end
- end)
- end
- end
- end
- else
- mason_notify(("Failed to update registries: %s"):format(updated_registries), vim.log.levels.ERROR)
- end
- end))
+function M.update(...)
+ mason_notify "Mason is disabled with nixvim"
end
---- Update all packages in Mason
function M.update_all()
- local registry_avail, registry = pcall(require, "mason-registry")
- if not registry_avail then
- vim.api.nvim_err_writeln "Unable to access mason registry"
- return
- end
-
- mason_notify "Checking for package updates..."
- registry.update(vim.schedule_wrap(function(success, updated_registries)
- if success then
- local installed_pkgs = registry.get_installed_packages()
- local running = #installed_pkgs
- local no_pkgs = running == 0
-
- if no_pkgs then
- mason_notify "No updates available"
- astro.event "MasonUpdateCompleted"
- else
- local updated = false
- for _, pkg in ipairs(installed_pkgs) do
- pkg:check_new_version(function(update_available, version)
- if update_available then
- updated = true
- mason_notify(("Updating `%s` to %s"):format(pkg.name, version.latest_version))
- pkg:install():on("closed", function()
- running = running - 1
- if running == 0 then
- mason_notify "Update Complete"
- astro.event "MasonUpdateCompleted"
- end
- end)
- else
- running = running - 1
- if running == 0 then
- if updated then
- mason_notify "Update Complete"
- else
- mason_notify "No updates available"
- end
- astro.event "MasonUpdateCompleted"
- end
- end
- end)
- end
- end
- else
- mason_notify(("Failed to update registries: %s"):format(updated_registries), vim.log.levels.ERROR)
- end
- end))
+ mason_notify "Mason is disabled with nixvim"
end
return M