问题
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