Home » Modeling » TMF (Xtext) » How to parse selected text in the XtextEditor
| |
Re: How to parse selected text in the XtextEditor [message #730112 is a reply to message #729932] |
Tue, 27 September 2011 16:19 |
David Struck Messages: 29 Registered: July 2010 |
Junior Member |
|
|
Hi Henrik, thanks for your response!
I think your suggestion mostly gets at the solution, but I still have a question: How do I find the semantic element located at an offset from a class that extends XtextEditor? One way I found to do it is:
@Inject
protected EObjectAtOffsetHelper eObjectAtOffsetHelper;
private EObject getContext(final int offset) {
return this.getDocument().readOnly(new IUnitOfWork<EObject, XtextResource>() {
public EObject exec(XtextResource localResource) throws Exception {
return eObjectAtOffsetHelper.resolveElementAt(localResource, offset);
}
});
}
But that seems a little clunky. Is there a better way?
Actually, I think what's more important is that I find the *node* that is at a certain position. I know that I can use NodeModelUtils to find a leaf node at an offset, but I need to pass a node into that method. How do I access a node from an extension of XtextEditor? (Once I have that node, I figure I should call getRootNode on it, then pass that into findLeafNodeAtOffset.)
Let's say I've figured the above out and I have the node that holds the Transition. (I'm using the scenario where "drawOpened => unlockedPanel" is highlighted as I said in my original post.) I want to check that the node's position in the Xtext editor matches the currently selected region. But when I call getLength on the node, it returns 10. (I presume this occurs because "drawOpened" is 10 characters long.) Why does it not return 27, which is the length of the string, "drawOpened => unlockedPanel"?
I think if I can get these answers, I can push forward.
Thanks,
David
|
|
|
Re: How to parse selected text in the XtextEditor [message #730219 is a reply to message #730112] |
Tue, 27 September 2011 22:04 |
Henrik Lindberg Messages: 2509 Registered: July 2009 |
Senior Member |
|
|
On 9/27/11 6:19 PM, David Struck wrote:
> Hi Henrik, thanks for your response!
>
> I think your suggestion mostly gets at the solution, but I still have a
> question: How do I find the semantic element located at an offset from a
> class that extends XtextEditor? One way I found to do it is:
>
>
> @Inject
> protected EObjectAtOffsetHelper eObjectAtOffsetHelper;
>
> private EObject getContext(final int offset) {
> return this.getDocument().readOnly(new IUnitOfWork<EObject,
> XtextResource>() {
> public EObject exec(XtextResource localResource) throws Exception {
> return eObjectAtOffsetHelper.resolveElementAt(localResource, offset);
> }
> });
> }
>
>
> But that seems a little clunky. Is there a better way?
>
Well, you need to make sure you get the text you intend is in the
correct context and that it does not change while you are computing the
information. So, yes, a bit of code is required to do that correctly.
> Actually, I think what's more important is that I find the *node* that
> is at a certain position. I know that I can use NodeModelUtils to find a
> leaf node at an offset, but I need to pass a node into that method. How
> do I access a node from an extension of XtextEditor? (Once I have that
> node, I figure I should call getRootNode on it, then pass that into
> findLeafNodeAtOffset.)
>
Once you have the/a semantic object, you can find its node (using
NodeModelUtils).
> Let's say I've figured the above out and I have the node that holds the
> Transition. (I'm using the scenario where "drawOpened => unlockedPanel"
> is highlighted as I said in my original post.) I want to check that the
> node's position in the Xtext editor matches the currently selected
> region. But when I call getLength on the node, it returns 10. (I presume
> this occurs because "drawOpened" is 10 characters long.) Why does it not
> return 27, which is the length of the string, "drawOpened =>
> unlockedPanel"?
>
The node you are looking for is probably (one of) the "drawOpened"
node's parents.
Hope that helps.
- henrik
|
|
| |
Re: How to parse selected text in the XtextEditor [message #730622 is a reply to message #730380] |
Wed, 28 September 2011 17:57 |
David Struck Messages: 29 Registered: July 2010 |
Junior Member |
|
|
Henrik and Jan, thanks again for all your help!
I was just told that I need to be able to parse a random string of text independent from the current Xtext editor. For example, we may want the user to be able to right-click on the Xtext editor and present him/her with a text box where he/she can enter a random string of text. We would then parse that string. So, if the user entered "drawOpened", we would return the Event instance named "drawOpened". If the user entered "drawOpened => waitingForLight", we would return the appropriate Transition instance. (For strings that don't match anything, we would return null.)
I noticed that I can inject an IParser into our subclass of XtextEditor then access its grammar. For example:
@Inject
protected IParser parser;
private void foo(String text) {
FSMParser antlrParser = (FSMParser) parser;
ParserRule rule = antlrParser.getGrammarAccess().getTransitionRule();
ParseResult result = (ParseResult) antlrParser.parse(rule, new StringReader(text));
if(!result.hasSyntaxErrors()) {
Transition transition = (Transition) result.getRootASTElement();
}
}
The problem with this approach is two-fold:
1) It is not general enough, e.g. I couldn't pass a string representing an Event to it and parse it
2) The Transition object I get back returns null for any other EObjects it references. (In this case, that would be the Event and the State associated with the Transition.)
Is there anyway to invoke a more general parser?
Thanks,
David
|
|
|
Re: How to parse selected text in the XtextEditor [message #730695 is a reply to message #730622] |
Wed, 28 September 2011 21:41 |
Henrik Lindberg Messages: 2509 Registered: July 2009 |
Senior Member |
|
|
Don't really understand why you want the editor to work that way (seems
odd) - you either edit text, or find declarations/references.
If you are going down this route - when you call parse you must
naturally direct the second parser with which rule it should start with.
What you get back is not linked - so naturally all references are null
at that point. You would need to also run linking. As you only have a
snippet, and not the full context it could get very complicated. (But
again, I don't know how general this mechanism needs to be given your
requirements).
- henrik
On 9/28/11 7:57 PM, David Struck wrote:
> Henrik and Jan, thanks again for all your help!
>
> I was just told that I need to be able to parse a random string of text
> independent from the current Xtext editor. For example, we may want the
> user to be able to right-click on the Xtext editor and present him/her
> with a text box where he/she can enter a random string of text. We would
> then parse that string. So, if the user entered "drawOpened", we would
> return the Event instance named "drawOpened". If the user entered
> "drawOpened => waitingForLight", we would return the appropriate
> Transition instance. (For strings that don't match anything, we would
> return null.)
>
> I noticed that I can inject an IParser into our subclass of XtextEditor
> then access its grammar. For example:
>
>
> @Inject
> protected IParser parser;
>
> private void foo(String text) {
> FSMParser antlrParser = (FSMParser) parser;
> ParserRule rule = antlrParser.getGrammarAccess().getTransitionRule();
> ParseResult result = (ParseResult) antlrParser.parse(rule, new
> StringReader(text));
> if(!result.hasSyntaxErrors()) {
> Transition transition = (Transition) result.getRootASTElement();
> }
> }
>
>
> The problem with this approach is two-fold:
>
> 1) It is not general enough, e.g. I couldn't pass a string representing
> an Event to it and parse it
>
> 2) The Transition object I get back returns null for any other EObjects
> it references. (In this case, that would be the Event and the State
> associated with the Transition.)
>
> Is there anyway to invoke a more general parser?
>
> Thanks,
>
> David
|
|
|
Goto Forum:
Current Time: Fri Mar 29 15:09:16 GMT 2024
Powered by FUDForum. Page generated in 0.03219 seconds
|