Skip to content

Commit

Permalink
plugins/telescope: refactor mkExtension
Browse files Browse the repository at this point in the history
Allow importing `mkExtension` without needing to supply any
dependencies.

All dependencies can be accessed via module args anyway.
  • Loading branch information
MattSturgeon committed Nov 19, 2024
1 parent c674f10 commit 929bb0c
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 136 deletions.
83 changes: 1 addition & 82 deletions plugins/by-name/telescope/extensions/_helpers.nix
Original file line number Diff line number Diff line change
@@ -1,85 +1,4 @@
{
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)
]
);
};

lib: rec {
# FIXME: don't manually put Default in the description
# TODO: Comply with #603
mkModeMappingsOption =
Expand Down
85 changes: 85 additions & 0 deletions plugins/by-name/telescope/extensions/_mk-extension.nix
Original file line number Diff line number Diff line change
@@ -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
];
}
14 changes: 5 additions & 9 deletions plugins/by-name/telescope/extensions/file-browser.nix
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
{
lib,
config,
pkgs,
...
}:
{ lib, ... }:
with lib;
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;
in
telescopeHelpers.mkExtension {
mkExtension {
name = "file-browser";
extensionName = "file_browser";
package = "telescope-file-browser-nvim";
Expand Down Expand Up @@ -183,7 +179,7 @@ telescopeHelpers.mkExtension {
Show the current relative path from cwd as the prompt prefix.
'';

mappings = telescopeHelpers.mkMappingsOption {
mappings = mkMappingsOption {
insertDefaults = ''
{
"<A-c>" = "require('telescope._extensions.file_browser.actions').create";
Expand Down
10 changes: 3 additions & 7 deletions plugins/by-name/telescope/extensions/frecency.nix
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
{
lib,
config,
pkgs,
...
}:
{ lib, ... }:
with lib;
let
inherit (lib.nixvim) defaultNullOpts mkNullOrOption mkNullOrStr;
mkExtension = import ./_mk-extension.nix;
in
(import ./_helpers.nix { inherit lib config pkgs; }).mkExtension {
mkExtension {
name = "frecency";
package = "telescope-frecency-nvim";

Expand Down
10 changes: 3 additions & 7 deletions plugins/by-name/telescope/extensions/fzf-native.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
{
lib,
config,
pkgs,
...
}:
{ lib, ... }:
let
inherit (lib.nixvim) defaultNullOpts;
mkExtension = import ./_mk-extension.nix;
in
(import ./_helpers.nix { inherit lib config pkgs; }).mkExtension {
mkExtension {
name = "fzf-native";
extensionName = "fzf";
package = "telescope-fzf-native-nvim";
Expand Down
10 changes: 3 additions & 7 deletions plugins/by-name/telescope/extensions/fzy-native.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
{
lib,
config,
pkgs,
...
}:
{ lib, ... }:
let
inherit (lib.nixvim) defaultNullOpts;
mkExtension = import ./_mk-extension.nix;
in
(import ./_helpers.nix { inherit lib config pkgs; }).mkExtension {
mkExtension {
name = "fzy-native";
extensionName = "fzy_native";
package = "telescope-fzy-native-nvim";
Expand Down
5 changes: 2 additions & 3 deletions plugins/by-name/telescope/extensions/live-greps-args.nix
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{
lib,
config,
pkgs,
...
}:
let
inherit (lib.nixvim) defaultNullOpts;
inherit (lib) types;
telescopeHelpers = import ./_helpers.nix { inherit lib config pkgs; };
mkExtension = import ./_mk-extension.nix;
in
telescopeHelpers.mkExtension {
mkExtension {
name = "live-grep-args";
extensionName = "live_grep_args";
package = "telescope-live-grep-args-nvim";
Expand Down
5 changes: 2 additions & 3 deletions plugins/by-name/telescope/extensions/manix.nix
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{
lib,
config,
pkgs,
...
}:
let
inherit (lib.nixvim) defaultNullOpts;

telescopeHelpers = import ./_helpers.nix { inherit lib config pkgs; };
mkExtension = import ./_mk-extension.nix;
in
telescopeHelpers.mkExtension {
mkExtension {
name = "manix";
package = "telescope-manix";

Expand Down
4 changes: 2 additions & 2 deletions plugins/by-name/telescope/extensions/media-files.nix
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
lib,
config,
pkgs,
...
}:
with lib;
let
inherit (lib.nixvim) defaultNullOpts;
mkExtension = import ./_mk-extension.nix;
in
(import ./_helpers.nix { inherit lib config pkgs; }).mkExtension {
mkExtension {
name = "media-files";
extensionName = "media_files";
package = "telescope-media-files-nvim";
Expand Down
11 changes: 4 additions & 7 deletions plugins/by-name/telescope/extensions/ui-select.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{
lib,
config,
pkgs,
...
}:
(import ./_helpers.nix { inherit lib config pkgs; }).mkExtension {
let
mkExtension = import ./_mk-extension.nix;
in
mkExtension {
name = "ui-select";
package = "telescope-ui-select-nvim";

Expand Down
14 changes: 5 additions & 9 deletions plugins/by-name/telescope/extensions/undo.nix
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
{
lib,
config,
pkgs,
...
}:
{ lib, ... }:
with lib;
let
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
telescopeHelpers.mkExtension {
mkExtension {
name = "undo";
package = "telescope-undo-nvim";

Expand Down Expand Up @@ -84,7 +80,7 @@ telescopeHelpers.mkExtension {
Can be set to a [Lua date format string](https://www.lua.org/pil/22.1.html).
'';

mappings = telescopeHelpers.mkMappingsOption {
mappings = mkMappingsOption {
insertDefaults = ''
{
"<cr>" = "require('telescope-undo.actions').yank_additions";
Expand Down

0 comments on commit 929bb0c

Please sign in to comment.