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 |
Marco Schulze 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
|
|
|
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 |
Marco Schulze Messages: 5 Registered: July 2009 |
Junior Member |
|
|
Hello *,
I just thought, I found it, but it seems to only work sometimes
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
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
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
[Updated on: Fri, 22 June 2012 15:42] Report message to a moderator
|
|
| |
Goto Forum:
Current Time: Thu Apr 25 07:44:59 GMT 2024
Powered by FUDForum. Page generated in 0.02744 seconds
|