Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Creating new xtext resource in quickfix(How to create a new xtext resource in quickfix with correct formatting)
Creating new xtext resource in quickfix [message #1781776] Tue, 13 February 2018 09:41 Go to next message
Markus Oley is currently offline Markus OleyFriend
Messages: 304
Registered: July 2009
Location: Germany
Senior Member
Hi,

I have a model composed of different languages. In one kind of file (workflow) I want to create another file of another language (mask) and reference this via crossreferences.

I have implemented a working solution, but one thing is missing...
The new file is not formatted at all, all content is in the first line.
I create it with:

org.eclipse.emf.common.util.URI uri = workflow.eResource().getURI().trimSegments(1).appendSegment(compound.getName() + ".mask");
XtextResource newResource = (XtextResource) workflow.eResource().getResourceSet().createResource(uri);
newResource.getContents().add(compound);
newResource.save(null);

Can you please give me advice how formatting this whole thing?

Thanks in advance
Markus
Re: Creating new xtext resource in quickfix [message #1781777 is a reply to message #1781776] Tue, 13 February 2018 09:44 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
you can pass SaveOptions.newBuilder().format().getOptions().toOptionsMap()

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Creating new xtext resource in quickfix [message #1781779 is a reply to message #1781777] Tue, 13 February 2018 09:56 Go to previous messageGo to next message
Markus Oley is currently offline Markus OleyFriend
Messages: 304
Registered: July 2009
Location: Germany
Senior Member
Hi Christian,

thank you for your fast answer, but nevertheless this don't change anything.
Still the same result.
Any further idea?

Cheers
Markus
Re: Creating new xtext resource in quickfix [message #1781788 is a reply to message #1781779] Tue, 13 February 2018 10:29 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
did you implement formatter?
does it work if you do it in a unit tests?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Creating new xtext resource in quickfix [message #1781824 is a reply to message #1781788] Tue, 13 February 2018 15:32 Go to previous messageGo to next message
Markus Oley is currently offline Markus OleyFriend
Messages: 304
Registered: July 2009
Location: Germany
Senior Member
Hi Christian,

yes, I implemented a formatter, which works when Trigger Source->Format in editor.

The quickfix itself I have tried in unit test and got the same result.
By the way, I did only test logic called by quickfix itself. Is there an example how I can write a unittest testing quickfix provider?

Thanks
Markus
Re: Creating new xtext resource in quickfix [message #1781825 is a reply to message #1781824] Tue, 13 February 2018 15:47 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
no i mean:

unit test:
simply call create
add stuff
resource.save(map)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Creating new xtext resource in quickfix [message #1781828 is a reply to message #1781825] Tue, 13 February 2018 16:34 Go to previous messageGo to next message
Markus Oley is currently offline Markus OleyFriend
Messages: 304
Registered: July 2009
Location: Germany
Senior Member
I do the following:

in WorkflowQuickfixProvider I do:
//1. create new EObject: JavaMask mask = MaskFactory.eINSTANCE.createJavaMask()

//2. do some other things with mask....(calling some setters)

//3. Create some resource and add the mask to it
org.eclipse.emf.common.util.URI uri = workflow.eResource().getURI().trimSegments(1).appendSegment(mask.getName() + ".mask");
					XtextResource newResource = (XtextResource) workflow.eResource().getResourceSet().createResource(uri);
					newResource.getContents().add(mask);

//4. Save the new file newResource.save(SaveOptions.newBuilder().format().getOptions().toOptionsMap() );

//5. connect new created mask to a step in workflowFile

//6. Calling builder to resolve references
ResourceSet resourceSet = workflow.eResource().getResourceSet();
					if (resourceSet instanceof XtextResourceSet) {
						XtextResourceSet xtextresourceset = (XtextResourceSet) resourceSet;
						Object classpathURIContext = xtextresourceset.getClasspathURIContext();
						if (classpathURIContext instanceof IJavaProject) {
							IJavaProject project = (IJavaProject) classpathURIContext;
							project.getProject().refreshLocal(IResource.DEPTH_INFINITE, PerfLogFactory.newProgressMonitor());
							project.getProject().build(IncrementalProjectBuilder.INCREMENTAL_BUILD, PerfLogFactory.newProgressMonitor());
						}
						else
							LOGGER.error("ClasspathURIContext of resource " + workflow.eResource().toString() + " was not an javaproject");
					}
					else
						LOGGER.error("resourceset of resource " + workflow.eResource().toString() + " was not an xtextresourceset");



In UnitTest I do..
- Load workflow object from testfile into resourceset
- Create provider like: WorkflowQuickfixProvider provider = new WorkflowQuickfixProvider();
- Call the method, which is also called in @Fix annotated method and contains the logic: provider.addMaskToStep(MaskFactory.eINSTANCE.createJavaMask(), guiStep);
- Afterwards load the created mask file and check content line per line


Re: Creating new xtext resource in quickfix [message #1781831 is a reply to message #1781825] Tue, 13 February 2018 16:49 Go to previous message
Markus Oley is currently offline Markus OleyFriend
Messages: 304
Registered: July 2009
Location: Germany
Senior Member
Hi again christian,

sorry - but meanwhile I found my mistake.....
I bound my formatter in the wrong way:

public Class<? extends JFormattedTextField.AbstractFormatter> bindFormatter() {
return MaskFormatter.class;
}

instead of:

public Class<? extends IFormatter> bindIFormatter() {
return MaskFormatter.class;
}

So - it's not a wonder why it doesn't work :-)
Thanks for your help
Cheers
Markus
Previous Topic:Xtext 2.11 in Maven central
Next Topic:Serialization: how to choose which optionnal rule is used
Goto Forum:
  


Current Time: Thu Apr 18 10:16:31 GMT 2024

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

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

Back to the top