问题
I've recently started using AutoComplPop, but I find the popup annoying when I'm writing comments (as I usually don't need autocomplition when writing a comment).
Is there a configuration option or a quick hack that can effectively disable AutoComplPop when the insertion point is in a comment?
回答1:
To answer the question of performance when checking the syntax on every cursor move, I had to implement this myself, and made this into the OnSyntaxChange plugin.
With this plugin, setting this up can be done in just three lines (e.g. in .vimrc):
call OnSyntaxChange#Install('Comment', '^Comment$', 0, 'i')
autocmd User SyntaxCommentEnterI silent! AcpLock
autocmd User SyntaxCommentLeaveI silent! AcpUnlock
For me, the performance impact is noticeable (depending on the filetype and syntax), but tolerable. Try for yourself!
回答2:
You need to check via a hook into CursorMovedI that you're currently in a comment, and can then use AutoComplPop's :AcpLock to disable it temporarily. (And undo with :AcpUnlock once you move out of comments.)
Detecting comments for various filetypes is best done by querying the syntax highlighting; this way, you benefit from the syntax definitions of existing filetypes.
Here's a snippet for this:
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
With this, you should be able to stitch together a solution.
来源:https://stackoverflow.com/questions/10723499/how-to-disable-autocomplpop-completions-in-comments