0

Let's say I create a folder called folder on my HOME directory. Let's also say that I enter this folder and create a node.js file called code.js. If I open this file with Visual Studio Code and I start typing a code that uses a library installed on the system, I see the following:

enter image description here

In this example... While I was typing process. I saw some suggestions of possible functions that I can use with this javascript object, which is from the library child_process that is installed on my system.

So far everything is fine. My issue is that I'd like to make Vim work exactly like that as well, without using Visual Studio Code. Is it possible? Can I make Vim read the node.js libraries that are installed on my system and show me which functions exist in the library while I'm typing?

4
  • Generally speaking, if you're trying to get as close to an IDE as possible you should look at YouCompleteMe or CoC completion plugins. Since you obviously have node.js installed CoC is a good choice (and if you don't mind a non-native solution like that it's preferable to YCM for other reasons, too, IMHO). Use in conjuction with an LSP server and you'll have Intellisense level completion (in addition to other solid functionality in the realm of linting and refactoring)...and you'll be cooking with gas. :)
    – B Layer
    Jun 20 at 4:13
  • @BLayer Thanks, I'm checking both of them here... In my case I'd like to use this feature with NeoVim since I want to use it together with firenvim for editing code on the browser. I've read on the YCM GitHub page that NeoVim is not officially supported while CoC officially supports Neovim as well. I'm not sure if I choose YCM I'll run into troubles in the future for that. Have you already used YCM with NeoVim before?
    – raylight
    Jun 20 at 5:17
  • @BLayer I ended up starting with CoC since it works with vimplug... The behavior I expected when I made the question works fine :)
    – raylight
    Jun 20 at 6:31
  • Well, I was recommending CoC the whole time not YCM so....you made the right decision. As far as providing an answer to this question...I'm not sure if "use CoC" makes for a good answer. I guess I could just dump my comment in the answer box. ;)
    – B Layer
    Jun 20 at 7:22
1

I hestitate to call this comment that I posted an answer but the OP read it, did as recommended, and got "the behavior I expected when I made the question" so... :)

Generally speaking, if you're trying to get as close to an IDE as possible you should look at YouCompleteMe or CoC completion plugins. Since you obviously have node.js installed CoC is a good choice (and if you don't mind a non-native solution like that it's preferable to YCM for other reasons, too, IMHO). Use in conjuction with an LSP server and you'll have Intellisense level completion (in addition to other solid functionality in the realm of linting and refactoring)...and you'll be cooking with gas. :)

But let me elaborate on some of this.

Conquer of Completion aka CoC aka the strangest name for a plugin I've ever heard is a plugin for Vim (and Neovim) that is written in Javascript. Since Vim doesn't have built-in Javascript support you must first install the Node.js Javascript runtime environment but if that isn't an issue for you CoC supports auto-completion and more for a large number of programming languages and for many of those languages has Language Server Protocol support.

The Language Server Protocol aka LSP enables communication between code editors and "language servers" that provide sophisticated language-specific features like code completion, refactoring, and syntax highlighting. Basically, this allows you to offload the expensive processing of some code editing functions from your editor (the client) to a dedicated program (the server). This happens through asynchronous operations that result, for the most part, in a seamless experience for the user. Each server, which you can run locally like any other program, supports a single language, in the typical case.

One of the nicer features of CoC is how easy it is to set up and configure. Enabling support of a new language requires a single command. Adding language server support usually requires just copying and pasting some JSON from the CoC wiki.

Finally, I mentioned "Intellisense". This is the name of the very popular intelligent code completion feature found in the Visual Studio Code IDE. It's a very slick form of auto-completion.

6
  • Great! I have one last doubt about CoC that goes a little beyond what I've asked but I'm struggling to figure it out here... On Visual Studio when I type a basic syntax like a for it gives me a small snippet of a for loop if I choose this option. Is it possible to do with CoC as well? Or this kind of stuff is not included on its autocompletions?
    – raylight
    Jun 20 at 8:33
  • I'm not 100% sure but I don't think so because I think they leave that to snippet plugins. (I use Ultisnips and it works okay with CoC.) If a language server supported snippets theoretically CoC could too but I'm not sure if they've done so yet. Might want to research that.
    – B Layer
    Jun 20 at 8:53
  • Just found out that by adding the coc-snippets it partially does these snippets autocompletion that I've mentioned. It seems that in python and bash I can use multiple snippets like that after installing coc-snippets... In javascript it doesn't have many of them yet apparently...
    – raylight
    Jun 20 at 10:25
  • 1
    There are tons of Javascript snippets. I have coc-snippets..it's basically just a compatibility/convenience layer. Install Ultisnips and a snippet library like vim-snippets.
    – B Layer
    Jun 20 at 16:31
  • Cool, installing it with vim-plug using Plug 'honza/vim-snippets' worked fine on Vim and Neovim...
    – raylight
    Jun 20 at 22:51
1

With an appropriately set include/includeexpr and path, you can complete identifiers from included files (among other things).

As the name suggests, this was originally used for C, but it’s applicable to many languages with some work.

When everything is set up correctly, you can use Ctrl-X Ctrl-I. See :help compl-keyword, and use :checkpath[!] to check that everything is set correctly.

I'm not sure about path for node (for the shell, e.g., one can split $PATH on :; tpope's apathy plugin does the right thing for several languages). For include, I would probably use (based on your file)

setlocal include=require('\\zs\\f\\+\\ze')

You might need to use includeexpr to transform the module name into a filename.

2
  • I'm not sure if I understood correctly. Do you mean adding a path where I install the libraries and then Vim will be able to see the libraries inside a specific folder? In my case, I'm using Firenvin which opens NeoVim on the browser, it creates temporary files in the same folder always, so just creating a soft link with ln -s $HOME/node_modules /run/user/1000/firenvim/node_modules solved the issue of backend libraries. I'm wondering if it's possible to receive suggestions for frontend javascript that's using some library though.
    – raylight
    Jun 20 at 22:48
  • @raylight you need to tell vim where the files are and which ones to look for. The path option controls the former and include/includeexpr the latter. It is a bit more rudimentary, but setting these options correctly brings other advantages (the find and include search mechanisms, for two). I don’t really follow your question about frontend libs bc idk what the state of frontend libs/use is in js
    – D. Ben Knoble
    Jun 20 at 23:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.