2424
2525function M .mounted (windows )
2626 windows = windows or state .windows
27- if
28- not state .windows
29- or not state .windows .output_buf
30- or not state .windows .output_win
31- or not vim .api .nvim_win_is_valid (windows .output_win )
32- then
33- return false
34- end
35-
36- return true
27+ return windows and windows .output_buf and windows .output_win and vim .api .nvim_win_is_valid (windows .output_win )
3728end
3829
3930function M .setup (windows )
@@ -66,6 +57,7 @@ function M.get_buf_line_count()
6657 if not M .mounted () then
6758 return 0
6859 end
60+ --- @cast state.windows { output_buf : integer }
6961
7062 return vim .api .nvim_buf_line_count (state .windows .output_buf )
7163end
@@ -78,28 +70,25 @@ function M.set_lines(lines, start_line, end_line)
7870 if not M .mounted () then
7971 return
8072 end
73+ --- @cast state.windows { output_buf : integer }
8174
8275 start_line = start_line or 0
8376 end_line = end_line or - 1
8477
85- local windows = state .windows
86- if not windows or not windows .output_buf then
87- return
88- end
89-
90- vim .api .nvim_set_option_value (' modifiable' , true , { buf = windows .output_buf })
91- vim .api .nvim_buf_set_lines (windows .output_buf , start_line , end_line , false , lines )
92- vim .api .nvim_set_option_value (' modifiable' , false , { buf = windows .output_buf })
78+ vim .api .nvim_set_option_value (' modifiable' , true , { buf = state .windows .output_buf })
79+ vim .api .nvim_buf_set_lines (state .windows .output_buf , start_line , end_line , false , lines )
80+ vim .api .nvim_set_option_value (' modifiable' , false , { buf = state .windows .output_buf })
9381end
9482
9583--- Clear output buf extmarks
9684--- @param start_line ? integer Line to start clearing , defaults 0
9785--- @param end_line ? integer Line to clear until , defaults to -1
9886--- @param clear_all ? boolean If true , clears all extmarks in the buffer
9987function M .clear_extmarks (start_line , end_line , clear_all )
100- if not M .mounted () or not state . windows . output_buf then
88+ if not M .mounted () then
10189 return
10290 end
91+ --- @cast state.windows { output_buf : integer }
10392
10493 start_line = start_line or 0
10594 end_line = end_line or - 1
@@ -108,12 +97,13 @@ function M.clear_extmarks(start_line, end_line, clear_all)
10897end
10998
11099--- Apply extmarks to the output buffer
111- --- @param extmarks table<number , OutputExtmark> Extmarks indexed by line
100+ --- @param extmarks table<number , OutputExtmark[] > Extmarks indexed by line
112101--- @param line_offset ? integer Line offset to apply to extmarks , defaults to 0
113102function M .set_extmarks (extmarks , line_offset )
114103 if not M .mounted () or not extmarks or type (extmarks ) ~= ' table' then
115104 return
116105 end
106+ --- @cast state.windows { output_buf : integer }
117107
118108 line_offset = line_offset or 0
119109
@@ -122,20 +112,26 @@ function M.set_extmarks(extmarks, line_offset)
122112 for line_idx , marks in pairs (extmarks ) do
123113 for _ , mark in ipairs (marks ) do
124114 local actual_mark = type (mark ) == ' function' and mark () or mark
125- local target_line = line_offset + line_idx
115+ local target_line = line_offset + line_idx --[[ @as integer ]]
126116 if actual_mark .end_row then
127117 actual_mark .end_row = actual_mark .end_row + line_offset
128118 end
129119 local start_col = actual_mark .start_col
130120 if actual_mark .start_col then
131121 actual_mark .start_col = nil
132122 end
123+ --- @cast actual_mark vim.api.keyset.set_extmark
133124 pcall (vim .api .nvim_buf_set_extmark , output_buf , M .namespace , target_line , start_col or 0 , actual_mark )
134125 end
135126 end
136127end
137128
138129function M .focus_output (should_stop_insert )
130+ if not M .mounted () then
131+ return
132+ end
133+ --- @cast state.windows { output_win : integer }
134+
139135 if should_stop_insert then
140136 vim .cmd (' stopinsert' )
141137 end
@@ -146,6 +142,8 @@ function M.close()
146142 if M .mounted () then
147143 return
148144 end
145+ --- @cast state.windows { output_win : integer , output_buf : integer }
146+
149147 pcall (vim .api .nvim_win_close , state .windows .output_win , true )
150148 pcall (vim .api .nvim_buf_delete , state .windows .output_buf , { force = true })
151149end
0 commit comments