Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions lua/mini/pick.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2200,11 +2200,12 @@ H.picker_advance = function(picker)
vim.schedule(function() vim.api.nvim_exec_autocmds('User', { pattern = 'MiniPickStart' }) end)

local do_match, is_aborted = false, false
local lmap = H.get_lmap()
for _ = 1, 1000000 do
if H.cache.is_force_stop_advance then break end
H.picker_update(picker, do_match)

local char = H.getcharstr(picker.opts.delay.async)
local char = H.getcharstr(picker.opts.delay.async, vim.o.iminsert == 0 and {} or lmap)
if H.cache.is_force_stop_advance then break end

is_aborted = char == nil
Expand Down Expand Up @@ -2729,7 +2730,7 @@ H.actions = {

H.picker_query_add = function(picker, char)
-- Determine if it **is** proper single character
if vim.fn.strchars(char) > 1 or vim.fn.char2nr(char) <= 31 then return end
if not H.is_query_char(char) then return end
table.insert(picker.query, picker.caret, char)
picker.caret = picker.caret + 1
H.querytick = H.querytick + 1
Expand All @@ -2740,6 +2741,8 @@ H.picker_query_add = function(picker, char)
if should_reset then picker.match_inds = H.seq_along(picker.items) end
end

H.is_query_char = function(char) return vim.fn.strchars(char) == 1 and vim.fn.char2nr(char) > 31 end

H.picker_query_delete = function(picker, n)
local delete_to_left = n > 0
local left = delete_to_left and math.max(picker.caret - n, 1) or picker.caret
Expand Down Expand Up @@ -2860,7 +2863,7 @@ H.picker_get_current_item = function(picker)
end

H.picker_get_register_contents = function(picker)
local register = H.getcharstr(picker.opts.delay.async)
local register = H.getcharstr(picker.opts.delay.async, {})
-- Mimic some "insert object under cursor" behavior of Command-line mode
local expand_var = ({ ['\1'] = '<cWORD>', ['\6'] = '<cfile>', ['\23'] = '<cword>' })[register]
if expand_var then
Expand Down Expand Up @@ -3586,7 +3589,7 @@ H.redraw = function() vim.cmd('redraw') end

H.redraw_scheduled = vim.schedule_wrap(H.redraw)

H.getcharstr = function(delay_async)
H.getcharstr = function(delay_async, lmap)
-- Ensure that redraws still happen
H.timers.getcharstr:start(0, delay_async, H.redraw_scheduled)
H.cache.is_in_getcharstr = true
Expand All @@ -3599,7 +3602,7 @@ H.getcharstr = function(delay_async)
if H.pickers.active ~= nil then main_win_id = H.pickers.active.windows.main end
local is_bad_mouse_click = vim.v.mouse_winid ~= 0 and vim.v.mouse_winid ~= main_win_id
if not ok or char == '' or char == '\3' or is_bad_mouse_click then return end
return char
return lmap[char] or char
end

H.tolower = (function()
Expand Down Expand Up @@ -3681,4 +3684,14 @@ end
-- TODO: Remove after compatibility with Neovim=0.9 is dropped
H.islist = vim.fn.has('nvim-0.10') == 1 and vim.islist or vim.tbl_islist

H.get_lmap = function()
local lmap = {}
for _, map in ipairs(vim.fn.maplist()) do
-- NOTE: Account only for characters that resolve to proper query character
local is_query_lmap = map.mode == 'l' and H.is_query_char(map.rhs)
if is_query_lmap then lmap[map.lhs] = map.rhs end
end
return lmap
end

return MiniPick
8 changes: 8 additions & 0 deletions tests/dir-pick/keymap/test_lhs.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
scriptencoding utf-8

let b:keymap_name = "test-lhs"

loadkeymap
aa A
bb B
bc C
8 changes: 8 additions & 0 deletions tests/dir-pick/keymap/test_rhs.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
scriptencoding utf-8

let b:keymap_name = "test-rhs"

loadkeymap
a <char-0x1f170>
b <char-0x1f171>
c <char-0x1f192>
8 changes: 8 additions & 0 deletions tests/dir-pick/keymap/test_simple.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
scriptencoding utf-8

let b:keymap_name = "test-simple"

loadkeymap
a 1
b 2
c 3
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
05|┌ tests/dir-pick ────────┐
06|│builtin-tests/ │
07|│file │
08|│lua/
09|│mocks/
10|│real-files/
11|│
08|│keymap/
09|│lua/
10|│mocks/
11|│real-files/
12|│ │
13|│ │
14|└ <No name> ────── 1|2|2 ┘
Expand All @@ -26,7 +26,7 @@
08|2444444444444444444444444211111111111111
09|2444444444444444444444444211111111111111
10|2444444444444444444444444211111111111111
11|2555555555555555555555555211111111111111
11|2444444444444444444444444211111111111111
12|2555555555555555555555555211111111111111
13|2555555555555555555555555211111111111111
14|2333333333332222223333333211111111111111
Expand Down
29 changes: 29 additions & 0 deletions tests/test_pick.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6222,4 +6222,33 @@ T['Stop']['triggers User event'] = function()
validate('<C-c>')
end

T['iminsert'] = new_set()

local load_keymap = function(keymap)
child.cmd('let &rtp .= ",' .. test_dir_absolute .. '"')
child.cmd('set keymap=' .. keymap)
eq(child.o.iminsert, 1)
end

T['iminsert']['works'] = function()
load_keymap('test_simple')
start_with_items({ 'a' })
type_keys('a', 'b', 'c')
eq(get_picker_query(), { '1', '2', '3' })
end

T['iminsert']['ignores nontrivial rhs'] = function()
load_keymap('test_rhs')
start_with_items({ 'a' })
type_keys('a', 'b', 'c')
eq(get_picker_query(), { 'a', 'b', 'c' })
end

T['iminsert']['ignores nontrivial lhs'] = function()
load_keymap('test_lhs')
start_with_items({ 'a' })
type_keys('a', 'a', 'b', 'b', 'c', 'c')
eq(get_picker_query(), { 'a', 'a', 'b', 'b', 'c', 'c' })
end

return T