Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Accessing Code behind Model
Accessing Code behind Model [message #1777693] Fri, 01 December 2017 15:59 Go to next message
Oliver Blasius is currently offline Oliver BlasiusFriend
Messages: 2
Registered: December 2017
Junior Member
Hello there,

I am using the xText embedded Editor and want to access the "source code" behind the model.

I access the generated model with this code:

Testfile res = editor.getDocument().readOnly(new IUnitOfWork<Testfile, XtextResource>() {
			@Override
			public Testfile exec(XtextResource resource) throws Exception {
				return EcoreUtil.copy((Testfile) resource.getContents().get(0));
			}
		});


My attempt to get the text behind a model element failed, because the returned node is always null:
ICompositeNode node = NodeModelUtils.getNode(step);
String code = NodeModelUtils.getTokenText(node);

How can I get the text behind the model element?
Re: Accessing Code behind Model [message #1777784 is a reply to message #1777693] Mon, 04 December 2017 15:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
you can directly can call INode.getText()

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Accessing Code behind Model [message #1777786 is a reply to message #1777784] Mon, 04 December 2017 15:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
besides this the question would be why is node null?
of course you should not return eobjects from the readOnly methods.
but i have no clear view of what you are doing


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Accessing Code behind Model [message #1777835 is a reply to message #1777786] Tue, 05 December 2017 08:22 Go to previous messageGo to next message
Oliver Blasius is currently offline Oliver BlasiusFriend
Messages: 2
Registered: December 2017
Junior Member
your answer already helped me.
The error was that I was returning a copy of the model with EcoreUtil.copy
Now I am returning the model directly.
Testfile res = editor.getDocument().readOnly(new IUnitOfWork<Testfile, XtextResource>() {
			@Override
			public Testfile exec(XtextResource resource) throws Exception {
				return ((Testfile) resource.getContents().get(0));
			}
		});


But what do you mean with Quote:
of course you should not return eobjects from the readOnly methods.
?
I only need to read the model. I dont want to modify anything.
Re: Accessing Code behind Model [message #1777838 is a reply to message #1777835] Tue, 05 December 2017 08:51 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
no you need to do all calculation inside the readonly.

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:How to extend OpenGeneratedFileHandler to position the cursor in the right region
Next Topic:Formatting list of strings?
Goto Forum:
  


Current Time: Fri Apr 26 11:31:16 GMT 2024

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

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

Back to the top