How to define different indentation levels in the same document with Xtext formatter

时光怂恿深爱的人放手 提交于 2019-12-01 13:14:09

问题


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


来源:https://stackoverflow.com/questions/45305293/how-to-define-different-indentation-levels-in-the-same-document-with-xtext-forma

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!