|
Re: Validation In Xtext [message #1197598 is a reply to message #1194166] |
Wed, 20 November 2013 00:38 |
Alan DW Messages: 119 Registered: March 2012 |
Senior Member |
|
|
Hi,
I'm not sure why you would want to do this, but here's how I would go about it:
// NOTES:
// - any of the calls below may return NULL. So check for it!
// - some of the calls below will work ONLY from the eclipse UI thread!
// - to switch to the UI thread from anywhere, use Display.getCurrent() and then use syncExec(Runnable runnable)
// - be careful about deadlocks and significant delays here.
IWorkbench iworkbench = PlatformUI.getWorkbench();
IWorkbenchWindow iworkbenchwindow = iworkbench.getActiveWorkbenchWindow();
IWorkbenchPage iworkbenchpage = iworkbenchwindow.getActivePage();
IEditorPart ieditorpart = iworkbenchpage.getActiveEditor();
Then, with the IEditorPart, you can get the IResource like this:
IResource extractResource(IEditorPart editor) {
IEditorInput input = editor.getEditorInput();
if (!(input instanceof IFileEditorInput))
return null;
return ((IFileEditorInput)input).getFile();
}
(NOTE: some of the code above may be out-dated and need a little fixing depending on your version of Eclipse. I've more or less copy-pasted the code from the official wiki page. Also make sure that your project has the necessary dependencies to the org.eclipse... plug-ins.)
Having obtained the IResource of your active editor, you can then check for its warning and error markers using one of:
- findMarker(long id)
- findMarkers(String type, boolean includeSubtypes, int depth)
- getMarker(long id)
When it comes to the markers, make sure you check the Javadoc of IResource for details. You'll have to find out which error codes Xtext is using, though (as far as I know, they have always the same ID, but I don't know that ID, unfortunately).
When you know if there are any markers you look for, then skip the validation method (a simple if-statement right at the beginning of the validator should handle this gracefully), otherwise run it.
Hope this helps.
Alan
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03600 seconds