@@ -141,11 +141,12 @@ local function extract_response_text(message)
141141 end
142142 end
143143
144- -- Remove code blocks and inline code
145- response_text = response_text :gsub (' ```[^`]*```' , ' ' )
146- response_text = response_text :gsub (' `[^`]*`' , ' ' )
144+ -- Remove code fences
145+ response_text = response_text :gsub (' ```[^\n ]*\n ?' , ' ' ) -- Remove opening code fence
146+ response_text = response_text :gsub (' \n ?```' , ' ' ) -- Remove closing code fence
147+ response_text = response_text :gsub (' `([^`\n ]*)`' , ' %1' ) -- Remove inline code backticks but keep content
147148
148- return vim . trim ( response_text )
149+ return response_text
149150end
150151
151152--- Applies raw code response to buffer (simple replacement)
@@ -163,8 +164,8 @@ local function apply_raw_code_response(buf, response_text, row, range)
163164
164165 if range then
165166 -- Replace the selected range
166- local start_line = range .start - 1 -- Convert to 0-indexed
167- local end_line = range .stop - 1 -- Convert to 0-indexed
167+ local start_line = math.floor ( range .start ) - 1 -- Convert to 0-indexed integer
168+ local end_line = math.floor ( range .stop ) - 1 -- Convert to 0-indexed integer
168169 vim .api .nvim_buf_set_lines (buf , start_line , end_line + 1 , false , lines )
169170 else
170171 -- Replace current line
@@ -277,18 +278,20 @@ local function generate_raw_code_instructions(context_config)
277278 local context_info = ' '
278279
279280 if context_config .selection and context_config .selection .enabled then
280- context_info = ' You have been provided with a code selection [SELECTED CODE]. '
281+ context_info = ' Output ONLY the code to replace the [SELECTED CODE]. '
281282 elseif context_config .cursor_data and context_config .cursor_data .enabled then
282- context_info = ' You have been provided with cursor context. [CURSOR POSITION] '
283+ context_info = ' Output ONLY the code to insert/append at the [CURRENT LINE]. '
283284 end
284285
285286 local buf = vim .api .nvim_get_current_buf ()
286- local filetype = vim .api .nvim_buf_get_option ( buf , ' filetype' )
287+ local filetype = vim .api .nvim_get_option_value ( ' filetype' , { buf = buf } )
287288
288289 return {
289290 ' I want you to act as a senior ' .. filetype .. ' developer. ' .. context_info ,
290- ' I will ask you specific questions and I want you to return raw code only ' ,
291- ' (no codeblocks, no explanations). ' ,
291+ ' I will ask you specific questions.' ,
292+ ' I want you to ALWAYS return valid raw code ONLY ' ,
293+ ' CRITICAL: NEVER add (codeblocks, explanations or any additional text). ' ,
294+ ' Respect the current indentation and formatting of the existing code. ' ,
292295 " If you can't respond with code, respond with nothing." ,
293296 }
294297end
0 commit comments