Skip to content

Support counts like all native tag nav mappings do #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
82 changes: 65 additions & 17 deletions plugin/vim-erlang-tags.vim
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,73 @@ endfunction

command! ErlangTags call VimErlangTags()

function! VimErlangTagsSelect(split)
if a:split
split
endif
let orig_isk = &isk
set isk+=:
normal "_vawo
if getline('.')[col('.') - 2] =~# '[#?]'
normal h
" Execute the given tag lookup for current word, where 'iskeyword' is
" temporarily set such that modules, records, and macros are included.
"
" Accepts a count as an optional second parameter which will be the window
" height for split or preview commands like 'stag', 'ptjump', etc., or the
" match index to jump to for 'tag'.
function! s:GoToErlangTag(cmd, ...)
let orig_isk = &l:isk
setl isk+=:,#,?
let ident = expand('<cword>')
let &l:isk = orig_isk

" With no count arg or zero count, we can execute directly
if a:0 == 0 || a:1 == 0
execute a:cmd ident
else
let cnt = a:1

if a:cmd ==# 'tag'
execute cnt a:cmd ident
elseif a:cmd =~# '^s'
let cmd = 'split +' . a:cmd[1:] . '\ ' . ident
execute cnt cmd
elseif a:cmd =~# '^p'
" :pedit has a cursor movement bug that we work around with a mark
" http://bit.ly/1F59kWA :-( Affects YouCompleteMe users, for one.
normal! m`

let cmd = 'pedit +' . a:cmd[1:] . '\ ' . ident
call s:ExecWithPreviewHeight(cmd, cnt)

normal! ``
endif
endif
let &isk = orig_isk
endfunction

" Because :pedit can't take height as count like :split does, ugh why.
function! s:ExecWithPreviewHeight(cmd, height)
let orig_height = &previewheight
let &previewheight = a:height

try
execute a:cmd
catch /E426/ " tag not found
pclose " Spurious preview window is left open, close it.
echohl WarningMsg | echom v:exception | echohl None
finally
let &previewheight = orig_height
endtry
endfunction

function! VimErlangTagsDefineMappings()
nnoremap <buffer> <c-]> :call VimErlangTagsSelect(0)<cr><c-]>
nnoremap <buffer> g<LeftMouse> :call VimErlangTagsSelect(0)<cr><c-]>
nnoremap <buffer> <c-LeftMouse> :call VimErlangTagsSelect(0)<cr><c-]>
nnoremap <buffer> g] :call VimErlangTagsSelect(0)<cr>g]
nnoremap <buffer> g<c-]> :call VimErlangTagsSelect(0)<cr>g<c-]>
nnoremap <buffer> <c-w><c-]> :call VimErlangTagsSelect(1)<cr><c-]>
nnoremap <buffer> <c-w>] :call VimErlangTagsSelect(1)<cr><c-]>
" Count is index of match to jump to, as with :tag
nnoremap <buffer> <c-]> :<C-U>call <SID>GoToErlangTag('tag', v:count1)<cr>
nnoremap <buffer> g<LeftMouse> :<C-U>call <SID>GoToErlangTag('tag', v:count1)<cr>
nnoremap <buffer> <c-LeftMouse> :<C-U>call <SID>GoToErlangTag('tag', v:count1)<cr>

nnoremap <buffer> g] :<C-U>call <SID>GoToErlangTag('tselect')<cr>
nnoremap <buffer> g<c-]> :<C-U>call <SID>GoToErlangTag('tjump')<cr>

" Count is window height for split and preview mappings
nnoremap <buffer> <c-w><c-]> :<C-U>call <SID>GoToErlangTag('stag', v:count)<cr>
nnoremap <buffer> <c-w>] :<C-U>call <SID>GoToErlangTag('stag', v:count)<cr>
nnoremap <buffer> <c-w>g] :<C-U>call <SID>GoToErlangTag('stselect', v:count)<cr>
nnoremap <buffer> <c-w>g<c-]> :<C-U>call <SID>GoToErlangTag('stjump', v:count)<cr>
nnoremap <buffer> <c-w>} :<C-U>call <SID>GoToErlangTag('ptag', v:count)<cr>
nnoremap <buffer> <c-w>g} :<C-U>call <SID>GoToErlangTag('ptjump', v:count)<cr>
endfunction

" vim:set expandtab sw=4 ts=4: