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/none-ls: refactor using mkSourcePlugin #1853

Merged
merged 3 commits into from
Jul 13, 2024
Merged
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
6 changes: 3 additions & 3 deletions flake-modules/updates/none-ls.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
pkgs,
}:
let
inherit (import ../../plugins/none-ls/packages.nix pkgs) packaged noPackage unpackaged;
inherit (import ../../plugins/none-ls/packages.nix pkgs) packaged noPackage;

builtinSources = lib.trivial.importJSON "${vimPlugins.none-ls-nvim.src}/doc/builtins.json";

Expand All @@ -14,11 +14,11 @@ let
toolNames = lib.unique (lib.flatten (lib.attrValues builtinSourceNames));

undeclaredTool = lib.filter (
name: !(lib.hasAttr name packaged || lib.elem name noPackage || lib.elem name unpackaged)
name: !(lib.hasAttr name packaged || lib.elem name noPackage)
) toolNames;

uselesslyDeclaredTool = lib.filter (name: !(lib.elem name toolNames)) (
unpackaged ++ noPackage ++ (lib.attrNames packaged)
noPackage ++ (lib.attrNames packaged)
);
in
writeText "efmls-configs-sources.nix" (
Expand Down
55 changes: 55 additions & 0 deletions plugins/none-ls/_mk-source-plugin.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# mkSourcePlugin, returns a module
sourceType: sourceName:
{
config,
pkgs,
lib,
helpers,
...
}:
let
inherit (import ./packages.nix pkgs) packaged;
pkg = packaged.${sourceName};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lazy evaluation is nice :D


cfg = config.plugins.none-ls;
cfg' = config.plugins.none-ls.sources.${sourceType}.${sourceName};
in
{
options.plugins.none-ls.sources.${sourceType}.${sourceName} =
{
enable = lib.mkEnableOption "the ${sourceName} ${sourceType} source for none-ls";
withArgs = helpers.mkNullOrOption helpers.nixvimTypes.strLua ''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be straighforward (in another PR) to have structured options here no? We'd need some kind of coercion from string to rawLua though

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. #1705 is tracking the idea.

Raw Lua code passed as an argument to the source's `with` method.
'';
}
# Only declare a package option if a package is required
// lib.optionalAttrs (packaged ? ${sourceName}) {
package = lib.mkOption (
{
type = lib.types.nullOr lib.types.package;
description =
"Package to use for ${sourceName}."
+ (lib.optionalString (pkg == null) (
"\n\n"
+ ''
Currently not packaged in nixpkgs.
Either set this to `null` and install ${sourceName} outside of nix,
or set this to a custom nix package.
''
));
}
// lib.optionalAttrs (pkg != null) { default = pkg; }
);
};

config = lib.mkIf (cfg.enable && cfg'.enable) {
plugins.none-ls.settings.sources = lib.mkDefault [
(
"require('null-ls').builtins.${sourceType}.${sourceName}"
+ lib.optionalString (cfg'.withArgs != null) ".with(${cfg'.withArgs})"
)
];

extraPackages = [ (cfg'.package or null) ];
};
}
2 changes: 1 addition & 1 deletion plugins/none-ls/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ helpers.neovim-plugin.mkNeovimPlugin config {
settingsPath = basePluginPath ++ [ "settings" ];
in
[
./servers.nix
./sources.nix
(mkRenamedOptionModule oldPluginPath basePluginPath)
(mkRenamedOptionModule (basePluginPath ++ [ "sourcesItems" ]) (settingsPath ++ [ "sources" ]))
];
Expand Down
Loading