@@ -2,6 +2,7 @@ local state = require('opencode.state')
22local config = require (' opencode.config' )
33
44local M = {}
5+ M .namespace = vim .api .nvim_create_namespace (' opencode_output' )
56
67function M .create_buf ()
78 local output_buf = vim .api .nvim_create_buf (false , true )
@@ -51,8 +52,9 @@ function M.setup(windows)
5152 M .update_dimensions (windows )
5253 M .setup_keymaps (windows )
5354 state .subscribe (' restore_points' , function (_ , new_val , old_val )
54- local outout_renderer = require (' opencode.ui.output_renderer' )
55- outout_renderer .render (state .windows , true )
55+ -- FIXME: restore points
56+ -- local outout_renderer = require('opencode.ui.output_renderer')
57+ -- outout_renderer.render(state.windows, true)
5658 end )
5759end
5860
@@ -63,21 +65,74 @@ function M.update_dimensions(windows)
6365 vim .api .nvim_win_set_config (windows .output_win , { width = width })
6466end
6567
66- function M .set_content (lines )
68+ function M .get_buf_line_count ()
69+ if not M .mounted () then
70+ return 0
71+ end
72+
73+ return vim .api .nvim_buf_line_count (state .windows .output_buf )
74+ end
75+
76+ --- Set the output buffer contents
77+ --- @param lines string[] The lines to set
78+ --- @param start_line ? integer The starting line to set , defaults to 0
79+ --- @param end_line ? integer The last line to set , defaults to -1
80+ function M .set_lines (lines , start_line , end_line )
6781 if not M .mounted () then
6882 return
6983 end
7084
85+ start_line = start_line or 0
86+ end_line = end_line or - 1
87+
7188 local windows = state .windows
7289 if not windows or not windows .output_buf then
7390 return
7491 end
92+
7593 vim .api .nvim_set_option_value (' modifiable' , true , { buf = windows .output_buf })
76- local padded = vim .tbl_extend (' force' , {}, lines )
77- vim .api .nvim_buf_set_lines (windows .output_buf , 0 , - 1 , false , padded )
94+ vim .api .nvim_buf_set_lines (windows .output_buf , start_line , end_line , false , lines )
7895 vim .api .nvim_set_option_value (' modifiable' , false , { buf = windows .output_buf })
7996end
8097
98+ --- Clear output buf extmarks
99+ --- @param start_line ? integer Line to start clearing , defaults 0
100+ --- @param end_line ? integer Line to to clear until , defaults to -1
101+ function M .clear_extmarks (start_line , end_line )
102+ if not M .mounted () or not state .windows .output_buf then
103+ return
104+ end
105+
106+ start_line = start_line or 0
107+ end_line = end_line or - 1
108+
109+ vim .api .nvim_buf_clear_namespace (state .windows .output_buf , M .namespace , start_line , end_line )
110+ end
111+
112+ --- Apply extmarks to the output buffer
113+ --- @param extmarks table<number , OutputExtmark> Extmarks indexed by line
114+ --- @param line_offset ? integer Line offset to apply to extmarks , defaults to 0
115+ function M .set_extmarks (extmarks , line_offset )
116+ if not M .mounted () or not extmarks or type (extmarks ) ~= ' table' then
117+ return
118+ end
119+
120+ line_offset = line_offset or 0
121+
122+ local output_buf = state .windows .output_buf
123+
124+ for line_idx , marks in pairs (extmarks ) do
125+ for _ , mark in ipairs (marks ) do
126+ local actual_mark = type (mark ) == ' function' and mark () or mark
127+ local target_line = line_offset + line_idx - 1
128+ if actual_mark .end_row then
129+ actual_mark .end_row = actual_mark .end_row + line_offset
130+ end
131+ pcall (vim .api .nvim_buf_set_extmark , output_buf , M .namespace , target_line , 0 , actual_mark )
132+ end
133+ end
134+ end
135+
81136function M .focus_output (should_stop_insert )
82137 if should_stop_insert then
83138 vim .cmd (' stopinsert' )
@@ -121,11 +176,8 @@ function M.setup_autocmds(windows, group)
121176end
122177
123178function M .clear ()
124- if not M .mounted () then
125- return
126- end
127- vim .api .nvim_buf_clear_namespace (state .windows .output_buf , - 1 , 0 , - 1 )
128- M .set_content ({})
179+ M .set_lines ({})
180+ M .clear_extmarks ()
129181end
130182
131183return M
0 commit comments