Repository navigation
Expand file tree
/
Copy pathinput_window.lua
More file actions
224 lines (185 loc) · 7.51 KB
/
Copy pathinput_window.lua
File metadata and controls
224 lines (185 loc) · 7.51 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
local state = require('opencode.state')
local config = require('opencode.config').get()
local M = {}
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 })
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,
}
end
function M.create_window(windows)
windows.input_win = vim.api.nvim_open_win(windows.input_buf, true, M._build_input_win_config())
end
function M.mounted(windows)
windows = windows or state.windows
if not windows or not windows.input_buf or not windows.input_win then
return false
end
return true
end
function M.close()
if not M.mounted() then
return
end
pcall(vim.api.nvim_win_close, state.windows.input_win, true)
pcall(vim.api.nvim_buf_delete, state.windows.input_buf, { force = true })
end
function M.handle_submit()
local windows = state.windows
if not windows or not M.mounted(windows) then
return
end
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,
})
vim.api.nvim_set_current_win(windows.output_win)
local line_count = vim.api.nvim_buf_line_count(windows.output_buf)
vim.api.nvim_win_set_cursor(windows.output_win, { line_count, 0 })
require('opencode.core').send_message(input_content)
end
function M.setup(windows)
vim.api.nvim_set_option_value('winhighlight', config.ui.window_highlight, { win = windows.input_win })
vim.api.nvim_set_option_value('wrap', config.ui.input.text.wrap, { win = windows.input_win })
vim.api.nvim_set_option_value('signcolumn', 'yes', { win = windows.input_win })
vim.api.nvim_set_option_value('cursorline', false, { win = windows.input_win })
vim.api.nvim_set_option_value('number', false, { win = windows.input_win })
vim.api.nvim_set_option_value('relativenumber', false, { win = windows.input_win })
vim.api.nvim_set_option_value('buftype', 'nofile', { buf = windows.input_buf })
vim.api.nvim_set_option_value('swapfile', false, { buf = windows.input_buf })
-- vim.b[windows.input_buf].completion = false
vim.api.nvim_set_option_value('winfixbuf', true, { win = windows.input_win })
vim.api.nvim_set_option_value('winfixheight', true, { win = windows.input_win })
vim.api.nvim_set_option_value('winfixwidth', true, { win = windows.input_win })
M.update_dimensions(windows)
M.refresh_placeholder(windows)
M.setup_keymaps(windows)
M.recover_input(windows)
end
function M.update_dimensions(windows)
if not M.mounted(windows) then
return
end
local total_width = vim.api.nvim_get_option_value('columns', {})
local total_height = vim.api.nvim_get_option_value('lines', {})
local width = math.floor(total_width * config.ui.window_width)
local height = math.floor(total_height * config.ui.input_height)
vim.api.nvim_win_set_config(windows.input_win, { width = width, height = height })
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 = vim.api.nvim_create_namespace('input_placeholder')
local win_width = vim.api.nvim_win_get_width(windows.input_win)
local padding = string.rep(' ', win_width)
local keys = config.keymap.window
vim.api.nvim_buf_set_extmark(windows.input_buf, ns_id, 0, 0, {
virt_text = {
{ 'Type your prompt here... ', 'OpenCodeHint' },
{ keys.slash_commands, 'OpencodeInputLegend' },
{ ' commands ', 'OpenCodeHint' },
{ keys.mention, 'OpencodeInputLegend' },
{ ' mentions ', 'OpenCodeHint' },
{ keys.mention_file, 'OpencodeInputLegend' },
{ ' to pick files' .. padding, 'OpenCodeHint' },
},
virt_text_pos = 'overlay',
})
else
vim.api.nvim_buf_clear_namespace(windows.input_buf, vim.api.nvim_create_namespace('input_placeholder'), 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, vim.api.nvim_create_namespace('input_placeholder'), 0, -1)
end
function M.recover_input(windows)
M.set_content(state.input_content)
require('opencode.ui.mention').highlight_all_mentions(windows.input_buf)
end
function M.focus_input()
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 M.mounted(windows) then
return
end
local lines = type(text) == 'table' and text or vim.split(tostring(text), '\n')
vim.api.nvim_buf_set_lines(windows.input_buf, 0, -1, false, lines)
end
function M.is_empty()
local windows = state.windows
if not windows or not M.mounted() then
return true
end
local lines = vim.api.nvim_buf_get_lines(windows.input_buf, 0, -1, false)
return #lines == 0 or (#lines == 1 and lines[1] == '')
end
function M.setup_keymaps(windows)
local map = require('opencode.keymap').buf_keymap
local nav_history = require('opencode.ui.util').navigate_history
local nav = require('opencode.ui.navigation')
local core = require('opencode.core')
local api = require('opencode.api')
local completion = require('opencode.ui.completion')
local keymaps = config.keymap.window
local input_buf = windows.input_buf
map(keymaps.submit, M.handle_submit, input_buf, 'n')
map(keymaps.submit_insert, M.handle_submit, input_buf, 'i')
map(keymaps.mention, completion.trigger_completion(keymaps.mention), input_buf, 'i')
map(keymaps.slash_commands, completion.trigger_completion(keymaps.slash_commands), input_buf, 'i')
map(keymaps.mention_file, core.add_file_to_context, input_buf, 'i')
map(keymaps.prev_prompt_history, nav_history(keymaps.prev_prompt_history, 'prev'), input_buf, { 'n', 'i' })
map(keymaps.next_prompt_history, nav_history(keymaps.next_prompt_history, 'next'), input_buf, { 'n', 'i' })
map(keymaps.switch_mode, api.switch_to_next_mode, input_buf, { 'n', 'i' })
map(keymaps.next_message, nav.goto_next_message, input_buf, 'n')
map(keymaps.prev_message, nav.goto_prev_message, input_buf, 'n')
map(keymaps.close, api.close, input_buf, 'n')
map(keymaps.stop, api.stop, input_buf, 'n')
map(keymaps.toggle_pane, api.toggle_pane, input_buf, { 'n', 'i' })
map(keymaps.select_child_session, api.select_child_session, input_buf, 'n')
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'
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)
end,
})
end
return M