I have the following line in my vimrc to enable Javascript completion:
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
The problem is that if I'm working on a JS file that contains a lot of comments, for example:
// draws the map and the pieces depending on the state of the game
Vim picks words from those comments, such as draws, map, etc.; and considers them as acceptable code suggestions, which they are clearly not. How can I filter them out?
Vim has a number of completion mechanism suited for different needs: keyword completion, file name completion… and omni completion, the kind of completion most suited to programming.
Omni completion is usually initiated by pressing <C-x><C-o> and will certainly never pick a suggestion from the comments in your file. Are you sure you are using omni completion and not something else? <C-n> or <C-p>, maybe? See :h ins-completion for the full list.
Also, that line is totally useless. Supposing you have filetype plugin indent on in your ~/.vimrc, it's completely unnecessary to tell Vim to use JS completion in JS file.
JS autocompletion requires dynamic type inference, which is difficult for non-IDE text editors.(JS has no static class, you know!) Vim probably just implement a fuzzy matching algorithm to make completion. (I just guess, because I don't use vim personally). I think Vim also takes comments into consideration because it probably does not interpret js!
You can try the new Ternjs plugin for Vim. You need node.JS. Make sure your Vim has Python support.
Online demo is on http://ternjs.net/
Generally, javascript autocompletion is difficult to accomplish due to the dynamic nature of that language. Ternjs makes type inference like javascript engine, and thus makes autocompletion better. (Personally I have tried tern in SublimeText. Great Experience except initial loading and no builtin function such as document.body.appendChild)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With