Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Web Tools Project (WTP) » Is there code example for inferring a method?
Is there code example for inferring a method? [message #529040] Thu, 22 April 2010 17:30 Go to next message
Eugene Ostroukhov is currently offline Eugene Ostroukhov
Messages: 66
Registered: July 2009
Member
I'm trying to implement content assist for PhoneGap. This library adds several properties to the navigator object and I need to make them available in the pop-up.

The PhoneGap JavaScript file is copied to the project. I can see classes but the properties are not there. Navigator class is declared in the Browser library that is on the project include path.

I tried to implement InferEngine - but I don't see a way to add those properties. Is there some article/code example?

I would really appreciate any pointers - i.e. APIs to look at, JSDT implementation classes.
Re: Is there code example for inferring a method? [message #529952 is a reply to message #529040] Tue, 27 April 2010 19:54 Go to previous messageGo to next message
Nitin Dahyabhai is currently offline Nitin Dahyabhai
Messages: 2307
Registered: July 2009
Senior Member
On 4/22/2010 1:30 PM, Eugene Ostroukhov wrote:
> I'm trying to implement content assist for PhoneGap. This library adds
> several properties to the navigator object and I need to make them
> available in the pop-up.
>
> The PhoneGap JavaScript file is copied to the project. I can see classes
> but the properties are not there. Navigator class is declared in the
> Browser library that is on the project include path.
>
> I tried to implement InferEngine - but I don't see a way to add those
> properties. Is there some article/code example?
>
> I would really appreciate any pointers - i.e. APIs to look at, JSDT
> implementation classes.

The best example is the default implementation in
org.eclipse.wst.jsdt.core.infer.InferEngine.

You would go about adding your properties by first adding the Navigator
type to the CompilationUnitDeclaration [part of the inference code being
provisional is that the relied upon CompilationUnitDeclaration class is
not itself yet correct API] and then adding your properties that that
InferredType object using the methods on
org.eclipse.wst.jsdt.core.infer.InferredType itself. The indexer and
content assist will take care of merging the properties when presented
to the user for content assist and search. You will want to take care
that your InferrenceProvider#applysTo() returns MAYBE_THIS only for
files where the PhoneGap library is present.

Are the properties on your classes declared in an unusual way so that
they're not picked up as expected?

--
Nitin Dahyabhai
Eclipse WTP Source Editing and JSDT
IBM Rational


---
Nitin Dahyabhai
Eclipse WTP, IBM
Re: Is there code example for inferring a method? [message #530034 is a reply to message #529952] Wed, 28 April 2010 09:09 Go to previous messageGo to next message
Eugene Ostroukhov is currently offline Eugene Ostroukhov
Messages: 66
Registered: July 2009
Member
PhonGap adds properties with following syntax:

if (typeof navigator.accelerometer == "undefined") navigator.accelerometer = new Accelerometer();

Note: I can see the Accelerometer class (i.e. I can do var acc = new Accelerometer() - and JSDT will properly suggest property and method names). But there are no added navigator properties - and it really harms the user experience.

Navigator class comes from the JSDT browser library.
Re: Is there code example for inferring a method? [message #530102 is a reply to message #529040] Wed, 28 April 2010 13:38 Go to previous messageGo to next message
Eugene Ostroukhov is currently offline Eugene Ostroukhov
Messages: 66
Registered: July 2009
Member
I added following code to my infer engine:
	@Override
	protected InferredType getInferredType2(IExpression fieldReceiver) {
		if (fieldReceiver.getASTType() == IASTNode.SINGLE_NAME_REFERENCE) {
			ISingleNameReference nameReference = (ISingleNameReference) fieldReceiver;
			if ("navigator".equals(String.valueOf(nameReference.getToken()))) {
				return addType(NAVIGATOR_TYPE);
			}
		}
		return super.getInferredType2(fieldReceiver);
	}


The code gets executed - in the debugger I can see as the type is built:
class Navigator extends Object{
  Accelerometer accelerometer;
  Camera camera;
  Contacts contacts;
  ?? device;
  Geolocation geolocation;
  Map map;
}


But still - there is nothing in CA pop-up.
Re: Is there code example for inferring a method? [message #530103 is a reply to message #529040] Wed, 28 April 2010 13:41 Go to previous messageGo to next message
Eugene Ostroukhov is currently offline Eugene Ostroukhov
Messages: 66
Registered: July 2009
Member
The project is a "WRT Application" It is a JS project with browser library, Window type is set as a global one. The project has basic.js and phonegap.js. We need to get content assist in basic.js to show information from phonegap.js. So far only types are shown (see first comment).

We are using 3.6M6.

[Updated on: Wed, 28 April 2010 13:42]

Report message to a moderator

Re: Is there code example for inferring a method? [message #530502 is a reply to message #530102] Thu, 29 April 2010 20:23 Go to previous messageGo to next message
Nitin Dahyabhai is currently offline Nitin Dahyabhai
Messages: 2307
Registered: July 2009
Senior Member
On 4/28/2010 9:38 AM, Eugene Ostroukhov wrote:
> I added following code to my infer engine:
....
> return addType(NAVIGATOR_TYPE);
....
> But still - there is nothing in CA pop-up.

The SourceElementParser won't transfer the InferredType's properties
into the Index unless you mark it's isDefined field as true.

--
Nitin Dahyabhai
Eclipse WTP Source Editing and JSDT
IBM Rational


---
Nitin Dahyabhai
Eclipse WTP, IBM
Re: Is there code example for inferring a method? [message #530903 is a reply to message #530502] Sun, 02 May 2010 15:08 Go to previous messageGo to next message
Eugene Ostroukhov is currently offline Eugene Ostroukhov
Messages: 66
Registered: July 2009
Member
Code still does not really work:
	protected InferredType getInferredType2(IExpression fieldReceiver) {
		if (fieldReceiver.getASTType() == IASTNode.SINGLE_NAME_REFERENCE) {
			ISingleNameReference nameReference = (ISingleNameReference) fieldReceiver;
			if ("navigator".equals(String.valueOf(nameReference.getToken()))) {
				return addType(NAVIGATOR_TYPE,true);
			}
		}
		return super.getInferredType2(fieldReceiver);
	}


I do not see class members unless I add something in leu
Navigator.prototype.contact2s = 2;
to phonegap.js
Re: Is there code example for inferring a method? [message #530904 is a reply to message #530903] Sun, 02 May 2010 15:10 Go to previous messageGo to next message
Eugene Ostroukhov is currently offline Eugene Ostroukhov
Messages: 66
Registered: July 2009
Member
I found one difference - if I add isInferred=true then "camera" and other PhoneGap contributions appear twice in the pop-up.
Re: Is there code example for inferring a method? [message #531045 is a reply to message #530904] Mon, 03 May 2010 13:49 Go to previous messageGo to next message
Nitin Dahyabhai is currently offline Nitin Dahyabhai
Messages: 2307
Registered: July 2009
Senior Member
Eugene Ostroukhov wrote:
> The code gets executed - in the debugger I can see as the type is built:
> class Navigator extends Object{
> Accelerometer accelerometer;
> Camera camera;
> Contacts contacts;
> ?? device;
> Geolocation geolocation;
> Map map;
> }
>
> I found one difference - if I add isInferred=true then "camera" and
> other PhoneGap contributions appear twice in the pop-up.

Generally you wouldn't want to add properties to every file that refers
to the navigator, only to files where a type you are interested in is
already being defined. One technique I've seen is to override the
addType() method to add specific properties from a white-list of type
names. Don't forget that any InferredAttributes you add should
themselves have their types set, and those inferred types also added,
although not necessarily isDefinition=true, to the
CompilationUnitDeclaration as needed.

--
Nitin Dahyabhai
Eclipse WTP Source Editing and JSDT
IBM Rational


---
Nitin Dahyabhai
Eclipse WTP, IBM
Re: Is there code example for inferring a method? [message #532053 is a reply to message #531045] Thu, 06 May 2010 22:48 Go to previous message
Eugene Ostroukhov is currently offline Eugene Ostroukhov
Messages: 66
Registered: July 2009
Member
Ok, I finally understood erratic behavior I was seeing.

1. Our projects are Eclipse project (not WTP/faceted) with JSDT nature, project root is a source folder. ECMAScript and JSDT browser libraries are added to the project build path. "Window" from the browser lib is a Global Super Type.
2. PhoneGap file(s) are kept in the projects (as opposed to adding them as JSDT libraries).
3. PhoneGap API relies on declarations similar to the following:
if (typeof navigator.geolocation == "undefined") navigator.geolocation = new Geolocation();

4. Our InferEngine is only invoked on the file(s) that declare PhoneGap classes. The engine is currently rather simple (it is only forcing "navigator" var type to be identified as Navigator from the JSDT Browser library:
public class PhoneGapInferEngine extends InferEngine {
    private static final char[] NAVIGATOR_TYPE = "Navigator".toCharArray();
    
    @Override
    protected InferredType getInferredType2(IExpression fieldReceiver) {
        if (fieldReceiver.getASTType() == IASTNode.SINGLE_NAME_REFERENCE) {
            ISingleNameReference nameReference = (ISingleNameReference) fieldReceiver;
            if ("navigator".equals(String.valueOf(nameReference.getToken()))) {
                return addType(NAVIGATOR_TYPE, true);
            }
        }
        return super.getInferredType2(fieldReceiver);
    }
}


PhoneGap libraries are not edited by the user - we edit different libraries.

The problem: there are no PhoneGap properties of the "navigator" object if the phonegap.js is opened in the editor. The properties are shown if the editor is closed.
There are no proposals if we disable our InferEngine.
Previous Topic:Eclipse + Tomcat Apache + Console Output
Next Topic:Re: Missing element in XML editor autocompletion
Goto Forum:
  


Current Time: Mon Oct 20 11:31:53 GMT 2014

Powered by FUDForum. Page generated in 0.02359 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software