Отступ строк текста (EString) с помощью API форматирования Xtext - PullRequest
0 голосов
/ 09 мая 2020

Как лучше всего сделать отступ для блока текста, состоящего из нескольких строк EString?

Обратите внимание на следующий сценарий.

Тестовый пример

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

Реализация форматирования

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

И результат ...

    ********** 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

Тест не пройден из-за отсутствия отступов.

Ура и спасибо за ваше время!

РЕДАКТИРОВАТЬ

Между прочим, нет c причины, по которой требуется для быть списком EString. Это был единственный способ заставить его работать.

// 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');
...