Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Custom xtext formatter
Custom xtext formatter [message #1454354] Tue, 28 October 2014 09:21 Go to next message
Eclipse UserFriend
I could not reply/edit messages in my previous topic (sent message to moderator) and because of that I have created a new one.


=====

My xtext grammar looks this:
Foo:
    {Foo}
    (bars+=Bar)*


Generated code:
public interface Foo extends EObject
{
  EList<Bar> getBars();
}


There are a lot of Bar elements in my grammar:

Bar:
    Bar1 |
    Bar2 |
    Bar3

Bar1: 
    'keyword1'
 
Bar2: 
    'keyword2'

But Bar3 elements is different one
Bar3 returns Bar:
     {Bar3} someVariableName=Expression 

Expression: {Expression}:
    'Expr'


And for example we have text:

keyword1
keyword2
Expr


Xtext AST model will looks like next nodes:
1. LeafNode(grammarElement Bar1.keyword1)
2. LeafNode(grammarElement Bar2.keyword2)
3. LeafNode(grammarElement ParseRule stuff for Expression element)


It means that I could not build AbstractDeclarativeFormatter for my grammar if I want to have \n for each Bar element in Foo. Because my rule for Bar3 statement, but in AST there is no elements for this.
c.setLinewrap(1).before(f.bar_Bar1.forKeyword1_0)
c.setLinewrap(1).before(f.bar_Bar2.forKeyword2_0)
c.setLinewrap(1).before(f.bar_Bar3.expressionExpression_ Bar3ParserRuleCall_1_0)


Also I could not write basic rule for Expression element because expressions can be nested.
Does it mean what I need to write my custom formatter?
Re: Custom xtext formatter [message #1454431 is a reply to message #1454354] Tue, 28 October 2014 11:31 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
hi,

did you try to assign the newline to the barsassignment or the BarRuleCall of Foo


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Custom xtext formatter [message #1454496 is a reply to message #1454431] Tue, 28 October 2014 13:08 Go to previous messageGo to next message
Eclipse UserFriend
Yes, I tried. But as I told this elements are not presented in AST.
Re: Custom xtext formatter [message #1454548 is a reply to message #1454496] Tue, 28 October 2014 14:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
looks like the formatter is buggy like hell

Foo:
	{Foo}
	(bars+=Bar)*;
Bar:
	Bar1 |
	Bar2 |
	Bar3;
Bar1:
	{Bar1} 'keyword1';
Bar2:
	{Bar2} 'keyword2';
Bar3 returns Bar:
	{Bar3} someVariableName=Expression;
Expression:
	{Expression}
	'Expr';    


@Inject MyDslGrammarAccess ga
	
	override protected void configureFormatting(FormattingConfig c) {
		c.setLinewrap(1).before(ga.bar1Rule)
		c.setLinewrap(1).before(ga.bar2Rule)
		c.setLinewrap(1).before(ga.expressionRule)
	}


seems to work


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Custom xtext formatter [message #1454615 is a reply to message #1454548] Tue, 28 October 2014 15:32 Go to previous messageGo to next message
Eclipse UserFriend
Yes it will work, but I told, that I could not use expression rule. Because in my grammar I can have nested expressions.
Expr(Expr(Expr))


And I don't want to have \n for each expression, only for each bar.
Re: Custom xtext formatter [message #1454638 is a reply to message #1454615] Tue, 28 October 2014 16:05 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
sorry i have no idea for that but as said before: looks like a bug to me. maybe sebastian szarnekow has an idea

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:JvmTypesAwareResourceSetInitializer - how to do?
Next Topic:Cross references Issues in Xtext
Goto Forum:
  


Current Time: Fri Apr 26 23:09:54 GMT 2024

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

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

Back to the top