From 43a9f0c4b7aa9139528bf778d120855dd74da51f Mon Sep 17 00:00:00 2001 From: Oli Morris Date: Wed, 18 Sep 2024 20:58:23 +0100 Subject: [PATCH] feat: :sparkles: `/terminal` slash command --- README.md | 3 +- doc/codecompanion.txt | 12 +++--- lua/codecompanion/config.lua | 16 +++++--- .../helpers/slash_commands/terminal.lua | 38 +++++++++++++++++++ lua/codecompanion/init.lua | 12 ++++++ 5 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 lua/codecompanion/helpers/slash_commands/terminal.lua diff --git a/README.md b/README.md index 8c614e0..b40756e 100644 --- a/README.md +++ b/README.md @@ -142,9 +142,10 @@ _Slash commands_, accessed via `/`, run commands to insert additional context in - `/buffer` - Insert open buffers - `/file` - Insert a file +- `/help` - Insert content from help tags - `/now` - Insert the current date and time - `/symbols` - Insert symbols for the active buffer -- `/help` - Insert content from help tags +- `/terminal` - Insert terminal output _Tools_, accessed via `@`, allow the LLM to function as an agent and carry out actions: diff --git a/doc/codecompanion.txt b/doc/codecompanion.txt index bf0db25..528ab58 100644 --- a/doc/codecompanion.txt +++ b/doc/codecompanion.txt @@ -43,11 +43,8 @@ Install the plugin with your preferred package manager: "nvim-lua/plenary.nvim", "nvim-treesitter/nvim-treesitter", "hrsh7th/nvim-cmp", -- Optional: For using slash commands and variables in the chat buffer - { - "stevearc/dressing.nvim", -- Optional: Improves the default Neovim UI - opts = {}, - }, "nvim-telescope/telescope.nvim", -- Optional: For using slash commands + { "stevearc/dressing.nvim", opts = {} }, -- Optional: Improves the default Neovim UI }, config = true } @@ -65,8 +62,8 @@ Install the plugin with your preferred package manager: "nvim-lua/plenary.nvim", "nvim-treesitter/nvim-treesitter", "hrsh7th/nvim-cmp", -- Optional: For using slash commands and variables in the chat buffer - "stevearc/dressing.nvim" -- Optional: Improves the default Neovim UI "nvim-telescope/telescope.nvim", -- Optional: For using slash commands + "stevearc/dressing.nvim" -- Optional: Improves the default Neovim UI } }) < @@ -79,8 +76,8 @@ Install the plugin with your preferred package manager: Plug 'nvim-lua/plenary.nvim' Plug 'nvim-treesitter/nvim-treesitter' Plug 'hrsh7th/nvim-cmp', " Optional: For using slash commands and variables in the chat buffer - Plug 'stevearc/dressing.nvim' " Optional: Improves the default Neovim UI Plug 'nvim-telescope/telescope.nvim', " Optional: For using slash commands + Plug 'stevearc/dressing.nvim' " Optional: Improves the default Neovim UI Plug 'olimorris/codecompanion.nvim' call plug#end() @@ -123,9 +120,10 @@ into the chat buffer: - `/buffer` - Insert open buffers - `/file` - Insert a file +- `/help` - Insert content from help tags - `/now` - Insert the current date and time - `/symbols` - Insert symbols for the active buffer -- `/help` - Insert content from help tags +- `/terminal` - Insert terminal output _Tools_, accessed via `@`, allow the LLM to function as an agent and carry out actions: diff --git a/lua/codecompanion/config.lua b/lua/codecompanion/config.lua index 7554498..0ff3936 100644 --- a/lua/codecompanion/config.lua +++ b/lua/codecompanion/config.lua @@ -60,6 +60,13 @@ return { provider = "telescope", -- telescope|mini_pick|fzf_lua }, }, + ["help"] = { + callback = "helpers.slash_commands.help", + description = "Insert content from help tags", + opts = { + contains_code = false, + }, + }, ["now"] = { callback = "helpers.slash_commands.now", description = "Insert the current date and time", @@ -74,11 +81,11 @@ return { contains_code = true, }, }, - ["help"] = { - callback = "helpers.slash_commands.help", - description = "Insert content from help tags", + ["terminal"] = { + callback = "helpers.slash_commands.terminal", + description = "Insert terminal output", opts = { - contains_code = true, + contains_code = false, }, }, }, @@ -248,7 +255,6 @@ return { description = "Supplement the LLM with real-time info from the internet", opts = { api_key = nil, - hide_output = true, }, }, opts = { diff --git a/lua/codecompanion/helpers/slash_commands/terminal.lua b/lua/codecompanion/helpers/slash_commands/terminal.lua new file mode 100644 index 0000000..84f5ecc --- /dev/null +++ b/lua/codecompanion/helpers/slash_commands/terminal.lua @@ -0,0 +1,38 @@ +CONSTANTS = { + NAME = "Terminal Output", +} + +---@class CodeCompanion.SlashCommandTerminal +local SlashCommandTerminal = {} + +---@class CodeCompanion.SlashCommandTerminal +---@field Chat CodeCompanion.Chat The chat buffer +---@field config table The config of the slash command +---@field context table The context of the chat buffer from the completion menu +function SlashCommandTerminal.new(args) + local self = setmetatable({ + Chat = args.Chat, + config = args.config, + context = args.context, + }, { __index = SlashCommandTerminal }) + + return self +end + +---Execute the slash command +---@return nil +function SlashCommandTerminal:execute() + local terminal_buf = _G.codecompanion_last_terminal + if not terminal_buf then + return + end + local content = vim.api.nvim_buf_get_lines(terminal_buf, 0, -1, false) + + local Chat = self.Chat + + Chat:append_to_buf({ content = "[!" .. CONSTANTS.NAME .. "]\n" }) + Chat:append_to_buf({ content = "```\n" .. table.concat(content, "\n") .. "\n```\n" }) + Chat:fold_code() +end + +return SlashCommandTerminal diff --git a/lua/codecompanion/init.lua b/lua/codecompanion/init.lua index ae15f17..c24a1b9 100644 --- a/lua/codecompanion/init.lua +++ b/lua/codecompanion/init.lua @@ -335,6 +335,18 @@ M.setup = function(opts) }) end + -- Capture the last terminal buffer + _G.codecompanion_last_terminal = nil + api.nvim_create_autocmd("TermEnter", { + desc = "Capture the last terminal buffer", + callback = function() + local bufnr = api.nvim_get_current_buf() + if vim.bo[bufnr].buftype == "terminal" then + _G.codecompanion_last_terminal = bufnr + end + end, + }) + vim.treesitter.language.register("markdown", "codecompanion") end