Indent lines of text (EString) using Xtext formatting API

耗尽温柔 提交于 2021-01-29 08:22:29

问题


What is the appropriate way to indent a block of text which consists of multiple lines of EString?

Please consider the following scenario.

Test Case

@Test
def void indentNarrative() {

    val toBeFormatted = '''
        Feature: The quick brown fox 
        Jumps over
        The lazy dog
    '''

    val expectation = '''
        Feature: The quick brown fox 
            Jumps over
            The lazy dog
    '''

    assertFormatted(toBeFormatted, expectation)
}

Formatter Implementation

def dispatch void format(Feature feature, extension IFormattableDocument document) {
    // indent interior
    feature.interior[indent]

    feature.narrative.format()

     println("********** DOCUMENT FORMAT **********")
     println(document)
}

def dispatch void format(Narrative narrative, extension IFormattableDocument document) {
    narrative.interior[indent]

    // Not working
    //val region = narrative.regionFor.feature(NARRATIVE__LINES)
    //region.prepend[indent]
}

And the result...

    ********** DOCUMENT FORMAT **********
    ----------- RootDocument with ITextReplacers (syntax: <offset|text>) -----------
    Feature:<8| >The quick brown fox 
    <30|>Jumps over
    The lazy dog
    <54|>
    --------------------------------------------------------------------------------
    8 1 " ": HiddenRegionReplacer: indentInc=1
    30 0 "": HiddenRegionReplacer: indentDec=2
    54 0 "": HiddenRegionReplacer: indentInc=1

Test fails due to no indentation.

Cheers, and thanks for your time!

EDIT

By the way, there is no specific reason it needs to be a list of EString. This was just the only way I could get it to work.

// The Description is expressed by any sequence of words that must not contain any keywords at start of lines.
// Description := (Word Space?)* ;
Description:
    lines+=DescriptionLine
;

DescriptionLine:
    ((WORD) (WORD)* EOL)+
;

terminal WORD: (LETTER)(LETTER|DIGIT)*;
terminal fragment LETTER: ('a'..'z'|'A'..'Z');
terminal fragment DIGIT: ('0'..'9');

terminal EOL: NL;
terminal fragment NL: ('\r'? '\n');

来源:https://stackoverflow.com/questions/61699940/indent-lines-of-text-estring-using-xtext-formatting-api

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