forked from sudo-tee/opencode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput_window.lua
More file actions
231 lines (196 loc) · 6.92 KB
/
output_window.lua
File metadata and controls
231 lines (196 loc) · 6.92 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
local state = require('opencode.state')
local config = require('opencode.config')
local Timer = require('opencode.ui.timer')
local topbar = require('opencode.ui.topbar')
local M = {}
M.namespace = vim.api.nvim_create_namespace('opencode_output')
function M.create_buf()
local output_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_set_option_value('filetype', 'opencode_output', { buf = output_buf })
return output_buf
end
function M._build_output_win_config()
return {
relative = 'editor',
width = config.ui.window_width or 80,
row = 2,
col = 2,
style = 'minimal',
border = 'rounded',
zindex = 40,
}
end
function M.mounted(windows)
windows = windows or state.windows
if
not state.windows
or not state.windows.output_buf
or not state.windows.output_win
or not vim.api.nvim_win_is_valid(windows.output_win)
then
return false
end
return true
end
function M.setup(windows)
vim.api.nvim_set_option_value('winhighlight', config.ui.window_highlight, { win = windows.output_win })
vim.api.nvim_set_option_value('wrap', true, { win = windows.output_win })
vim.api.nvim_set_option_value('number', false, { win = windows.output_win })
vim.api.nvim_set_option_value('relativenumber', false, { win = windows.output_win })
vim.api.nvim_set_option_value('modifiable', false, { buf = windows.output_buf })
vim.api.nvim_set_option_value('buftype', 'nofile', { buf = windows.output_buf })
vim.api.nvim_set_option_value('swapfile', false, { buf = windows.output_buf })
vim.api.nvim_set_option_value('winfixbuf', true, { win = windows.output_win })
vim.api.nvim_set_option_value('winfixheight', true, { win = windows.output_win })
vim.api.nvim_set_option_value('winfixwidth', true, { win = windows.output_win })
vim.api.nvim_set_option_value('signcolumn', 'yes', { scope = 'local', win = windows.output_win })
vim.api.nvim_set_option_value('list', false, { scope = 'local', win = windows.output_win })
M.update_dimensions(windows)
M.setup_keymaps(windows)
end
function M.update_dimensions(windows)
local total_width = vim.api.nvim_get_option_value('columns', {})
local width = math.floor(total_width * config.ui.window_width)
vim.api.nvim_win_set_config(windows.output_win, { width = width })
end
function M.get_buf_line_count()
if not M.mounted() then
return 0
end
return vim.api.nvim_buf_line_count(state.windows.output_buf)
end
---Set the output buffer contents
---@param lines string[] The lines to set
---@param start_line? integer The starting line to set, defaults to 0
---@param end_line? integer The last line to set, defaults to -1
function M.set_lines(lines, start_line, end_line)
if not M.mounted() then
return
end
start_line = start_line or 0
end_line = end_line or -1
local windows = state.windows
if not windows or not windows.output_buf then
return
end
vim.api.nvim_set_option_value('modifiable', true, { buf = windows.output_buf })
vim.api.nvim_buf_set_lines(windows.output_buf, start_line, end_line, false, lines)
vim.api.nvim_set_option_value('modifiable', false, { buf = windows.output_buf })
end
---Clear output buf extmarks
---@param start_line? integer Line to start clearing, defaults 0
---@param end_line? integer Line to clear until, defaults to -1
---@param clear_all? boolean If true, clears all extmarks in the buffer
function M.clear_extmarks(start_line, end_line, clear_all)
if not M.mounted() or not state.windows.output_buf then
return
end
start_line = start_line or 0
end_line = end_line or -1
vim.api.nvim_buf_clear_namespace(state.windows.output_buf, clear_all and -1 or M.namespace, start_line, end_line)
end
---Apply extmarks to the output buffer
---@param extmarks table<number, OutputExtmark> Extmarks indexed by line
---@param line_offset? integer Line offset to apply to extmarks, defaults to 0
function M.set_extmarks(extmarks, line_offset)
if not M.mounted() or not extmarks or type(extmarks) ~= 'table' then
return
end
line_offset = line_offset or 0
local output_buf = state.windows.output_buf
for line_idx, marks in pairs(extmarks) do
for _, mark in ipairs(marks) do
local actual_mark = type(mark) == 'function' and mark() or mark
local target_line = line_offset + line_idx
if actual_mark.end_row then
actual_mark.end_row = actual_mark.end_row + line_offset
end
local start_col = actual_mark.start_col
if actual_mark.start_col then
actual_mark.start_col = nil
end
pcall(vim.api.nvim_buf_set_extmark, output_buf, M.namespace, target_line, start_col or 0, actual_mark)
end
end
end
function M.focus_output(should_stop_insert)
if should_stop_insert then
vim.cmd('stopinsert')
end
vim.api.nvim_set_current_win(state.windows.output_win)
end
function M.close()
if M.mounted() then
return
end
pcall(vim.api.nvim_win_close, state.windows.output_win, true)
pcall(vim.api.nvim_buf_delete, state.windows.output_buf, { force = true })
end
function M.setup_keymaps(windows)
local keymap = require('opencode.keymap')
keymap.setup_window_keymaps(config.keymap.output_window, windows.output_buf)
end
function M.setup_autocmds(windows, group)
local guard_timer = nil
local function start_guard_timer()
if guard_timer then
return -- Timer already running
end
guard_timer = Timer.new({
interval = 1000, -- 1 second
repeat_timer = true,
on_tick = function()
if state.windows and vim.api.nvim_win_is_valid(state.windows.output_win) then
topbar.render()
return true
end
return false
end,
})
guard_timer:start_and_tick()
end
local function stop_guard_timer()
if guard_timer then
guard_timer:stop()
guard_timer = nil
end
end
-- Start the timer when the window is created (visible) and tick immediately
start_guard_timer()
vim.api.nvim_create_autocmd('WinEnter', {
group = group,
buffer = windows.output_buf,
callback = function()
vim.cmd('stopinsert')
state.last_focused_opencode_window = 'output'
require('opencode.ui.input_window').refresh_placeholder(state.windows)
end,
})
vim.api.nvim_create_autocmd('BufEnter', {
group = group,
buffer = windows.output_buf,
callback = function()
vim.cmd('stopinsert')
state.last_focused_opencode_window = 'output'
require('opencode.ui.input_window').refresh_placeholder(state.windows)
end,
})
-- Stop the timer when the window is closed
vim.api.nvim_create_autocmd('WinClosed', {
group = group,
pattern = tostring(windows.output_win),
callback = function()
stop_guard_timer()
end,
})
state.subscribe('current_permission', function()
require('opencode.keymap').toggle_permission_keymap(windows.output_buf)
end)
end
function M.clear()
M.set_lines({})
-- clear extmarks in all namespaces as I've seen RenderMarkdown leave some
-- extmarks behind
M.clear_extmarks(0, -1, true)
end
return M