Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [xtext 2] How to get EObject from an specific line of an XtextEditor?
[xtext 2] How to get EObject from an specific line of an XtextEditor? [message #661690] Sat, 26 March 2011 09:00 Go to next message
Alex Ruiz is currently offline Alex RuizFriend
Messages: 103
Registered: March 2011
Senior Member
Greetings,

How can I get the EObject that is the result of parsing a particular line in an XtextEditor?

So far I have this:

    IXtextDocument document = editor.getDocument();
    EList<EObject> objects = document.readOnly(new IUnitOfWork<EList<EObject>, XtextResource>() {
      @Override public EList<EObject> exec(XtextResource state) throws Exception {
        return state.getContents();
      }
    });


but it returns all the EObjects from the contents of the XtextEditor, which is useful to me as well. The only missing part is getting the EObject that belongs to a line in the XtextEditor (e.g. EObject from line 6 in an XtextEditor.)

Any help will be greatly appreciated Smile

Thanks,
-Alex


[Updated on: Sun, 03 April 2011 08:16]

Report message to a moderator

Re: How to get EObject from an specific line of an XtextEditor? [message #661693 is a reply to message #661690] Sat, 26 March 2011 10:19 Go to previous messageGo to next message
Alex Ruiz is currently offline Alex RuizFriend
Messages: 103
Registered: March 2011
Senior Member
I actually found a way. Please let me know if there is a better one Smile

  @Inject private ContentAssistContext.Factory contextFactory;

  /** {@inheritDoc} */
  @Override protected void someMethod(final XtextEditor editor) {
    StyledText styledText = styledTextFrom(editor);
    IXtextDocument document = editor.getDocument();
    final int offset = styledText.getCaretOffset();
    ContentAssistContext[] contextArray = document.readOnly(new IUnitOfWork<ContentAssistContext[], XtextResource>() {
      @Override public ContentAssistContext[] exec(XtextResource state) throws Exception {
        return contextFactory.create(editor.getInternalSourceViewer(), offset, state);
      }
    });


Then the relevant EObject would be in the first ContentAssistContext of the returned array.
Re: How to get EObject from an specific line of an XtextEditor? [message #661701 is a reply to message #661693] Sat, 26 March 2011 12:04 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

have a look and use the EObjectAtOffsetHelper. Also you should do whatever you want to do with the objects *within* the unit of work. Never return a resource/objects from that unit. You risk the model's integrity.

Alex
Re: How to get EObject from an specific line of an XtextEditor? [message #661703 is a reply to message #661690] Sat, 26 March 2011 12:41 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
You can get a Node from an EObject (it is in an adapter), or the EObject
from a Node.
You can traverse the Node tree, or the EObject tree (whatever works best
in your use case).

To make it general you probably have to deal with things like:
- Several EObjects on one line
- An EObject that is on several lines
- An EObject ending on one line, followed by one or more EObjects.
- Hidden text (WS, comments etc.)

- henrik

On 3/26/11 10:01 AM, Alex Ruiz wrote:
> Greetings,
>
> How can I get the EObject that is the result of parsing a particular
> line in an XtextEditor?
>
> So far I have this:
>
>
> IXtextDocument document = editor.getDocument();
> EList<EObject> objects = document.readOnly(new
> IUnitOfWork<EList<EObject>, XtextResource>() {
> @Override public EList<EObject> exec(XtextResource state) throws
> Exception {
> return state.getContents();
> }
> });
>
>
> but it returns all the EObjects from the contents of the XtextEditor,
> which is useful to me as well. The only missing part is getting the
> EObject that belongs to a line in the XtextEditor (e.g. EObject from
> line 6 in an XtextEditor.)
>
> Any help will be greatly appreciated :)
>
> Thanks,
> -Alex
>
>
>
Re: How to get EObject from an specific line of an XtextEditor? [message #661721 is a reply to message #661690] Sat, 26 March 2011 17:19 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Alex,

please have a look at the EObjectAtOffsetHelper which should do the
trick for you. Important to note: You should never return an EObject
from the unit of work. All the work should be done inside this unit.
That's why it is called unit of _work_ and not EObjectFinder or
something. See also the javadoc in the IUnitOfWork.

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


Am 26.03.11 10:01, schrieb Alex Ruiz:
> Greetings,
>
> How can I get the EObject that is the result of parsing a particular
> line in an XtextEditor?
>
> So far I have this:
>
>
> IXtextDocument document = editor.getDocument();
> EList<EObject> objects = document.readOnly(new
> IUnitOfWork<EList<EObject>, XtextResource>() {
> @Override public EList<EObject> exec(XtextResource state) throws
> Exception {
> return state.getContents();
> }
> });
>
>
> but it returns all the EObjects from the contents of the XtextEditor,
> which is useful to me as well. The only missing part is getting the
> EObject that belongs to a line in the XtextEditor (e.g. EObject from
> line 6 in an XtextEditor.)
>
> Any help will be greatly appreciated :)
>
> Thanks,
> -Alex
>
>
>
Re: How to get EObject from an specific line of an XtextEditor? [message #661749 is a reply to message #661721] Sun, 27 March 2011 07:26 Go to previous message
Alex Ruiz is currently offline Alex RuizFriend
Messages: 103
Registered: March 2011
Senior Member
Thanks so much Alex, Henrik and Sebastian for the prompt reply Smile

EObjectAtOffsetHelper did the trick!

Cheers!
-Alex
Previous Topic:Extending grammars and runtime environments
Next Topic:Problem with the editor
Goto Forum:
  


Current Time: Fri Mar 29 13:28:58 GMT 2024

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

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

Back to the top