Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Modifying a file that's not open in an editor?
Modifying a file that's not open in an editor? [message #859596] Fri, 27 April 2012 18:11 Go to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Hi,

I'm implementing an Xtext quickfix that needs to modify more than one file.
For the file showing the validation issue I'm using an ISemanticModification,
whereas for any secondary files more indirections seem to be necessary.
I have a solution that works IFF the other file is currently open in an Xtext editor.
However, if the file is not open this call
XtextDocumentUtil.get(fileResource)

returns null. Can anyone recommend how to proceed?

- is IXtextDocument the appropriate object to operate on or can I apply my modifications (a la ISemanticModification) using a different API?
- can I obtain an IXtextDocument even if the file is not open in an editor?
- should I force load the file into an Xtext editor (behind the scenes?)?

thanks,
Stephan
Re: Modifying a file that's not open in an editor? [message #859740 is a reply to message #859596] Fri, 27 April 2012 19:48 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

what about letting Xtext opening the editor for you by calling org.eclipse.xtext.ui.editor.model.edit.IModificationContext.getXtextDocument(URI)

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Modifying a file that's not open in an editor? [message #859770 is a reply to message #859596] Fri, 27 April 2012 20:07 Go to previous messageGo to next message
Vlad Dumitrescu is currently offline Vlad DumitrescuFriend
Messages: 431
Registered: July 2009
Location: Gothenburg
Senior Member
Hi,

I can't check the code at the moment and I might forget about it, so I ask here: how does the generated support for the renaming refactoring? I assume it works across multiple files and doesn't open all of them in editors; at least I didn't notice something like that when I used it. If language-specific support is needed I suppose Xbase/Xtend would be a good example to check.

regards,
Vlad
Re: Modifying a file that's not open in an editor? [message #859907 is a reply to message #859596] Fri, 27 April 2012 21:35 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
No idea if this is the correct answer - but...
I would try just loading the resource into a resource set, change the
model and then serialize/save it.

I would first check if an editor has that particular open / dirty, and
then perform the action "the editor way".

- henrik

On 2012-27-04 20:11, Stephan Herrmann wrote:
> Hi,
>
> I'm implementing an Xtext quickfix that needs to modify more than one file.
> For the file showing the validation issue I'm using an
> ISemanticModification, whereas for any secondary files more indirections
> seem to be necessary.
> I have a solution that works IFF the other file is currently open in an
> Xtext editor.
> However, if the file is not open this call
> XtextDocumentUtil.get(fileResource)
> returns null. Can anyone recommend how to proceed?
>
> - is IXtextDocument the appropriate object to operate on or can I apply
> my modifications (a la ISemanticModification) using a different API?
> - can I obtain an IXtextDocument even if the file is not open in an editor?
> - should I force load the file into an Xtext editor (behind the scenes?)?
>
> thanks,
> Stephan
>
Re: Modifying a file that's not open in an editor? [message #865169 is a reply to message #859740] Mon, 30 April 2012 09:13 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Thanks for suggestions.

Christian Dietrich wrote on Fri, 27 April 2012 21:48
Hi,

what about letting Xtext opening the editor for you by calling org.eclipse.xtext.ui.editor.model.edit.IModificationContext.getXtextDocument(URI)

~Christian


This seems to be the one closest to what I want to do, great.

One caveat: it seems the URI must be a platform URI, classpath: doesn't work (might be worth documenting in API doc Smile

Actually opening editors for secondary files is tolerable in my case since normally only one additional file is affected. However, if a quick fix should affect more files opening all these might become an issue.

Especially when I start implementing refactorings I don't want to open all affected files in editors.

Is there any other API for rewriting that does not depend on an editor? Anything similar to JDT's ASTRewrite? (I wouldn't want to go back to serializing entire files because the layout of the source code must be preserved as much as possible.)

thanks,
Stephan
Re: Modifying a file that's not open in an editor? [message #865444 is a reply to message #865169] Mon, 30 April 2012 11:55 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Stephan,

please file a ticket. IModificationContext#getXtextDocument could work
with the plain IXtextDocument and use the IDocumentProvider to save the
changes.

You may want to customize the
org.eclipse.xtext.ui.editor.model.edit.IssueModificationContext.Factory
to achieve that for your language.

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

Am 30.04.12 11:13, schrieb Stephan Herrmann:
> Thanks for suggestions.
>
> Christian Dietrich wrote on Fri, 27 April 2012 21:48
>> Hi,
>>
>> what about letting Xtext opening the editor for you by calling
>> org.eclipse.xtext.ui.editor.model.edit.IModificationContext.getXtextDocument(URI)
>>
>>
>> ~Christian
>
>
> This seems to be the one closest to what I want to do, great.
>
> One caveat: it seems the URI must be a platform URI, classpath: doesn't
> work (might be worth documenting in API doc :)
>
> Actually opening editors for secondary files is tolerable in my case
> since normally only one additional file is affected. However, if a quick
> fix should affect more files opening all these might become an issue.
>
> Especially when I start implementing refactorings I don't want to open
> all affected files in editors.
> Is there any other API for rewriting that does not depend on an editor?
> Anything similar to JDT's ASTRewrite? (I wouldn't want to go back to
> serializing entire files because the layout of the source code must be
> preserved as much as possible.)
>
> thanks,
> Stephan
Re: Modifying a file that's not open in an editor? [message #865525 is a reply to message #865444] Mon, 30 April 2012 12:50 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Sebastian Zarnekow wrote on Mon, 30 April 2012 13:55

please file a ticket. IModificationContext#getXtextDocument could work
with the plain IXtextDocument and use the IDocumentProvider to save the
changes.


See https://bugs.eclipse.org/bugs/show_bug.cgi?id=378074

thanks,
Stephan
Re: Modifying a file that's not open in an editor? [message #865806 is a reply to message #865525] Mon, 30 April 2012 15:32 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
In the same context I'm now facing problems with updating cross-file references.
Grammar 2 has plenty of references to types from Grammar 1 ("do") like this:

JavaEntity:
    'entity' reference=[do::Entity]


While looking at a file of grammar 2 I want to rename an object from grammar 1.
Since references are by name, only renaming the object from grammar 1 breaks the reference.
While I'm in the quickfix for grammar 2 I perform a IUnitOfWork.modify on the other resource.

I thought I came pretty close to my goal by passing the object used during this modification back to the original quickfix, where I do a lookup of the do:Entity by name and assign that to JavaEntity.reference.

However, once the grammar 2 file is being rewritten I get this exception:
java.lang.RuntimeException: No EObjectDescription could be found in Scope JavaType(JavaEntity).reference for Model.elements[0]->DomainObject'OrderPlan'.elements[0]->Entity'OrderPlanMO'
Semantic Object: JavaModel.objects[1]->JavaDomainObject.elements[0]->JavaEntity
	at org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic$ExceptionThrowingAcceptor.accept(ISerializationDiagnostic.java:70)
	at org.eclipse.xtext.serializer.tokens.CrossReferenceSerializer.getCrossReferenceNameFromScope(CrossReferenceSerializer.java:143)
	at org.eclipse.xtext.serializer.tokens.CrossReferenceSerializer.serializeCrossRef(CrossReferenceSerializer.java:116)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.getToken(SequenceFeeder.java:448)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.accept(SequenceFeeder.java:220)
	at org.eclipse.xtext.serializer.sequencer.BacktrackingSemanticSequencer.accept(BacktrackingSemanticSequencer.java:387)
	at org.eclipse.xtext.serializer.sequencer.BacktrackingSemanticSequencer.createSequence(BacktrackingSemanticSequencer.java:423)


Indeed, the scope still contains an entry by the old name.

This makes me wonder whether I'm missing some explicit handling of some kind of working copies to ensure operating on a consistent set of resources / objects?
I tried inserting an XtextResource.save(null) call but to no avail.

Re: Modifying a file that's not open in an editor? [message #865823 is a reply to message #865806] Mon, 30 April 2012 15:39 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
As mentioned before: there is already rename refactoring to look at

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Modifying a file that's not open in an editor? [message #865886 is a reply to message #865823] Mon, 30 April 2012 16:23 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Christian Dietrich wrote on Mon, 30 April 2012 17:39
As mentioned before: there is already rename refactoring to look at


So, what do you recommend?
- Invoke rename refactoring from quickfix? Is that possible?
- Copy the strategy from the refactoring to my quickfix implementation?
- If neither is promising: should I abstain from implementing this as a quickfix and rather write a menu action from scratch?

In any case I'd appreciate a pointer where in the refactoring implementation I should start searching.

Thanks,
Stephan
Re: Modifying a file that's not open in an editor? [message #865931 is a reply to message #865886] Mon, 30 April 2012 16:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

it is hard to give advice since i did not leverage the rename refactoring myself. but it is there. you can use it out of the box. no quickfix, no menu action. for everything else you have to dig into the code yourself i fear. use the package org.eclipse.xtext.ui.refactoring and its subpackages in the org.eclipse.xtext.ui plugin as starting point

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Modifying a file that's not open in an editor? [message #866031 is a reply to message #865931] Mon, 30 April 2012 17:54 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Christian Dietrich wrote on Mon, 30 April 2012 18:52
it is hard to give advice since i did not leverage the rename refactoring myself. but it is there. you can use it out of the box. no quickfix, no menu action.

In my case renaming some elements is only part of a compound operation. So unless the refactoring can be easily composed into such a compound operation (how?) using it out of the box doesn't help.

Quote:
for everything else you have to dig into the code yourself i fear. use the package org.eclipse.xtext.ui.refactoring and its subpackages in the org.eclipse.xtext.ui plugin as starting point


Hm, that's quite a lot of code to dig through just to get an answer to this question:
- when performing changes to multiple files as part of a quickfix operation: how do I ensure that serializing the final result doesn't blow up because of the use of an outdated scope?

Maybe s.o. else has a hint?
Stephan
Previous Topic:semantic change to resource with unresolved references
Next Topic:extend the editor with popup menu
Goto Forum:
  


Current Time: Thu Apr 18 10:30:25 GMT 2024

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

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

Back to the top