Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to parse selected text in the XtextEditor
How to parse selected text in the XtextEditor [message #729763] Mon, 26 September 2011 23:06 Go to next message
David Struck is currently offline David StruckFriend
Messages: 29
Registered: July 2010
Junior Member
Hi all, I am running an Xtext application based off the Finite State Machine example described in the Xtext tutorials. In the editor, part of the text looks like this:

state active
  drawOpened => waitingForLight
  MyTest    => waitingForDraw
end

state waitingForDraw
  drawOpened => unlockedPanel
end

state unlockedPanel
  panelClosed => idle
end


These three State objects all define a list of Transitions where a Transition describes how an Event leads to another State. Let's say I have the text
drawOpened => unlockedPanel
highlighted. I know how to get the highlighted text's offset and length (via
getSourceViewer().getSelectedRange()
). Is there a way I can then parse the text such that I am given back a Transition object?

Thanks,

David
Re: How to parse selected text in the XtextEditor [message #729932 is a reply to message #729763] Tue, 27 September 2011 09:58 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
You will always have the parsed result (a model) available. You simply
ask for the semantic object associated with the position in text.
There is no need to again parse what was selected.

But, maybe I did not understand your question.
Regards
- henrik

On 9/27/11 1:06 AM, David Struck wrote:
> Hi all, I am running an Xtext application based off the Finite State
> Machine example described in the Xtext tutorials. In the editor, part of
> the text looks like this:
>
>
> state active
> drawOpened => waitingForLight
> MyTest => waitingForDraw
> end
>
> state waitingForDraw
> drawOpened => unlockedPanel
> end
>
> state unlockedPanel
> panelClosed => idle
> end
>
>
> These three State objects all define a list of Transitions where a
> Transition describes how an Event leads to another State. Let's say I
> have the text drawOpened => unlockedPanel highlighted. I know how to get
> the highlighted text's offset and length (via
> getSourceViewer().getSelectedRange()). Is there a way I can then parse
> the text such that I am given back a Transition object?
>
> Thanks,
>
> David
Re: How to parse selected text in the XtextEditor [message #730112 is a reply to message #729932] Tue, 27 September 2011 16:19 Go to previous messageGo to next message
David Struck is currently offline David StruckFriend
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 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
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 #730380 is a reply to message #729763] Wed, 28 September 2011 09:31 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
Have a look at the EObjectAtOffsetHelper.

Am 27.09.11 01:06, schrieb David Struck:
> Hi all, I am running an Xtext application based off the Finite State
> Machine example described in the Xtext tutorials. In the editor, part of
> the text looks like this:
>
>
> state active
> drawOpened => waitingForLight
> MyTest => waitingForDraw
> end
>
> state waitingForDraw
> drawOpened => unlockedPanel
> end
>
> state unlockedPanel
> panelClosed => idle
> end
>
>
> These three State objects all define a list of Transitions where a
> Transition describes how an Event leads to another State. Let's say I
> have the text drawOpened => unlockedPanel highlighted. I know how to get
> the highlighted text's offset and length (via
> getSourceViewer().getSelectedRange()). Is there a way I can then parse
> the text such that I am given back a Transition object?
>
> Thanks,
>
> David


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


---
Get professional support from the Xtext committers at www.typefox.io
Re: How to parse selected text in the XtextEditor [message #730622 is a reply to message #730380] Wed, 28 September 2011 17:57 Go to previous messageGo to next message
David Struck is currently offline David StruckFriend
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 Go to previous message
Henrik Lindberg is currently offline Henrik LindbergFriend
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
Previous Topic:Search results and DefaultDescriptionLabelProvider
Next Topic:Re: Referencing models defined in files outside of eclipse
Goto Forum:
  


Current Time: Fri Mar 29 15:09:16 GMT 2024

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

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

Back to the top