-
-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plugins/none-ls: refactor using
mkSourcePlugin
- Loading branch information
1 parent
abc409c
commit 4dae6fb
Showing
2 changed files
with
61 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# mkSourcePlugin, returns a module | ||
sourceType: sourceName: | ||
{ | ||
config, | ||
pkgs, | ||
lib, | ||
helpers, | ||
... | ||
}: | ||
let | ||
inherit (import ./packages.nix pkgs) packaged unpackaged; | ||
|
||
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 '' | ||
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} || lib.elem sourceName unpackaged) { | ||
package = | ||
let | ||
pkg = packaged.${sourceName} or null; | ||
in | ||
lib.mkOption ( | ||
{ | ||
type = lib.types.nullOr lib.types.package; | ||
description = | ||
"Package to use for ${sourceName} by none-ls. " | ||
+ (lib.optionalString (pkg == null) '' | ||
Not handled in nixvim, either install externally and set to null or set the option with a derivation. | ||
''); | ||
} | ||
// 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) ]; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,22 @@ | ||
{ | ||
pkgs, | ||
config, | ||
lib, | ||
helpers, | ||
... | ||
}: | ||
{ config, lib, ... }: | ||
let | ||
noneLsBuiltins = import ../../generated/none-ls.nix; | ||
|
||
inherit (import ./packages.nix pkgs) packaged unpackaged; | ||
|
||
# Does this builitin require a package ? | ||
builitinNeedsPackage = source: lib.hasAttr source packaged || lib.elem source unpackaged; | ||
mkSourcePlugin = import ./_mk-source-plugin.nix; | ||
in | ||
{ | ||
imports = [ ./prettier.nix ]; | ||
|
||
options.plugins.none-ls.sources = lib.mapAttrs ( | ||
sourceType: sources: | ||
lib.listToAttrs ( | ||
lib.map (source: { | ||
name = source; | ||
value = | ||
{ | ||
enable = lib.mkEnableOption "the ${source} ${sourceType} source for none-ls"; | ||
withArgs = helpers.mkNullOrOption helpers.nixvimTypes.strLua '' | ||
Raw Lua code passed as an argument to the source's `with` method. | ||
''; | ||
} | ||
// lib.optionalAttrs (builitinNeedsPackage source) { | ||
package = | ||
let | ||
pkg = packaged.${source} or null; | ||
in | ||
lib.mkOption ( | ||
{ | ||
type = lib.types.nullOr lib.types.package; | ||
description = | ||
"Package to use for ${source} by none-ls. " | ||
+ (lib.optionalString (pkg == null) '' | ||
Not handled in nixvim, either install externally and set to null or set the option with a derivation. | ||
''); | ||
} | ||
// lib.optionalAttrs (pkg != null) { default = pkg; } | ||
); | ||
}; | ||
}) sources | ||
) | ||
) noneLsBuiltins; | ||
imports = | ||
[ ./prettier.nix ] | ||
++ (lib.flatten ( | ||
lib.mapAttrsToList (category: (lib.map (mkSourcePlugin category))) noneLsBuiltins | ||
)); | ||
|
||
config = | ||
let | ||
cfg = config.plugins.none-ls; | ||
gitsignsEnabled = cfg.sources.code_actions.gitsigns.enable; | ||
|
||
flattenedSources = lib.flatten ( | ||
lib.mapAttrsToList ( | ||
sourceType: sources: | ||
(lib.mapAttrsToList (sourceName: source: source // { inherit sourceType sourceName; }) sources) | ||
) cfg.sources | ||
); | ||
|
||
enabledSources = builtins.filter (source: source.enable) flattenedSources; | ||
in | ||
lib.mkIf cfg.enable { | ||
plugins.none-ls.settings.sources = lib.mkIf (enabledSources != [ ]) ( | ||
map ( | ||
{ | ||
sourceType, | ||
sourceName, | ||
withArgs, | ||
... | ||
}: | ||
"require('null-ls').builtins.${sourceType}.${sourceName}" | ||
+ lib.optionalString (withArgs != null) ".with(${withArgs})" | ||
) enabledSources | ||
); | ||
# Enable gitsigns if the gitsigns source is enabled | ||
plugins.gitsigns.enable = lib.mkIf gitsignsEnabled true; | ||
extraPackages = map (source: source.package or null) enabledSources; | ||
}; | ||
} |