@@ -281,15 +281,22 @@ function Dialog:format_options(output, options)
281281 label = label .. ' - ' .. option .description
282282 end
283283
284- local line_idx = output :get_line_count ()
285284 local is_selected = self ._selected_index == i
286285 local line_text = is_selected and string.format (' %d. %s ' , i , label ) or string.format (' %d. %s' , i , label )
287286
288- output :add_line (line_text )
287+ -- Output uses 0-based indexing for extmarks. The correct target for
288+ -- extmarks is the previous line count (0-based) because add_line will
289+ -- append a new line and increase the 1-based line count. Capture the
290+ -- current count first and then add the line so we can use that 0-based
291+ -- index for extmarks.
292+ -- add_line returns a 1-based line index; Output extmarks use 0-based
293+ -- keys, so subtract 1 to get the correct extmark key.
294+ local added_idx = output :add_line (line_text )
289295
290296 if is_selected then
291- output :add_extmark (line_idx , { line_hl_group = ' OpencodeDialogOptionHover' } --[[ @as OutputExtmark]] )
292- output :add_extmark (line_idx , {
297+ local extmark_idx = added_idx - 1
298+ output :add_extmark (extmark_idx , { line_hl_group = ' OpencodeDialogOptionHover' } --[[ @as OutputExtmark]] )
299+ output :add_extmark (extmark_idx , {
293300 start_col = 2 ,
294301 virt_text = { { ' › ' , ' OpencodeDialogOptionHover' } },
295302 virt_text_pos = ' overlay' ,
@@ -313,11 +320,16 @@ function Dialog:_setup_keymaps()
313320 if keymaps .up then
314321 for _ , key in ipairs (keymaps .up ) do
315322 if key and key ~= ' ' then
316- vim .keymap .set (' n' , key , function ()
317- self :navigate (- 1 )
318- end , vim .tbl_extend (' force' , keymap_opts , {
319- desc = ' Dialog: navigate up' ,
320- }))
323+ vim .keymap .set (
324+ ' n' ,
325+ key ,
326+ function ()
327+ self :navigate (- 1 )
328+ end ,
329+ vim .tbl_extend (' force' , keymap_opts , {
330+ desc = ' Dialog: navigate up' ,
331+ })
332+ )
321333 table.insert (self ._keymaps , key )
322334 end
323335 end
@@ -326,31 +338,46 @@ function Dialog:_setup_keymaps()
326338 if keymaps .down then
327339 for _ , key in ipairs (keymaps .down ) do
328340 if key and key ~= ' ' then
329- vim .keymap .set (' n' , key , function ()
330- self :navigate (1 )
331- end , vim .tbl_extend (' force' , keymap_opts , {
332- desc = ' Dialog: navigate down' ,
333- }))
341+ vim .keymap .set (
342+ ' n' ,
343+ key ,
344+ function ()
345+ self :navigate (1 )
346+ end ,
347+ vim .tbl_extend (' force' , keymap_opts , {
348+ desc = ' Dialog: navigate down' ,
349+ })
350+ )
334351 table.insert (self ._keymaps , key )
335352 end
336353 end
337354 end
338355
339356 if keymaps .select and keymaps .select ~= ' ' then
340- vim .keymap .set (' n' , keymaps .select , function ()
341- self :select ()
342- end , vim .tbl_extend (' force' , keymap_opts , {
343- desc = ' Dialog: select option' ,
344- }))
357+ vim .keymap .set (
358+ ' n' ,
359+ keymaps .select ,
360+ function ()
361+ self :select ()
362+ end ,
363+ vim .tbl_extend (' force' , keymap_opts , {
364+ desc = ' Dialog: select option' ,
365+ })
366+ )
345367 table.insert (self ._keymaps , keymaps .select )
346368 end
347369
348370 if keymaps .dismiss and keymaps .dismiss ~= ' ' then
349- vim .keymap .set (' n' , keymaps .dismiss , function ()
350- self :dismiss ()
351- end , vim .tbl_extend (' force' , keymap_opts , {
352- desc = ' Dialog: dismiss' ,
353- }))
371+ vim .keymap .set (
372+ ' n' ,
373+ keymaps .dismiss ,
374+ function ()
375+ self :dismiss ()
376+ end ,
377+ vim .tbl_extend (' force' , keymap_opts , {
378+ desc = ' Dialog: dismiss' ,
379+ })
380+ )
354381 table.insert (self ._keymaps , keymaps .dismiss )
355382 end
356383
@@ -359,15 +386,20 @@ function Dialog:_setup_keymaps()
359386 local number_keymap_opts = vim .tbl_extend (' force' , keymap_opts , { nowait = true })
360387 for i = 1 , math.min (option_count , 9 ) do
361388 local key = tostring (i )
362- vim .keymap .set (' n' , key , function ()
363- if not self ._active or not self ._config .check_focused () then
364- return
365- end
366- self ._selected_index = i
367- self ._config .on_select (i )
368- end , vim .tbl_extend (' force' , number_keymap_opts , {
369- desc = ' Dialog: select option ' .. key ,
370- }))
389+ vim .keymap .set (
390+ ' n' ,
391+ key ,
392+ function ()
393+ if not self ._active or not self ._config .check_focused () then
394+ return
395+ end
396+ self ._selected_index = i
397+ self ._config .on_select (i )
398+ end ,
399+ vim .tbl_extend (' force' , number_keymap_opts , {
400+ desc = ' Dialog: select option ' .. key ,
401+ })
402+ )
371403 table.insert (self ._keymaps , key )
372404 end
373405 end
0 commit comments