Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim Javascript autocompletion picks comments as suggestions

Tags:

javascript

vim

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?


2 Answers

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.

like image 128
romainl Avatar answered Aug 23 '26 13:08

romainl


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)

like image 42
Herrington Darkholme Avatar answered Aug 23 '26 13:08

Herrington Darkholme



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!