How to define different indentation levels in the

2020-03-31 08:45发布

问题:

Is it possible to format a document as follows, using Xtext formatting? As you can see, Test children are indented with 4 spaces while External children are indented with 2 spaces only. I am using Xtext 2.12.0.

Test my_prog {
    Device = "my_device";
    Param = 0;
}

External {
  Path = "my_path";
  File = "my_file";
}

回答1:

you could try to work with custom replacers, dont know if this will work with nested block though

def dispatch void format(External model, extension IFormattableDocument document) {
    model.regionFor.keyword("}").prepend[newLine]
    for (l : model.ids) {
        val region = l.regionFor.feature(MyDslPackage.Literals.IDX__NAME)
        region.prepend[newLine]
        val r = new AbstractTextReplacer(document, region) {
            override createReplacements(ITextReplacerContext it) {
                val offset = region.offset
                it.addReplacement(region.textRegionAccess.rewriter.createReplacement(offset, 0, "  "))
                it
            }
        }
        addReplacer(r)
    }
}