2014-04-12 74 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 
相關問題