Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Refactoring for xtextEditors
Refactoring for xtextEditors [message #550166] Thu, 29 July 2010 12:31 Go to next message
John Locke is currently offline John LockeFriend
Messages: 23
Registered: July 2010
Junior Member
Hey,

i'am working on refactoring interface for xtextEditors.

At the moment i can execute refactorings on xtextEditors. I extract the EObject of the editor with the ParseTree Class. Then i use LTK to execute the refactoring.

The problem is that the changes of the refactoring are not displayed in the xtexteditor. What do i have to do to view the changes in the xtextEditor?

I will be very thankful for any hints,

John
Re: Refactoring for xtextEditors [message #550395 is a reply to message #550166] Fri, 30 July 2010 08:59 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi John,

please use the IDocumentEditor or the SemanticModificationWrapper as a
starting point to perform modifications on the document itself.

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

Am 29.07.10 14:31, schrieb john_locke@gmx.de:
> Hey,
>
> i'am working on refactoring interface for xtextEditors.
> At the moment i can execute refactorings on xtextEditors. I extract the
> EObject of the editor with the ParseTree Class. Then i use LTK to
> execute the refactoring.
>
> The problem is that the changes of the refactoring are not displayed in
> the xtexteditor. What do i have to do to view the changes in the
> xtextEditor?
>
> I will be very thankful for any hints,
>
> John
Re: Refactoring for xtextEditors [message #550436 is a reply to message #550395] Fri, 30 July 2010 11:22 Go to previous messageGo to next message
John Locke is currently offline John LockeFriend
Messages: 23
Registered: July 2010
Junior Member
Hi Sebastian,

thanks for your answer.

Here are some code snippets.

First step: I use the IXtextDocument to get the EObject.

xEditor.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>() {
	public void process(XtextResource resource) throws Exception {
		IParseResult parseRes = resource.getParseResult();
		if(parseRes==null)
			return;
		CompositeNode rootNode = parseRes.getRootNode();
		int offset = tSelection.getOffset();
		AbstractNode node = ParseTreeUtil.getCurrentOrFollowingNodeByOffset(rootNode, offset);
		EObject object = NodeUtil.getNearestSemanticObject(node);
                          // ...
	}
});


Second step: EMF Refactor(LTK) i needs the EditingDomain for the next step.

...
ResourceSet synRset = selection.get(0).eResource()
	.getResourceSet();
TransactionalEditingDomain domain = TransactionalEditingDomain.Factory.INSTANCE
	.createEditingDomain(synRset);
...


Third step: EMF Refactor calls LTK with object and domain to generate the changes.

Is this proceeding okay or not? The changes are applied to the synRset, but the changes are not visible in the xtextEditor. What is the problem of this approach?

Regards,
John
Re: Refactoring for xtextEditors [message #550502 is a reply to message #550436] Fri, 30 July 2010 14:21 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi John,

xtextDocument.readOnly(..) indicates that the resource should not be
modified in the UnitOfWork, doesn't it ;-)
Please use the IDocumentEditor instead. It offers a similar API:
IDocumentEditor.process(unitOfWork, xtextDocument);

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

Am 30.07.10 13:22, schrieb john_locke@gmx.de:
> Hi Sebastian,
>
> thanks for your answer.
> Here are some code snippets.
>
> First step: I use the IXtextDocument to get the EObject.
>
>
> xEditor.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>() {
> public void process(XtextResource resource) throws Exception {
> IParseResult parseRes = resource.getParseResult();
> if(parseRes==null)
> return;
> CompositeNode rootNode = parseRes.getRootNode();
> int offset = tSelection.getOffset();
> AbstractNode node =
> ParseTreeUtil.getCurrentOrFollowingNodeByOffset(rootNode, offset);
> EObject object = NodeUtil.getNearestSemanticObject(node);
> // ...
> }
> });
>
>
> Second step: EMF Refactor(LTK) i needs the EditingDomain for the next step.
>
>
> ..
> ResourceSet synRset = selection.get(0).eResource()
> .getResourceSet();
> TransactionalEditingDomain domain =
> TransactionalEditingDomain.Factory.INSTANCE
> .createEditingDomain(synRset);
> ..
>
>
> Third step: EMF Refactor calls LTK with object and domain to generate
> the changes.
>
> Is this proceeding okay or not? The changes are applied to the synRset,
> but the changes are not visible in the xtextEditor. What is the problem
> of this approach?
>
> Regards,
> John
Re: Refactoring for xtextEditors [message #551100 is a reply to message #550166] Thu, 05 August 2010 08:45 Go to previous messageGo to next message
John Locke is currently offline John LockeFriend
Messages: 23
Registered: July 2010
Junior Member
Hi,

thanks for your answer. The changes are not made in the IUnitOfWork. I only use this to get the EObject of the selectet element.

The changes are made by EMFRefactor (LTK). LTK uses the ecore model to perform the changes. My approache is to use this ecore model.

Is it possible to change the document not directly but through the ecore model?

If i apply the refactorings on the tree-based editor and save, the changes are also made in the xtext-document.

Regards,
John
Re: Refactoring for xtextEditors [message #551708 is a reply to message #550166] Mon, 09 August 2010 09:48 Go to previous messageGo to next message
John Locke is currently offline John LockeFriend
Messages: 23
Registered: July 2010
Junior Member
Hi,

is it possible to change an XtextRessource outside an IUnitOfWork?

Regards,
John
Re: Refactoring for xtextEditors [message #551760 is a reply to message #551708] Mon, 09 August 2010 13:32 Go to previous messageGo to next message
John Locke is currently offline John LockeFriend
Messages: 23
Registered: July 2010
Junior Member
Hi,

i try to change the document like this way.

String erg = ...

IDocumentEditor documentEditor = new DefaultDocumentEditor();

documentEditor.process(new IUnitOfWork.Void<XtextResource>() {
       public void process(XtextResource resource) {
    	   resource.update(0, erg.length(), erg);
       }
}, xEditor.getDocument());


But now i get the message: "cannot modify resource set without a write transaction"
How can i solve this problem?

Regards,
John
Re: Refactoring for xtextEditors [message #552840 is a reply to message #550166] Fri, 13 August 2010 17:23 Go to previous messageGo to next message
John Locke is currently offline John LockeFriend
Messages: 23
Registered: July 2010
Junior Member
Hi,

here is my basic idea of the implementation for Xtext.
I want to use EMFRefactor (uses internal Language Toolkit) to apply the refactorings.
EMFRefactor needs the EObject on which the refactoring should be applied. The EditingDomain of this EObject is also needed.

1) I use the ParseTree to get the EObject of the selected text area in the XtextEditor.
if (selection instanceof TextSelection) {
	final TextSelection tSelection = (TextSelection) selection;
	XtextEditor xEditor = EditorUtils.getActiveXtextEditor();
	if (xEditor != null) {
		final List<Object> list = new ArrayList<Object>();
		xEditor.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>() {
			public void process(XtextResource resource) throws Exception {
				IParseResult parseRes = resource.getParseResult();
				if(parseRes==null)
					return;
				CompositeNode rootNode = parseRes.getRootNode();
				int offset = tSelection.getOffset();
				AbstractNode node = ParseTreeUtil.getCurrentOrFollowingNodeByOffset(rootNode, offset);
				EObject object = NodeUtil.getNearestSemanticObject(node);
				list.add(object);
			}
		});
		return list.toArray();
	}
}


2) I create the EditingDomain for the given EObject.
if(null == editingDomain){
	XtextResource xResource = (XtextResource) selection.get(0).eResource();
	editingDomain = TransactionalEditingDomain.Factory.INSTANCE
		.createEditingDomain(xResource.getResourceSet());
}

I use create, because getEdtingDomain(...) returns always "null".

3) EMFRefactor performs the changes with LTK.

4) I want to update the XtextEditor.
XtextResource xResource = (XtextResource) selection.get(0).eResource();
String erg = xResource.getSerializer().serialize(xResource.getContents().get(0));

XtextEditor xEditor = EditorUtils.getActiveXtextEditor();
IXtextDocument xDocument = xEditor.getDocument();
xDocument.replace(0, xDocument.getLength(), erg);


The following exception appears after step 4).
!ENTRY org.eclipse.core.jobs 4 2 2010-08-13 18:45:45.233
!MESSAGE An internal error occurred during: "XtextReconcilerJob".
!STACK 0
java.lang.IllegalStateException: Cannot modify resource set without a write transaction
	at org.eclipse.emf.transaction.impl.TransactionChangeRecorder.assertWriting(TransactionChangeRecorder.java:348)
	at org.eclipse.emf.transaction.impl.TransactionChangeRecorder.appendNotification(TransactionChangeRecorder.java:302)
	at org.eclipse.emf.transaction.impl.TransactionChangeRecorder.processResourceNotification(TransactionChangeRecorder.java:272)
	at org.eclipse.emf.transaction.impl.TransactionChangeRecorder.notifyChanged(TransactionChangeRecorder.java:238)
	at org.eclipse.emf.common.notify.impl.BasicNotifierImpl.eNotify(BasicNotifierImpl.java:380)
	at org.eclipse.emf.common.notify.impl.NotifyingListImpl.dispatchNotification(NotifyingListImpl.java:267)
	at org.eclipse.emf.common.notify.impl.NotifyingListImpl.clear(NotifyingListImpl.java:1120)
	at org.eclipse.xtext.resource.XtextResource.clearInternalState(XtextResource.java:230)
	at org.eclipse.xtext.resource.XtextResource.reparse(XtextResource.java:167)
	at org.eclipse.xtext.ui.editor.model.XtextDocument$XtextDocumentLocker.modify(XtextDocument.java:152)
	at org.eclipse.xtext.ui.editor.model.XtextDocument.modify(XtextDocument.java:62)
	at org.eclipse.xtext.ui.editor.reconciler.XtextDocumentReconcileStrategy.reconcile(XtextDocumentReconcileStrategy.java:27)
	at org.eclipse.xtext.ui.editor.reconciler.XtextReconciler.run(XtextReconciler.java:179)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)


What is the problem or the fault of my idea/implementation?

I will be very thankful for any hints,
John
Re: Refactoring for xtextEditors [message #552850 is a reply to message #552840] Fri, 13 August 2010 19:01 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi John,

I guess you have to remove the editing domain after you performed the
refactoring. Furthermore it is strongly recommend - guess what - to
perform the changes by means of the IDocumentEditor. Why don't you just
put all your code into an IUnitOfWork (by the way: according to the
docs, you should never ever return an eObject from a unit of work).

@Inject
IDocumentEditor editor;

editor.process(new IUnitOfWork.Void<XtextResource>() {
void process(XtextResource res) {
try {
.. attach editing domain
.. find objects
.. perform refactoring
} finally {
.. remove editing domain
}
}
}, document);

Just as a sidenode: You should have a look at the EObjectAtOffsetHelper.

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

Am 13.08.10 19:23, schrieb John:
> Hi,
>
> here is my basic idea of the implementation for Xtext.
> I want to use EMFRefactor (uses internal Language Toolkit) to apply the
> refactorings.
> EMFRefactor needs the EObject on which the refactoring should be
> applied. The EditingDomain of this EObject is also needed.
>
> 1) I use the ParseTree to get the EObject of the selected text area in
> the XtextEditor.
>
> if (selection instanceof TextSelection) {
> final TextSelection tSelection = (TextSelection) selection;
> XtextEditor xEditor = EditorUtils.getActiveXtextEditor();
> if (xEditor != null) {
> final List<Object> list = new ArrayList<Object>();
> xEditor.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>() {
> public void process(XtextResource resource) throws Exception {
> IParseResult parseRes = resource.getParseResult();
> if(parseRes==null)
> return;
> CompositeNode rootNode = parseRes.getRootNode();
> int offset = tSelection.getOffset();
> AbstractNode node =
> ParseTreeUtil.getCurrentOrFollowingNodeByOffset(rootNode, offset);
> EObject object = NodeUtil.getNearestSemanticObject(node);
> list.add(object);
> }
> });
> return list.toArray();
> }
> }
>
>
> 2) I create the EditingDomain for the given EObject.
>
> if(null == editingDomain){
> XtextResource xResource = (XtextResource) selection.get(0).eResource();
> editingDomain = TransactionalEditingDomain.Factory.INSTANCE
> .createEditingDomain(xResource.getResourceSet());
> }
>
> I use create, because getEdtingDomain(...) returns always "null".
>
> 3) EMFRefactor performs the changes with LTK.
> 4) I want to update the XtextEditor.
>
> XtextResource xResource = (XtextResource) selection.get(0).eResource();
> String erg =
> xResource.getSerializer().serialize(xResource.getContents(). get(0));
>
> XtextEditor xEditor = EditorUtils.getActiveXtextEditor();
> IXtextDocument xDocument = xEditor.getDocument();
> xDocument.replace(0, xDocument.getLength(), erg);
>
>
> The following exception appears after step 4).
>
> !ENTRY org.eclipse.core.jobs 4 2 2010-08-13 18:45:45.233
> !MESSAGE An internal error occurred during: "XtextReconcilerJob".
> !STACK 0
> java.lang.IllegalStateException: Cannot modify resource set without a
> write transaction
> at
> org.eclipse.emf.transaction.impl.TransactionChangeRecorder.a ssertWriting(TransactionChangeRecorder.java:348)
>
> at
> org.eclipse.emf.transaction.impl.TransactionChangeRecorder.a ppendNotification(TransactionChangeRecorder.java:302)
>
> at
> org.eclipse.emf.transaction.impl.TransactionChangeRecorder.p rocessResourceNotification(TransactionChangeRecorder.java:27 2)
>
> at
> org.eclipse.emf.transaction.impl.TransactionChangeRecorder.n otifyChanged(TransactionChangeRecorder.java:238)
>
> at
> org.eclipse.emf.common.notify.impl.BasicNotifierImpl.eNotify (BasicNotifierImpl.java:380)
>
> at
> org.eclipse.emf.common.notify.impl.NotifyingListImpl.dispatc hNotification(NotifyingListImpl.java:267)
>
> at
> org.eclipse.emf.common.notify.impl.NotifyingListImpl.clear(N otifyingListImpl.java:1120)
>
> at
> org.eclipse.xtext.resource.XtextResource.clearInternalState( XtextResource.java:230)
>
> at org.eclipse.xtext.resource.XtextResource.reparse(XtextResour ce.java:167)
> at
> org.eclipse.xtext.ui.editor.model.XtextDocument$XtextDocumen tLocker.modify(XtextDocument.java:152)
>
> at
> org.eclipse.xtext.ui.editor.model.XtextDocument.modify(Xtext Document.java:62)
>
> at
> org.eclipse.xtext.ui.editor.reconciler.XtextDocumentReconcil eStrategy.reconcile(XtextDocumentReconcileStrategy.java:27)
>
> at
> org.eclipse.xtext.ui.editor.reconciler.XtextReconciler.run(X textReconciler.java:179)
>
> at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
>
>
> What is the problem or the fault of my idea/implementation?
>
> I will be very thankful for any hints,
> John
Re: Refactoring for xtextEditors [message #554240 is a reply to message #552850] Fri, 20 August 2010 17:26 Go to previous messageGo to next message
John Locke is currently offline John LockeFriend
Messages: 23
Registered: July 2010
Junior Member
Hi Sebastian,

your answer was very helpful.

I could solve the problem with the editing domain and i put my code in an IDocumentEditor. Now i could apply refactorings but i have two problems.

1. After the refactoring is the code not pretty formated.
Before the refactoring:
class {
   attribute name : string {}
}

After the refactoring "rename class":
class { attribute name : string {} }

If i save the XtextRessource after the refactoring the code is well formated. Is there any method to do this without saving the resource. When i save the resource also the references are updated.
What do i have to do that the code is pretty formated?

2. References to the class are not renamed. If a field in a class is of the type of the renamed class. The references is not changed to the new name.
Whats the problem? Become references not updated by changes inside a IDocumentEditor?

Regards,
John
Re: Refactoring for xtextEditors [message #554576 is a reply to message #554240] Mon, 23 August 2010 15:20 Go to previous messageGo to next message
John Locke is currently offline John LockeFriend
Messages: 23
Registered: July 2010
Junior Member
Hi everybody,

i tried to solve my problem with the following code:

Injector injector = Guice.createInjector(new SimpleClassRuntimeModule());
final IDocumentEditor iDocEditor = injector.getInstance(IDocumentEditor.class);
XtextEditor xEditor = EditorUtils.getActiveXtextEditor();

iDocEditor.process(new IUnitOfWork.Void<XtextResource>() {
	@Override
	public void process(XtextResource state) throws Exception {
		try {
			//... doing some semantic model changes
			String res = state.getSerializer().serialize(state.getContents().get(0));
			xEditor.getDocument().set(res);
			System.out.println("*.SimpleClass");
			System.out.println(res);
		}
	}
}


The problem is no changes are applied to the IXtextDocument. The res string is exactly the result i need.
Are there any other possibilities to trigger the serializer without the save methode of the XtextResource?
If i use the following code the changes applied to the editor but also the XtextResource is saved.
Injector injector = Guice.createInjector(new SimpleClassRuntimeModule());
final IDocumentEditor iDocEditor = injector.getInstance(IDocumentEditor.class);
XtextEditor xEditor = EditorUtils.getActiveXtextEditor();

iDocEditor.process(new IUnitOfWork.Void<XtextResource>() {
	@Override
	public void process(XtextResource state) throws Exception {
		try {
			//... doing some semantic model changes
			state.save(null);
		}
	}
}

How could i adapt the behavior of the save method without saving.

Regards,
John

[Updated on: Mon, 23 August 2010 15:23]

Report message to a moderator

Re: Refactoring for xtextEditors [message #554584 is a reply to message #554576] Mon, 23 August 2010 15:31 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi John,

I think you are missing the point of the IDocumentEditor. It
encapsulates any necessary serializing operations when you perform
semantic changes on your model. All this is nicely protected in a unit
of work and your code has exclusive access to the resource. The idea is
not to perform modifications on the document or on any other eobject
that is not contained in the resource. You will not be able to perform
cross-file refactorings inside of a single document edit action. That's
why it's called document editor and not global
all-instances-in-workspace-editor. Furthermore it is strictly probibited
to create a new injector with a runtime module as it may cause
side-effects and corrupt the internal state of your running eclipse
instance.
Please read about the executable extension factory in the Xtext docs to
learn about injected fields in handlers.

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

Am 23.08.10 17:20, schrieb John:
> Hi everybody,
>
> i tried to solve my problem with the following code:
>
>
> Injector injector = Guice.createInjector(new SimpleClassRuntimeModule());
> final IDocumentEditor iDocEditor =
> injector.getInstance(IDocumentEditor.class);
> XtextEditor xEditor = EditorUtils.getActiveXtextEditor();
>
> iDocEditor.process(new IUnitOfWork.Void<XtextResource>() {
> @Override
> public void process(XtextResource state) throws Exception {
> try {
> //... doing some semantic model changes
> String res = state.getSerializer().serialize(state.getContents().get(0));
> xEditor.getDocument().set(res);
> System.out.println("*.SimpleClass");
> System.out.println(res);
> }
> }
> }
>
>
> The problem is no changes are applied to the IXtextDocument. Are there
> any other possibilities to trigger the serializer without the save
> methode of the XtextResource?
> If i use the following code the changes applied to the editor but also
> the XtextResource is saved.
> Injector injector = Guice.createInjector(new SimpleClassRuntimeModule());
> final IDocumentEditor iDocEditor =
> injector.getInstance(IDocumentEditor.class);
> XtextEditor xEditor = EditorUtils.getActiveXtextEditor();
>
> iDocEditor.process(new IUnitOfWork.Void<XtextResource>() {
> @Override
> public void process(XtextResource state) throws Exception {
> try {
> //... doing some semantic model changes
> state.save(null);
> }
> }
> }
>
> How could i adapt the behavior of the save method without saving.
>
> Regards,
> John
Re: Refactoring for xtextEditors [message #554585 is a reply to message #554584] Mon, 23 August 2010 15:42 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: arendt.mathematik.uni-marburg.de

Hi Sebastian,

you wrote

> All this is nicely protected in a unit of work and your code has exclusive access to the resource. The idea is
> not to perform modifications on the document or on any other eobject
> that is not contained in the resource.

But John was performing modifications on an eobject that WAS contained
in the resource, I think. He only wants to write back the changes.

You further wrote

> You will not be able to perform cross-file refactorings inside of a single document edit action.

What do you mean by 'CROSS-FILE refactorings'?

Regards,
Thorsten
Re: Refactoring for xtextEditors [message #554599 is a reply to message #554585] Mon, 23 August 2010 16:12 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Thorsten,

> But John was performing modifications on an eobject that WAS contained
> in the resource, I think. He only wants to write back the changes.

I didn't want to be rude, sorry for that.
My previous answer was refering to previous approaches that were
discussed in this and other threads. And I'm pretty sure the document
editor will write the changes back when used appropriatly (obtained from
the right injector etc). See
org.eclipse.xtext.ui.tests.editor.model.edit.DefaultDocument EditorTest.testProcess()

> What do you mean by 'CROSS-FILE refactorings'?

Foo.dmodel contains Entity A
Bar.dmodel has Entity B extends A

Renaming A in Foo will not modify resource Bar.

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

Am 23.08.10 17:42, schrieb Thorsten Arendt:
> Hi Sebastian,
>
> you wrote
>
>> All this is nicely protected in a unit of work and your code has exclusive access to the resource. The idea is
>> not to perform modifications on the document or on any other eobject
>> that is not contained in the resource.
>
> But John was performing modifications on an eobject that WAS contained
> in the resource, I think. He only wants to write back the changes.
>
> You further wrote
>
>> You will not be able to perform cross-file refactorings inside of a single document edit action.
>
> What do you mean by 'CROSS-FILE refactorings'?
>
> Regards,
> Thorsten
>
Re: Refactoring for xtextEditors [message #554612 is a reply to message #554576] Mon, 23 August 2010 16:50 Go to previous messageGo to next message
Christian Schneider is currently offline Christian SchneiderFriend
Messages: 23
Registered: July 2009
Junior Member
Hi John,

hope I understood you right.
In case you want to modify the content of an active xtext editor
(not only a parsed model you loaded programmatically)
by means of updating the underlying semantic model try the following code:

XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor();

IDocumentEditor documentEditor = MyDSLActivator.getInstance()
.getInjector(<<Name of your language>>)
.getInstance(IDocumentEditor.class);

documentEditor.process(<<your UnitOfWork>>, xtextEditor.getDocument());

MyDSLActivator is the activator class of the *.ui plugin generated by xtext.
Have a look on it for, e.g., the language name.

If the SWT toolkit is complaining you should let the code run by the Display thread:

PlatformUI
.getWorkbench()
.getDisplay()
.asyncExec(
new Runnable() {
public void run() {

XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor();

IDocumentEditor documentEditor = MyDSLActivator.getInstance()
.getInjector(<<Name of your language>>)
.getInstance(IDocumentEditor.class);

documentEditor.process(<<your UnitOfWork>>, xtextEditor.getDocument());
}
});

Note: restrict yourself straightly on the transformation of the underlying model;
do not try to invoke the serialization manually! This will be done by the framework.


Best,
Christian


On 8/23/10 5:20 PM, John wrote:
> Hi everybody,
>
> i tried to solve my problem with the following code:
>
>
> Injector injector = Guice.createInjector(new SimpleClassRuntimeModule());
> final IDocumentEditor iDocEditor =
> injector.getInstance(IDocumentEditor.class);
> XtextEditor xEditor = EditorUtils.getActiveXtextEditor();
>
> iDocEditor.process(new IUnitOfWork.Void<XtextResource>() {
> @Override
> public void process(XtextResource state) throws Exception {
> try {
> //... doing some semantic model changes
> String res =
> state.getSerializer().serialize(state.getContents().get(0));
> xEditor.getDocument().set(res);
> System.out.println("*.SimpleClass");
> System.out.println(res);
> }
> }
> }
>
>
> The problem is no changes are applied to the IXtextDocument. Are there
> any other possibilities to trigger the serializer without the save
> methode of the XtextResource?
> If i use the following code the changes applied to the editor but also
> the XtextResource is saved.
> Injector injector = Guice.createInjector(new SimpleClassRuntimeModule());
> final IDocumentEditor iDocEditor =
> injector.getInstance(IDocumentEditor.class);
> XtextEditor xEditor = EditorUtils.getActiveXtextEditor();
>
> iDocEditor.process(new IUnitOfWork.Void<XtextResource>() {
> @Override
> public void process(XtextResource state) throws Exception {
> try {
> //... doing some semantic model changes
> state.save(null);
> }
> }
> }
>
> How could i adapt the behavior of the save method without saving.
>
> Regards,
> John
Re: Refactoring for xtextEditors [message #554617 is a reply to message #554612] Mon, 23 August 2010 17:27 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi John,

sounds good to me, although I'ld prefer to initialize the handler by
means of google guice and inject the document editor directly into the
handler class.

@Inject
IDocumentEditor docEditor;

public void ... {
XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor();
docEditor.process(...);
}

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

Am 23.08.10 18:50, schrieb Christian Schneider:
> Hi John,
>
> hope I understood you right.
> In case you want to modify the content of an active xtext editor
> (not only a parsed model you loaded programmatically)
> by means of updating the underlying semantic model try the following code:
>
> XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor();
>
> IDocumentEditor documentEditor = MyDSLActivator.getInstance()
> .getInjector(<<Name of your language>>)
> .getInstance(IDocumentEditor.class);
>
> documentEditor.process(<<your UnitOfWork>>, xtextEditor.getDocument());
>
> MyDSLActivator is the activator class of the *.ui plugin generated by xtext.
> Have a look on it for, e.g., the language name.
>
> If the SWT toolkit is complaining you should let the code run by the Display thread:
>
> PlatformUI
> .getWorkbench()
> .getDisplay()
> .asyncExec(
> new Runnable() {
> public void run() {
>
> XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor();
>
> IDocumentEditor documentEditor = MyDSLActivator.getInstance()
> .getInjector(<<Name of your language>>)
> .getInstance(IDocumentEditor.class);
>
> documentEditor.process(<<your UnitOfWork>>, xtextEditor.getDocument());
> }
> });
>
> Note: restrict yourself straightly on the transformation of the underlying model;
> do not try to invoke the serialization manually! This will be done by the framework.
>
>
> Best,
> Christian
>
>
> On 8/23/10 5:20 PM, John wrote:
>> Hi everybody,
>>
>> i tried to solve my problem with the following code:
>>
>>
>> Injector injector = Guice.createInjector(new SimpleClassRuntimeModule());
>> final IDocumentEditor iDocEditor =
>> injector.getInstance(IDocumentEditor.class);
>> XtextEditor xEditor = EditorUtils.getActiveXtextEditor();
>>
>> iDocEditor.process(new IUnitOfWork.Void<XtextResource>() {
>> @Override
>> public void process(XtextResource state) throws Exception {
>> try {
>> //... doing some semantic model changes
>> String res =
>> state.getSerializer().serialize(state.getContents().get(0));
>> xEditor.getDocument().set(res);
>> System.out.println("*.SimpleClass");
>> System.out.println(res);
>> }
>> }
>> }
>>
>>
>> The problem is no changes are applied to the IXtextDocument. Are there
>> any other possibilities to trigger the serializer without the save
>> methode of the XtextResource?
>> If i use the following code the changes applied to the editor but also
>> the XtextResource is saved.
>> Injector injector = Guice.createInjector(new SimpleClassRuntimeModule());
>> final IDocumentEditor iDocEditor =
>> injector.getInstance(IDocumentEditor.class);
>> XtextEditor xEditor = EditorUtils.getActiveXtextEditor();
>>
>> iDocEditor.process(new IUnitOfWork.Void<XtextResource>() {
>> @Override
>> public void process(XtextResource state) throws Exception {
>> try {
>> //... doing some semantic model changes
>> state.save(null);
>> }
>> }
>> }
>>
>> How could i adapt the behavior of the save method without saving.
>>
>> Regards,
>> John
>
Re: Refactoring for xtextEditors [message #554819 is a reply to message #554617] Tue, 24 August 2010 13:28 Go to previous messageGo to next message
Holger Schill is currently offline Holger SchillFriend
Messages: 75
Registered: July 2009
Member
Hi together,

I implemented a crossresourcerefactoring for xtext some time ago for a customer.
I was wondering why you do not use the org.eclipse.ltk.core.refactoring.DocumentChange to make changes on the IXtextDocument. For me it is the easiest way to implement refactorings. If there is no Editor open for a model that has to be changed you could use org.eclipse.ltk.core.refactoring.TextFileChange and if there is an editor open for a model you could use the class mentioned above. Inside that Refactoring you could use org.eclipse.text.edits.ReplaceEdit to replace the stuff that has to change. Xtext has excellent tooling (mentioned by Sebastian) to get the offset and length of Nodes that represents EObjects. Of course you could use the serializer to get the String for a changes EObject.

Regards,

Holger

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


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Refactoring for xtextEditors [message #557164 is a reply to message #554617] Sun, 05 September 2010 16:00 Go to previous messageGo to next message
John Locke is currently offline John LockeFriend
Messages: 23
Registered: July 2010
Junior Member
Hi,

thanks for your answers.

@Holger: Thanks for your hint i will try it if i have solved the problems for one document. Smile

@Sebastian and Christian:
Is there any other way to get the active injector? I can't access the <MyDSL>Activator.
Can you show my an simple example how to use google guice for an plugin, which should work an any editor which is "generated" by xtext?

Regards,
John
Re: Refactoring for xtextEditors [message #557171 is a reply to message #557164] Sun, 05 September 2010 16:35 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
Am 9/5/10 6:00 PM, schrieb John:
> Hi,
>
> thanks for your answers.
> @Holger: Thanks for your hint i will try it if i have solved the
> problems for one document. :)
>
> @Sebastian and Christian:
> Is there any other way to get the active injector?

Yes, through injection ;-)
But you don't want an injector. You want an IDcoumentEditor.

So add

@Inject
private IDocumentEditor doumentEditor

to your class and make sure the class is created by guice:
If you have the class initialized through the plugin registry, just
monkey see/monkey do what is used for the other extensions in the
plugin.xml (i.e. use the IExecutableExtensionFactory).

If you hold your refactoring code in a separate bundle, thing become a
bit more complicated (but not too complicated...).

Sven


--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de
Re: Refactoring for xtextEditors [message #557366 is a reply to message #551760] Tue, 07 September 2010 06:54 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
This error comes from EMF Transaction (TransactionalEditingDomain).
Note that we have our own means of sychronizing model access, and have
deliberately not chosen EMF Transaction for this purpose.

You might want to read
http://koehnlein.blogspot.com/2010/06/semantic-model-access- in-xtext.html

If you want to stick with EMF Transaction, you must wrap your changes
(and our the whole IDocumentEditor I guess) inside an EMF Command that
you issue on the TEDs command stack. See EMF Transaction docs for details.

Reagrds
Jan


Am 09.08.10 15:32, schrieb John:
> Hi,
>
> i try to change the document like this way.
>
> String erg = ...
>
> IDocumentEditor documentEditor = new DefaultDocumentEditor();
>
> documentEditor.process(new IUnitOfWork.Void<XtextResource>() {
> public void process(XtextResource resource) {
> resource.update(0, erg.length(), erg);
> }
> }, xEditor.getDocument());
>
>
> But now i get the message: "cannot modify resource set without a write
> transaction"
> How can i solve this problem?
>
> Regards,
> John


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


---
Get professional support from the Xtext committers at www.typefox.io
Re: Refactoring for xtextEditors [message #558255 is a reply to message #557171] Fri, 10 September 2010 16:49 Go to previous messageGo to next message
John Locke is currently offline John LockeFriend
Messages: 23
Registered: July 2010
Junior Member
Hi,

thanks for your answers.

@Jan: I have read your blog and i solved my problem with the EditingDomain.

@Sven: Your reply sounds realy good. My refactoring code is in a separate bundle. Can you explain how i can use a IDocumentEditor in this bundle? Or are there any examples?

Regards,
John
Re: Refactoring for xtextEditors [message #558330 is a reply to message #558255] Sat, 11 September 2010 09:30 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
Am 9/10/10 6:49 PM, schrieb John:
> Hi,
>
> thanks for your answers.
> @Jan: I have read your blog and i solved my problem with the EditingDomain.
>
> @Sven: Your reply sounds realy good. My refactoring code is in a
> separate bundle. Can you explain how i can use a IDocumentEditor in this
> bundle? Or are there any examples?

You should move it to the ui bundle is possible.
Otherwise, you'll have to make the activator of the ui bundle accessible
(exporting it in the Manifest).

Sven

--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de
Re: Refactoring for xtextEditors [message #558345 is a reply to message #554576] Sat, 11 September 2010 11:39 Go to previous messageGo to next message
Jan Reimann is currently offline Jan ReimannFriend
Messages: 140
Registered: July 2009
Senior Member
I haven't read the thread but only the title. In the EMFText project a
generic refactoring framework was developed. You can define refactorings
independently from the metamodel and then apply it to a specific
metamodel with a simple mapping. An editor connector for Xtext is in
development. You can find it in the Marketplace under the name
'Refactory' or under the following link with install instructions:
http://emftext.org/index.php/Refactoring:News

best regards
Jan
Re: Refactoring for xtextEditors [message #558401 is a reply to message #558330] Sun, 12 September 2010 11:57 Go to previous messageGo to next message
John Locke is currently offline John LockeFriend
Messages: 23
Registered: July 2010
Junior Member
Hi,

@Sven:
Is there any other possibility to use @Inject without editing the code of the xtext project?

@Jan:
Thanks for that information i will take a look at the emftext project.

Regards,
John
Re: Refactoring for xtextEditors [message #558490 is a reply to message #557164] Mon, 13 September 2010 08:42 Go to previous message
Christian Schneider is currently offline Christian SchneiderFriend
Messages: 23
Registered: July 2009
Junior Member
Hey John,

On 9/5/10 6:00 PM, John wrote:
> Hi,
>
> thanks for your answers.
> @Holger: Thanks for your hint i will try it if i have solved the
> problems for one document. :)
>
> @Sebastian and Christian:
> Is there any other way to get the active injector? I can't access the
> <MyDSL>Activator. Can you show my an simple example how to use google
> guice for an plugin, which should work an any editor which is
> "generated" by xtext?

If you neither can access the .ui plugin's activator nor modify the .ui
plugin's code you seem to be a bit lost to me.

If you can add new classes to your .ui project I suggest to you the
creation of a new <YourDSL>editor class extending the Xtexteditor.class
providing a getDocumentEditor() method.

You can let the injector fill a private field 'documentEditor' of your
specialized editor, which is accessible by means of the above method.

Then you have to register your new class to be the preferred editor
class. Therefore, modify the class attribute the org.eclipse.ui.editors
extension in your .ui plugin's plugin.xml

Finally, in your logic you can apply an 'instanceof' test on the active
editor and cast it to your specialized editor class.

Best,
Christian
Previous Topic:Build Model from Project
Next Topic:How to compare two xtext models
Goto Forum:
  


Current Time: Tue Apr 23 17:38:56 GMT 2024

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

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

Back to the top