Skip to content

Commit 05e3c6f

Browse files
committed
fix: prevent echoed prompts from resurfacing
Restore output viewport without forcing synchronous redraw
1 parent 494e474 commit 05e3c6f

5 files changed

Lines changed: 75 additions & 3 deletions

File tree

‎lua/opencode/state/optimistic.lua‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,19 @@ end
6969
---@return OpencodeOptimisticPrompt[]
7070
local function visible(observation)
7171
local result = {}
72+
local remaining = {}
7273
local entries = observation:read().entries_by_id
7374
for _, prompt in ipairs(prompts[observation] or {}) do
7475
if not (prompt.server_id and entries[prompt.server_id]) then
7576
result[#result + 1] = prompt
77+
remaining[#remaining + 1] = prompt
7678
end
7779
end
80+
if #remaining > 0 then
81+
prompts[observation] = remaining
82+
else
83+
prompts[observation] = nil
84+
end
7885
return result
7986
end
8087

‎lua/opencode/ui/output_window.lua‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,14 @@ end
319319
---@param topline integer
320320
function M.restore_view_topline(win, topline)
321321
vim.api.nvim_win_call(win, function()
322+
local cursor = vim.api.nvim_win_get_cursor(0)
322323
local view = vim.fn.winsaveview()
323324
view.topline = math.max(1, topline)
324325
view.skipcol = 0
325326
vim.fn.winrestview(view)
326-
-- Apply the restored viewport before later line('w$') reads inspect it.
327-
vim.cmd('redraw')
327+
-- Reapplying cursor through the API resolves wrapped-line skipcol without
328+
-- synchronously redrawing while Tree-sitter may still hold stale ranges.
329+
vim.api.nvim_win_set_cursor(0, cursor)
328330
end)
329331
end
330332

‎tests/unit/optimistic_spec.lua‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ describe('optimistic prompt state', function()
2929
assert.is_false(optimistic.is_sending(observation))
3030
end)
3131

32+
it('does not resurface a prompt after its server echo leaves observed history', function()
33+
local prompt = optimistic.begin(observation, 'hello')
34+
local echo = { id = 'server-id', kind = 'user', content = {} }
35+
optimistic.bind(prompt, echo.id)
36+
observation:read().entries_by_id[echo.id] = echo
37+
assert.same({ echo }, optimistic.project(observation, { echo }))
38+
39+
observation:read().entries_by_id[echo.id] = nil
40+
assert.same({}, optimistic.project(observation, {}))
41+
assert.is_false(optimistic.is_sending(observation))
42+
optimistic.finish(observation, prompt)
43+
end)
44+
3245
it('does not match another client or repeated prompt by text', function()
3346
local first = optimistic.begin(observation, 'same')
3447
local second = optimistic.begin(observation, 'same')

‎tests/unit/output_window_spec.lua‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,47 @@ describe('output_window.setup', function()
360360
end)
361361
end)
362362

363+
describe('output_window.restore_view_topline', function()
364+
local buf
365+
local win
366+
local command_stub
367+
368+
before_each(function()
369+
buf = vim.api.nvim_create_buf(false, true)
370+
local lines = {}
371+
for i = 1, 30 do
372+
lines[i] = tostring(i)
373+
end
374+
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
375+
win = vim.api.nvim_open_win(buf, true, {
376+
relative = 'editor',
377+
width = 80,
378+
height = 10,
379+
row = 0,
380+
col = 0,
381+
})
382+
end)
383+
384+
after_each(function()
385+
if command_stub then
386+
command_stub:revert()
387+
command_stub = nil
388+
end
389+
pcall(vim.api.nvim_win_close, win, true)
390+
pcall(vim.api.nvim_buf_delete, buf, { force = true })
391+
end)
392+
393+
it('restores the viewport without forcing a synchronous redraw', function()
394+
vim.api.nvim_win_set_cursor(win, { 8, 0 })
395+
command_stub = stub(vim, 'cmd')
396+
397+
output_window.restore_view_topline(win, 4)
398+
399+
assert.equals(4, output_window.get_visible_top_line(win))
400+
assert.stub(command_stub).was_not_called()
401+
end)
402+
end)
403+
363404
describe('output_window extmarks', function()
364405
local buf
365406

‎tests/unit/renderer_reconciliation_spec.lua‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ describe('renderer incremental reconciliation', function()
119119
end
120120
end)
121121

122-
it('renders a local prompt before server events and replaces it with the authoritative entry', function()
122+
it('keeps an echoed prompt suppressed after observed history is replaced', function()
123123
writes:revert()
124124
writes = spy.on(output_window, 'set_lines')
125125
local pending = optimistic.begin(observation, 'instant optimistic text')
@@ -145,6 +145,15 @@ describe('renderer incremental reconciliation', function()
145145
assert.is_not_nil(contexts.current().render_state:get_message('msg_echo'))
146146
local _, count = output():gsub('instant optimistic text', '')
147147
assert.equals(1, count)
148+
149+
observed.entries_by_id.msg_echo = nil
150+
observed.entry_order = { 'msg_one', 'msg_two' }
151+
notify('messages')
152+
assert.is_nil(contexts.current().render_state:get_message(pending.id))
153+
assert.is_nil(contexts.current().render_state:get_message('msg_echo'))
154+
local _, resurfaced = output():gsub('instant optimistic text', '')
155+
assert.equals(0, resurfaced)
156+
148157
optimistic.finish(observation, pending)
149158
end)
150159

0 commit comments

Comments
 (0)