簡體   English   中英

Xtext自定義DSL格式

[英]Xtext Custom DSL formatting

在我的DSL中,例如條件和一些聲明(例如block(a; b; c; d;);

在我的configureFormatting函數中,我按以下順序進行操作:

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());

預期為:

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

實際結果:

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

您會看到實際結果,因為在for循環中您明確設置了您不希望在第一個'('和')'之后使用空格。

請嘗試以下操作:

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 )
}

希望有幫助!

好吧,我已經弄清楚了。 很簡單。 我做了以下事情:

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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM