@@ -11,7 +11,9 @@ local state = require('opencode.state')
1111function M .open_json_file (data )
1212 local tmpfile = vim .fn .tempname () .. ' .json'
1313 local json_str = vim .json .encode (data )
14- vim .api .nvim_set_current_win (state .last_code_win_before_opencode )
14+ if state .last_code_win_before_opencode then
15+ vim .api .nvim_set_current_win (state .last_code_win_before_opencode --[[ @as integer]] )
16+ end
1517 vim .fn .writefile (vim .split (json_str , ' \n ' ), tmpfile )
1618 vim .cmd (' e ' .. tmpfile )
1719 if vim .fn .executable (' jq' ) == 1 then
2729
2830function M .debug_message ()
2931 local renderer = require (' opencode.ui.renderer' )
30- local current_line = vim .api .nvim_win_get_cursor (state .windows .output_win )[1 ]
31- local message_data = renderer ._render_state :get_message_at_line (current_line )
32- if message_data and message_data .message then
33- M .open_json_file (message_data .message )
34- else
35- vim .notify (' No message found at current line' , vim .log .levels .WARN )
32+ if not state .windows or not state .windows .output_win then
33+ vim .notify (' Output window not available' , vim .log .levels .WARN )
34+ return
35+ end
36+ local current_line = vim .api .nvim_win_get_cursor (state .windows .output_win --[[ @as integer]] )[1 ]
37+
38+ -- Search backwards from current line to find nearest message
39+ for line = current_line , 1 , - 1 do
40+ local message_data = renderer ._render_state :get_message_at_line (line )
41+ if message_data and message_data .message then
42+ M .open_json_file (message_data .message )
43+ return
44+ end
3645 end
46+
47+ vim .notify (' No message found in previous lines' , vim .log .levels .WARN )
3748end
3849
3950function M .debug_session ()
@@ -43,7 +54,9 @@ function M.debug_session()
4354 print (' No active session' )
4455 return
4556 end
46- vim .api .nvim_set_current_win (state .last_code_win_before_opencode )
57+ if state .last_code_win_before_opencode then
58+ vim .api .nvim_set_current_win (state .last_code_win_before_opencode --[[ @as integer]] )
59+ end
4760 vim .cmd (' e ' .. session_path .. ' /' .. state .active_session .id .. ' .json' )
4861end
4962
0 commit comments