Skip to content

Commit cf6f517

Browse files
committed
fix: improve sticky prompt handling and section detection
This commit improves handling of sticky prompts by: 1. Using proper line splitting and startswith check instead of gmatch 2. Fixing the sticky prefix removal to only match start of line 3. Ensuring proper rendering before section/block detection The changes make sticky prompts more reliable and fix edge cases in the UI rendering. Closes CopilotC-Nvim#614
1 parent c1c7630 commit cf6f517

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

lua/CopilotChat/init.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,15 @@ local function finish(start_of_chat)
287287

288288
state.chat:append(M.config.question_header .. M.config.separator .. '\n\n')
289289

290+
-- Reinsert sticky prompts from last prompt
290291
if state.last_prompt then
291292
local has_sticky = false
292-
for sticky_line in state.last_prompt:gmatch('(>%s+[^\n]+)') do
293-
state.chat:append(sticky_line .. '\n')
294-
has_sticky = true
293+
local lines = vim.split(state.last_prompt, '\n')
294+
for _, line in ipairs(lines) do
295+
if vim.startswith(line, '> ') then
296+
state.chat:append(line .. '\n')
297+
has_sticky = true
298+
end
295299
end
296300
if has_sticky then
297301
state.chat:append('\n')
@@ -666,7 +670,7 @@ function M.ask(prompt, config)
666670
-- Remove sticky prefix
667671
prompt = vim.trim(table.concat(
668672
vim.tbl_map(function(l)
669-
return l:gsub('>%s+', '')
673+
return l:gsub('^>%s+', '')
670674
end, vim.split(resolved_prompt, '\n')),
671675
'\n'
672676
))

lua/CopilotChat/ui/chat.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ function Chat:get_closest_section()
242242
return nil
243243
end
244244

245+
self:render()
245246
local cursor_pos = vim.api.nvim_win_get_cursor(self.winnr)
246247
local cursor_line = cursor_pos[1]
247248
local closest_section = nil
@@ -279,6 +280,7 @@ function Chat:get_closest_block()
279280
return nil
280281
end
281282

283+
self:render()
282284
local cursor_pos = vim.api.nvim_win_get_cursor(self.winnr)
283285
local cursor_line = cursor_pos[1]
284286
local closest_block = nil
@@ -318,7 +320,6 @@ function Chat:clear_prompt()
318320
end
319321

320322
self:render()
321-
322323
local section = self.sections[#self.sections]
323324
if not section or section.answer then
324325
return

0 commit comments

Comments
 (0)