如何通过多个缩进缩进一个区域?

时间:2018-02-20 09:01:43

标签: indentation xtext formatter

我有以下语法:

Department:
  'Department:' name = ID
  'Company:' companyName = STRING
  'Persons:' persons += Person+
;

Person:
  firstName = STRING
  ':'
  surname = STRING
  ':'
  address = STRING
;

我希望有这样的格式:

Department: Department1
    Company: "Company1"
    Persons:
        "Person1FirstName" : "Person1LastName" : "Person1Address"
        "Person2FirstName" : "Person2LastName" : "Person2Address"

但是当我实现格式化程序代码时,我似乎无法使用缩进两次,因为空格格式化合并而不是连接。

class TestsFormatter extends AbstractFormatter2
{
    def dispatch void format(Department department, extension IFormattableDocument document)
    {
        department.interior[indent]
        department.regionFor.keyword("Department:").prepend[setNewLines(2)]
        department.regionFor.keyword("Department:").append[oneSpace]
        department.regionFor.keyword("Company:").prepend[newLine]
        department.regionFor.keyword("Company:").append[oneSpace]
        department.regionFor.keyword("Persons:").prepend[newLine]

        for (Person person : department.persons)
        {
            person.format
        }
    }

    def dispatch void format(Person person, extension IFormattableDocument document)
    {
        person.prepend[indent]
        person.prepend[indent]
    }
}

我发现有一个例子" increaseIndentation / decreaseIndentation"在AbstractFormatter2的文档中。但是当我尝试使用它们时,它无法解决它们。

1 个答案:

答案 0 :(得分:1)

您可以使用类似How to define different indentation levels in the same document with Xtext formatter

中所述的自定义替换程序来实现此目的