Skip to content

Commit 22443d9

Browse files
committed
fix(quick_chat): improve code extraction and raw replace instructions
- Refactor regex for code fence and inline code removal - Update raw code insertion instructions for clarity and consistency - Fix display label length calculation in file completion
1 parent 9020c78 commit 22443d9

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

lua/opencode/quick_chat.lua

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
149150
end
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
}
294297
end

lua/opencode/ui/completion/files.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ local function create_file_item(file, suffix, priority)
9191
local file_config = config.ui.completion.file_sources
9292
local max_display_len = file_config.max_display_length or 50
9393
if #display_label > max_display_len then
94-
display_label = '...' .. display_label:sub(-(max_display_len - 3))
94+
display_label = '...' .. display_label:sub(-(math.floor(max_display_len) - 3))
9595
end
9696
local kind = vim.endswith(file, '/') and 'folder' or 'file'
9797
return {

0 commit comments

Comments
 (0)