Skip to content

Commit

Permalink
refactor(inline): trying to improve inline prompting
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Dec 18, 2024
1 parent d3ba3d9 commit b7fdcbd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
21 changes: 7 additions & 14 deletions lua/codecompanion/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -629,33 +629,26 @@ Use Markdown formatting and include the programming language name at the start o
stop_context_insertion = true,
},
prompts = {
{
role = constants.SYSTEM_ROLE,
content = function(context)
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,
opts = {
visible = false,
tag = "system_tag",
},
},
{
role = constants.USER_ROLE,
content = function(context)
local buf_utils = require("codecompanion.utils.buffers")

return string.format(
return fmt(
[[Here is the content of a buffer, for context:
```%s
%s
```
NOTE: The cursor is currently on line %d, which is `%s`.
]],
context.filetype,
buf_utils.get_content(context.bufnr)
buf_utils.get_content(context.bufnr),
context.cursor_pos[1],
vim.trim(buf_utils.get_line(context.bufnr, context.cursor_pos[1]))
)
end,
opts = {
Expand Down
12 changes: 11 additions & 1 deletion lua/codecompanion/strategies/inline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ Here are some example prompts and their correct method classification ("<method>
The user may also provide a prompt which references a conversation you've had with them previously. Just focus on determining the correct method classification.
Please respond to this prompt in the format "<method>", placing the classifiction in a tag. For example "replace" would be `<replace>`, "add" would be `<add>`, "before" would be `<before>`, "new" would be `<new>` and "chat" would be `<chat>`. If you can't classify the message, reply with `<error>`. Do not provide any other content in your response or you'll break the plugin this is being called from.]],
CODE_ONLY_PROMPT = [[Respond with code only. DO NOT format the code in Markdown code blocks, DO NOT use backticks AND DO NOT provide any explanations. If you cannot do this, reply with "Error"]],
CODE_ONLY_PROMPT = [[The following response must contain ONLY raw code that can be directly written to a Neovim buffer:
1. No Markdown formatting or backticks
2. No explanations or prose
3. Use proper indentation for the target language
4. Include language-appropriate comments when needed
5. Use actual line breaks (not \n)
6. Preserve all whitespace
7. Only include relevant code (no full file echoing)
If you cannot provide clean file-ready code, reply with `<error>`]],
}

---Format given lines into a code block alongside a prompt
Expand Down
8 changes: 8 additions & 0 deletions lua/codecompanion/utils/buffers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ function M.get_content(bufnr, range)
return content
end

---Get the content of a given line in a buffer
---@param bufnr number
---@param line number
---@return string
function M.get_line(bufnr, line)
return api.nvim_buf_get_lines(bufnr, line - 1, line, false)[1] or ""
end

---Formats the content of a buffer into a markdown string
---@param buffer table The buffer data to include
---@param lines string The lines of the buffer to include
Expand Down

0 comments on commit b7fdcbd

Please sign in to comment.