Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java-like code autocompletion with JavaScript?

As a Java developer, I am used to being able to see all relevant information out of the code completion of the IDE already. Here is an example of code autocompletion from the Eclipse IDE. For instance, you can see that the return type of the function contentEquals is a boolean and it expect one parameter with type StringBuffer. Additionally, if the JavaDoc is there, you even get a nice explanation of how the function is supposed to be used.

enter image description here

Now I am looking for something similar when coding with JavaScript. I started using Webstorm as I wanted an IDE with the more advanced features but when you look at the code autocompletion, it either seems rather poor or I'm not using it right.

Can someone help me figure it out?

Here is a more specific example:

Let's say, you want to get the user's position by using the navigator.geolocation. Let's assume you know as much as that it can be done by calling the getCurrentPosition() method.

You go ahead and type in your IDE (Webstorm in my case):

navigator.geolocation.getCurrentPosition()

Here is what the Webstorm IDE makes out of it:

enter image description here

Ok, it tells me there is one mandatory and a couple of optional parameters to call this method. Let's say, you want to call this with only the mandatory parameter, which is supposed to be a function. So far so good, but what kind of function? Is this function supposed to take parameters? Does it return anything? There's no hint about it.

I found in various examples on the web, that this callback function actually does take one parameter, so I go ahead and write it:

function printPosition(position) {
    // do something
}

But here comes the next question: what type is this parameter position? What can you do with it?

So I try the code autocompletion on it: enter image description here

Whooot? It gives me all kinds of stuff, but not at all what I'm looking for. So I'm ending up with google again.

And here it is how it is supposed to look like:

console.log(position.coords.latitude + ' , ' + position.coords.longitude);

Honestly, I never would have guessed that this is how it's done only by looking at the autocompletion which is very annoying because I am used to being able to do that when coding in Java.

Can anyone relate to what I am experiencing? Am I missing something? Any help would be highly appreciated!

like image 917
Hans Avatar asked Jul 11 '17 18:07

Hans


People also ask

What is autocomplete in JavaScript?

The autocomplete feature in JavaScript gives relevant suggestions when someone starts typing in the text field. For example, if a user types the character “c” the autocomplete feature will show a filtered list of all the words containing the “c” letter.

How do I enable autocomplete in VS code?

You can trigger IntelliSense in any editor window by typing Ctrl+Space or by typing a trigger character (such as the dot character (.) in JavaScript).

How do I autocomplete in Visual Studio?

The suggestion list of Automatic completion appears as soon as you start typing a new identifier. The suggestion list of Basic completion appears when you press the default Visual Studio IntelliSense shortcut Ctrl+Space . If necessary, you can always return to the Visual Studio's native's IntelliSense.


1 Answers

In a nutshell, because JavaScript isn't a strongly typed language like Java, you can't get the super granular autocompletion like you get in Java, unless the developer has included comments that WebStorm (or any IDE) can interpret.

You can implement those comments using Google Closure Annotation or JSDoc3. Using those, you can manually specify comments that will give you autocompletion.

Without those though, WebStorm really is just doing it's best to guess at what it needs, and it's usually not able to guess too well (which is why you get that giant list of basically everything in your last screenshot).

With JavaScript, you'll just have to rely on documentation.

like image 104
samanime Avatar answered Oct 04 '22 06:10

samanime