Skip to content

Commit

Permalink
Support Dictionary-function elements within the description dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
rene-descartes2021 committed Jun 6, 2022
1 parent 165772f commit 7d17e03
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ See more details about commands and options via `:h vim-which-key`.

See [#178](/~https://github.com/liuchengxu/vim-which-key/issues/178).

#### How to set keybindings on filetype or other condition?

You may use BufEnter/BufLeave [#132](/~https://github.com/liuchengxu/vim-which-key/issues/132), a `dictionary-function` [#209](/~https://github.com/liuchengxu/vim-which-key/pull/209), or *[experimental]* setup per buffer [#48](/~https://github.com/liuchengxu/vim-which-key/pull/48).

## Credit

- [vim-leader-guide](/~https://github.com/hecal3/vim-leader-guide)
20 changes: 13 additions & 7 deletions autoload/which_key.vim
Original file line number Diff line number Diff line change
Expand Up @@ -115,31 +115,37 @@ function! s:merge(target, native) " {{{
let target = a:target
let native = a:native

for [k, v] in items(target)
for [k, V] in items(target)

if type(v) == s:TYPE.dict && has_key(native, k)
" Support a `Dictionary-function` for on-the-fly mappings
if type(V) == s:TYPE.funcref
" Evaluate the funcref, to allow the result to be processed
let target[k] = V()
endif

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 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

" Support add a description to an existing map without dual definition
elseif type(v) == s:TYPE.string && k !=# 'name'
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]
\ V]
else
let target[k] = [
\ has_key(native, k) ? native[k][0] : 'which_key#error#missing_mapping()',
\ v]
\ V]
endif

endif
Expand Down

0 comments on commit 7d17e03

Please sign in to comment.