Any way to "suggest" object types for code complet

Get help with Spket IDE.
Post Reply
Gary
Posts: 18
Joined: Fri May 30, 2008 5:57 pm

Any way to "suggest" object types for code complet

Post by Gary » Fri May 30, 2008 6:34 pm

I work with an application that uses JavaScript as an internal coding language. The application DOM is quite different from a browser DOM, so I'm writing a bunch of "skeleton" classes that can be included in other JS files to "mimick" the application DOM. This way, as I develop code for this application, I can use code completion to reference the apps native DOM.

One thing I have to represent is a Collection object which can contain 1 or more subtypes of the Collection's core object. For example:

/** @constructor */
Section = function( ) { this.Name = "some name";}

/** @constructor */
SectionTypeA = function( ) { this.Prop1 = "some property"; }

SectionTypeA.prototype=new Section( );

/** @class */
Sections = { /** @type {Section} */ Item : function( ) {return Section} }

Suppose this represents my Sections class (a DOM collection) which returns a Section class (base class) which, in reality, is a SectionTypeA (sub-class). Is there any way to "suggest" to the IDE that the return type is really a SectionTypeA and not Section? I've tried...

var s = SectionTypeA(Sections.Item( ));

... but when I do that, code completion only gives me Prop1 (defined in the sub-class) and it skips Name (defined in the base class). I've also tried putting a JSDoc style comment on the variable to indicate it's true type, but that hasn't worked either (didn't think it would).

Any thoughts or suggestions? I really can't create different methods for the collection object since it is only a representation of the actual DOM object in the parent application. The only thing I can think of is to not sub-class the different internal-app types and just use one HUGE object that has all of the different props/methods of the sub-types. That would be rather ugly and I'd like to avoid it.

eric_suen
Site Admin
Posts: 678
Joined: Wed Sep 28, 2005 7:00 am
Contact:

Post by eric_suen » Sat May 31, 2008 3:11 am

Should the code

Code: Select all

var s = SectionTypeA(Sections.Item( ));
be

Code: Select all

var s = new SectionTypeA(Sections.Item( ));
Otherwise the variable s is undefined?

Gary
Posts: 18
Joined: Fri May 30, 2008 5:57 pm

Post by Gary » Sat May 31, 2008 1:36 pm

Eric, good catch... made me feel a bit silly.

The code I posted was a watered down, 10th attempt of trying things.
I made that mistake because I started by trying to use class definitions instead of constructors. I still have a problem though if you consider that the variable s is not a new object... just a "typing" of object returned by the Item( ) function. If I re-write the Sections Collection and the subsequent calls as such...

Code: Select all

/** @class */
Sections  = { 
	myItems : new Array( ),
	Count : myItems.length,
	/** @type {Section} */ Item : function(x) {return myItems[x]},
	/** @param {Section} */ Add : function(section){myItems.push(section)}
}

Sections.Add(new SectionTypeA( ));
var s = Sections.Item(0);
... this demonstrates my issue a little better. In this case, the returned object from Item( ) is a SectionTypeA object but since the Item() function is defined as returning the generic base class, I cannot hint to the IDE that the real returned type is a SectionTypeA sub-class.

To make matters worse, my JS skeletons representing the DOM can't (and shouldn't) actually DO anything. Any code I write against these skeleton classes will only run when it is moved out of the IDE and into this third party app where the REAL Section, SectionTypeA and Sections Collection are defined. In this app, the Sections Collection is already populated with Section sub-classes that were defined by users from the apps GUI.

What I'm really trying to do is find some way in my written code to "type" a returned object without actually modifying or creating that object. Something like...

Code: Select all

var s = /**@type SectionTypeA */ Sections.Item(0);
I know I'm trying to use the IDE for something it wasn't really designed by trying to implement a proprietary DOM through these skeleton JS classes so there is probably no way to do this.

Thanks for any advice/thoughts!

eric_suen
Site Admin
Posts: 678
Joined: Wed Sep 28, 2005 7:00 am
Contact:

Post by eric_suen » Sat May 31, 2008 2:55 pm

Actually the code

Code: Select all

/**@type SectionTypeA */
var s;
s.{cursor}
will make code assist works as you expected, unfortunately, when you assign a value to s, the @type annotation will stop working, I will fix this issue in next version.

Gary
Posts: 18
Joined: Fri May 30, 2008 5:57 pm

Post by Gary » Sat May 31, 2008 4:10 pm

Excellent!

Eric, thanks for the quick reply and the great work on this IDE. I'll be looking forward to the next release.

Gary
Posts: 18
Joined: Fri May 30, 2008 5:57 pm

Post by Gary » Wed Jun 25, 2008 9:37 pm

Eric, follow up question. Will your next release also allow you to re-type a variable?

In the examples we used for this thread, it will often be the case that you don't know the sub-type of the class returned from the array when you declare the variable. It would be helpful to re-type after determining which sub-class was returned.

Thanks!

-Gary

eric_suen
Site Admin
Posts: 678
Joined: Wed Sep 28, 2005 7:00 am
Contact:

Post by eric_suen » Thu Jun 26, 2008 8:21 am

You means this one?

Code: Select all

var s = /**@type SectionTypeA */ Sections.Item(0);
support this should be very easy, I will add it in next version.

Gary
Posts: 18
Joined: Fri May 30, 2008 5:57 pm

Post by Gary » Thu Jun 26, 2008 4:30 pm

Sort of.

Something like this I think...

Code: Select all

var s = Sections.Item(0); // s is typed as Section base class
if(s.Type==0) {
   // somehow re-type variable s as SectionTypeA
   // do stuff
} else if(s.Type==1) {
   // somehow re-type variable s as SectionTypeB
   // do other stuff
} else {
   // leave s typed as the generic Section class
   // do different stuff
}
Sort of like re-classing in Java...

Code: Select all

(class)variable
... except only done with comments and only applicable to the IDE.

waynesi
Posts: 1
Joined: Fri Oct 10, 2008 3:26 pm

Post by waynesi » Fri Oct 10, 2008 3:30 pm

eric_suen wrote:You means this one?

Code: Select all

var s = /**@type SectionTypeA */ Sections.Item(0);
support this should be very easy, I will add it in next version.
Yes, that's what's I think really helpful! Please do provide this feature in the next release, or a quick fix is best! :D

You know this feature can help the programmers to avoid many mistakes on data type in the coding phase!

eric_suen
Site Admin
Posts: 678
Joined: Wed Sep 28, 2005 7:00 am
Contact:

Post by eric_suen » Fri Oct 10, 2008 4:50 pm

Hmm, this feature already there since version 1.6.15, but default it is not turn on, you can change it using preferences "Spket -> Editors -> JavaScript Editor", check the option "Enable inline type hint for assigment statement".

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests