plugins/telescope: refactor mkExtension

Allow importing `mkExtension` without needing to supply any
dependencies.

All dependencies can be accessed via module args anyway.
This commit is contained in:
Matt Sturgeon 2024-11-19 17:01:19 +00:00
parent c674f10d18
commit 929bb0cd1c
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
11 changed files with 115 additions and 136 deletions

View file

@ -1,85 +1,4 @@
{ lib: rec {
lib,
config,
pkgs,
...
}:
let
inherit (lib.nixvim) mkSettingsOption toSnakeCase;
inherit (lib) mkPackageOption;
in
rec {
mkExtension =
{
name,
package,
extensionName ? name,
settingsOptions ? { },
settingsExample ? null,
extraOptions ? { },
imports ? [ ],
optionsRenamedToSettings ? [ ],
extraConfig ? cfg: { },
}:
{
# TODO remove this once all deprecation warnings will have been removed.
imports =
let
basePluginPath = [
"plugins"
"telescope"
"extensions"
name
];
settingsPath = basePluginPath ++ [ "settings" ];
in
imports
++ (map (
option:
let
optionPath = lib.toList option;
optionPathSnakeCase = map toSnakeCase optionPath;
in
lib.mkRenamedOptionModule (basePluginPath ++ optionPath) (settingsPath ++ optionPathSnakeCase)
) optionsRenamedToSettings);
options.plugins.telescope.extensions.${name} = {
enable = lib.mkEnableOption "the `${name}` telescope extension";
package = mkPackageOption pkgs name {
default = [
"vimPlugins"
package
];
};
settings = mkSettingsOption {
description = "settings for the `${name}` telescope extension.";
options = settingsOptions;
example = settingsExample;
};
} // extraOptions;
config =
let
cfg = config.plugins.telescope.extensions.${name};
in
lib.mkIf cfg.enable (
lib.mkMerge [
{
extraPlugins = [ cfg.package ];
plugins.telescope = {
enabledExtensions = [ extensionName ];
settings.extensions.${extensionName} = cfg.settings;
};
}
(extraConfig cfg)
]
);
};
# FIXME: don't manually put Default in the description # FIXME: don't manually put Default in the description
# TODO: Comply with #603 # TODO: Comply with #603
mkModeMappingsOption = mkModeMappingsOption =

View file

@ -0,0 +1,85 @@
{
name,
package,
extensionName ? name,
settingsOptions ? { },
settingsExample ? null,
extraOptions ? { },
imports ? [ ],
optionsRenamedToSettings ? [ ],
extraConfig ? cfg: { },
}:
let
getPluginAttr = attrs: attrs.plugins.telescope.extensions.${name};
renameModule =
{ lib, ... }:
{
# TODO remove this once all deprecation warnings will have been removed.
imports =
let
basePluginPath = [
"plugins"
"telescope"
"extensions"
name
];
settingsPath = basePluginPath ++ [ "settings" ];
in
builtins.map (
option:
let
optionPath = lib.toList option;
optionPathSnakeCase = map lib.nixvim.toSnakeCase optionPath;
in
lib.mkRenamedOptionModule (basePluginPath ++ optionPath) (settingsPath ++ optionPathSnakeCase)
) optionsRenamedToSettings;
};
module =
{
lib,
config,
pkgs,
...
}:
let
cfg = getPluginAttr config;
in
{
options.plugins.telescope.extensions.${name} = {
enable = lib.mkEnableOption "the `${name}` telescope extension";
package = lib.mkPackageOption pkgs name {
default = [
"vimPlugins"
package
];
};
settings = lib.nixvim.mkSettingsOption {
description = "settings for the `${name}` telescope extension.";
options = settingsOptions;
example = settingsExample;
};
} // extraOptions;
config = lib.mkIf cfg.enable {
extraPlugins = [ cfg.package ];
plugins.telescope = {
enabledExtensions = [ extensionName ];
settings.extensions.${extensionName} = cfg.settings;
};
};
};
extraConfigModule = { config, ... }: extraConfig (getPluginAttr config);
in
{
imports = imports ++ [
module
extraConfigModule
renameModule
];
}

View file

@ -1,15 +1,11 @@
{ { lib, ... }:
lib,
config,
pkgs,
...
}:
with lib; with lib;
let let
telescopeHelpers = import ./_helpers.nix { inherit lib config pkgs; }; inherit (import ./_helpers.nix lib) mkMappingsOption;
mkExtension = import ./_mk-extension.nix;
inherit (lib.nixvim) defaultNullOpts mkNullOrStr; inherit (lib.nixvim) defaultNullOpts mkNullOrStr;
in in
telescopeHelpers.mkExtension { mkExtension {
name = "file-browser"; name = "file-browser";
extensionName = "file_browser"; extensionName = "file_browser";
package = "telescope-file-browser-nvim"; package = "telescope-file-browser-nvim";
@ -183,7 +179,7 @@ telescopeHelpers.mkExtension {
Show the current relative path from cwd as the prompt prefix. Show the current relative path from cwd as the prompt prefix.
''; '';
mappings = telescopeHelpers.mkMappingsOption { mappings = mkMappingsOption {
insertDefaults = '' insertDefaults = ''
{ {
"<A-c>" = "require('telescope._extensions.file_browser.actions').create"; "<A-c>" = "require('telescope._extensions.file_browser.actions').create";

View file

@ -1,14 +1,10 @@
{ { lib, ... }:
lib,
config,
pkgs,
...
}:
with lib; with lib;
let let
inherit (lib.nixvim) defaultNullOpts mkNullOrOption mkNullOrStr; inherit (lib.nixvim) defaultNullOpts mkNullOrOption mkNullOrStr;
mkExtension = import ./_mk-extension.nix;
in in
(import ./_helpers.nix { inherit lib config pkgs; }).mkExtension { mkExtension {
name = "frecency"; name = "frecency";
package = "telescope-frecency-nvim"; package = "telescope-frecency-nvim";

View file

@ -1,13 +1,9 @@
{ { lib, ... }:
lib,
config,
pkgs,
...
}:
let let
inherit (lib.nixvim) defaultNullOpts; inherit (lib.nixvim) defaultNullOpts;
mkExtension = import ./_mk-extension.nix;
in in
(import ./_helpers.nix { inherit lib config pkgs; }).mkExtension { mkExtension {
name = "fzf-native"; name = "fzf-native";
extensionName = "fzf"; extensionName = "fzf";
package = "telescope-fzf-native-nvim"; package = "telescope-fzf-native-nvim";

View file

@ -1,13 +1,9 @@
{ { lib, ... }:
lib,
config,
pkgs,
...
}:
let let
inherit (lib.nixvim) defaultNullOpts; inherit (lib.nixvim) defaultNullOpts;
mkExtension = import ./_mk-extension.nix;
in in
(import ./_helpers.nix { inherit lib config pkgs; }).mkExtension { mkExtension {
name = "fzy-native"; name = "fzy-native";
extensionName = "fzy_native"; extensionName = "fzy_native";
package = "telescope-fzy-native-nvim"; package = "telescope-fzy-native-nvim";

View file

@ -1,15 +1,14 @@
{ {
lib, lib,
config,
pkgs, pkgs,
... ...
}: }:
let let
inherit (lib.nixvim) defaultNullOpts; inherit (lib.nixvim) defaultNullOpts;
inherit (lib) types; inherit (lib) types;
telescopeHelpers = import ./_helpers.nix { inherit lib config pkgs; }; mkExtension = import ./_mk-extension.nix;
in in
telescopeHelpers.mkExtension { mkExtension {
name = "live-grep-args"; name = "live-grep-args";
extensionName = "live_grep_args"; extensionName = "live_grep_args";
package = "telescope-live-grep-args-nvim"; package = "telescope-live-grep-args-nvim";

View file

@ -1,15 +1,14 @@
{ {
lib, lib,
config,
pkgs, pkgs,
... ...
}: }:
let let
inherit (lib.nixvim) defaultNullOpts; inherit (lib.nixvim) defaultNullOpts;
telescopeHelpers = import ./_helpers.nix { inherit lib config pkgs; }; mkExtension = import ./_mk-extension.nix;
in in
telescopeHelpers.mkExtension { mkExtension {
name = "manix"; name = "manix";
package = "telescope-manix"; package = "telescope-manix";

View file

@ -1,14 +1,14 @@
{ {
lib, lib,
config,
pkgs, pkgs,
... ...
}: }:
with lib; with lib;
let let
inherit (lib.nixvim) defaultNullOpts; inherit (lib.nixvim) defaultNullOpts;
mkExtension = import ./_mk-extension.nix;
in in
(import ./_helpers.nix { inherit lib config pkgs; }).mkExtension { mkExtension {
name = "media-files"; name = "media-files";
extensionName = "media_files"; extensionName = "media_files";
package = "telescope-media-files-nvim"; package = "telescope-media-files-nvim";

View file

@ -1,10 +1,7 @@
{ let
lib, mkExtension = import ./_mk-extension.nix;
config, in
pkgs, mkExtension {
...
}:
(import ./_helpers.nix { inherit lib config pkgs; }).mkExtension {
name = "ui-select"; name = "ui-select";
package = "telescope-ui-select-nvim"; package = "telescope-ui-select-nvim";

View file

@ -1,15 +1,11 @@
{ { lib, ... }:
lib,
config,
pkgs,
...
}:
with lib; with lib;
let let
inherit (lib.nixvim) defaultNullOpts mkNullOrOption; inherit (lib.nixvim) defaultNullOpts mkNullOrOption;
telescopeHelpers = import ./_helpers.nix { inherit lib config pkgs; }; inherit (import ./_helpers.nix lib) mkMappingsOption;
mkExtension = import ./_mk-extension.nix;
in in
telescopeHelpers.mkExtension { mkExtension {
name = "undo"; name = "undo";
package = "telescope-undo-nvim"; package = "telescope-undo-nvim";
@ -84,7 +80,7 @@ telescopeHelpers.mkExtension {
Can be set to a [Lua date format string](https://www.lua.org/pil/22.1.html). Can be set to a [Lua date format string](https://www.lua.org/pil/22.1.html).
''; '';
mappings = telescopeHelpers.mkMappingsOption { mappings = mkMappingsOption {
insertDefaults = '' insertDefaults = ''
{ {
"<cr>" = "require('telescope-undo.actions').yank_additions"; "<cr>" = "require('telescope-undo.actions').yank_additions";