From 49697a751fb865a7b877f1f31768b30312afe06b Mon Sep 17 00:00:00 2001 From: Ches Martin Date: Wed, 4 Feb 2015 05:10:33 +0700 Subject: [PATCH 1/3] Add preview window support Generalizes the approach so that the function doesn't need logic to handle split, preview, etc. Also no longer results in the cursor being moved to beginning of the function call (or record/macro after #15) in the original buffer. --- plugin/vim-erlang-tags.vim | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/plugin/vim-erlang-tags.vim b/plugin/vim-erlang-tags.vim index 839b699..6fcf96b 100644 --- a/plugin/vim-erlang-tags.vim +++ b/plugin/vim-erlang-tags.vim @@ -50,25 +50,29 @@ endfunction command! ErlangTags call VimErlangTags() -function! VimErlangTagsSelect(split) - if a:split - split - endif +" Execute the given tag lookup for current word, where 'iskeyword' is +" temporarily set such that modules, records, and macros are included. +function! s:GoToErlangTag(cmd) let orig_isk = &isk - set isk+=: - normal "_vawo - if getline('.')[col('.') - 2] =~# '[#?]' - normal h - endif + set isk+=:,#,? + let ident = expand('') let &isk = orig_isk + + execute a:cmd ident endfunction function! VimErlangTagsDefineMappings() - nnoremap :call VimErlangTagsSelect(0) - nnoremap g :call VimErlangTagsSelect(0) - nnoremap :call VimErlangTagsSelect(0) - nnoremap g] :call VimErlangTagsSelect(0)g] - nnoremap g :call VimErlangTagsSelect(0)g - nnoremap :call VimErlangTagsSelect(1) - nnoremap ] :call VimErlangTagsSelect(1) + nnoremap :call GoToErlangTag('tag') + nnoremap g :call GoToErlangTag('tag') + nnoremap :call GoToErlangTag('tag') + nnoremap g] :call GoToErlangTag('tselect') + nnoremap g :call GoToErlangTag('tjump') + nnoremap :call GoToErlangTag('stag') + nnoremap ] :call GoToErlangTag('stag') + nnoremap g] :call GoToErlangTag('stselect') + nnoremap g :call GoToErlangTag('stjump') + nnoremap } :call GoToErlangTag('ptag') + nnoremap g} :call GoToErlangTag('ptjump') endfunction + +" vim:set expandtab sw=4 ts=4: From d8167f5d3d599bf29f80dae652179ad147d00b20 Mon Sep 17 00:00:00 2001 From: Ches Martin Date: Wed, 4 Feb 2015 19:09:16 +0700 Subject: [PATCH 2/3] Support counts like all native tag nav mappings do Vim's native window tag mappings (split- and preview-related) use count to affect the size of the new window. Regular mappings like `CTRL-]` use count to jump to a match index like `:tag` does. These changes mimic all of the above behavior accurately with this plugin's enhanced notion of tag identifiers for Erlang. --- plugin/vim-erlang-tags.vim | 65 ++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 14 deletions(-) diff --git a/plugin/vim-erlang-tags.vim b/plugin/vim-erlang-tags.vim index 6fcf96b..34c43b3 100644 --- a/plugin/vim-erlang-tags.vim +++ b/plugin/vim-erlang-tags.vim @@ -52,27 +52,64 @@ command! ErlangTags call VimErlangTags() " Execute the given tag lookup for current word, where 'iskeyword' is " temporarily set such that modules, records, and macros are included. -function! s:GoToErlangTag(cmd) - let orig_isk = &isk - set isk+=:,#,? +" +" 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('') - let &isk = orig_isk + 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) - execute a:cmd ident + normal! `` + endif + endif +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 + execute a:cmd + let &previewheight = orig_height endfunction function! VimErlangTagsDefineMappings() - nnoremap :call GoToErlangTag('tag') - nnoremap g :call GoToErlangTag('tag') - nnoremap :call GoToErlangTag('tag') + " Count is index of match to jump to, as with :tag + nnoremap :call GoToErlangTag('tag', v:count1) + nnoremap g :call GoToErlangTag('tag', v:count1) + nnoremap :call GoToErlangTag('tag', v:count1) + nnoremap g] :call GoToErlangTag('tselect') nnoremap g :call GoToErlangTag('tjump') - nnoremap :call GoToErlangTag('stag') - nnoremap ] :call GoToErlangTag('stag') - nnoremap g] :call GoToErlangTag('stselect') - nnoremap g :call GoToErlangTag('stjump') - nnoremap } :call GoToErlangTag('ptag') - nnoremap g} :call GoToErlangTag('ptjump') + + " Count is window height for split and preview mappings + nnoremap :call GoToErlangTag('stag', v:count) + nnoremap ] :call GoToErlangTag('stag', v:count) + nnoremap g] :call GoToErlangTag('stselect', v:count) + nnoremap g :call GoToErlangTag('stjump', v:count) + nnoremap } :call GoToErlangTag('ptag', v:count) + nnoremap g} :call GoToErlangTag('ptjump', v:count) endfunction " vim:set expandtab sw=4 ts=4: From ad6abc5ff6bbdfae190048afbca0c75b96a7d60b Mon Sep 17 00:00:00 2001 From: Ches Martin Date: Mon, 9 Feb 2015 01:43:04 +0700 Subject: [PATCH 3/3] Close spurious preview window on tag not found error --- plugin/vim-erlang-tags.vim | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/plugin/vim-erlang-tags.vim b/plugin/vim-erlang-tags.vim index 34c43b3..67af195 100644 --- a/plugin/vim-erlang-tags.vim +++ b/plugin/vim-erlang-tags.vim @@ -90,8 +90,15 @@ endfunction function! s:ExecWithPreviewHeight(cmd, height) let orig_height = &previewheight let &previewheight = a:height - execute a:cmd - let &previewheight = orig_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()