Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Xtext formatter
Xtext formatter [message #62232] Wed, 22 July 2009 04:24 Go to next message
Eclipse UserFriend
Hello,

I really like the formatting support in the new Xtext version. However,
creating a formatting configuration for a large grammer is often tedious.
As far as I understood the documentation, I have to configure the
formatter for each rule specific token, e.g.:

c.setNoSpace().around(f.getParamAccess().getColonKeyword_1() );

In our grammar we have 10 different rules in which we use a colon keyword
and therefore I have to configure each of them separately. In order to
avoid this, my idea was to define a terminal for the colon keyword and
then configure the formatter based on the terminal. This would enable the
reuse of formatting rules across different grammar rules:

c.setNoSpace().around(f.getDOTRule());

However, this did not work. Is there another way to configure the
formatter in a more general way?

This also applies to curly braces where would like to write something like:

c.setIndentation(
f.getLeftCurlyBracketRule(),
f.getRightCurlyBracketRule());
c.setLinewrap().after(
f.getLeftCurlyBracketRule());
c.setLinewrap().after(
f.getRightCurlyBracketRule());

Regards,

Sebastian
Re: Xtext formatter [message #62355 is a reply to message #62232] Wed, 22 July 2009 05:22 Go to previous messageGo to next message
Eclipse UserFriend
Hi Sebastian,

> I really like the formatting support in the new Xtext version.

good to hear!

> However,
> creating a formatting configuration for a large grammer is often
> tedious. As far as I understood the documentation, I have to configure
> the formatter for each rule specific token, e.g.:
>
> c.setNoSpace().around(f.getParamAccess().getColonKeyword_1() );

that is right.

> In our grammar we have 10 different rules in which we use a colon
> keyword and therefore I have to configure each of them separately. In
> order to avoid this, my idea was to define a terminal for the colon
> keyword and then configure the formatter based on the terminal. This
> would enable the reuse of formatting rules across different grammar rules:
>
> c.setNoSpace().around(f.getDOTRule());

Assigning formatting to rules currently only works with rules for hidden
tokens..

> However, this did not work. Is there another way to configure the
> formatter in a more general way?

you can build your own:
<pseudocode>
for(AbstractElement e: grammarAccess.getGrammar().eAllElements())
if(e instanceof Keyword) {
Keyword k = (Keyword) e;
if(".".equals(k.getValue()))
cfg.setNoSpace().around(k);
}
</psudocode>


> This also applies to curly braces where would like to write something like:
>
> c.setIndentation(
> f.getLeftCurlyBracketRule(),
> f.getRightCurlyBracketRule());
> c.setLinewrap().after(
> f.getLeftCurlyBracketRule());
> c.setLinewrap().after(
> f.getRightCurlyBracketRule());

I guess you can think of a similar loop for finding all pairs of curly
brackets...

Generally speaking, I can say that we a re aware of this situation and
thinking about how to improve it. Another issue is, that methods-names
of the GrammarAccess tend to change when you modify your grammar.

One idea is to extend the GrammarAccess with methods that return all
keywords of a certain type, e.g.

ga.getAllColonKeywords()

Another is to introduce find-methods:

ga.findKeywords(".")


hth,
Moritz

--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Xtext formatter [message #62731 is a reply to message #62355] Wed, 22 July 2009 10:40 Go to previous message
Eclipse UserFriend
Hi Moritz,

thanks for your help! It works as you said:

protected void configureFormatting(FormattingConfig c) {
MyGrammarAccess f = (MyGrammarAccess) getGrammarAccess();

// colons
Iterable<Keyword> keywords = GrammarUtil.containedKeywords(f.getGrammar());
for (Keyword currentKeyword : keywords) {
if (".".equals(currentKeyword.getValue())) {
c.setNoSpace().around(keyword);
}
}

// braces
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);
}
}
}

}
I think it would be really helpful if one could configure the formatting
at rule level, for example:

Statement:
Assignment | Return
;
...

=>

c.setLinewrap().after(f.getStatementRule());


Regards,

Sebastian



Moritz Eysholdt wrote:

> Hi Sebastian,

>> I really like the formatting support in the new Xtext version.

> good to hear!

>> However,
>> creating a formatting configuration for a large grammer is often
>> tedious. As far as I understood the documentation, I have to configure
>> the formatter for each rule specific token, e.g.:
>>
>> c.setNoSpace().around(f.getParamAccess().getColonKeyword_1() );

> that is right.

>> In our grammar we have 10 different rules in which we use a colon
>> keyword and therefore I have to configure each of them separately. In
>> order to avoid this, my idea was to define a terminal for the colon
>> keyword and then configure the formatter based on the terminal. This
>> would enable the reuse of formatting rules across different grammar rules:
>>
>> c.setNoSpace().around(f.getDOTRule());

> Assigning formatting to rules currently only works with rules for hidden
> tokens..

>> However, this did not work. Is there another way to configure the
>> formatter in a more general way?

> you can build your own:
> <pseudocode>
> for(AbstractElement e: grammarAccess.getGrammar().eAllElements())
> if(e instanceof Keyword) {
> Keyword k = (Keyword) e;
> if(".".equals(k.getValue()))
> cfg.setNoSpace().around(k);
> }
> </psudocode>


>> This also applies to curly braces where would like to write something like:
>>
>> c.setIndentation(
>> f.getLeftCurlyBracketRule(),
>> f.getRightCurlyBracketRule());
>> c.setLinewrap().after(
>> f.getLeftCurlyBracketRule());
>> c.setLinewrap().after(
>> f.getRightCurlyBracketRule());

> I guess you can think of a similar loop for finding all pairs of curly
> brackets...

> Generally speaking, I can say that we a re aware of this situation and
> thinking about how to improve it. Another issue is, that methods-names
> of the GrammarAccess tend to change when you modify your grammar.

> One idea is to extend the GrammarAccess with methods that return all
> keywords of a certain type, e.g.

> ga.getAllColonKeywords()

> Another is to introduce find-methods:

> ga.findKeywords(".")


> hth,
> Moritz
Previous Topic:[Xtext] Ecore Metamodel Question
Next Topic:[XText] cross reference ... lazy
Goto Forum:
  


Current Time: Sun Jun 01 13:56:28 EDT 2025

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

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

Back to the top