Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Custom xtext formatter
Custom xtext formatter [message #1454354] Tue, 28 October 2014 05: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 07:31 Go to previous messageGo to next message
Eclipse UserFriend
hi,

did you try to assign the newline to the barsassignment or the BarRuleCall of Foo
Re: Custom xtext formatter [message #1454496 is a reply to message #1454431] Tue, 28 October 2014 09: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 10:17 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Custom xtext formatter [message #1454615 is a reply to message #1454548] Tue, 28 October 2014 11: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 12:05 Go to previous message
Eclipse UserFriend
sorry i have no idea for that but as said before: looks like a bug to me. maybe sebastian szarnekow has an idea
Previous Topic:JvmTypesAwareResourceSetInitializer - how to do?
Next Topic:Cross references Issues in Xtext
Goto Forum:
  


Current Time: Tue Jul 15 20:52:14 EDT 2025

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

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

Back to the top