Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to access a cross reference from a content-assist-provider?
How to access a cross reference from a content-assist-provider? [message #888871] Mon, 18 June 2012 18:33 Go to next message
Marco Schulze is currently offline Marco SchulzeFriend
Messages: 5
Registered: July 2009
Junior Member
Hello *,

I've been searching the web and debugging through the object graphs for quite a while and unfortunately didn't find an answer to this question: How can I access the cross-references from within my ProposalsProvider?

Here's my use-case:

I'm working on Vestigo which is a JDO/JPA query editor and browser. I would like to provide content-assist for fields (of an entity-class), so let's take this example query:

SELECT movie.| FROM my.package.Movie movie

Let '|' be the cursor position where I press Ctrl+Space. It triggers my overriden method:

@Override
public void completeAliasAttributeExpression_Attributes(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor)


My cross-reference seems to be declared correctly (in the xtext grammar), because Shift+Alt+R works fine. However, I didn't find "movie" (or more precisely "my.package.Movie") anywhere in the object graphs passed to this method.

First I thought, I'll have to navigate from my 'context.getCurrentNode()' backwards via the siblings. However, the following is null:

context.getCurrentNode().getPreviousSibling()

I don't really understand why it is null, but well, I continued searching in the 'context', but didn't get further.

So I looked at 'assignment' and found this:

TreeIterator<EObject> eAllContents = assignment.eContainer().eAllContents();
while (eAllContents.hasNext()) {
EObject next = eAllContents.next();
EList<EObject> eCrossReferences = next.eCrossReferences();


This code finds one cross-reference, which seems very promising. However, I didn't figure out, how to get the contents out of it. My goal is to get the class name "my.package.Movie" (which is declared before the alias "movie" which is a cross-reference).

Any hint to some helpful documentation or a code example would be highly appreciated!

Thanks a lot in advance!!!

Best regards, Marco Smile
Re: How to access a cross reference from a content-assist-provider? [message #890577 is a reply to message #888871] Fri, 22 June 2012 15:40 Go to previous messageGo to next message
Marco Schulze is currently offline Marco SchulzeFriend
Messages: 5
Registered: July 2009
Junior Member
Hello *,

I just thought, I found it, but it seems to only work sometimes Crying or Very Sad

context.getCurrentNode().getPreviousSibling()
sometimes returns what I expect, but sometimes it returns null:

In this example, it works:

SELECT movie.|, movie.name FROM my.package.Movie movie


Again, the '|' represents the cursor position when hitting Ctrl+Space.

The following code outputs "currentNode.text: ," and "previousSibling.text: movie." as expected:

System.out.println("currentNode.text: " + context.getCurrentNode().getText());
INode previousSibling = context.getCurrentNode().getPreviousSibling();
if (previousSibling != null) {
	System.out.println("previousSibling.text: " + previousSibling.getText());
}


However, if my parsed text is slightly different, I don't get "movie." from anywhere:

SELECT movie.| FROM my.package.Movie movie


In this case,
context.getCurrentNode().getPreviousSibling()
returns null,
context.getCurrentNode().getText()
returns " " (one space) and
context.getCurrentNode().getNextSibling().getText()
returns "FROM". The part inbetween the space (before "movie." [or is it the one before the "FROM"?]) and the "FROM" is missing. Exactly the interesting text "movie." cannot be found anywhere anymore Crying or Very Sad

Is this maybe related to the unability of the parser to correctly parse the part that I'm looking for? If so, how can I still obtain it? Do I need to change my xtext grammar?

The xtext editor shows an error marker with the hint "no viable alternative at input FROM", hence I thought I might get the obviously unparseable text from the method
getSyntaxErrorMessage()

However, this method returns null for the current node, the parent, the previous sibling and the next sibling.

If I change my parsed text into this (note the comma):
SELECT movie.|, FROM my.package.Movie movie

Then the text is still not parsable and I still see the same error marker in the editor (additional to a second one saying "no viable alternative at input ,"), but
context.getCurrentNode().getPreviousSibling().getText()
is " movie." (which I expect).

If I could somehow reliably get the string before my cursor (in this example "movie" or "movie.", I could look up the cross reference via the following code:

SelectStatement selectStatement = (SelectStatement) context.getResource().getParseResult().getRootASTElement();
selectStatement.getFromClause().getFromEntries();


Does anyone have an idea? Do you need additional information from me?

Best regards, Marco Smile

[Updated on: Fri, 22 June 2012 15:42]

Report message to a moderator

Re: How to access a cross reference from a content-assist-provider? [message #890969 is a reply to message #890577] Sat, 23 June 2012 16:02 Go to previous message
Marco Schulze is currently offline Marco SchulzeFriend
Messages: 5
Registered: July 2009
Junior Member
Hello *,

I found it:
ContentAssistContext.getLastCompleteNode()
always provides access to the tokens parsed directly before the cursor position (even in case of parsing errors when getCurrentNode().getPreviousSibling() returns null). Via a loop over getPreviousSibling() until it returns null, the entire interesting expression can be obtained. I handle the referencing manually myself via searching through my model (rather than jumping along the cross-reference, as I first thought I could). I don't know, if this is the intended way to do it, but at least it works.

I just wanted to write a comment to www.eclipse.org/Xtext/documentation/2_0_0/150-contentassist.php (sorry - not allowed to post links here) but I first have to set up an OpenID server myself - I don't want others (especially not google or facebook) to be able to track my activities in the web so easily. How come, that I cannot login there with my Eclipse login, btw.? I mean the one I currently use to write into this forum.

Best regards, Marco Smile
Previous Topic:AutoConpletion and validation strange behaviour
Next Topic:Initializing field in generated class
Goto Forum:
  


Current Time: Thu Apr 25 07:44:59 GMT 2024

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

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

Back to the top