Skip to content

Commit 314befe

Browse files
committed
fix(pick): update all set_picker_xxx() to always first validate input
Details: - Previously there was an `is_picker_active()` check before argument validation, except `set_picker_items()`. Although not usually important, adapt a consistent approach of first validating inputs and then checking if there is an active picker. As this is a more safe "fail early" option. Resolve #2161
1 parent 342fa09 commit 314befe

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

lua/mini/pick.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,8 +1699,8 @@ MiniPick.get_picker_query = function() return vim.deepcopy((H.pickers.active or
16991699
---@seealso |MiniPick.get_picker_items()| and |MiniPick.get_picker_stritems()|
17001700
MiniPick.set_picker_items = function(items, opts)
17011701
if not H.islist(items) then H.error('`items` should be an array.') end
1702-
if not MiniPick.is_picker_active() then return end
17031702
opts = vim.tbl_deep_extend('force', { do_match = true, querytick = nil }, opts or {})
1703+
if not MiniPick.is_picker_active() then return end
17041704

17051705
-- Set items in async because computing lower `stritems` can block much time
17061706
coroutine.wrap(H.picker_set_items)(H.pickers.active, items, opts)
@@ -1728,11 +1728,11 @@ end
17281728
---
17291729
---@seealso |MiniPick.get_picker_items()| and |MiniPick.get_picker_stritems()|
17301730
MiniPick.set_picker_items_from_cli = function(command, opts)
1731-
if not MiniPick.is_picker_active() then return end
17321731
local is_valid_command = H.is_array_of(command, 'string') and #command >= 1
17331732
if not is_valid_command then H.error('`command` should be an array of strings.') end
17341733
local default_opts = { postprocess = H.cli_postprocess, set_items_opts = {}, spawn_opts = {} }
17351734
opts = vim.tbl_deep_extend('force', default_opts, opts or {})
1735+
if not MiniPick.is_picker_active() then return end
17361736

17371737
local executable, args = command[1], vim.list_slice(command, 2, #command)
17381738
local process, pid, stdout = nil, nil, vim.loop.new_pipe()
@@ -1779,10 +1779,11 @@ end
17791779
---
17801780
---@seealso |MiniPick.get_picker_matches()|
17811781
MiniPick.set_picker_match_inds = function(match_inds, match_type)
1782-
if not MiniPick.is_picker_active() then return end
17831782
if not H.is_array_of(match_inds, 'number') then H.error('`match_inds` should be an array of numbers.') end
17841783
local set = H.picker_set_inds[match_type or 'all']
17851784
if set == nil then H.error('`match_type` should be one of "all", "marked", "current"') end
1785+
if not MiniPick.is_picker_active() then return end
1786+
17861787
set(H.pickers.active, match_inds)
17871788
H.picker_update(H.pickers.active, false)
17881789
end
@@ -1807,8 +1808,8 @@ end
18071808
---
18081809
---@seealso |MiniPick.get_picker_state()|
18091810
MiniPick.set_picker_target_window = function(win_id)
1810-
if not MiniPick.is_picker_active() then return end
18111811
if not H.is_valid_win(win_id) then H.error('`win_id` is not a valid window identifier.') end
1812+
if not MiniPick.is_picker_active() then return end
18121813
H.pickers.active.windows.target = win_id
18131814
end
18141815

@@ -1818,8 +1819,8 @@ end
18181819
---
18191820
---@seealso |MiniPick.get_picker_query()|
18201821
MiniPick.set_picker_query = function(query)
1821-
if not MiniPick.is_picker_active() then return end
18221822
if not H.is_array_of(query, 'string') then H.error('`query` should be an array of strings.') end
1823+
if not MiniPick.is_picker_active() then return end
18231824

18241825
H.pickers.active.query, H.pickers.active.caret = vim.deepcopy(query), #query + 1
18251826
H.querytick = H.querytick + 1

tests/test_pick.lua

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4202,7 +4202,6 @@ T['set_picker_items()']['does not block picker'] = function()
42024202
end
42034203

42044204
T['set_picker_items()']['validates arguments'] = function()
4205-
start_with_items()
42064205
expect.error(function() set_picker_items(1) end, '`items`.*array')
42074206
end
42084207

@@ -4363,7 +4362,6 @@ T['set_picker_items_from_cli()']['forces absolute path of `opts.spawn_opts.cwd`'
43634362
end
43644363

43654364
T['set_picker_items_from_cli()']['validates arguments'] = function()
4366-
start_with_items()
43674365
expect.error(function() set_picker_items_from_cli(1) end, '`command`.*array of strings')
43684366
expect.error(function() set_picker_items_from_cli({}) end, '`command`.*array of strings')
43694367
expect.error(function() set_picker_items_from_cli({ 'a', 2, 'c' }) end, '`command`.*array of strings')
@@ -4504,7 +4502,6 @@ T['set_picker_match_inds()']['can set marked match indexes'] = function()
45044502
end
45054503

45064504
T['set_picker_match_inds()']['validates arguments'] = function()
4507-
start_with_items()
45084505
expect.error(function() set_picker_match_inds(1) end, '`match_inds`.*array')
45094506
expect.error(function() set_picker_match_inds({ 'a' }) end, '`match_inds`.*numbers')
45104507
expect.error(function() set_picker_match_inds({ 1 }, 1) end, '`match_type`.*one of')
@@ -4576,7 +4573,6 @@ T['set_picker_target_window()']['works'] = function()
45764573
end
45774574

45784575
T['set_picker_target_window()']['validates arguments'] = function()
4579-
start_with_items()
45804576
expect.error(function() child.lua('MiniPick.set_picker_target_window(-1)') end, '`win_id`.*not a valid window')
45814577
end
45824578

@@ -4654,7 +4650,6 @@ T['set_picker_query()']['resets match inds prior to matching'] = function()
46544650
end
46554651

46564652
T['set_picker_query()']['validates arguments'] = function()
4657-
start_with_items()
46584653
expect.error(function() set_picker_query(1) end, '`query`.*array')
46594654
expect.error(function() set_picker_query({ 1 }) end, '`query`.*strings')
46604655
end

0 commit comments

Comments
 (0)