[solved] Indentation of elements in EList using formatting2 API [message #1827721] |
Thu, 21 May 2020 17:31 |
Aaron Miller Messages: 5 Registered: May 2020 |
Junior Member |
|
|
Hello,
I am having trouble figuring out how to indent elements in an EList using the formatting2 API. Everything I've tried so far leads to this strange "indentInc=1;indentDec=1" result or an error along the lines of "That is not allowed on an AST element.
Could anyone please help me understand what is happening when I get "indentInc=1;indentDec=1"? And of course any guidance on the proper way to go about this will be greatly appreciated.
Cheers and thank you!
grammar org.xtext.example.mydsl.MyDsl hidden(WS)
import "URL_REMOVED" as ecore
generate myDsl "URL_REMOVED"
Model:
greeting=Greeting
;
Greeting:
'Hello' ','? name=SIMPLE_WORD '!' NL+
lines+=Line*
;
Line: {Line}
SIMPLE_WORD+
NL
;
terminal SIMPLE_WORD:
('0'..'9' | 'a'..'z' | 'A'..'Z')
('0'..'9' | 'a'..'z' | 'A'..'Z' | '-' | '_')*
;
terminal NL: ('\r'? '\n');
terminal WS: (' ' | '\t');
class MyDslFormatter extends AbstractFormatter2 {
@Inject extension MyDslGrammarAccess
def dispatch void format(Model model, extension IFormattableDocument document) {
model.greeting.format()
println(document)
}
def dispatch void format(Greeting greeting, extension IFormattableDocument document) {
for (line : greeting.lines) {
line.format()
line.prepend[indent]
}
}
def dispatch void format(Line line, extension IFormattableDocument document) {
// TODO...
}
}
@RunWith(XtextRunner)
@InjectWith(MyDslInjectorProvider)
class MyDslFormattingTest {
@Inject extension FormatterTestHelper
@Test
def void indentLines() {
assertFormatted[
toBeFormatted = '''
Hello, World!
The quick brown fox
Jumps over the lazy dog
'''
expectation = '''
Hello, World!
The quick brown fox
Jumps over the lazy dog
'''
]
}
}
Quote:
----------- RootDocument with ITextReplacers (syntax: <offset|text>) -----------
Hello, World!
<14|>The quick brown fox
<34|>Jumps over the lazy dog
--------------------------------------------------------------------------------
14 0 "": HiddenRegionReplacer: indentInc=1;indentDec=1
34 0 "": HiddenRegionReplacer: indentInc=1;indentDec=1
[Updated on: Fri, 22 May 2020 20:20] Report message to a moderator
|
|
|
Re: Indentation of elements in EList using formatting2 API [message #1827739 is a reply to message #1827721] |
Fri, 22 May 2020 08:50 |
|
Hello Aaron,
the following configuration works for me:
def dispatch void format(Greeting greeting, extension IFormattableDocument document) {
val begin = greeting.regionFor.ruleCallTo(NLRule)
val end = greeting.lines.last.regionFor.ruleCallTo(NLRule)
interior(begin, end)[indent]
for (line : greeting.lines) {
line.format
line.prepend[indent]
}
}
Hope that helps,
Tamás
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03148 seconds