11local state = require (' opencode.state' )
2- local keymap = require (' opencode.keymap' )
32local output_window = require (' opencode.ui.output_window' )
43
54local M = {}
@@ -26,6 +25,13 @@ function M.setup_contextual_actions(windows)
2625 callback = function ()
2726 vim .schedule (function ()
2827 local line_num = vim .api .nvim_win_get_cursor (0 )[1 ]
28+
29+ if not line_num or line_num <= 0 or not state .windows or not state .windows .output_buf then
30+ return
31+ end
32+
33+ line_num = line_num - 1 -- need api-indexing (e.g. 0 based line #), win_get_cursor returns 1 based line #
34+
2935 local actions = require (' opencode.ui.renderer' ).get_actions_for_line (line_num )
3036 last_line_num = line_num
3137
@@ -34,7 +40,7 @@ function M.setup_contextual_actions(windows)
3440
3541 if actions and # actions > 0 then
3642 dirty = true
37- M .show_contextual_actions_menu (state .windows .output_buf , line_num , actions , ns_id )
43+ M .show_contextual_actions_menu (state .windows .output_buf , actions , ns_id )
3844 end
3945 end )
4046 end ,
@@ -48,6 +54,7 @@ function M.setup_contextual_actions(windows)
4854 if not output_window .mounted () then
4955 return
5056 end
57+ --- @cast state.windows { output_buf : integer }
5158 local line_num = vim .api .nvim_win_get_cursor (0 )[1 ]
5259 if last_line_num == line_num and not dirty then
5360 return
@@ -61,15 +68,17 @@ function M.setup_contextual_actions(windows)
6168 group = augroup ,
6269 buffer = windows .output_buf ,
6370 callback = function ()
64- vim .api .nvim_buf_clear_namespace (state .windows .output_buf , ns_id , 0 , - 1 )
65- clear_keymaps (state .windows .output_buf )
71+ if state .windows and state .windows .output_buf then
72+ vim .api .nvim_buf_clear_namespace (state .windows .output_buf , ns_id , 0 , - 1 )
73+ clear_keymaps (state .windows .output_buf )
74+ end
6675 last_line_num = nil
6776 dirty = false
6877 end ,
6978 })
7079end
7180
72- function M .show_contextual_actions_menu (buf , line_num , actions , ns_id )
81+ function M .show_contextual_actions_menu (buf , actions , ns_id )
7382 clear_keymaps (buf )
7483
7584 for _ , action in ipairs (actions ) do
@@ -80,7 +89,7 @@ function M.show_contextual_actions_menu(buf, line_num, actions, ns_id)
8089 hl_mode = ' combine' ,
8190 }
8291
83- vim .api .nvim_buf_set_extmark (buf , ns_id , action .display_line - 1 , 0 , mark )
92+ vim .api .nvim_buf_set_extmark (buf , ns_id , action .display_line , 0 , mark --[[ @as vim.api.keyset.set_extmark ]] )
8493 end
8594 -- Setup key mappings for actions
8695 for _ , action in ipairs (actions ) do
0 commit comments