Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Check for syntax errors before formatting.
Check for syntax errors before formatting. [message #805969] Fri, 24 February 2012 06:02 Go to next message
Eclipse UserFriend
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 18:45 Go to previous messageGo to next message
Eclipse UserFriend
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 03:56 Go to previous messageGo to next message
Eclipse UserFriend
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 12:15 Go to previous messageGo to next message
Eclipse UserFriend
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 16:59] by Moderator

Re: Check for syntax errors before formatting. [message #850178 is a reply to message #832000] Thu, 19 April 2012 17:01 Go to previous messageGo to next message
Eclipse UserFriend
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 17:38 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Check for syntax errors before formatting. [message #850228 is a reply to message #850202] Thu, 19 April 2012 18:18 Go to previous message
Eclipse UserFriend
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 23 17:24:02 EDT 2025

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

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

Back to the top