Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Validation In Xtext(Validation In Xtext @ Runtime)
icon5.gif  Validation In Xtext [message #1194166] Mon, 18 November 2013 11:18 Go to next message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
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


Arshad
Re: Validation In Xtext [message #1197598 is a reply to message #1194166] Wed, 20 November 2013 00:38 Go to previous messageGo to next message
Alan DW is currently offline Alan DWFriend
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
Re: Validation In Xtext [message #1203034 is a reply to message #1194166] Fri, 22 November 2013 12:49 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
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 07:29 Go to previous message
Arshad Adavani is currently offline Arshad AdavaniFriend
Messages: 163
Registered: July 2013
Location: Bangalore
Senior Member
Hi Alan and Sven,
Thanks for your suggestions Smile


Arshad
Previous Topic:[General question] : Xtext interoperability
Next Topic:Generating code for Boolean expressions
Goto Forum:
  


Current Time: Thu Apr 25 17:03:25 GMT 2024

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

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

Back to the top