Skip to content

Commit ce0fa88

Browse files
authored
fix(ui): show input when adding context (#227)
1 parent 6668627 commit ce0fa88

2 files changed

Lines changed: 86 additions & 4 deletions

File tree

lua/opencode/ui/input_window.lua

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ M._prompt_add_to_context = function(cmd, output, exit_code)
187187
end
188188

189189
M._append_to_input = function(text)
190+
if M._hidden then
191+
M._show()
192+
end
193+
190194
if not M.mounted() then
191195
return
192196
end
@@ -378,12 +382,22 @@ end
378382

379383
function M.set_content(text, windows)
380384
windows = windows or state.windows
381-
if not M.mounted(windows) then
385+
if not windows or not windows.input_buf then
382386
return
383387
end
384-
---@cast windows { input_win: integer, input_buf: integer }
385388

386389
local lines = type(text) == 'table' and text or vim.split(tostring(text), '\n')
390+
local has_content = #lines > 1 or (lines[1] and lines[1] ~= '')
391+
392+
if has_content and M._hidden then
393+
M._show()
394+
windows = state.windows
395+
end
396+
397+
if not M.mounted(windows) then
398+
return
399+
end
400+
---@cast windows { input_win: integer, input_buf: integer }
387401

388402
vim.api.nvim_buf_set_lines(windows.input_buf, 0, -1, false, lines)
389403
end

tests/unit/input_window_spec.lua

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ describe('input_window', function()
8484
output_lines = lines
8585
end
8686

87-
output_window.clear = function()
88-
end
87+
output_window.clear = function() end
8988

9089
vim.system = function(cmd, opts, callback)
9190
vim.schedule(function()
@@ -358,4 +357,73 @@ describe('input_window', function()
358357
state.windows = nil
359358
end)
360359
end)
360+
361+
describe('auto-show when hidden', function()
362+
local input_buf, output_buf, input_win, output_win
363+
364+
before_each(function()
365+
input_buf = vim.api.nvim_create_buf(false, true)
366+
output_buf = vim.api.nvim_create_buf(false, true)
367+
input_win = vim.api.nvim_open_win(input_buf, true, {
368+
relative = 'editor',
369+
width = 80,
370+
height = 10,
371+
row = 0,
372+
col = 0,
373+
})
374+
output_win = vim.api.nvim_open_win(output_buf, false, {
375+
relative = 'editor',
376+
width = 80,
377+
height = 10,
378+
row = 11,
379+
col = 0,
380+
})
381+
382+
state.windows = {
383+
input_buf = input_buf,
384+
input_win = input_win,
385+
output_buf = output_buf,
386+
output_win = output_win,
387+
}
388+
end)
389+
390+
after_each(function()
391+
pcall(vim.api.nvim_win_close, input_win, true)
392+
pcall(vim.api.nvim_win_close, output_win, true)
393+
pcall(vim.api.nvim_buf_delete, input_buf, { force = true })
394+
pcall(vim.api.nvim_buf_delete, output_buf, { force = true })
395+
state.windows = nil
396+
input_window._hidden = false
397+
end)
398+
399+
it('should auto-show when set_content is called with non-empty content while hidden', function()
400+
input_window._hidden = true
401+
pcall(vim.api.nvim_win_close, input_win, true)
402+
state.windows.input_win = nil
403+
404+
input_window.set_content('test content')
405+
406+
assert.is_false(input_window._hidden)
407+
end)
408+
409+
it('should not auto-show when set_content is called with empty content while hidden', function()
410+
input_window._hidden = true
411+
pcall(vim.api.nvim_win_close, input_win, true)
412+
state.windows.input_win = nil
413+
414+
input_window.set_content('')
415+
416+
assert.is_true(input_window._hidden)
417+
end)
418+
419+
it('should auto-show when _append_to_input is called while hidden', function()
420+
input_window._hidden = true
421+
pcall(vim.api.nvim_win_close, input_win, true)
422+
state.windows.input_win = nil
423+
424+
input_window._append_to_input('appended content')
425+
426+
assert.is_false(input_window._hidden)
427+
end)
428+
end)
361429
end)

0 commit comments

Comments
 (0)