forked from sudo-tee/opencode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_window.lua
More file actions
613 lines (509 loc) · 17.2 KB
/
input_window.lua
File metadata and controls
613 lines (509 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
local state = require('opencode.state')
local config = require('opencode.config')
local M = {}
-- Track hidden state
M._hidden = false
-- Flag to prevent WinClosed autocmd from closing all windows during toggle
M._toggling = false
M._resize_scheduled = false
-- Cache namespace ID to avoid repeated creation
local placeholder_ns = vim.api.nvim_create_namespace('input_placeholder')
local function get_content_height(windows)
local line_count = vim.api.nvim_buf_line_count(windows.input_buf)
if line_count <= 0 then
return 1
end
if config.ui.input.text.wrap then
local ok, result = pcall(vim.api.nvim_win_text_height, windows.input_win, {
start_row = 0,
end_row = math.max(0, line_count - 1),
})
if ok and result and result.all then
return result.all
end
end
return line_count
end
local function get_winbar_height(windows)
local ok, winbar = pcall(vim.api.nvim_get_option_value, 'winbar', { win = windows.input_win })
if ok and type(winbar) == 'string' and winbar ~= '' then
return 1
end
return 0
end
local function calculate_height(windows)
local total_height = vim.api.nvim_get_option_value('lines', {})
local min_height = math.max(1, math.floor(total_height * config.ui.input.min_height))
local max_height = math.max(min_height, math.floor(total_height * config.ui.input.max_height))
local content_height = get_content_height(windows) + get_winbar_height(windows)
return math.min(max_height, math.max(min_height, content_height))
end
local function apply_dimensions(windows, height)
if config.ui.position == 'current' then
pcall(vim.api.nvim_win_set_height, windows.input_win, height)
return
end
local total_width = vim.api.nvim_get_option_value('columns', {})
local width_ratio = state.pre_zoom_width and config.ui.zoom_width or config.ui.window_width
local width = math.floor(total_width * width_ratio)
vim.api.nvim_win_set_config(windows.input_win, { width = width, height = height })
end
function M.create_buf()
local input_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_set_option_value('filetype', 'opencode', { buf = input_buf })
local buffixwin = require('opencode.ui.buf_fix_win')
buffixwin.fix_to_win(input_buf, function()
return state.windows and state.windows.input_win
end)
return input_buf
end
function M._build_input_win_config()
return {
relative = 'editor',
width = config.ui.window_width or 80,
height = 3,
col = 2,
style = 'minimal',
zindex = 41,
} --[[@as vim.api.keyset.win_config]]
end
function M.create_window(windows)
windows.input_win = vim.api.nvim_open_win(windows.input_buf, true, M._build_input_win_config())
end
---@return_cast windows { input_win: integer, input_buf: integer }
function M.mounted(windows)
windows = windows or state.windows
if
not windows
or not windows.input_buf
or not windows.input_win
or not vim.api.nvim_win_is_valid(windows.input_win)
then
return false
end
return true
end
function M.close()
if not M.mounted() then
return
end
---@cast state.windows { input_win: integer, input_buf: integer }
pcall(vim.api.nvim_win_close, state.windows.input_win, true)
pcall(vim.api.nvim_buf_delete, state.windows.input_buf, { force = true })
end
---Handle submit action from input window
---@return boolean true if a message was sent to the AI, false otherwise
function M.handle_submit()
local windows = state.windows
if not windows or not M.mounted(windows) then
return false
end
---@cast windows { input_buf: integer }
local input_content = table.concat(vim.api.nvim_buf_get_lines(windows.input_buf, 0, -1, false), '\n')
vim.api.nvim_buf_set_lines(windows.input_buf, 0, -1, false, {})
vim.api.nvim_exec_autocmds('TextChanged', {
buffer = windows.input_buf,
modeline = false,
})
if input_content == '' then
return false
end
if input_content:match('^!') then
M._execute_shell_command(input_content:sub(2))
return false
end
local key = config.get_key_for_function('input_window', 'slash_commands') or '/'
if input_content:match('^' .. key) then
M._execute_slash_command(input_content)
return false
end
require('opencode.core').send_message(input_content)
return true
end
M._execute_shell_command = function(command)
local cmd = command:match('^%s*(.-)%s*$')
if cmd == '' then
return
end
local shell = vim.o.shell
local shell_cmd = { shell, '-c', cmd }
vim.system(shell_cmd, { text = true }, function(result)
vim.schedule(function()
if result.code ~= 0 then
vim.notify('Command failed with exit code ' .. result.code, vim.log.levels.ERROR)
end
local output = result.stdout or ''
if result.stderr and result.stderr ~= '' then
output = output .. '\n' .. result.stderr
end
M._prompt_add_to_context(cmd, output, result.code)
end)
end)
end
M._prompt_add_to_context = function(cmd, output, exit_code)
local output_window = require('opencode.ui.output_window')
if not output_window.mounted() then
return
end
local formatted_output = string.format('$ %s\n%s', cmd, output)
local lines = vim.split(formatted_output, '\n')
output_window.set_lines(lines)
local picker = require('opencode.ui.picker')
picker.select({ 'Yes', 'No' }, {
prompt = 'Add command + output to context?',
}, function(choice)
if choice == 'Yes' then
local message = string.format('Command: `%s`\nExit code: %d\nOutput:\n```\n%s```', cmd, exit_code, output)
M._append_to_input(message)
end
output_window.clear()
require('opencode.ui.input_window').focus_input()
end)
end
M._append_to_input = function(text)
if M._hidden then
M._show()
end
if not M.mounted() then
return
end
---@cast state.windows -nil
local current_lines = vim.api.nvim_buf_get_lines(state.windows.input_buf, 0, -1, false)
local new_lines = vim.split(text, '\n')
if #current_lines == 1 and current_lines[1] == '' then
vim.api.nvim_buf_set_lines(state.windows.input_buf, 0, -1, false, new_lines)
else
vim.api.nvim_buf_set_lines(state.windows.input_buf, -1, -1, false, { '', '---', '' })
vim.api.nvim_buf_set_lines(state.windows.input_buf, -1, -1, false, new_lines)
end
M.refresh_placeholder(state.windows)
require('opencode.ui.mention').highlight_all_mentions(state.windows.input_buf)
local line_count = vim.api.nvim_buf_line_count(state.windows.input_buf)
vim.api.nvim_win_set_cursor(state.windows.input_win, { line_count, 0 })
end
M._execute_slash_command = function(command)
local slash_commands = require('opencode.api').get_slash_commands():await()
local key = config.get_key_for_function('input_window', 'slash_commands') or '/'
local cmd = command:sub(2):match('^%s*(.-)%s*$')
if cmd == '' then
return
end
local parts = vim.split(cmd, ' ')
local command_cfg = vim.tbl_filter(function(c)
return c.slash_cmd == key .. parts[1]
end, slash_commands)[1]
if command_cfg then
local args = #parts > 1 and vim.list_slice(parts, 2) or nil
command_cfg.fn(args)
else
vim.notify('Unknown command: ' .. cmd, vim.log.levels.WARN)
end
end
local function set_win_option(option, value, windows)
windows = windows or state.windows
vim.api.nvim_set_option_value(option, value, { win = windows.input_win, scope = 'local' })
end
local function set_buf_option(option, value, windows)
windows = windows or state.windows
vim.api.nvim_set_option_value(option, value, { buf = windows.input_buf })
end
function M.setup(windows)
if config.ui.input.text.wrap then
set_win_option('wrap', true, windows)
set_win_option('linebreak', true, windows)
end
set_buf_option('filetype', 'opencode', windows)
set_win_option('winhighlight', config.ui.window_highlight, windows)
set_win_option('signcolumn', 'yes', windows)
set_win_option('cursorline', false, windows)
set_win_option('number', false, windows)
set_win_option('relativenumber', false, windows)
set_buf_option('buftype', 'nofile', windows)
set_buf_option('bufhidden', 'hide', windows)
set_buf_option('buflisted', false, windows)
set_buf_option('swapfile', false, windows)
if config.ui.position ~= 'current' then
set_win_option('winfixbuf', true, windows)
end
set_win_option('winfixheight', true, windows)
set_win_option('winfixwidth', true, windows)
M.update_dimensions(windows)
M.refresh_placeholder(windows)
M.setup_keymaps(windows)
M.recover_input(windows)
require('opencode.ui.context_bar').render(windows)
end
function M.update_dimensions(windows)
if not M.mounted(windows) then
return
end
local height = calculate_height(windows)
apply_dimensions(windows, height)
end
function M.schedule_resize(windows)
windows = windows or state.windows
if not M.mounted(windows) or M._resize_scheduled then
return
end
M._resize_scheduled = true
vim.schedule(function()
M._resize_scheduled = false
if M.mounted(windows) then
M.update_dimensions(windows)
end
end)
end
function M.refresh_placeholder(windows, input_lines)
if not M.mounted(windows) then
return
end
if not input_lines then
input_lines = vim.api.nvim_buf_get_lines(windows.input_buf, 0, -1, false)
end
if #input_lines == 1 and input_lines[1] == '' then
local ns_id = placeholder_ns
local win_width = vim.api.nvim_win_get_width(windows.input_win)
local padding = string.rep(' ', win_width)
local slash_key = config.get_key_for_function('input_window', 'slash_commands')
local mention_key = config.get_key_for_function('input_window', 'mention')
local mention_file_key = config.get_key_for_function('input_window', 'mention_file')
local context_key = config.get_key_for_function('input_window', 'context_items')
local virt_text = {
{ 'Type your prompt here... ', 'OpencodeHint' },
{ '!', 'OpencodeInputLegend' },
{ ' shell ', 'OpencodeHint' },
}
if slash_key then
table.insert(virt_text, { slash_key, 'OpencodeInputLegend' })
table.insert(virt_text, { ' commands ', 'OpencodeHint' })
end
if mention_key then
table.insert(virt_text, { mention_key, 'OpencodeInputLegend' })
table.insert(virt_text, { ' mentions ', 'OpencodeHint' })
end
if mention_file_key then
table.insert(virt_text, { mention_file_key, 'OpencodeInputLegend' })
table.insert(virt_text, { ' files ', 'OpencodeHint' })
end
if context_key then
table.insert(virt_text, { context_key, 'OpencodeInputLegend' })
table.insert(virt_text, { ' context', 'OpencodeHint' })
end
table.insert(virt_text, { padding, 'OpencodeHint' })
vim.api.nvim_buf_set_extmark(windows.input_buf, ns_id, 0, 0, {
virt_text = virt_text,
virt_text_pos = 'overlay',
})
else
vim.api.nvim_buf_clear_namespace(windows.input_buf, placeholder_ns, 0, -1)
end
end
function M.clear_placeholder(windows)
if not windows or not windows.input_buf then
return
end
vim.api.nvim_buf_clear_namespace(windows.input_buf, placeholder_ns, 0, -1)
end
function M.recover_input(windows)
M.set_content(state.input_content, windows)
require('opencode.ui.mention').highlight_all_mentions(windows.input_buf)
M.update_dimensions(windows)
end
function M.focus_input()
if M._hidden then
M._show()
return
end
if not M.mounted() then
return
end
---@cast state.windows { input_win: integer, input_buf: integer }
vim.api.nvim_set_current_win(state.windows.input_win)
local lines = vim.api.nvim_buf_get_lines(state.windows.input_buf, 0, -1, false)
if #lines == 1 and lines[1] == '' then
require('opencode.ui.input_window').refresh_placeholder(state.windows)
else
require('opencode.ui.input_window').clear_placeholder(state.windows)
end
end
function M.set_content(text, windows)
windows = windows or state.windows
if not windows or not windows.input_buf then
return
end
local lines = type(text) == 'table' and text or vim.split(tostring(text), '\n')
local has_content = #lines > 1 or (lines[1] and lines[1] ~= '')
if has_content and M._hidden then
M._show()
windows = state.windows
end
if not M.mounted(windows) then
return
end
---@cast windows { input_win: integer, input_buf: integer }
vim.api.nvim_buf_set_lines(windows.input_buf, 0, -1, false, lines)
end
function M.set_current_line(text, windows)
windows = windows or state.windows
if not M.mounted(windows) then
return
end
vim.api.nvim_set_current_line(text)
end
function M.remove_mention(mention_name, windows)
windows = windows or state.windows
if not M.mounted(windows) then
return
end
---@cast windows { input_buf: integer }
local lines = vim.api.nvim_buf_get_lines(windows.input_buf, 0, -1, false)
for i, line in ipairs(lines) do
local mention_key = config.get_key_for_function('input_window', 'mention')
local pattern = vim.pesc(mention_key .. mention_name)
local updated_line = line:gsub(pattern, '')
if updated_line ~= line then
lines[i] = updated_line
end
end
vim.api.nvim_buf_set_lines(windows.input_buf, 0, -1, false, lines)
require('opencode.ui.mention').highlight_all_mentions(windows.input_buf)
end
function M.is_empty()
if not M.mounted() then
return true
end
---@cast state.windows { input_buf: integer }
local lines = vim.api.nvim_buf_get_lines(state.windows.input_buf, 0, -1, false)
return #lines == 0 or (#lines == 1 and lines[1] == '')
end
local keymaps_set_for_buf = {}
function M.setup_keymaps(windows)
if keymaps_set_for_buf[windows.input_buf] then
return
end
keymaps_set_for_buf[windows.input_buf] = true
local keymap = require('opencode.keymap')
keymap.setup_window_keymaps(config.keymap.input_window, windows.input_buf, true)
end
function M.setup_autocmds(windows, group)
vim.api.nvim_create_autocmd('WinEnter', {
group = group,
buffer = windows.input_buf,
callback = function()
M.refresh_placeholder(windows)
state.last_focused_opencode_window = 'input'
require('opencode.ui.context_bar').render()
end,
})
vim.api.nvim_create_autocmd('WinLeave', {
group = group,
buffer = windows.input_buf,
callback = function()
-- Auto-hide input window when auto_hide is enabled and focus leaves
-- Don't hide if displaying a route (slash command output like /help)
-- Don't hide if input contains content
if
config.ui.input.auto_hide
and not M.is_hidden()
and not state.display_route
and #state.input_content == 1
and state.input_content[1] == ''
then
M._hide()
end
end,
})
vim.api.nvim_create_autocmd({ 'TextChanged', 'TextChangedI' }, {
buffer = windows.input_buf,
callback = function()
local input_lines = vim.api.nvim_buf_get_lines(windows.input_buf, 0, -1, false)
state.input_content = input_lines
M.refresh_placeholder(windows, input_lines)
require('opencode.ui.context_bar').render()
M.schedule_resize(windows)
end,
})
vim.api.nvim_create_autocmd('CursorMoved', {
group = group,
buffer = windows.input_buf,
callback = function()
state.save_cursor_position('input', windows.input_win)
end,
})
end
---Toggle the input window visibility (hide/show)
---When hidden, the input window is closed entirely
---When shown, the input window is recreated
function M.toggle()
local windows = state.windows
if not windows then
return
end
if M._hidden then
M._show()
else
M._hide()
end
end
---Hide the input window by closing it
function M._hide()
local windows = state.windows
if not M.mounted(windows) then
return
end
local output_window = require('opencode.ui.output_window')
local was_at_bottom = output_window.is_at_bottom(windows.output_win)
M._hidden = true
M._toggling = true
pcall(vim.api.nvim_win_close, windows.input_win, false)
windows.input_win = nil
vim.schedule(function()
M._toggling = false
end)
output_window.focus_output(true)
if was_at_bottom then
vim.schedule(function()
require('opencode.ui.renderer').scroll_to_bottom(true)
end)
end
end
---Show the input window by recreating it
function M._show()
local windows = state.windows
if not windows or not windows.input_buf or not windows.output_win then
return
end
-- Don't recreate if already visible
if windows.input_win and vim.api.nvim_win_is_valid(windows.input_win) then
M._hidden = false
return
end
local output_window = require('opencode.ui.output_window')
local was_at_bottom = output_window.is_at_bottom(windows.output_win)
local output_win = windows.output_win
if not vim.api.nvim_win_is_valid(output_win) then
return
end
vim.api.nvim_set_current_win(output_win)
local input_position = config.ui.input_position or 'bottom'
vim.cmd((input_position == 'top' and 'aboveleft' or 'belowright') .. ' split')
local input_win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(input_win, windows.input_buf)
windows.input_win = input_win
-- Re-apply window settings
M.setup(windows)
M._hidden = false
-- Focus the input window
M.focus_input()
if was_at_bottom then
vim.schedule(function()
require('opencode.ui.renderer').scroll_to_bottom(true)
end)
end
end
---Check if the input window is currently hidden
---@return boolean
function M.is_hidden()
return M._hidden
end
return M