Skip to content

Commit

Permalink
feat: ✨ /terminal slash command
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Sep 18, 2024
1 parent 691a624 commit 43a9f0c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
12 changes: 5 additions & 7 deletions doc/codecompanion.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
})
<
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 11 additions & 5 deletions lua/codecompanion/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
},
},
},
Expand Down Expand Up @@ -248,7 +255,6 @@ return {
description = "Supplement the LLM with real-time info from the internet",
opts = {
api_key = nil,
hide_output = true,
},
},
opts = {
Expand Down
38 changes: 38 additions & 0 deletions lua/codecompanion/helpers/slash_commands/terminal.lua
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions lua/codecompanion/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 43a9f0c

Please sign in to comment.