Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Validation In Xtext(Validation In Xtext @ Runtime)
icon5.gif  Validation In Xtext [message #1194166] Mon, 18 November 2013 06:18 Go to next message
Eclipse UserFriend
Hello all,
I have written a validation method. I want this validation method to execute ONLY IF my editor DOES NOT have any grammatical errors (i.e. I have typed my language incorrectly somewhere). How can I achieve this ?


Thanks in advance Smile
Re: Validation In Xtext [message #1197598 is a reply to message #1194166] Tue, 19 November 2013 19:38 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Validation In Xtext [message #1203034 is a reply to message #1194166] Fri, 22 November 2013 07:49 Go to previous messageGo to next message
Eclipse UserFriend
You can either ask the eobject's resource whether it contains errors,
or more specifically you can consult the node model (see NodeModelUtils
and INode.getSyntaxErrorMessage()).

Am 11/18/13 12:18 PM, schrieb Arshad Adavani:
> Hello all,
> I have written a validation method. I want this validation method to
> execute ONLY IF my editor DOES NOT have any grammatical errors (i.e. I
> have typed my language incorrectly somewhere). How can I achieve this ?
>
>
> Thanks in advance :)


--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de
icon14.gif  Re: Validation In Xtext [message #1204683 is a reply to message #1197598] Sat, 23 November 2013 02:29 Go to previous message
Eclipse UserFriend
Hi Alan and Sven,
Thanks for your suggestions Smile
Previous Topic:[General question] : Xtext interoperability
Next Topic:Generating code for Boolean expressions
Goto Forum:
  


Current Time: Wed Jul 23 09:59:13 EDT 2025

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

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

Back to the top