Custom prompt's response inserts code around selection instead of replacing it #630
-
Hi! First off, thank you for this awesome plugin - it's been super helpful in my workflow! I'm trying to set up a custom prompt to refactor code, but I'm running into a small issue. When I use the prompt with Here's my current code for creating the local M = {}
local function create_context(context)
local buf_utils = require("codecompanion.utils.buffers")
return string.format(
[[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),
context.cursor_pos[1],
vim.trim(buf_utils.get_line(context.bufnr, context.cursor_pos[1]))
)
end
local function create_visual_selection(context)
local selection = require("codecompanion.helpers.actions").get_code(context.start_line, context.end_line)
return string.format(
[[```%s
%s
```
]],
context.filetype,
selection
)
end
M.prompts = {
["Refactor"] = {
strategy = "inline",
description = "Refactor the selected code to improve readability and reduce complexity.",
opts = {
modes = { "v" },
is_default = false,
is_slash_cmd = false,
short_name = "refactor",
auto_submit = true,
user_prompt = false,
stop_context_insertion = true,
placement = "replace|false",
},
prompts = {
{
role = "user",
content = create_context,
opts = {
contains_code = true,
visible = false,
},
},
{
role = "user",
condition = function(context)
return context.is_visual
end,
content = function(context)
return string.format(
[[Please refactor the following code to improve its readability and reduce complexity. Only return the refactored code and nothing else. Consider:
- Simplifying complex expressions
- Breaking down large functions
- Improving naming
- Applying appropriate design patterns
- Maintaining the same functionality
Here's the code to refactor:
%s]],
create_visual_selection(context)
)
end,
opts = {
contains_code = true,
visible = true,
tag = "visual",
},
},
},
},
}
return M I'm calling the prompt with a keyboard shortcut: vim.api.nvim_set_keymap("v", "<leader>ir", "", {
desc = "Refactor selected code",
callback = function()
require("codecompanion").prompt("refactor")
end,
noremap = true,
silent = true,
}) I've attached two screenshots to show what's happening: [Screenshot 2] - After refactoring Am I doing something wrong in my setup, or could this be a bug? Would really appreciate any pointers! Using Neovim 0.10.3 and codecompanion 11.8.6 Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
If you change: placement = "replace|false", to: placement = "replace", That should work. I'll be updating the documentation in the coming days to better reflect this. |
Beta Was this translation helpful? Give feedback.
-
Cool, this seems to work! Thank you. I'm looking forward to the new documentation. Keep up the great work! For context, I got the incorrect syntax from this document. There are also some broken links (such as |
Beta Was this translation helpful? Give feedback.
If you change:
to:
That should work. I'll be updating the documentation in the coming days to better reflect this.