From b7fdcbdd2b04caa63bd9cbbf584ba448e028aa26 Mon Sep 17 00:00:00 2001 From: Oli Morris Date: Wed, 18 Dec 2024 22:53:37 +0000 Subject: [PATCH] refactor(inline): trying to improve inline prompting --- lua/codecompanion/config.lua | 21 +++++++-------------- lua/codecompanion/strategies/inline.lua | 12 +++++++++++- lua/codecompanion/utils/buffers.lua | 8 ++++++++ 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/lua/codecompanion/config.lua b/lua/codecompanion/config.lua index db3fd20a..064932ce 100644 --- a/lua/codecompanion/config.lua +++ b/lua/codecompanion/config.lua @@ -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 = { diff --git a/lua/codecompanion/strategies/inline.lua b/lua/codecompanion/strategies/inline.lua index 0bf442c5..25d8ff68 100644 --- a/lua/codecompanion/strategies/inline.lua +++ b/lua/codecompanion/strategies/inline.lua @@ -33,7 +33,17 @@ Here are some example prompts and their correct method classification (" 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 "", placing the classifiction in a tag. For example "replace" would be ``, "add" would be ``, "before" would be ``, "new" would be `` and "chat" would be ``. If you can't classify the message, reply with ``. 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 ``]], } ---Format given lines into a code block alongside a prompt diff --git a/lua/codecompanion/utils/buffers.lua b/lua/codecompanion/utils/buffers.lua index 108034f1..2925e13f 100644 --- a/lua/codecompanion/utils/buffers.lua +++ b/lua/codecompanion/utils/buffers.lua @@ -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