2012-05-23 65 views

回答

2

要在檢查每個光標移動的語法時回答性能問題,我必須自己實現這一點,並將其製作爲OnSyntaxChange插件。

有了這個插件,設置此功能可以在短短三行進行(例如,在.vimrc裏):

call OnSyntaxChange#Install('Comment', '^Comment$', 0, 'i') 
autocmd User SyntaxCommentEnterI silent! AcpLock 
autocmd User SyntaxCommentLeaveI silent! AcpUnlock 

對我來說,對性能的影響是顯着的(根據文件類型和語法),但容忍的。自己試試!

+0

謝謝,非常酷的插件 – Josh

+0

我用了一些非常相似的東西,但是用了'NeoComplCacheLock'和'NeoComplCacheUnlock' - 感謝這個偉大的插件! – Matt

+0

如何在python docstring(多行註釋)中實現同樣的效果? – Matt

2

您需要通過鉤子檢查CursorMovedI您目前在評論中,然後可以使用AutoComplPop的:AcpLock臨時禁用它。 (並且在您移出評論後撤銷:AcpUnlock。)

檢測各種文件類型的註釋最好通過查詢語法突出顯示來完成;這樣,您可以從現有文件類型的語法定義中受益。

下面是這個片段:

function! IsOnSyntaxItem(syntaxItemPattern) 
    " Taking the example of comments: 
    " Other syntax groups (e.g. Todo) may be embedded in comments. We must thus 
    " check whole stack of syntax items at the cursor position for comments. 
    " Comments are detected via the translated, effective syntax name. (E.g. in 
    " Vimscript, 'vimLineComment' is linked to 'Comment'.) 
    for l:id in synstack(line('.'), col('.')) 
     let l:actualSyntaxItemName = synIDattr(l:id, 'name') 
     let l:effectiveSyntaxItemName = synIDattr(synIDtrans(l:id), 'name') 
     if l:actualSyntaxItemName =~# a:syntaxItemPattern || l:effectiveSyntaxItemName =~# a:syntaxItemPattern 
      return 1 
     endif 
    endfor 
    return 0 
endfunction 

有了這個,你應該能夠縫合在一起的解決方案。

+0

如何檢查這會影響vim的性能?它會讓它變慢嗎? –

+0

沒關係;看到我現成的解決方案的其他答案,併爲自己嘗試。 –