From 309e5644fc72ce82b31da5290638019f60a0a84b Mon Sep 17 00:00:00 2001 From: Gaetan Lepage Date: Fri, 15 Dec 2023 14:00:55 +0100 Subject: [PATCH] plugins/wtf: init --- plugins/default.nix | 1 + plugins/lsp/wtf.nix | 124 +++++++++++++++++++++++++ tests/test-sources/plugins/lsp/wtf.nix | 39 ++++++++ 3 files changed, 164 insertions(+) create mode 100644 plugins/lsp/wtf.nix create mode 100644 tests/test-sources/plugins/lsp/wtf.nix diff --git a/plugins/default.nix b/plugins/default.nix index db32aae77..559b6dbb7 100644 --- a/plugins/default.nix +++ b/plugins/default.nix @@ -77,6 +77,7 @@ ./lsp/lsp-lines.nix ./lsp/nvim-lightbulb.nix ./lsp/trouble.nix + ./lsp/wtf.nix ./none-ls diff --git a/plugins/lsp/wtf.nix b/plugins/lsp/wtf.nix new file mode 100644 index 000000000..b9f23c6ed --- /dev/null +++ b/plugins/lsp/wtf.nix @@ -0,0 +1,124 @@ +{ + lib, + helpers, + config, + pkgs, + ... +}: +with lib; let + cfg = config.plugins.wtf; + + defaultKeymaps = { + ai = { + key = "gw"; + mode = ["n" "x"]; + action = "require('wtf').ai"; + lua = true; + }; + + search = { + key = "gW"; + mode = "n"; + action = "require('wtf').search"; + lua = true; + }; + }; +in { + options = { + plugins.wtf = + helpers.extraOptionsOptions + // { + enable = mkEnableOption "wtf.nvim"; + + package = helpers.mkPackageOption "wtf.nvim" pkgs.vimPlugins.wtf-nvim; + + keymaps = + mapAttrs + ( + action: defaults: + helpers.mkNullOrOption + ( + with types; + either + str + (helpers.keymaps.mkMapOptionSubmodule defaults) + ) + "Keymap for the ${action} action." + ) + defaultKeymaps; + + popupType = helpers.defaultNullOpts.mkEnum ["popup" "horizontal" "vertical"] "popup" '' + Default AI popup type. + ''; + + openaiApiKey = helpers.mkNullOrOption (with types; either str helpers.rawType) '' + An alternative way to set your API key. + ''; + + openaiModelId = helpers.defaultNullOpts.mkStr "gpt-3.5-turbo" "ChatGPT Model."; + + context = helpers.defaultNullOpts.mkBool true "Send code as well as diagnostics."; + + language = helpers.defaultNullOpts.mkStr "english" '' + Set your preferred language for the response. + ''; + + additionalInstructions = helpers.mkNullOrOption types.str "Any additional instructions."; + + searchEngine = + helpers.defaultNullOpts.mkEnum + ["google" "duck_duck_go" "stack_overflow" "github"] + "google" + "Default search engine."; + + hooks = { + requestStarted = helpers.mkNullOrOption types.str "Callback for request start."; + + requestFinished = helpers.mkNullOrOption types.str "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; + openai_model_id = openaiModelId; + inherit + context + language + ; + additional_instructions = additionalInstructions; + search_engine = searchEngine; + hooks = { + request_started = helpers.mkRaw hooks.requestStarted; + request_finished = helpers.mkRaw hooks.requestFinished; + }; + inherit winhighlight; + } + // cfg.extraOptions; + in + mkIf cfg.enable { + extraPlugins = [cfg.package]; + + keymaps = filter (keymap: keymap != null) ( + mapAttrsToList + ( + action: value: + if isString value + then defaultKeymaps.${action} // {key = value;} + else value + ) + cfg.keymaps + ); + + extraConfigLua = '' + require("wtf").setup(${helpers.toLuaObject setupOptions}) + ''; + }; +} diff --git a/tests/test-sources/plugins/lsp/wtf.nix b/tests/test-sources/plugins/lsp/wtf.nix new file mode 100644 index 000000000..34ac1639d --- /dev/null +++ b/tests/test-sources/plugins/lsp/wtf.nix @@ -0,0 +1,39 @@ +{ + empty = { + plugins.wtf.enable = true; + }; + + example = { + plugins.wtf = { + enable = true; + + keymaps = { + ai = "gw"; + search = { + mode = ["n" "x"]; + options.desc = "Search diagnostic with Google"; + }; + }; + popupType = "popup"; + openaiApiKey = null; + openaiModelId = "gpt-3.5-turbo"; + context = true; + 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) + ''; + }; + winhighlight = "Normal:Normal,FloatBorder:FloatBorder"; + }; + }; +}