Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Check for syntax errors before formatting.
Check for syntax errors before formatting. [message #805969] Fri, 24 February 2012 11:02 Go to next message
Tobi Miller is currently offline Tobi MillerFriend
Messages: 12
Registered: January 2012
Junior Member
Hello,
I have a working grammar and formatter. But if the editor conatins syntax errors, the formatter destroys the code. Is there any way to check in the formatter if there are syntax errors in the current document?
Re: Check for syntax errors before formatting. [message #806413 is a reply to message #805969] Fri, 24 February 2012 23:45 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 2012-24-02 12:02, Tobi Miller wrote:
> Hello, I have a working grammar and formatter. But if the editor
> conatins syntax errors, the formatter destroys the code. Is there any
> way to check in the formatter if there are syntax errors in the current
> document?

IIRC, no, but you could try overriding where the call to the formatter
is made from the UI and perform the check there.

Regards
- henrik
Re: Check for syntax errors before formatting. [message #807980 is a reply to message #806413] Mon, 27 February 2012 08:56 Go to previous messageGo to next message
John J. Camilleri is currently offline John J. CamilleriFriend
Messages: 33
Registered: November 2011
Location: Göteborg
Member
My guess is overriding: org.eclipse.xtext.ui.editor.formatting.ContentFormatterFactory.ContentFormatter#format()
Re: Check for syntax errors before formatting. [message #832000 is a reply to message #807980] Thu, 29 March 2012 16:15 Go to previous messageGo to next message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
Hey,

thank you John for the hint. Here is my code I used to override the behaviour of IContentFormatterFactory:

public class MyDQLContentFormatterFactory extends ContentFormatterFactory {

	@Inject
	private XtextResource resource;

	public class MyDSLContentFormatter implements IContentFormatter {
		public void format(IDocument document, IRegion region) {

			// if formatting is performed with errors, the code will be destroyed
			if (!resource.getErrors().isEmpty()) {
				return;
			}

			new ContentFormatter().format(document, region);
		}

		public IFormattingStrategy getFormattingStrategy(String contentType) {
			return null;
		}
	}

	@Override
	public IContentFormatter createConfiguredFormatter(SourceViewerConfiguration configuration, ISourceViewer sourceViewer) {
		return new MyDSLContentFormatter();
	}
}


as well as the method in MyDSLUiModule:

	@Override
	public Class<? extends IContentFormatterFactory> bindIContentFormatterFactory() {
		return MyDQLContentFormatterFactory.class;
	}


It is pretty strange since resource.getErrors().isEmpty() always returns true, even the Eclipse Error View says that MyDQL language contains errors.

Why do I always get an empty error list? Am I doing something wrong? Thank you!

Kon

[Updated on: Thu, 19 April 2012 20:59]

Report message to a moderator

Re: Check for syntax errors before formatting. [message #850178 is a reply to message #832000] Thu, 19 April 2012 21:01 Go to previous messageGo to next message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
Has anyone a hint?

Any help is appreciated Smile Thank you!
Re: Check for syntax errors before formatting. [message #850202 is a reply to message #850178] Thu, 19 April 2012 21:38 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13492
Registered: July 2009
Senior Member
Hi,

if you inject any resource (=new XtextResource()) no wonder that it is has no errors.

if you want to do advanced things in Xtext you must not be shy to use a debugger.

The passed IDocument may be a XtextDocument that gives you access to the resource.
org.eclipse.xtext.util.concurrent.IReadAccess.readOnly(IUnitOfWork<T, P>)
never the less maybe hooking in somewhere later may be better because it is already
Xtextish Wink

public class MyDslNodeModelFormatter extends DefaultNodeModelFormatter {
	
	@Override
	public IFormattedRegion format(ICompositeNode root, int offset, int length) {
		Resource eResource = NodeModelUtils.findActualSemanticObjectFor(root).eResource();
		if (eResource.getErrors().size() > 0) {
			return new FormattedRegion(0, 0, "");
		}
		return super.format(root, offset, length);
	}

}


~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Check for syntax errors before formatting. [message #850228 is a reply to message #850202] Thu, 19 April 2012 22:18 Go to previous message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
Hey Christian,

thank you! That hint did the job Smile And I'm not afraid of the Debugger. I pretty much like it and also did spend a couple hours trying ... before I tried it here Smile

Just in case, someone else is struggling with the same issue. Use the code above from Christian and also add following method to MyDslRuntimeModule:

	public Class<? extends INodeModelFormatter> bindINodeModelFormatter() {
		return MyDslNodeModelFormatter.class;
	}


Thank you!
Previous Topic:How to open a project/file browser for a importURI content assist?
Next Topic:Adding built-in types to the domain model example
Goto Forum:
  


Current Time: Wed Jul 08 15:33:26 GMT 2020

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

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

Back to the top