Skip to content

Commit defe235

Browse files
committed
feat(renderer): config for markdown rendering
Rather than only supporting `RenderMarkdown`, make it user configurable. Also add `Markview` as another option if `RenderMarkdown` isn't present.
1 parent 6068b83 commit defe235

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ require('opencode').setup({
211211
max_files = 10,
212212
max_display_length = 50, -- Maximum length for file path display in completion, truncates from left with "..."
213213
},
214+
on_data_rendered = nil, -- Called when new data is rendered (debounced to 250ms), useful to trigger markdown rendering. Set to false to disable default behavior of checking for RenderMarkdown and Markview
214215
},
215216
},
216217
context = {

lua/opencode/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ M.defaults = {
9999
text = {
100100
wrap = false,
101101
},
102+
on_data_rendered = nil,
102103
},
103104
completion = {
104105
file_sources = {

lua/opencode/types.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
---@field output { tools: { show_output: boolean } }
9696
---@field input { text: { wrap: boolean } }
9797
---@field completion OpencodeCompletionConfig
98+
---@field on_data_rendered fun(ctx: { buf: integer, win: integer })|nil
9899

99100
---@class OpencodeContextConfig
100101
---@field enabled boolean

lua/opencode/ui/renderer.lua

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ M._subscriptions = {}
1111
M._prev_line_count = 0
1212
M._render_state = RenderState.new()
1313

14+
local trigger_on_data_rendered = require('opencode.util').debounce(function()
15+
local cb_type = type(config.ui.on_data_rendered)
16+
17+
if cb_type == 'boolean' then
18+
return
19+
end
20+
21+
if cb_type == 'function' then
22+
pcall(config.ui.on_data_rendered, state.windows.output_buf, state.windows.output_win)
23+
elseif vim.fn.exists(':RenderMarkdown') > 0 then
24+
vim.cmd(':RenderMarkdown')
25+
elseif vim.fn.exists(':Markview') then
26+
vim.cmd(':Markview render ' .. state.windows.output_buf)
27+
end
28+
end, 250)
29+
1430
---Reset renderer state
1531
function M.reset()
1632
M._prev_line_count = 0
@@ -21,6 +37,7 @@ function M.reset()
2137
state.messages = {}
2238
state.last_user_message = nil
2339
state.current_permission = nil
40+
trigger_on_data_rendered()
2441
end
2542

2643
---Set up all subscriptions, for both local and server events
@@ -145,11 +162,10 @@ function M.render_output(output_data)
145162
return
146163
end
147164

148-
-- FIXME: add actions to RenderState?
149-
150165
output_window.set_lines(output_data.lines)
151166
output_window.clear_extmarks()
152167
output_window.set_extmarks(output_data.extmarks)
168+
M._scroll_to_bottom()
153169
end
154170

155171
---Auto-scroll to bottom if user was already at bottom
@@ -170,6 +186,8 @@ function M._scroll_to_bottom()
170186

171187
local was_at_bottom = (botline >= prev_line_count) or prev_line_count == 0
172188

189+
trigger_on_data_rendered()
190+
173191
if is_focused and cursor_row < prev_line_count - 1 then
174192
return
175193
end

lua/opencode/ui/ui.lua

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ local topbar = require('opencode.ui.topbar')
1111
function M.scroll_to_bottom()
1212
local line_count = vim.api.nvim_buf_line_count(state.windows.output_buf)
1313
vim.api.nvim_win_set_cursor(state.windows.output_win, { line_count, 0 })
14-
15-
-- TODO: shouldn't have hardcoded calls to render_markdown,
16-
-- should support user callbacks
17-
vim.defer_fn(function()
18-
output_renderer.render_markdown()
19-
end, 200)
2014
end
2115

2216
---@param windows OpencodeWindowState
@@ -188,7 +182,6 @@ function M.clear_output()
188182
output_window.clear()
189183
footer.clear()
190184
topbar.render()
191-
output_renderer.render_markdown()
192185
-- state.restore_points = {}
193186
end
194187

@@ -199,9 +192,6 @@ end
199192
function M.render_lines(lines)
200193
M.clear_output()
201194
renderer.render_lines(lines)
202-
203-
-- FIXME: rehook up markdown at some point (user provided callback?)
204-
-- output_renderer.render_markdown()
205195
end
206196

207197
function M.select_session(sessions, cb)

0 commit comments

Comments
 (0)