Skip to content

Commit

Permalink
fix(pick): handle error when choosing item(s)
Browse files Browse the repository at this point in the history
Resolve #1363
  • Loading branch information
echasnovski committed Nov 26, 2024
1 parent 620ac68 commit c8922ae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lua/mini/pick.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2536,7 +2536,11 @@ H.actions = {
choose_in_split = function(picker, _) return H.picker_choose(picker, 'split') end,
choose_in_tabpage = function(picker, _) return H.picker_choose(picker, 'tabnew') end,
choose_in_vsplit = function(picker, _) return H.picker_choose(picker, 'vsplit') end,
choose_marked = function(picker, _) return not picker.opts.source.choose_marked(MiniPick.get_picker_matches().marked) end,
choose_marked = function(picker, _)
local ok, res = pcall(picker.opts.source.choose_marked, MiniPick.get_picker_matches().marked)
if not ok then vim.schedule(function() H.error('Error during choose marked:\n' .. res) end) end
return not (ok and res)
end,

delete_char = function(picker, _) H.picker_query_delete(picker, 1) end,
delete_char_right = function(picker, _) H.picker_query_delete(picker, 0) end,
Expand Down Expand Up @@ -2631,8 +2635,11 @@ H.picker_choose = function(picker, pre_command)
end)
end

-- Returning nothing, `nil`, or `false` should lead to picker stop
return not picker.opts.source.choose(cur_item)
local ok, res = pcall(picker.opts.source.choose, cur_item)
-- Delay error to have time to hide picker window
if not ok then vim.schedule(function() H.error('Error during choose:\n' .. res) end) end
-- Error or returning nothing, `nil`, or `false` should lead to picker stop
return not (ok and res)
end

H.picker_mark_indexes = function(picker, range_type)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_pick.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5250,6 +5250,19 @@ T['Choose']['uses output as "should continue"'] = function()
validate({ '<C-x>', '<M-CR>' })
end

T['Choose']['handles error in choose function'] = function()
child.lua_notify([[MiniPick.start({ source = { items = { 'a' }, choose = function() error('Oops!') end } })]])
expect.error(function() type_keys('<CR>') end, 'Error during choose:.*Oops!')
eq(is_picker_active(), false)
eq(#child.api.nvim_list_wins(), 1)

child.lua_notify([[MiniPick.start({ source = { items = { 'a' }, choose_marked = function() error('Oops!') end } })]])
type_keys('<C-x>')
expect.error(function() type_keys('<M-CR>') end, 'Error during choose marked:.*Oops!')
eq(is_picker_active(), false)
eq(#child.api.nvim_list_wins(), 1)
end

T['Mark'] = new_set()

T['Mark']['works'] = function()
Expand Down

0 comments on commit c8922ae

Please sign in to comment.