Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Acessing Editor contents
Acessing Editor contents [message #668439] Thu, 05 May 2011 16:41 Go to next message
Oscar Rodriguez is currently offline Oscar RodriguezFriend
Messages: 5
Registered: May 2011
Junior Member
Hello,

I need to extend xtext functionality. I completed the coloring of source code and the syntax checking, but need to access the editor contents. I need to load the editor contents from a buffer and also to have access to that content all the time among other methods to fit xtext it into my eclipse application.

Can anyone provide an example on how to do it?

I am looking for something like:

myExtendedXTextClass x = new MyExtendedXTextClass();
x.loadContentFromStringBuffer("My source code contents");

String x=x.getEditorContent();

Thanks in adavance.

/Oscar
Re: Acessing Editor contents [message #668453 is a reply to message #668439] Thu, 05 May 2011 16:58 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Take a look at org.eclipse.xtext.junit.AbstractXtextTests which has a
method for returning a resource given a string in your DSL language.

Naturally, the AbstractXtextTests class has lots of functionality
relating to tests, but that method should give you a good starting point
for what you need to do in your code.

- henrik

On 5/5/11 6:41 PM, Oscar Rodriguez wrote:
> Hello,
>
> I need to extend xtext functionality. I completed the coloring of source
> code and the syntax checking, but need to access the editor contents. I
> need to load the editor contents from a buffer and also to have access
> to that content all the time among other methods to fit xtext it into my
> eclipse application.
>
> Can anyone provide an example on how to do it?
>
> I am looking for something like:
>
> myExtendedXTextClass x = new MyExtendedXTextClass();
> x.loadContentFromStringBuffer("My source code contents");
>
> String x=x.getEditorContent();
>
> Thanks in adavance.
>
> /Oscar
Re: Acessing Editor contents [message #668466 is a reply to message #668439] Thu, 05 May 2011 19:02 Go to previous messageGo to next message
Oscar Rodriguez is currently offline Oscar RodriguezFriend
Messages: 5
Registered: May 2011
Junior Member
Henri,

Thanks for your reply.

I am pretty new with xtext. I reviewed xtext JUnit and honestly did not find anything useful.

Let explain better what my problem is:

My own language is already modeled in xtext. Syntax Coloring is fine as well. That part is more or less solved.

What I need is to get access to my source code in the editor. To be clear, putting it into a known language: I need access to the actual C code inside the xtext editor (whatever editor object it is), not to the xtext's C language definition.

I can see that there are URIs everywhere to load resoruces, not sure if they are used only for loading the language definition files or the actual source code files.. but anyway, I guess I cannot use URIs as all my data is in String buffers (not formated in anything but in my own programming language). What I need is two things:

To load the editor content from a String when xtext editor gains focus, and to get the editor content when it losses focus.

Is there a way to do it?

Thanks,

/Oscar
Re: Acessing Editor contents [message #668481 is a reply to message #668466] Thu, 05 May 2011 19:49 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Hi,
not sure what it is you are trying to do... but:
- You can naturally always find the text of what is in any open editor.
Either as text, or the parsed model.
- If you want to parse a file, or a string you do that simply by opening
it as an EMF resource (as the text->parser->model transformation is
registered by file extension).

So... if you want the text of the editor - the Eclipse framework will
give you handles to the editor, from which you get its input, a document
etc. and from those you can either open the same file (looking at the
URI in the input), or get the actual XtextDocument, then get the
XtextResource.

Is the functionality invoked from a menu, button or is it triggered by
something else? Could you explain the use case a bit more? I find that
one almost never have to get the actual text in some complicated way...
(so hence my questions).

Regards
- henrik

On 5/5/11 9:02 PM, Oscar Rodriguez wrote:
> Henri,
>
> Thanks for your reply.
>
> I am pretty new with xtext. I reviewed xtext JUnit and honestly did not
> find anything useful.
>
> Let explain better what my problem is:
>
> My own language is already modeled in xtext. Syntax Coloring is fine as
> well. That part is more or less solved.
>
> What I need is to get access to my source code in the editor. To be
> clear, putting it into a known language: I need access to the actual C
> code inside the xtext editor (whatever editor object it is), not to the
> xtext's C language definition.
>
> I can see that there are URIs everywhere to load resoruces, not sure if
> they are used only for loading the language definition files or the
> actual source code files.. but anyway, I guess I cannot use URIs as all
> my data is in String buffers (not formated in anything but in my own
> programming language). What I need is two things:
>
> To load the editor content from a String when xtext editor gains focus,
> and to get the editor content when it losses focus.
>
> Is there a way to do it?
>
> Thanks,
>
> /Oscar
Re: Acessing Editor contents [message #668528 is a reply to message #668481] Fri, 06 May 2011 06:25 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

maybe EditorUtils.getActiveXtextEditor() is helpful for you. It returns an XtextEditor which you can ask for its document (getDocument). This opens all the doors you want (get() for getting the content as String, readOnly()... for obtaining information from the semantic model, modify() for making changes to the semantic model).

Alex
Re: Acessing Editor contents [message #668725 is a reply to message #668481] Fri, 06 May 2011 20:42 Go to previous messageGo to next message
Oscar Rodriguez is currently offline Oscar RodriguezFriend
Messages: 5
Registered: May 2011
Junior Member
Hi Hernik,

Thanks again for replying.
Let me give a clearer picture of what I need to do:

My application requires that the actual file contents can be changed from several sources semi-concurrently: from the user editing directly in xtext's editor or from other threads running in the application. It forces to have an editor that does not really edit files, but edit buffers and those buffers are passed to another plug-in that keeps the consistency of the file contents. That plug-in is in charge of sending the buffers to xtext editor and receiving them and merging with the other data produced by other elements of the application.

So, what I need is:

1. An xtext constructor that, instead opening a file, requests a buffer from the plug-in and displays it in the editor.
2. Gain access to eclipse's get focus event, so that, xtext editor content can be reloaded from a buffer requested to the plug-in.
3. Gain access to the lost focus event so that the editor data is flushed into the plugin and gets merged with the other data.

Sumarizing, xtext will never really work with files, and need free access to the editor content. No URIs for loading either.

I found org.eclipse.xtext.example.ui.autoedit example, which seem to have access to the text implmenting IAutoEditStrategy, however, I am still lost.


Any idea?

Thanks,

/Oscar

Henrik Lindberg wrote on Thu, 05 May 2011 15:49
Hi,
not sure what it is you are trying to do... but:
- You can naturally always find the text of what is in any open editor.
Either as text, or the parsed model.
- If you want to parse a file, or a string you do that simply by opening
it as an EMF resource (as the text->parser->model transformation is
registered by file extension).

So... if you want the text of the editor - the Eclipse framework will
give you handles to the editor, from which you get its input, a document
etc. and from those you can either open the same file (looking at the
URI in the input), or get the actual XtextDocument, then get the
XtextResource.

Is the functionality invoked from a menu, button or is it triggered by
something else? Could you explain the use case a bit more? I find that
one almost never have to get the actual text in some complicated way...
(so hence my questions).

Regards
- henrik

On 5/5/11 9:02 PM, Oscar Rodriguez wrote:
> Henri,
>
> Thanks for your reply.
>
> I am pretty new with xtext. I reviewed xtext JUnit and honestly did not
> find anything useful.
>
> Let explain better what my problem is:
>
> My own language is already modeled in xtext. Syntax Coloring is fine as
> well. That part is more or less solved.
>
> What I need is to get access to my source code in the editor. To be
> clear, putting it into a known language: I need access to the actual C
> code inside the xtext editor (whatever editor object it is), not to the
> xtext's C language definition.
>
> I can see that there are URIs everywhere to load resoruces, not sure if
> they are used only for loading the language definition files or the
> actual source code files.. but anyway, I guess I cannot use URIs as all
> my data is in String buffers (not formated in anything but in my own
> programming language). What I need is two things:
>
> To load the editor content from a String when xtext editor gains focus,
> and to get the editor content when it losses focus.
>
> Is there a way to do it?
>
> Thanks,
>
> /Oscar

[Updated on: Fri, 06 May 2011 20:45]

Report message to a moderator

Re: Acessing Editor contents [message #668726 is a reply to message #668528] Fri, 06 May 2011 20:47 Go to previous messageGo to next message
Oscar Rodriguez is currently offline Oscar RodriguezFriend
Messages: 5
Registered: May 2011
Junior Member
Let me check this information, thanks a lot Alex!
Re: Acessing Editor contents [message #668754 is a reply to message #668481] Sat, 07 May 2011 02:40 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Sounds very complex. Are you using EMF for this in any way?

Much of the Xtext functionality depends on the content being an eclipse
Resource (IFile etc.) since this is used for many of the features you
expect in a normal eclipse editor (markers being one of them).

I found this out when working with external files, and my solution was
to link such files to the workspace in a hidden project.

I am afraid I am not knowledgeable enough to give you advice on how to
best map your concurrently modified model to URI's, EMF Resource,
storages and Eclipse resources.

Regards
- henrik

On 5/6/11 10:24 PM, Oscar Rodriguez wrote:
> Henrik,
>
> Thanks a lot for taking the time trying to help me.
>
> Let me try to summarize what I need to do:
>
> I have files that can be modified in several ways and
> somehow semi-concurrently. It means that a file can be
> changed by a user directly in xtext editor and by other
> threads running underneath.
>
> Said that, xtext will NEVER open the files directly, but a
> file handler plug-in will, in order to keep the data
> consistency given this environment. That is why I need
> xtext editor to load its contents from a buffer instead of
> a file (in the constructor). The content will be flushed to
> the file handler plugins when xtext losses focus, and will
> reload its contents (from a buffer) when it gains focus
> again.
>
> So, I need 3 things:
>
> 1. A constructor to feed the contents of the editor from
> a buffer
> 2. To catch the lost focus event to flush the editor
> content to the file handler plug-in
> 3. To catch the gain focus event to reload the editor's
> contents from the file handler buffer.
>
> I found org.eclipse.xtext.example.ui.autoedit example,
> which seem to have access to the text implmenting
> IAutoEditStrategy, however, I am still lost.
>
> thanks!
>
> /Oscar
Re: Acessing Editor contents [message #668789 is a reply to message #668725] Sat, 07 May 2011 13:54 Go to previous message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

are you sure you need to do it in such a complicated way. Try opening your model in the Xtext editor and parallel in the sample ecore editor (open with). And no do modifications in both editors and save them. This is what the resource implementation is for. Obtain the document and do read- and modify-units-of-work as you require. The framework should ensure that all participants are working in a consistent way.

Alex
Previous Topic:[Xtext 2.0] How to loose up member order in grammar
Next Topic:[Xtext 2.0] How to loose up member order in grammar
Goto Forum:
  


Current Time: Sat Apr 27 03:24:10 GMT 2024

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

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

Back to the top