Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 305694

Summary: [hotbug][Code Assist] Namespace not supported - no proposal after "new" keyword
Product: [WebTools] JSDT Reporter: Rosa Tse <bunnyyappi>
Component: GeneralAssignee: Chris Jaun <cmjaun>
Status: RESOLVED FIXED QA Contact: Nitin Dahyabhai <thatnitind>
Severity: major    
Priority: P2 CC: chrismarx, cmjaun, eclipse, patrick.myles, yenlu
Version: unspecifiedFlags: thatnitind: review+
Target Milestone: 3.2.1   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Animal listed as a proposal after typing "new a"
none
"a.b" as proposal after typing "new b"
none
Proposed patch
none
New patch with working copy fix
none
Updated patch
none
More updates to thie patch none

Description Rosa Tse CLA 2010-03-12 10:40:16 EST
Build Identifier: 3.5.1 M20090917-0800

I would like to know how I can get code completion to work for class in a namespace.

If I define the following:

Animal.prototype=new Object();
function Animal(){};
Animal.prototype.property="";
Animal.prototype.eat=function(food){};
Animal.prototype.sleep=function(){};
Animal.prototype.speak=function(){ return ""; };

After typing "new A", I can get Animal as one of the proposals.

However if I define something like this:

a = new Object();
a.b = new Object();
a.b.prototype = new Object();
a.b = function(){};
a.b.prototype.operationA=function(){};
a.b.prototype.operationB=function(){};

When I type "new a", I don't get any proposal. However, if I type "new b", then I see "a.b" listed.

Reproducible: Always

Steps to Reproduce:
1. Use these 2 extensions: org.eclipse.wst.jsdt.ui.JsGlobalScopeContainerPage, org.eclipse.wst.jsdt.core.JsGlobalScopeContainerInitializer
2. In the JS file referenced by the initializer's library, define Animal and a.b as listed in details.
3. Create a web project, add the JavaScript library.
4. Create a JS file.
5. Type "var obj = new a", type Ctrl+Space to invoke code assistance. Only "Animal" listed in proposal, "a.b" is not there.
6. Type "var obj = new b", type Ctrl+Space to invoke code assistance. Notice "a.b" is listed.
Comment 1 Rosa Tse CLA 2010-03-12 10:41:51 EST
Created attachment 161887 [details]
Animal listed as a proposal after typing "new a"
Comment 2 Rosa Tse CLA 2010-03-12 10:42:26 EST
Created attachment 161888 [details]
"a.b" as proposal after typing "new b"
Comment 3 Chris Jaun CLA 2010-03-12 14:17:37 EST
Thank you for the detailed bug report.

This is a problem we don't have a good solution for right now.

Not sure if we'll have time for this in WTP 3.2, but we'll take a look at it when planning our next milestone.
Comment 4 Rosa Tse CLA 2010-03-12 14:56:23 EST
I think I found out why it does not consider matches in packages/namespaces.

In org.eclipse.wst.jsdt.internal.codeassist.CompletionEngine,
findTypesAndPackages, the find packages code has been commented out.
Comment 5 Chris Jaun CLA 2010-03-12 15:23:45 EST
Packages in JSDT are mostly leftover from when it was cloned from JDT.

We don't really use them, other than the "default" package for our searching.

Generally when we infer something as a type we use the entire string as the type name.

In this case we probably use "a.b" as the complete type name. The "." is just considered part of the name, not as a package separator.
Comment 6 Rosa Tse CLA 2010-04-05 16:53:32 EDT
(In reply to comment #5)
> Packages in JSDT are mostly leftover from when it was cloned from JDT.
> 
> We don't really use them, other than the "default" package for our searching.
> 
> Generally when we infer something as a type we use the entire string as the
> type name.
> 
> In this case we probably use "a.b" as the complete type name. The "." is just
> considered part of the name, not as a package separator.

We are trying to provide code completion for a library that relies HEAVILY on namespace/package. This is a show-stopper for us. Who should I talk to about this?
Comment 7 gossi CLA 2010-04-05 17:02:38 EDT
See issue #284833 (https://bugs.eclipse.org/bugs/show_bug.cgi?id=284833). I think after having sth. like a spec for a library (with included namespaces) it's also easy for library authors to "create autocompletion". As a library author I would feel fine with that option. For sure it takes time to implement...
Comment 8 chris marx CLA 2010-04-09 14:50:47 EDT
Just wanted to add my support for including autocomplete for namespaced variables. I often do this

var com = {
  cornell:{
    myproject:{}
  }
}

and then 

com.cornell.myproject = function(){
  //etc.
}


this is usually dont with a namespacing script for convenience. but once i namespace variables like this, the autocomplete offers no suggestions :(
Comment 9 Rosa Tse CLA 2010-05-07 09:47:36 EDT
Adding information as per the WTP Hotbug policy (http://wiki.eclipse.org/WTP/Hotbug_Policy):

1. Affiliation: RIM
2. Targeted Release: The fix needs to go into the final release of WTP 3.2 so it's fixed in Eclipse 3.6.
3. Reason why this is a hotbug: This bug blocks our adoption of WTP because the JavaScript code completion is a critical feature of our product. We need to provide code completion for a library that relies heavily on namespace. The feature cannot be implemented without this bug fix.
Comment 10 Chris Jaun CLA 2010-05-17 17:04:31 EDT
Created attachment 168827 [details]
Proposed patch
Comment 11 Chris Jaun CLA 2010-05-17 17:06:46 EDT
Attached a patch for a proposed fix.

This fix has some risk and requires significant additional testing before I would be willing to put it into an 3.2 RC build.

Asking Nitin to review to get any initial comments from him.
Comment 12 Chris Jaun CLA 2010-05-17 17:29:32 EDT
First patch doesn't work for working copies...marked obsolete.
Comment 13 Chris Jaun CLA 2010-05-17 17:42:17 EDT
Created attachment 168831 [details]
New patch with working copy fix

My concern now is the confusing nature of how the proposals are sorted in the content assist popup for large libraries.
Comment 14 Chris Jaun CLA 2010-05-19 11:43:08 EDT
Further testing of this patch has found it breaks type hierarchy construction. I have marked obsolete.

This will not make RC2 and therefore will likely have to be re-targeted for 3.2.1.
Comment 15 Rosa Tse CLA 2010-05-21 13:31:45 EDT
(In reply to comment #14)
> Further testing of this patch has found it breaks type hierarchy construction.
> I have marked obsolete.
> 
> This will not make RC2 and therefore will likely have to be re-targeted for
> 3.2.1.

Thanks for attempting the fix. Would it be possible for this fix to go into Eclipse 3.6 release?
Comment 16 Chris Jaun CLA 2010-05-24 16:57:42 EDT
Created attachment 169730 [details]
Updated patch

My latest attempt at this....some quick testing looked good. Needs lots more before I'd consider included in the build.
Comment 17 Chris Jaun CLA 2010-05-25 11:26:14 EDT
Created attachment 169843 [details]
More updates to thie patch
Comment 18 Chris Jaun CLA 2010-05-25 11:26:34 EDT
I think this is ready for your review Nitin.
Comment 19 Nitin Dahyabhai CLA 2010-06-08 23:47:09 EDT
Don't suppose it should be checking instanceof ISuperReference instead of SuperReference?
Comment 20 Chris Jaun CLA 2010-06-09 09:36:37 EDT
I don't know. I suppose it could.
Comment 21 Chris Jaun CLA 2010-06-22 15:28:27 EDT
Fix checked into 3.2.1
Comment 22 chris marx CLA 2010-11-07 11:20:40 EST
Just upgraded to Helios, great to see that namespacing works for prototyped functions, but it doesn't appear to work for static or literal objects. This works:

com.cornell.project = function(){};
com.cornell.project.prototype.init = function(){};

this doesn't:

com.cornell.project = {
  init:function(){},
  etc:function(){}
}

files with that kind of object don't show up in the Javascript Resource source folder, but files with the prototyped variety do. Is this intentional? bug?