Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Formatting guides?
Formatting guides? [message #755554] Tue, 08 November 2011 19:28 Go to next message
David Struck is currently offline David StruckFriend
Messages: 29
Registered: July 2010
Junior Member
Are there any guides that provide detailed explanations of how to work with the formatting functionality? I know I can create my own formatter by extending AbstractDeclarativeFormatter and I've taken a look at this guide here -- http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.xtext.doc%2Fhelp%2Fformatting.html.

But I'm still struggling a lot to get my formatter to work for me. As an example, here's a file I am trying to format:

action Assisted_ingress_exists(float ev1, ev2, float st) [duration]
{
 
  constant float s1 := 3;
  
  [all] contains {
              s1: Ingress_by_two(ev2,st);
              s2: Untether_for_ingress(ev1,st); //change s2 to s1 
  };

  start(s2) == start(s2) + s1; 
  end(s1) == end(s2);     
  
} 

action Ingress_by_two(float ev, float st)
{
    duration := 2;
}

action Untether_for_ingress(float ev1, float st)
{
    duration := 2.0;
}


A simple thing, for starters, would be to put newlines before and after both left and right curly braces. I found some code online that looks great:

		Iterable<Keyword> keywords = GrammarUtil.containedKeywords(f.getGrammar());
		Stack<Keyword> openBraceStack = new Stack<Keyword>();
		for(Keyword currentKeyword : keywords) {
			if("{".equals(currentKeyword.getValue())) {
				openBraceStack.add(currentKeyword);
				c.setLinewrap().after(currentKeyword);
			}
			else if("}".equals(currentKeyword.getValue())) {
				c.setLinewrap().after(currentKeyword);
				c.setLinewrap().before(currentKeyword);
				if(!openBraceStack.isEmpty()) {
					c.setIndentation(openBraceStack.pop(), currentKeyword);
				}
			}
		}


Unfortunately, it doesn't work for me. No linewraps or indentation is done when I try to format my file. Does anyone have any idea why that might be?

Thanks,

David
Re: Formatting guides? [message #755576 is a reply to message #755554] Tue, 08 November 2011 20:31 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi David,

how does your grammar look like? How does your complete formatter look
like? Did you verify that the for-loop is actually executed?

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

On 08.11.11 20:28, David Struck wrote:
> Are there any guides that provide detailed explanations of how to work
> with the formatting functionality? I know I can create my own formatter
> by extending AbstractDeclarativeFormatter and I've taken a look at this
> guide here --
> http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.xtext.doc%2Fhelp%2Fformatting.html.
>
>
> But I'm still struggling a lot to get my formatter to work for me. As an
> example, here's a file I am trying to format:
>
>
> action Assisted_ingress_exists(float ev1, ev2, float st) [duration]
> {
>
> constant float s1 := 3;
>
> [all] contains {
> s1: Ingress_by_two(ev2,st);
> s2: Untether_for_ingress(ev1,st); //change s2 to s1 };
>
> start(s2) == start(s2) + s1; end(s1) == end(s2);
> }
> action Ingress_by_two(float ev, float st)
> {
> duration := 2;
> }
>
> action Untether_for_ingress(float ev1, float st)
> {
> duration := 2.0;
> }
>
>
> A simple thing, for starters, would be to put newlines before and after
> both left and right curly braces. I found some code online that looks
> great:
>
>
> Iterable<Keyword> keywords = GrammarUtil.containedKeywords(f.getGrammar());
> Stack<Keyword> openBraceStack = new Stack<Keyword>();
> for(Keyword currentKeyword : keywords) {
> if("{".equals(currentKeyword.getValue())) {
> openBraceStack.add(currentKeyword);
> c.setLinewrap().after(currentKeyword);
> }
> else if("}".equals(currentKeyword.getValue())) {
> c.setLinewrap().after(currentKeyword);
> c.setLinewrap().before(currentKeyword);
> if(!openBraceStack.isEmpty()) {
> c.setIndentation(openBraceStack.pop(), currentKeyword);
> }
> }
> }
>
>
> Unfortunately, it doesn't work for me. No linewraps or indentation is
> done when I try to format my file. Does anyone have any idea why that
> might be?
>
> Thanks,
>
> David
Re: Formatting guides? [message #755599 is a reply to message #755576] Tue, 08 November 2011 22:01 Go to previous messageGo to next message
David Struck is currently offline David StruckFriend
Messages: 29
Registered: July 2010
Junior Member
Sebastian, the grammar is quite long (1300+ lines) so I didn't think it made sense to post it. The formatter implementation is pretty simple. Here it is:

public class TESTFormatter extends AbstractDeclarativeFormatter {

	@Override
	protected void configureFormatting(FormattingConfig c) {
		TESTGrammarAccess f = (TESTGrammarAccess) getGrammarAccess();

		// It's usually a good idea to activate the following three statements.
		// They will add and preserve newlines around comments
		c.setLinewrap(0, 1, 2).before(f.getSL_COMMENTRule());
		c.setLinewrap(0, 1, 2).before(f.getML_COMMENTRule());
		c.setLinewrap(0, 1, 1).after(f.getML_COMMENTRule());

		Iterable<Keyword> keywords = GrammarUtil.containedKeywords(f.getGrammar());
		Stack<Keyword> openBraceStack = new Stack<Keyword>();
		for(Keyword currentKeyword : keywords) {
			if("{".equals(currentKeyword.getValue())) {
				openBraceStack.add(currentKeyword);
				c.setLinewrap().after(currentKeyword);
			}
			else if("}".equals(currentKeyword.getValue())) {
				c.setLinewrap().after(currentKeyword);
				c.setLinewrap().before(currentKeyword);
				if(!openBraceStack.isEmpty()) {
					c.setIndentation(openBraceStack.pop(), currentKeyword);
				}
			}
		}
	}



If I throw a print statement in the for loop, I can see that the left curly brace is being found. But, for whatever reason, the calls to setLinewrap, etc. don't work. Hopefully that's enough to go on.

Thanks,

David
Re: Formatting guides? [message #755608 is a reply to message #755599] Tue, 08 November 2011 23:14 Go to previous message
David Struck is currently offline David StruckFriend
Messages: 29
Registered: July 2010
Junior Member
Looks like we were able to figure this out a bit -- the left curly braces and semi-colons are defined as tokens in the grammar, so we are able to use code like this:

		c.setLinewrap().after(f.getLeftC_TokenRule());
		c.setLinewrap().before(f.getRightC_TokenRule());
		c.setLinewrap().after(f.getRightC_TokenRule());
		c.setIndentationIncrement().after(f.getLeftC_TokenRule());
		c.setIndentationDecrement().before(f.getRightC_TokenRule());
		c.setNoSpace().before(f.getDot_TokenRule());
		c.setNoSpace().after(f.getDot_TokenRule());
		c.setLinewrap().after(f.getSemi_TokenRule());


This is working quite well.

Thanks,

David
Previous Topic:Problem with compatible feature type when redefining variable declaration from xbase
Next Topic:Ordered groups question
Goto Forum:
  


Current Time: Thu Apr 25 16:52:50 GMT 2024

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

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

Back to the top