0

In my DSL I have () for a lot of stuff for example if conditions and some declarations such as block(a;b;c;d;);

In my configureFormatting function I do this in the following order:

for (Pair<Keyword, Keyword> pair : grammarAccess.findKeywordPairs("(", ")"))
{
   c.setNoSpace().after(pair.getFirst());
   c.setNoSpace().before(pair.getSecond());
}
c.setIndentation(block.getLeftParenthesisKeyword(),block.getRightParenthesisKeyword());
c.setLinewrap().after(block.getLeftParenthesisKeyword());
c.setLinewrap().before(block.getRightParenthesisKeyword());

Expected is:

block (
     int z;
     int a;
     int y;
);
if (a = 1)

Actual Result:

block (int z;
     int a;
     int y;);
if (a = 1)
Ayman Salah
  • 1,039
  • 14
  • 35

2 Answers2

1

you see the actual result because within the for-loop you set explicitly that you don't want spaces after the first '(' and before the ')'.

Try the following:

for (Pair<Keyword, Keyword> pair : grammarAccess.findKeywordPairs("(", ")")) {
    c.setIndentation(pair.getFirst(), pair.getSecond()); // indent between ( )
    c.setLinewrap().after(pair.getFirst()); // linewrap after (
    c.setLinewrap().before(pair.getSecond()); // linewrap before )
    c.setNoSpace().after(pair.getSecond()); // no space after )
}

Hope that helps!

Franz Becker
  • 705
  • 5
  • 8
  • This didn't solve my problem. The result was that everything that has brackets is on a new line which is not what i want. What i want was to have stuff like the If condition looking like this if (a = 1) with no spaces and other stuff having brackets that i want looking like the block indented and on lines in between the brackets not next to the brackets themselves. Do you get me? – Ayman Salah Jun 18 '15 at 11:06
  • Sure I did not consider the other use case in your grammar, but glad you found a solution. By the way, the is a new formatter API (still marked as beta, though): http://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#formatting it's more powerful and easier to write. – Franz Becker Jun 18 '15 at 16:16
  • Yeah I have seen it. But I can't really work with a beta at the moment. Thanks a lot though. – Ayman Salah Jun 18 '15 at 16:21
0

Well I have figured it out. It was simple. I made the following:

for (Pair<Keyword, Keyword> pair : grammarAccess.findKeywordPairs("(", ")"))
{
   if(pair.getFirst() != block.getLeftParenthesisKeyword())
        c.setNoSpace().after(pair.getFirst());
   if(pair.getSecond() != block.getRightParenthesisKeyword())       
        c.setNoSpace().before(pair.getSecond());
}
c.setIndentation(block.getLeftParenthesisKeyword(),block.getRightParenthesisKeyword());
c.setLinewrap().after(block.getLeftParenthesisKeyword());
c.setLinewrap().before(block.getRightParenthesisKeyword());
Ayman Salah
  • 1,039
  • 14
  • 35