-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathoutput_window.lua
More file actions
235 lines (197 loc) · 7.49 KB
/
output_window.lua
File metadata and controls
235 lines (197 loc) · 7.49 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
local state = require('opencode.state')
local config = require('opencode.config')
local M = {}
M.namespace = vim.api.nvim_create_namespace('opencode_output')
M.viewport_at_bottom = true
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
return windows and windows.output_buf and windows.output_win and vim.api.nvim_win_is_valid(windows.output_win)
end
---Check if the output window is currently at the bottom
---@param win? integer Window ID, defaults to state.windows.output_win
---@return boolean true if at bottom, false otherwise
function M.is_at_bottom(win)
if config.ui.output.always_scroll_to_bottom then
return true
end
win = win or (state.windows and state.windows.output_win)
if not win or not vim.api.nvim_win_is_valid(win) then
return true
end
if not state.windows or not state.windows.output_buf then
return true
end
local ok, line_count = pcall(vim.api.nvim_buf_line_count, state.windows.output_buf)
if not ok or not line_count or line_count == 0 then
return true
end
local botline = vim.fn.line('w$', win)
-- Consider at bottom if bottom visible line is at or near the end
-- Use -1 tolerance for wrapped lines
return botline >= line_count - 1
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 })
if config.ui.position ~= 'current' then
vim.api.nvim_set_option_value('winfixbuf', true, { win = windows.output_win })
end
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 })
vim.api.nvim_set_option_value('statuscolumn', '', { win = windows.output_win })
M.update_dimensions(windows)
M.setup_keymaps(windows)
end
function M.update_dimensions(windows)
if config.ui.position == 'current' then
return
end
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
---@cast state.windows { output_buf: integer }
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
---@cast state.windows { output_buf: integer }
start_line = start_line or 0
end_line = end_line or -1
vim.api.nvim_set_option_value('modifiable', true, { buf = state.windows.output_buf })
vim.api.nvim_buf_set_lines(state.windows.output_buf, start_line, end_line, false, lines)
vim.api.nvim_set_option_value('modifiable', false, { buf = state.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() then
return
end
---@cast state.windows { output_buf: integer }
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
---@cast state.windows { output_buf: integer }
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 --[[@as integer]]
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
---@cast actual_mark vim.api.keyset.set_extmark
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 not M.mounted() then
return
end
---@cast state.windows { output_win: integer }
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
---@cast state.windows { output_win: integer, output_buf: integer }
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)
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,
})
state.subscribe('current_permission', function()
require('opencode.keymap').toggle_permission_keymap(windows.output_buf)
end)
-- Track scroll position when window is scrolled
vim.api.nvim_create_autocmd('WinScrolled', {
group = group,
buffer = windows.output_buf,
callback = function()
M.viewport_at_bottom = M.is_at_bottom(windows.output_win)
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)
M.viewport_at_bottom = true
end
return M