1
0
Fork 0
mirror of https://github.com/nix-community/nixvim.git synced 2025-11-08 19:46:06 +01:00

plugins/wtf: migrate to mkNeovimPlugin

This commit is contained in:
Gaetan Lepage 2025-10-11 18:01:06 +02:00 committed by Gaétan Lepage
parent 2ad8d87e42
commit c660702482
4 changed files with 102 additions and 172 deletions

View file

@ -52,8 +52,6 @@
keymapOptions = [
options.keymaps
options.keymapsOnEvents
options.plugins.wtf.keymaps.ai
options.plugins.wtf.keymaps.search
# NOTE: lsp `diagnostic` and `lspBuf` don't use `mapOptionSubmodule` yet
# So we only need `lua` deprecation in lsp's `extra` option
options.plugins.lsp.keymaps.extra

View file

@ -1,149 +1,21 @@
{
lib,
helpers,
config,
pkgs,
...
}:
with lib;
let
cfg = config.plugins.wtf;
{ lib, ... }:
lib.nixvim.plugins.mkNeovimPlugin {
name = "wtf";
package = "wtf-nvim";
maintainers = [ lib.maintainers.GaetanLepage ];
defaultKeymaps = {
ai = {
key = "gw";
mode = [
"n"
"x"
];
action.__raw = "require('wtf').ai";
};
# TODO: introduced 2025-10-11: remove after 26.05
inherit (import ./deprecations.nix lib) deprecateExtraOptions optionsRenamedToSettings imports;
search = {
key = "gW";
mode = "n";
action.__raw = "require('wtf').search";
};
history = {
key = "wh";
mode = "n";
action.__raw = "require('wtf').history";
};
grep_history = {
key = "wg";
mode = "n";
action.__raw = "require('wtf').grep_history";
settingsExample = {
popup_type = "popup";
providers.openai = {
api_key = lib.nixvim.nestedLiteralLua "vim.env.OPENAI_API_KEY";
model_id = "gpt-3.5-turbo";
};
context = true;
language = "english";
search_engine = "phind";
winhighlight = "Normal:Normal,FloatBorder:FloatBorder";
};
in
{
imports = [
(lib.mkRemovedOptionModule [ "plugins" "wtf" "context" ] ''
context is no longer supported, please remove it from your config
'')
];
options = {
plugins.wtf = lib.nixvim.plugins.neovim.extraOptionsOptions // {
enable = mkEnableOption "wtf.nvim";
package = lib.mkPackageOption pkgs "wtf.nvim" {
default = [
"vimPlugins"
"wtf-nvim"
];
};
keymaps = mapAttrs (
action: defaults:
helpers.mkNullOrOption' {
type =
with types;
coercedTo str (key: defaultKeymaps.${action} // { inherit key; }) (
helpers.keymaps.mkMapOptionSubmodule {
inherit defaults;
lua = true;
}
);
apply = v: if v == null then null else helpers.keymaps.removeDeprecatedMapAttrs v;
description = "Keymap for the ${action} action.";
}
) defaultKeymaps;
popupType =
helpers.defaultNullOpts.mkEnumFirstDefault
[
"popup"
"horizontal"
"vertical"
]
''
Default AI popup type.
'';
openaiApiKey = helpers.defaultNullOpts.mkStr null ''
An alternative way to set your API key.
'';
openaiModelId = helpers.defaultNullOpts.mkStr "gpt-3.5-turbo" "ChatGPT Model.";
language = helpers.defaultNullOpts.mkStr "english" ''
Set your preferred language for the response.
'';
additionalInstructions = helpers.mkNullOrOption types.str "Any additional instructions.";
searchEngine = helpers.defaultNullOpts.mkEnumFirstDefault [
"google"
"duck_duck_go"
"stack_overflow"
"github"
] "Default search engine.";
hooks = {
requestStarted = helpers.defaultNullOpts.mkLuaFn "nil" "Callback for request start.";
requestFinished = helpers.defaultNullOpts.mkLuaFn "nil" "Callback for request finished.";
};
winhighlight = helpers.defaultNullOpts.mkStr "Normal:Normal,FloatBorder:FloatBorder" ''
Add custom colours.
'';
};
};
config =
let
setupOptions =
with cfg;
{
popup_type = popupType;
openai_api_key = openaiApiKey;
providers = {
openai = {
model_id = openaiModelId;
};
};
inherit language;
additional_instructions = additionalInstructions;
search_engine = searchEngine;
hooks = {
request_started = hooks.requestStarted;
request_finished = hooks.requestFinished;
};
inherit winhighlight;
}
// cfg.extraOptions;
in
mkIf cfg.enable {
extraPlugins = [ cfg.package ];
keymaps = filter (keymap: keymap != null) (attrValues cfg.keymaps);
extraConfigLua = ''
require("wtf").setup(${lib.nixvim.toLuaObject setupOptions})
'';
};
}

View file

@ -0,0 +1,55 @@
lib: {
imports =
let
basePluginPath = [
"plugins"
"wtf"
];
in
[
(lib.mkRemovedOptionModule
(
basePluginPath
++ [
"keymaps"
]
)
''
Use nixvim's top-level `keymaps` option to manually declare your 'wtf-nvim' keymaps.
''
)
(lib.mkRemovedOptionModule (basePluginPath ++ [ "context" ]) ''
context is no longer supported, please remove it from your config.
'')
];
deprecateExtraOptions = true;
optionsRenamedToSettings = [
{
old = "openaiModelId";
new = [
"providers"
"openai"
"model_id"
];
}
{
old = "openaiApiKey";
new = [
"providers"
"openai"
"api_key"
];
}
]
++ map (lib.splitString ".") [
"popupType"
"language"
"additionalInstructions"
"searchEngine"
"hooks.requestStarted"
"hooks.requestFinished"
"winhighlight"
];
}

View file

@ -1,3 +1,4 @@
{ lib, ... }:
{
empty = {
plugins.wtf.enable = true;
@ -7,35 +8,39 @@
plugins.wtf = {
enable = true;
keymaps = {
ai.key = "gw";
search = {
mode = [
"n"
"x"
];
options.desc = "Search diagnostic with Google";
settings = {
popup_type = "popup";
providers.openai = {
api_key = lib.nixvim.mkRaw "vim.env.OPENAI_API_KEY";
model_id = "gpt-3.5-turbo";
};
language = "english";
search_engine = "phind";
winhighlight = "Normal:Normal,FloatBorder:FloatBorder";
};
popupType = "popup";
openaiApiKey = null;
openaiModelId = "gpt-3.5-turbo";
language = "english";
additionalInstructions = "Hello world !";
searchEngine = "google";
hooks = {
requestStarted = ''
function()
vim.cmd("hi StatusLine ctermbg=NONE ctermfg=yellow")
end
'';
requestFinished = ''
vim.schedule_wrap(function()
vim.cmd("hi StatusLine ctermbg=NONE ctermfg=NONE")
end)
'';
};
};
defaults = {
plugins.wtf = {
enable = true;
settings = {
additional_instructions = null;
chat_dir = lib.nixvim.mkRaw "vim.fn.stdpath('data'):gsub('/$', \"\") .. '/wtf/chats'";
language = "english";
picker = "telescope";
popup_type = "horizontal";
provider = "openai";
search_engine = "google";
# Extracting the default value would be annoying
# providers = create_provider_defaults()
hooks = {
request_started = null;
request_finished = null;
};
winhighlight = "Normal:Normal,FloatBorder:FloatBorder";
};
winhighlight = "Normal:Normal,FloatBorder:FloatBorder";
};
};
}