Skip to content

re: Help needed with D3 and autocompletion. VIEW POST


Thanks @jamestthompson3 for the pointer! Setting the ominfunc to 'javascriptcomplete#CompleteJS' in my html filetype file did provide access to the javascript autocompletion, no d3 functions.

What does work is having the 'import * as d3 from 'location of d3.js file' in my case it was 'import * from '../d3/d3.js'' in the main.js.

I ended up creating vim autocommands to comment out the import statement on save and uncomment when I enter insert mode. Yes, a bit of a hack... I will put the code below if anyone is interested. You can put in your vimrc or a javascript.vim file located in a /.vim/after/ftplugin/ directory. That way changes are only added to the filetype that vim has already set. I think this is a little safer? Anyways, code is below. Hope it helps someone else.

augroup d3_javascript
    autocmd InsertEnter <buffer> :1 :s /^\/\/ import \* as d3 from\>/import \* as d3 from/e
    autocmd BufWrite <buffer> :1 :s /^import \* as d3 from\>/\/\/ import \* as d3 from/e | ''
augroup END

The 'e' at the end ignores errors if already commented out and the ( | ''), two single quotes, will return your cursor back to your previous line after execution.


Are you bundling your javascript before serving it, or is all the code in the main.js file? It seems that YCM is using tsserver for javascript completion which would explain why you need the import statement to get it to work. If you are bundling, then you don't need to import D3 in your HTML file, and YCM should give correct completion without having to comment and uncomment your import statement every time you edit / save the file.


Pretty new at this. No, not doing any bundling. I work on my main.js file which is referenced in my index.html.

I've been reading a bit on bundling though. I do have nodejs and npm installed on my machine now. Just reading on where to go from here. I see that I need to do a 'npm init' to get my package.json initialized...from there I am still reading.

If you’re not bundling, I would recommend using completion based on your path. For more info about setting the path in vim, I recommend this article.

code of conduct - report abuse