Skip to content

Commit

Permalink
feat: move inline helpers to their own menu
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Feb 10, 2024
1 parent ef58059 commit 6f774fa
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 172 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ require("codecompanion").setup({
-- Default settings for the Completions API
-- See https://platform.openai.com/docs/api-reference/chat/create
advisor = {
model = "gpt-4-1106-preview",
model = "gpt-4-0125-preview",
temperature = 1,
top_p = 1,
stop = nil,
Expand All @@ -114,7 +114,7 @@ require("codecompanion").setup({
user = nil,
},
inline = {
model = "gpt-4-1106-preview",
model = "gpt-3.5-turbo-0125",
temperature = 1,
top_p = 1,
stop = nil,
Expand All @@ -125,7 +125,7 @@ require("codecompanion").setup({
user = nil,
},
chat = {
model = "gpt-4-1106-preview",
model = "gpt-4-0125-preview",
temperature = 1,
top_p = 1,
stop = nil,
Expand Down Expand Up @@ -184,7 +184,7 @@ require("codecompanion").setup({
["["] = "keymaps.previous", -- Move to the previous header in the chat
},
log_level = "ERROR", -- TRACE|DEBUG|ERROR
send_code = true, -- Send code context to the API?
send_code = true, -- Send code context to the API? Disable to prevent leaking code to OpenAI
silence_notifications = false, -- Silence notifications for actions like saving saving chats?
use_default_actions = true, -- Use the default actions in the action palette?
})
Expand Down Expand Up @@ -294,8 +294,8 @@ If `display.chat.show_settings` is set to `true`, at the very top of the chat bu
The plugin comes with a number of [in-built actions](https://github.com/olimorris/codecompanion.nvim/blob/main/lua/codecompanion/actions.lua) which aim to improve your Neovim workflow. Actions make use of strategies which are abstractions built around Neovim and OpenAI functionality. Before we dive in to the actions, it's worth explaining what each of the strategies do:

- `chat` - A strategy for opening up a chat buffer allowing the user to converse directly with OpenAI
- `inline` - A strategy for allowing OpenAI responses to be written directly into a Neovim buffer
- `advisor` - A strategy for outputting OpenAI responses into a split or a popup, alongside a Neovim buffer
- `inline` - A strategy for allowing OpenAI responses to be written inline to a Neovim buffer
- `advisor` - A strategy for providing specific advice on a selection of code via a chat buffer

#### Chat and Chat as

Expand All @@ -305,17 +305,19 @@ Both of these actions utilise the `chat` strategy. The `Chat` action opens up a
#### Open chats

This action enables users to easily navigate between their open chat buffers. A chat buffer maybe deleted (and removed from this action) by pressing `<C-q>` when in the chat buffer.
This action enables users to easily navigate between their open chat buffers. A chat buffer maybe deleted (and removed from this action) by pressing `<C-q>` from within it.

#### Inline code

This action utilises the `inline` strategy. This action can be useful for writing code into a buffer or even refactoring a visual selection; all based on a user's prompt. The action is designed to write code for the buffer filetype that it is initated in, or, if run from a terminal prompt, to write commands.
This action utilises the `inline` strategy. This action can be useful for writing inline code in a buffer or even refactoring a visual selection; all based on a user's prompt. The action is designed to write code for the buffer filetype that it is initated in, or, if run from a terminal prompt, to write commands.

The strategy comes with a number of helpers which the user can type in the prompt, similar to GitHub Copilot Chat:
The strategy comes with a number of helpers which the user can type in the prompt, similar to [GitHub Copilot Chat](https://github.blog/changelog/2024-01-30-code-faster-and-better-with-github-copilots-new-features-in-visual-studio/):

- `/doc` to add a documentation comment
- `/tests` to create unit tests for the selected code
- `/optimize` to analyze and improve the running time of the selected code
- `/tests` to create unit tests for the selected code

> **Note**: The options available to the user in the Action Palette will depend on the Vim mode.
#### Code advisor

Expand Down
31 changes: 18 additions & 13 deletions doc/codecompanion.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Click to see the default configuration ~
-- Default settings for the Completions API
-- See https://platform.openai.com/docs/api-reference/chat/create
advisor = {
model = "gpt-4-1106-preview",
model = "gpt-4-0125-preview",
temperature = 1,
top_p = 1,
stop = nil,
Expand All @@ -89,7 +89,7 @@ Click to see the default configuration ~
user = nil,
},
inline = {
model = "gpt-4-1106-preview",
model = "gpt-3.5-turbo-0125",
temperature = 1,
top_p = 1,
stop = nil,
Expand All @@ -100,7 +100,7 @@ Click to see the default configuration ~
user = nil,
},
chat = {
model = "gpt-4-1106-preview",
model = "gpt-4-0125-preview",
temperature = 1,
top_p = 1,
stop = nil,
Expand Down Expand Up @@ -159,7 +159,7 @@ Click to see the default configuration ~
["["] = "keymaps.previous", -- Move to the previous header in the chat
},
log_level = "ERROR", -- TRACE|DEBUG|ERROR
send_code = true, -- Send code context to the API?
send_code = true, -- Send code context to the API? Disable to prevent leaking code to OpenAI
silence_notifications = false, -- Silence notifications for actions like saving saving chats?
use_default_actions = true, -- Use the default actions in the action palette?
})
Expand Down Expand Up @@ -298,8 +298,8 @@ are abstractions built around Neovim and OpenAI functionality. Before we dive
in to the actions, it’s worth explaining what each of the strategies do:

- `chat` - A strategy for opening up a chat buffer allowing the user to converse directly with OpenAI
- `inline` - A strategy for allowing OpenAI responses to be written directly into a Neovim buffer
- `advisor` - A strategy for outputting OpenAI responses into a split or a popup, alongside a Neovim buffer
- `inline` - A strategy for allowing OpenAI responses to be written inline to a Neovim buffer
- `advisor` - A strategy for providing specific advice on a selection of code via a chat buffer


CHAT AND CHAT AS
Expand All @@ -317,23 +317,28 @@ OPEN CHATS

This action enables users to easily navigate between their open chat buffers. A
chat buffer maybe deleted (and removed from this action) by pressing `<C-q>`
when in the chat buffer.
from within it.


INLINE CODE

This action utilises the `inline` strategy. This action can be useful for
writing code into a buffer or even refactoring a visual selection; all based on
a user’s prompt. The action is designed to write code for the buffer filetype
that it is initated in, or, if run from a terminal prompt, to write commands.
writing inline code in a buffer or even refactoring a visual selection; all
based on a user’s prompt. The action is designed to write code for the buffer
filetype that it is initated in, or, if run from a terminal prompt, to write
commands.

The strategy comes with a number of helpers which the user can type in the
prompt, similar to GitHub Copilot Chat:
prompt, similar to GitHub Copilot Chat
<https://github.blog/changelog/2024-01-30-code-faster-and-better-with-github-copilots-new-features-in-visual-studio/>

- `/doc` to add a documentation comment
- `/tests` to create unit tests for the selected code
- `/doc`to add a documentation comment
- `/optimize` to analyze and improve the running time of the selected code
- `/tests` to create unit tests for the selected code


**Note**The options available to the user in the Action Palette will depend on
the Vim mode.

CODE ADVISOR

Expand Down
208 changes: 120 additions & 88 deletions lua/codecompanion/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -261,26 +261,127 @@ M.static.actions = {
},
},
{
name = "Inline code",
name = "Inline code ...",
strategy = "inline",
description = "Get OpenAI to write/refactor code for you",
opts = {
user_prompt = true,
send_visual_selection = true,
},
prompts = {
{
role = "system",
content = function(context)
if context.buftype == "terminal" then
return "I want you to act as an expert in writing terminal commands that will work for my current shell "
.. os.getenv("SHELL")
.. ". I will ask you specific questions and I want you to return the raw command only (no codeblocks and explanations). If you can't respond with a command, respond with nothing"
end
return "I want you to act as a senior "
.. context.filetype
.. " developer. I will ask you specific questions and I want you to return raw code only (no codeblocks and no explanations). If you can't respond with code, respond with nothing"
end,
picker = {
prompt = "Select an inline code action",
items = {
{
name = "Custom",
strategy = "inline",
description = "Custom user input",
opts = {
user_prompt = true,
send_visual_selection = true,
},
prompts = {
{
role = "system",
content = function(context)
if context.buftype == "terminal" then
return "I want you to act as an expert in writing terminal commands that will work for my current shell "
.. os.getenv("SHELL")
.. ". I will ask you specific questions and I want you to return the raw command only (no codeblocks and explanations). If you can't respond with a command, respond with nothing"
end
return "I want you to act as a senior "
.. context.filetype
.. " developer. I will ask you specific questions and I want you to return raw code only (no codeblocks and no explanations). If you can't respond with code, respond with nothing"
end,
},
},
},
{
name = "/doc",
strategy = "inline",
description = "Add a documentation comment",
opts = {
modes = { "v" },
placement = "before", -- before|after|replace|new
},
prompts = {
{
role = "system",
content = function(context)
return "You are an expert coder and helpful assistant who can help write documentation comments for the "
.. context.filetype
.. " language"
end,
},
{
role = "user",
contains_code = true,
content = function(context)
return send_code(context)
end,
},
{
role = "user",
content = "Please add a documentation comment to the provided code and reply with just the comment only and no explanation, no codeblocks and do not return the code either. If neccessary add parameter and return types",
},
},
},
{
name = "/optimize",
strategy = "inline",
description = "Optimize the selected code",
opts = {
modes = { "v" },
placement = "replace",
},
prompts = {
{
role = "system",
content = function(context)
return "You are an expert coder and helpful assistant who can help optimize code for the "
.. context.filetype
.. " language"
end,
},

{
role = "user",
contains_code = true,
content = function(context)
return send_code(context)
end,
},
{
role = "user",
content = "Please optimize the provided code. Please just respond with the code only and no explanation or markdown block syntax",
},
},
},
{
name = "/test",
strategy = "inline",
description = "Create unit tests for the selected code",
opts = {
modes = { "v" },
placement = "new",
},
prompts = {
{
role = "system",
content = function(context)
return "You are an expert coder and helpful assistant who can help write unit tests for the "
.. context.filetype
.. " language"
end,
},
{
role = "user",
contains_code = true,
content = function(context)
return send_code(context)
end,
},
{
role = "user",
content = "Please create a unit test for the provided code. Please just respond with the code only and no explanation or markdown block syntax",
},
},
},
},
},
},
Expand Down Expand Up @@ -364,7 +465,7 @@ M.static.actions = {
},
},
{
name = "Load chats",
name = "Load chats ...",
strategy = "saved_chats",
description = "Load your previous chats",
condition = function()
Expand Down Expand Up @@ -401,73 +502,4 @@ M.static.actions = {
},
}

M.static.commands = {
{
command = "/doc",
description = "Add a documentation comment",
opts = {
placement = "before", -- before|after|replace|new
send_visual_selection = true,
},
prompts = {
{
role = "system",
content = function(context)
return "You are an expert coder and helpful assistant who can help write documentation comments for the "
.. context.filetype
.. " language"
end,
},
{
role = "user",
content = "Please add a documentation comment to the provided code and reply with just the comment only and no explanation, no codeblocks and do not return the code either. If neccessary add parameter and return types",
},
},
},
{
command = "/optimize",
description = "Optimize the selected code",
opts = {
placement = "replace",
send_visual_selection = true,
},
prompts = {
{
role = "system",
content = function(context)
return "You are an expert coder and helpful assistant who can help optimize code for the "
.. context.filetype
.. " language"
end,
},
{
role = "user",
content = "Please optimize the provided code. Please just respond with the code only and no explanation or markdown block syntax",
},
},
},
{
command = "/test",
description = "Create unit tests for the selected code",
opts = {
placement = "new",
send_visual_selection = true,
},
prompts = {
{
role = "system",
content = function(context)
return "You are an expert coder and helpful assistant who can help write unit tests for the "
.. context.filetype
.. " language"
end,
},
{
role = "user",
content = "Please create a unit test for the provided code. Please just respond with the code only and no explanation or markdown block syntax",
},
},
},
}

return M
Loading

0 comments on commit 6f774fa

Please sign in to comment.