2014-04-12 42 views
2

我有了ctags正确配置代码库。当我这样做时,:tjump keyword它向我显示了keyword的潜在匹配列表。寻找一种方法来正确排序的ctags匹配

但是这些匹配的排序不正确。我正在寻找一种方法来正确地排列比赛,以便最佳匹配位于列表的顶部。即: - 当我直接使用Ctrl-]应该去正确的地方第一跳。

对于带有gf的GetFile导航,我发现includeexpr,它允许我运行自定义逻辑来确定要跳转到的文件。

Vim是否有类似的功能来改变tags结果?

我正在考虑另一种方法是从:tjump抢标签的列表,请整理,并覆盖映射Ctrl-]

对于这种方法,是否有函数从:tjump获取匹配列表?

任何其他的想法,以确保正确的比赛是在顶部也欢迎!

谢谢。

回答

3

它往往不明确“正确”的比赛是什么。目前,Vim使用下面的逻辑(从:help tag-priority):

When there are multiple matches for a tag, this priority is used: 
1. "FSC" A full matching static tag for the current file. 
2. "F C" A full matching global tag for the current file. 
3. "F " A full matching global tag for another file. 
4. "FS " A full matching static tag for another file. 
5. " SC" An ignore-case matching static tag for the current file. 
6. " C" An ignore-case matching global tag for the current file. 
7. " " An ignore-case matching global tag for another file. 
8. " S " An ignore-case matching static tag for another file. 

如果你想实现自己的定制逻辑,没有什么(我知道的)类似includeexpr,可以帮助你。

您可以创建多个标签,并在编码您喜欢这样的方式,责令其在tags设置。尽管这很难说,但很可能需要一些试验。

你可以做的另一个更复杂的事情是覆盖<c-]>密钥(也可能是其他人,比如<c-w>])来做一些不同的事情。喜欢的东西:

nnoremap <c-]> :call <SID>JumpToTag()<cr> 

function! s:JumpToTag() 
    " try to find a word under the cursor 
    let current_word = expand("<cword>") 

    " check if there is one 
    if current_word == '' 
    echomsg "No word under the cursor" 
    return 
    endif 

    " find all tags for the given word 
    let tags = taglist('^'.current_word.'$') 

    " if no tags are found, bail out 
    if empty(tags) 
    echomsg "No tags found for: ".current_word 
    return 
    endif 

    " take the first tag, or implement some more complicated logic here 
    let selected_tag = tags[0] 

    " edit the relevant file, jump to the tag's position 
    exe 'edit '.selected_tag.filename 
    exe selected_tag.cmd 
endfunction 

可以使用taglist()功能定位标签光标下的单词。然后,而不是let selected_tag = tags[0],您可以实现自己的逻辑,如筛选出测试文件或按特定条件排序。

不幸的是,由于您正在手动编辑文件,因此这不会保留:tnext:tprevious命令。您可以用quickfix或位置列表替换它,使用setqflist()函数,并按照您喜欢的方式对标签进行排序,然后使用:cnext和​​进行导航。但是这是一个更多的脚本:)。如果你决定放下这个兔子洞,你可能想看看我的tagfinder插件的来源,以获取灵感。

+0

这是一个超级回答!谢谢!我要用'taglist'去。我只有几个'interface' defs是需要过滤掉的误报。 Tagfinder插件看起来不错,谢谢。 –

0

基于对@ AndrewRadev的回答您的评论:

我经常创造一个“mktags”脚本建立的ctags,然后过滤掉的标签文件,我想省略的。例如(对于sh,ksh,bash,zsh):

ctags "[email protected]" 
egrep -v "RE-for-tags-to-delete" tags > tags.$$ 
mv tags.$$ tags 
相关问题