Skip to content

Commit

Permalink
expand <Leader> and <LocalLeader> in shortcut menu
Browse files Browse the repository at this point in the history
This allows for more reliable fallback behavior, where the user's input
would match shortcuts containing their <Leader> and <LocalLeader> keys.
  • Loading branch information
sunaku committed Jan 13, 2018
1 parent 657fbf5 commit 4195e88
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
19 changes: 13 additions & 6 deletions plugin/shortcut.vim
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ function! s:shortcut_menu_command(fullscreen) range abort
endfunction

function! s:shortcut_menu_items() abort
let pad = 4 + max(map(keys(g:shortcuts), 'len(v:val)'))
return values(map(copy(g:shortcuts), "printf('%-".pad."S%s', v:key, v:val)"))
let labels = map(copy(g:shortcuts), 'ShortcutLeaderKeys(v:key)')
let width = max(map(values(labels), 'len(v:val)')) + 4
return values(map(labels, "printf('%-".width."S%s', v:val, g:shortcuts[v:key])"))
endfunction

function! s:shortcut_menu_item_action(choice) abort
Expand Down Expand Up @@ -64,15 +65,21 @@ function! ShortcutTypeaheadInput()
return chars
endfunction

function! ShortcutKeystrokes(input) abort
let escaped = substitute(a:input, '\ze[\<"]', '\', 'g')
function! ShortcutLeaderKeys(input) abort
let result = a:input

let leader = get(g:, 'mapleader', '\')
let escaped = substitute(escaped, '\c<Leader>', leader, 'g')
let result = substitute(result, '\c<Leader>', leader, 'g')

let localleader = get(g:, 'maplocalleader', '\')
let escaped = substitute(escaped, '\c<LocalLeader>', localleader, 'g')
let result = substitute(result, '\c<LocalLeader>', localleader, 'g')

return result
endfunction

function! ShortcutKeystrokes(input) abort
let leadered = ShortcutLeaderKeys(a:input)
let escaped = substitute(leadered, '\ze[\<"]', '\', 'g')
execute 'return "'. escaped .'"'
endfunction

Expand Down
21 changes: 21 additions & 0 deletions t/shortcut_test.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
source plugin/shortcut.vim

describe 'ShortcutLeaderKeys()'
it 'compiles leader keys into \ when not defined'
Expect ShortcutLeaderKeys('<Leader>') == '\'
Expect ShortcutLeaderKeys('<leader>') == '\'
Expect ShortcutLeaderKeys('<leader><Leader>') == '\\'
Expect ShortcutLeaderKeys('<LocalLeader>') == '\'
Expect ShortcutLeaderKeys('<localleader>') == '\'
Expect ShortcutLeaderKeys('<localleader><LocalLeader>') == '\\'
end

it 'compiles leader keys into their defined values'
let g:mapleader = 'x'
Expect ShortcutLeaderKeys('<Leader>') == 'x'
unlet g:mapleader

let g:maplocalleader = 'y'
Expect ShortcutLeaderKeys('<LocalLeader>') == 'y'
unlet g:maplocalleader
end
end

describe 'ShortcutKeystrokes()'
it 'preserves non-symbolic keys during compile'
Expect ShortcutKeystrokes('') == ''
Expand Down

0 comments on commit 4195e88

Please sign in to comment.