Skip to content

Commit

Permalink
Simplify content creation (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Sep 19, 2018
1 parent 06135bb commit 09ae48d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 28 deletions.
28 changes: 9 additions & 19 deletions autoload/which_key/util.vim
Expand Up @@ -53,6 +53,7 @@ function! which_key#util#create_string(layout, mappings) abort
let row = 0
let col = 0
let smap = sort(filter(keys(mappings), 'v:val !=# "name"'),'1')
for k in smap
let key = get(s:displaynames, toupper(k), k)
let desc = type(mappings[k]) == type({}) ? mappings[k].name : mappings[k][1]
Expand Down Expand Up @@ -91,20 +92,9 @@ function! which_key#util#create_string(layout, mappings) abort
silent execute "cnoremap <nowait> <buffer> ".substitute(k, "|", "<Bar>", ""). " " . s:escape_keys(k) ."<CR>"
endfor

let r = []
let mlen = 0
for ro in rows
let line = join(ro, '')
call add(r, line)
if strdisplaywidth(line) > mlen
let mlen = strdisplaywidth(line)
endif
endfor

call insert(r, '')
let output = join(r, "\n ")
call map(rows, 'join(v:val, "")')

return output
return rows
endfunction " }}}

function! s:escape_keys(inp) abort
Expand Down Expand Up @@ -139,13 +129,13 @@ function! which_key#util#get_register() "{{{
return s:reg
endfunction "}}}

function! which_key#util#escape_mappings(mapping) abort" {{{
function! which_key#util#escape_mappings(mapping) abort " {{{
let feedkeyargs = a:mapping.noremap ? "nt" : "mt"
let rstring = substitute(a:mapping.rhs, '\', '\\\\', 'g')
let rstring = substitute(rstring, '<\([^<>]*\)>', '\\<\1>', 'g')
let rstring = substitute(rstring, '"', '\\"', 'g')
let rstring = 'call feedkeys("'.rstring.'", "'.feedkeyargs.'")'
return rstring
let rhs = substitute(a:mapping.rhs, '\', '\\\\', 'g')
let rhs = substitute(rhs, '<\([^<>]*\)>', '\\<\1>', 'g')
let rhs = substitute(rhs, '"', '\\"', 'g')
let rhs = 'call feedkeys("'.rhs.'", "'.feedkeyargs.'")'
return rhs
endfunction " }}}

function! which_key#util#get_sep() abort
Expand Down
7 changes: 2 additions & 5 deletions autoload/which_key/window.vim
Expand Up @@ -10,15 +10,12 @@ function! which_key#window#open(runtime) abort

let runtime = a:runtime
let layout = which_key#util#calc_layout(runtime)
let string = which_key#util#create_string(layout, runtime)
let rows = which_key#util#create_string(layout, runtime)

let resize = g:which_key_vertical ? 'vertical resize' : 'resize'
noautocmd execute resize layout.win_dim

setlocal modifiable
silent 1put!=string
silent normal! gg"_dd
setlocal nomodifiable
call setline(1, rows)

call which_key#wait_for_input()
endfunction
Expand Down
4 changes: 2 additions & 2 deletions doc/vim-which-key.txt
Expand Up @@ -268,10 +268,10 @@ which_key#register({prefix}, {dict}) *which_key#register()*
Example:
>
" register dictionary for the <Space>-prefix
call leaderGuide#register_prefix_descriptions(' ', "g:space_prefix_dict")
call which_key#register(' ', "g:space_prefix_dict")
" register dictionary for the ,-prefix
call leaderGuide#register_prefix_descriptions(',', "g:comma_prefix_dict")
call which_key#register(',', "g:comma_prefix_dict")
<

which_key#start({vis}, {bang}, {prefix}) *which_key#start()*
Expand Down
4 changes: 2 additions & 2 deletions syntax/which_key.vim
Expand Up @@ -6,9 +6,9 @@ let b:current_syntax = 'which_key'
let s:sep = which_key#util#get_sep()

execute 'syntax match WhichKeySeperator' '/'.s:sep.'/' 'contained'
execute 'syntax match WhichKey' '/\s.\{1,8}'.s:sep.'/' 'contains=WhichKeySeperator'
execute 'syntax match WhichKey' '/[^\s].\{1,3}'.s:sep.'/' 'contains=WhichKeySeperator'
syntax match WhichKeyGroup / +[0-9A-Za-z_/-]*/
syntax region WhichKeyDesc start="^" end="$" contains=WhichKey, WhichKeyGroup
syntax region WhichKeyDesc start="^" end="$" contains=WhichKey, WhichKeyGroup, WhichKeySeperator

highlight default link WhichKey Function
highlight default link WhichKeySeperator DiffAdded
Expand Down

0 comments on commit 09ae48d

Please sign in to comment.