【问题标题】:Xtext Custom DSL formattingXtext 自定义 DSL 格式
【发布时间】:2015-06-18 19:15:13
【问题描述】:

在我的 DSL 中,我有很多东西,例如 if 条件和一些声明,例如 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)

【问题讨论】:

    标签: eclipse formatting dsl xtext xtend


    【解决方案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 )
    }
    

    希望有帮助!

    【讨论】:

    • 这并没有解决我的问题。结果是所有有括号的东西都在一个新的行上,这不是我想要的。我想要的是像 if (a = 1) 这样没有空格的条件和其他带有括号的东西,我希望这些东西看起来像缩进的块,并且在括号之间的行上而不是在括号本身旁边。你明白我了吗?
    • 当然,我没有考虑您语法中的其他用例,但很高兴您找到了解决方案。顺便说一句,这是一个新的格式化程序 API(但仍标记为 beta):eclipse.org/Xtext/documentation/… 它更强大且更易于编写。
    • 是的,我看过。但我目前无法真正使用测试版。不过非常感谢。
    【解决方案2】:

    嗯,我想通了。这很简单。我做了以下:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-05
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多