Skip to content

Commit

Permalink
<C-I> keymaps now merged into <Tab> properly
Browse files Browse the repository at this point in the history
  • Loading branch information
rene-descartes2021 committed Jun 27, 2022
1 parent ef677b8 commit 35078ec
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 40 deletions.
71 changes: 35 additions & 36 deletions autoload/which_key.vim
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ function! which_key#start(vis, bang, prefix) " {{{

let key = a:prefix
let s:which_key_trigger = key ==# ' ' ? '<space>' : key

if !has_key(s:cache[mode], key) || g:which_key_run_map_on_popup
" First run
let s:cache[mode][key] = {}
call which_key#mappings#parse(key, s:cache[mode][key], mode)
endif
call s:cache_key(mode, key)

" s:runtime is a dictionary combining the native key mapping dictionary
" parsed by vim-which-key itself with user defined prefix dictionary if avaliable.
Expand Down Expand Up @@ -101,6 +96,17 @@ function! which_key#start(vis, bang, prefix) " {{{
call which_key#window#show(s:runtime)
endfunction

function! s:cache_key(mode, key)
let mode = a:mode
let key = a:key
if !has_key(s:cache[mode], key) || g:which_key_run_map_on_popup
if key !=? '<C-I>'
let s:cache[mode][key] = {}
endif
call which_key#mappings#parse(key, s:cache[mode], mode)
endif
endfunction

function! s:create_runtime(mode, key)
let mode = a:mode
let key = a:key
Expand All @@ -121,52 +127,45 @@ endfunction
function! s:merge(target, native) " {{{
let target = a:target
let native = a:native

if has_key(target, '<C-I>')
if has_key(target, '<Tab>')
call extend(target['<Tab>'], target['<C-I>'], 'keep')
else
let target['<Tab>'] = target['<C-I>']
endif
call remove(target, '<C-I>')
endif
for [k, V] in items(target)

" Support a `Dictionary-function` for on-the-fly mappings
while type(target[k]) == s:TYPE.funcref
" Evaluate the funcref, to allow the result to be processed
let target[k] = target[k]()
endwhile

if type(V) == s:TYPE.dict && has_key(native, k)

if type(native[k]) == s:TYPE.dict
if has_key(V, 'name')
let native[k].name = V.name
if type(V) == s:TYPE.dict
if has_key(native, k)
if type(native[k]) == s:TYPE.dict
if has_key(V, 'name')
let native[k].name = V.name
endif
call s:merge(target[k], native[k])
elseif type(native[k]) == s:TYPE.list
let target[k] = native[k]
endif
call s:merge(target[k], native[k])
elseif type(native[k]) == s:TYPE.list
let target[k] = native[k]
else
" Process leaf nodes
call s:merge(target[k], {})
endif

" Support add a description to an existing map without dual definition
elseif type(V) == s:TYPE.string && k !=# 'name'

" <Tab> <C-I>
if k ==# '<Tab>' && has_key(native, '<C-I>')
let target[k] = [
\ native['<C-I>'][0],
\ V]
if has_key(native, k)
let target[k] = [native[k][0], V]
else
let target[k] = [
\ has_key(native, k) ? native[k][0] : 'which_key#error#missing_mapping()',
\ V]
let target[k] = ['which_key#error#missing_mapping()', V]
endif

endif

endfor

" TODO handle <C-I>, <Tab> more clearly
if has_key(native, '<C-I>')
if !has_key(target, '<Tab>')
let target['<Tab>'] = native['<C-I>']
endif
call remove(native, '<C-I>')
endif

call extend(target, native, 'keep')
endfunction

Expand Down
9 changes: 6 additions & 3 deletions autoload/which_key/mappings.vim
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ endfunction

" Parse key-mappings gathered by `:map` and feed them into dict
function! which_key#mappings#parse(key, dict, visual) " {{{
let key = a:key ==? ' ' ? '<Space>' : a:key
let key = a:key ==? ' ' ? '<Space>' : (a:key ==? '<C-I>' ? '<Tab>' : a:key)
let visual = a:visual ==# 'v'

let lines = s:get_raw_map_info(key)
if key ==# '<Tab>'
call extend(lines, s:get_raw_map_info('<C-I>'))
endif

for line in lines
let mapd = maparg(split(line[3:])[0], line[0], 0, 1)
Expand All @@ -52,7 +55,7 @@ function! which_key#mappings#parse(key, dict, visual) " {{{
endif

let mapd.lhs = substitute(mapd.lhs, '<Space>', ' ', 'g')
let mapd.lhs = substitute(mapd.lhs, '<Tab>', '<C-I>', 'g')
let mapd.lhs = substitute(mapd.lhs, '<C-I>', '<Tab>', 'g')

let mapd.rhs = substitute(mapd.rhs, '<SID>', '<SNR>'.mapd['sid'].'_', 'g')

Expand All @@ -65,7 +68,7 @@ function! which_key#mappings#parse(key, dict, visual) " {{{
if mapd.lhs !=# '' && mapd.display !~# 'WhichKey.*'
if (match(mapd.mode, visual ? '[vx ]' : '[n ]') >= 0)
let mapd.lhs = s:string_to_keys(mapd.lhs)
call s:add_map_to_dict(mapd, 0, a:dict)
call s:add_map_to_dict(mapd, 0, a:dict[key])
endif
endif
endfor
Expand Down
1 change: 0 additions & 1 deletion autoload/which_key/renderer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ let s:TYPE = g:which_key#TYPE
let s:default_displaynames = {
\ ' ': 'SPC',
\ '<C-H>': 'BS',
\ '<C-I>': 'TAB',
\ '<TAB>': 'TAB',
\ }

Expand Down

0 comments on commit 35078ec

Please sign in to comment.