Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugins/lazy.nvim: Import Statements & Adding user-specified packages to lazyPath #1904

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 66 additions & 38 deletions plugins/pluginmanagers/lazy.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ let
processPlugin =
plugin:
let
hasNonNullAttr = str: attrs: (builtins.hasAttr str attrs) && (attrs."${str}" != null);
hasNonNullName = plugin: hasNonNullAttr "name" plugin;
hasNonNullPkg = plugin: hasNonNullAttr "pkg" plugin;
hasNonNullPackages = plugin: hasNonNullAttr "packages" plugin;
hasNonNullDependencies = plugin: hasNonNullAttr "dependencies" plugin;

mkEntryFromDrv =
p:
if lib.isDerivation p then
Expand All @@ -22,16 +28,27 @@ let
}
else
{
name = "${lib.getName p.pkg}";
path = p.pkg;
name =
if hasNonNullName p then
p.name
else if hasNonNullPkg p then
"${lib.getName p.pkg}"
else
"${lib.getName p.path}";
path = if p ? path then p.path else p.pkg;
};
processDependencies =
if plugin ? dependencies && plugin.dependencies != null then
builtins.concatMap processPlugin plugin.dependencies
processPackages =
if hasNonNullPackages plugin then
if lib.isList plugin.packages then
builtins.concatMap processPlugin plugin.packages
else
builtins.concatMap processPlugin [ plugin.packages ]
else
[ ];
processDependencies =
if hasNonNullDependencies plugin then builtins.concatMap processPlugin plugin.dependencies else [ ];
in
[ (mkEntryFromDrv plugin) ] ++ processDependencies;
[ (mkEntryFromDrv plugin) ] ++ processPackages ++ processDependencies;

processedPlugins = builtins.concatLists (builtins.map processPlugin lazyPlugins);
lazyPath = pkgs.linkFarm "lazy-plugins" processedPlugins;
Expand All @@ -55,6 +72,8 @@ in

name = helpers.mkNullOrOption str "Name of the plugin to install";

import = helpers.mkNullOrOption (helpers.nixvimTypes.eitherRecursive str (listOf str)) "Name of additional plugin module/modules to import";

dev = helpers.defaultNullOpts.mkBool false ''
When true, a local plugin directory will be used instead.
See config.dev
Expand All @@ -76,6 +95,8 @@ in
or firenvim for example. (accepts fun(LazyPlugin):boolean)
'';

packages = helpers.mkNullOrOption (helpers.nixvimTypes.eitherRecursive types.package listOfPackages) "Additional packages to be made available in the `lazy-plugins` path";

dependencies = helpers.mkNullOrOption (helpers.nixvimTypes.eitherRecursive str listOfPlugins) "Plugin dependencies";

init = helpers.mkNullOrLuaFn "init functions are always executed during startup";
Expand Down Expand Up @@ -140,6 +161,7 @@ in
};
});

listOfPackages = types.listOf (helpers.nixvimTypes.eitherRecursive types.package types.attrs);
listOfPlugins = types.listOf pluginType;
in
mkOption {
Expand All @@ -160,42 +182,48 @@ in
plugin:
let
keyExists = keyToCheck: attrSet: lib.elem keyToCheck (lib.attrNames attrSet);
pluginImportToLua = pluginImport: { import = pluginImport; };
in
if isDerivation plugin then
{ dir = "${lazyPath}/${lib.getName plugin}"; }
else
{
"__unkeyed" = plugin.name;

inherit (plugin)
cmd
cond
config
dev
enabled
event
ft
init
keys
lazy
main
module
name
optional
opts
priority
submodules
;

dependencies = helpers.ifNonNull' plugin.dependencies (
if isList plugin.dependencies then (pluginListToLua plugin.dependencies) else plugin.dependencies
);

dir =
if plugin ? dir && plugin.dir != null then plugin.dir else "${lazyPath}/${lib.getName plugin.pkg}";
};

pluginListToLua = map pluginToLua;
[
{
"__unkeyed" = plugin.name;

inherit (plugin)
cmd
cond
config
dev
enabled
event
ft
init
keys
lazy
main
module
name
optional
opts
priority
submodules
;

import = helpers.ifNonNull' plugin.import (if isList plugin.import then null else plugin.import);

dependencies = helpers.ifNonNull' plugin.dependencies (
if isList plugin.dependencies then (pluginListToLua plugin.dependencies) else plugin.dependencies
);

dir =
if plugin ? dir && plugin.dir != null then plugin.dir else "${lazyPath}/${lib.getName plugin.pkg}";
}
]
++ (if isList plugin.import then (map pluginImportToLua plugin.import) else [ ]);

pluginListToLua = pluginList: flatten (map pluginToLua pluginList);

plugins = pluginListToLua cfg.plugins;

Expand Down
54 changes: 54 additions & 0 deletions tests/test-sources/plugins/pluginmanagers/lazy.nix
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,60 @@
cmd = "ALEEnable";
}

# Plugins can import a plugin module
{
pkg = LazyVim;
import = "lazyvim.plugins";
}

# Plugins can import several plugin modules
{
pkg = LazyVim;
import = [
"lazyvim.plugins"
"lazyvim.plugins.extras.coding.copilot"
];
}

# Plugins can add an additional package to the lazy path
{
pkg = LazyVim;
packages = catppuccin-nvim;
}

# Plugins can add several additional packages to the lazy path
{
pkg = LazyVim;
packages = [
bufferline-nvim
catppuccin-nvim
cmp-buffer
];
}

# Plugins can add an additional package to the lazy path, and import a
# plugin module
{
pkg = LazyVim;
packages = catppuccin-nvim;
import = "lazyvim.plugins";
}

# Plugins can add several additional packages to the lazy path, and
# import several plugin modules
{
pkg = LazyVim;
packages = [
bufferline-nvim
catppuccin-nvim
cmp-buffer
];
import = [
"lazyvim.plugins"
"lazyvim.plugins.extras.coding.copilot"
];
}

# Plugins can have dependencies on other plugins
{
pkg = completion-nvim;
Expand Down