From a6dc729e90ed4713f2ef0ea1b8ae45a0e16c4f75 Mon Sep 17 00:00:00 2001 From: Kalle Jepsen Date: Sun, 24 Mar 2024 12:31:06 +0100 Subject: [PATCH] assistant/chatgpt: Add jackMort/ChatGPT.nvim --- configuration.nix | 1 + flake.lock | 17 ++++++++++ flake.nix | 5 +++ lib/types/plugins.nix | 1 + modules/assistant/chatgpt/chatgpt.nix | 23 ++++++++++++++ modules/assistant/chatgpt/config.nix | 46 +++++++++++++++++++++++++++ modules/assistant/chatgpt/default.nix | 6 ++++ modules/assistant/default.nix | 1 + 8 files changed, 100 insertions(+) create mode 100644 modules/assistant/chatgpt/chatgpt.nix create mode 100644 modules/assistant/chatgpt/config.nix create mode 100644 modules/assistant/chatgpt/default.nix diff --git a/configuration.nix b/configuration.nix index 1875eaae..d4a8f4e5 100644 --- a/configuration.nix +++ b/configuration.nix @@ -221,6 +221,7 @@ inputs: let enable = isMaximal; cmp.enable = isMaximal; }; + chatgpt.enable = isMaximal; }; vim.session = { diff --git a/flake.lock b/flake.lock index 44d96fad..670f9073 100644 --- a/flake.lock +++ b/flake.lock @@ -80,6 +80,22 @@ "type": "github" } }, + "chatgpt": { + "flake": false, + "locked": { + "lastModified": 1709721561, + "narHash": "sha256-vD3NEsYmPRWlxBSOxyIMIQiJXQXxx0hhsw4zIxxXB3o=", + "owner": "jackMort", + "repo": "ChatGPT.nvim", + "rev": "df53728e05129278d6ea26271ec086aa013bed90", + "type": "github" + }, + "original": { + "owner": "jackMort", + "repo": "ChatGPT.nvim", + "type": "github" + } + }, "cheatsheet-nvim": { "flake": false, "locked": { @@ -1488,6 +1504,7 @@ "catppuccin": "catppuccin", "ccc": "ccc", "cellular-automaton": "cellular-automaton", + "chatgpt": "chatgpt", "cheatsheet-nvim": "cheatsheet-nvim", "cinnamon-nvim": "cinnamon-nvim", "cmp-buffer": "cmp-buffer", diff --git a/flake.nix b/flake.nix index 67b65777..fa41c15e 100644 --- a/flake.nix +++ b/flake.nix @@ -545,6 +545,11 @@ flake = false; }; + chatgpt = { + url = "github:jackMort/ChatGPT.nvim"; + flake = false; + }; + # Session management nvim-session-manager = { url = "github:Shatur/neovim-session-manager"; diff --git a/lib/types/plugins.nix b/lib/types/plugins.nix index f20a9144..05bca28e 100644 --- a/lib/types/plugins.nix +++ b/lib/types/plugins.nix @@ -66,6 +66,7 @@ with lib; let "noice-nvim" "nui-nvim" "copilot-lua" + "chatgpt" "tabnine-nvim" "nvim-session-manager" "gesture-nvim" diff --git a/modules/assistant/chatgpt/chatgpt.nix b/modules/assistant/chatgpt/chatgpt.nix new file mode 100644 index 00000000..b7491495 --- /dev/null +++ b/modules/assistant/chatgpt/chatgpt.nix @@ -0,0 +1,23 @@ +{lib, ...}: let + inherit (lib.options) mkEnableOption; + inherit (lib) mkMappingOption; +in { + options.vim.assistant.chatgpt = { + enable = mkEnableOption "ChatGPT AI assistant. Requires the environment variable OPENAI_API_KEY to be set"; + mappings = { + chatGpt = mkMappingOption "ChatGPT" "ac"; + editWithInstructions = mkMappingOption "[ChatGPT] Edit with instructions" "ae"; + grammarCorrection = mkMappingOption "[ChatGPT] Grammar correction" "ag"; + translate = mkMappingOption "[ChatGPT] Translate" "at"; + keyword = mkMappingOption "[ChatGPT] Keywords" "ak"; + docstring = mkMappingOption "[ChatGPT] Docstring" "ad"; + addTests = mkMappingOption "[ChatGPT] Add tests" "aa"; + optimize = mkMappingOption "[ChatGPT] Optimize code" "ao"; + summarize = mkMappingOption "[ChatGPT] Summarize" "as"; + fixBugs = mkMappingOption "[ChatGPT] Fix bugs" "af"; + explain = mkMappingOption "[ChatGPT] Explain code" "ax"; + roxygenEdit = mkMappingOption "[ChatGPT] Roxygen edit" "ar"; + readabilityanalysis = mkMappingOption "[ChatGPT] Code reability analysis" "al"; + }; + }; +} diff --git a/modules/assistant/chatgpt/config.nix b/modules/assistant/chatgpt/config.nix new file mode 100644 index 00000000..49c5b08c --- /dev/null +++ b/modules/assistant/chatgpt/config.nix @@ -0,0 +1,46 @@ +{ + config, + lib, + ... +}: let + inherit (lib.modules) mkIf mkMerge; + inherit (lib.nvim.dag) entryAnywhere; + inherit (lib) addDescriptionsToMappings mkSetBinding; + + cfg = config.vim.assistant.chatgpt; + + self = import ./chatgpt.nix {inherit lib;}; + mappingDefinitions = self.options.vim.assistant.chatgpt.mappings; + mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions; + maps = mkMerge [ + (mkSetBinding mappings.editWithInstructions "ChatGPTEditWithInstruction") + (mkSetBinding mappings.grammarCorrection "ChatGPTRun grammar_correction") + (mkSetBinding mappings.translate "ChatGPTRun translate") + (mkSetBinding mappings.keyword "ChatGPTRun keywords") + (mkSetBinding mappings.docstring "ChatGPTRun docstring") + (mkSetBinding mappings.addTests "ChatGPTRun add_tests") + (mkSetBinding mappings.optimize "ChatGPTRun optimize_code") + (mkSetBinding mappings.summarize "ChatGPTRun summarize") + (mkSetBinding mappings.fixBugs "ChatGPTRun fix_bugs") + (mkSetBinding mappings.explain "ChatGPTRun explain_code") + (mkSetBinding mappings.roxygenEdit "ChatGPTRun roxygen_edit") + (mkSetBinding mappings.readabilityanalysis "ChatGPTRun code_readability_analysis") + ]; +in { + config = mkIf cfg.enable { + vim = { + startPlugins = [ + "chatgpt" + ]; + luaConfigRC.chagpt = entryAnywhere '' + require("chatgpt").setup({ + }) + ''; + maps.normal = mkMerge [ + (mkSetBinding mappings.chatGpt "ChatGPT") + maps + ]; + maps.visual = maps; + }; + }; +} diff --git a/modules/assistant/chatgpt/default.nix b/modules/assistant/chatgpt/default.nix new file mode 100644 index 00000000..0f237716 --- /dev/null +++ b/modules/assistant/chatgpt/default.nix @@ -0,0 +1,6 @@ +{ + imports = [ + ./chatgpt.nix + ./config.nix + ]; +} diff --git a/modules/assistant/default.nix b/modules/assistant/default.nix index a8096c8a..167f2313 100644 --- a/modules/assistant/default.nix +++ b/modules/assistant/default.nix @@ -1,6 +1,7 @@ _: { imports = [ ./copilot + ./chatgpt # ./tabnine.nix # removed until I find a way around the initialisation script the plugin requires ]; }