Guido Visser
Guido Visser

Reputation: 2299

Intellisense on object using special comments

In Sublime or VS Code you can define a special comment (DocBlockr or JSDocs as example) that Intellisense will recognize en give you smart tooltip functionality.

I have a function which takes an options parameter. This is an object and can have several properties that could contain functions, strings, ints etc. An example would be:

function foo(options){
    options = options || {};

    if(options.foo){
        console.log(options.foo);
    }
    if(options.bar) {
        console.log(options.bar());
    }
}
foo({foo: 'foo', bar: function(){return 'bar';}});

I could add a DocBlockr comment, but that would only yield a tooltip that shows it needs an object.

Is it possible to make some sort of definition of that options object, so it would popup using intellisense?

Upvotes: 1

Views: 94

Answers (1)

Lorenzo Pichilli
Lorenzo Pichilli

Reputation: 3429

For Sublime Text 3, you can use my JavaScript Enhancement plugin (you could find it also on Package Control) that will turn it into a JavaScript IDE like (It uses Flow under the hood). In your case, using Flow type annotations, you could use this code to get what you want:

//@flow

function foo(options /*: { foo: string, bar: function} */){
    options = options || {};

    if(options.foo){
        console.log(options.foo);
    }    
    if(options.bar) {
        console.log(options.bar());
    }
}

foo()

So, on the Sublime Text 3 editor, you will get something like this: enter image description here

also on multiple lines (/* : need to be on the same line of the parameter):

enter image description here

Also, the plugin offers not only a smart javascript autocomplete but also a lot of features about creating, developing and managing javascript projects (real-time errors, code refactoring, etc.).

Upvotes: 1

Related Questions