google / tern-closure

A Tern plugin adding Closure support.
Apache License 2.0
32 stars 18 forks source link

Completion for object litteral #30

Open angelozerr opened 10 years ago

angelozerr commented 10 years ago

Even if tern doesn't support completion for object litteral (see issue 311), I create this issue because tern-closure needs that too.

For instance you can write this JS :

var newHeader = goog.dom.createDom('h1', {'style': 'background-color:#EEE'},
    'Hello world!');

It should be very cool if we can have completion like this :

var newHeader = goog.dom.createDom('h1', {'s // here Ctrl+Space shows 'style'

and :

var newHeader = goog.dom.createDom('h1', {'style': 'back // here Ctrl+Space shows background-color
jgiles commented 10 years ago

Hmm, this could be really tricky. We don't even know what the properties should be for the second argument for createDom (it's typed as (Object|null|string|undefined)), and even if we did we'd have to map that type onto the argument we're passing in (Tern doesn't support that currently), and I'm guessing we would have trouble with the parser here. In this particular instance, you really could pass any keys in for the properties. I'm also not sure that that this is common enough to be all that helpful even if we did get it working.

angelozerr commented 10 years ago

Hmm, this could be really tricky.

I know but in AngularJS Eclipse (https://github.com/angelozerr/angularjs-eclipse) I have more and more issues about this feature. More once this feature will be done, I could validate too the parameters with tern-lint (https://github.com/angelozerr/tern-lint/wiki/Validation-Rules)

So I have started to develop this feature for AngularJS (see https://github.com/marijnh/tern/issues/387)

The basic idea is to hard code the JSON schema of the object (ex : createDom has style property, etc) and attach this schema in the objectType retrieved by the function declared with !effect. In this function we search the object litteral and attach the well JSON schema.

After that I use completion hook to search ObjectExpression and test if the current objectType have JSON schema attached. If I have a JSON schema, I populate the tern completion with missing properties.

We don't even know what the properties should be for the second argument for createDom

Yes I know, We need to do that with !effect function

In this particular instance, you really could pass any keys in for the properties.

Sorry I don't understand?

I'm also not sure that that this is common enough to be all that helpful even if we did get it working.

JS framework uses a lot of object litteral and I find it's very hard to know which properties you can use it.I would like to for instance support this feature too for Google Maps Tern plugin https://github.com/angelozerr/tern.java/wiki/Tern-&-Google-Maps-support

As you are very talented, I would like to find a clean mean to support objet litteral inside tern. Hope you will able to help me. Many thank's!