Creating new xtext resource in quickfix [message #1781776] |
Tue, 13 February 2018 09:41 |
|
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 #1781824 is a reply to message #1781788] |
Tue, 13 February 2018 15:32 |
|
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 #1781828 is a reply to message #1781825] |
Tue, 13 February 2018 16:34 |
|
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 |
|
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
|
|
|
Powered by
FUDForum. Page generated in 0.03576 seconds