Migrating from formatting to formatting2 [message #1699469] |
Wed, 24 June 2015 13:18 |
ayman salah Messages: 131 Registered: June 2015 |
Senior Member |
|
|
The DSL I am working on is huge and my formatter is complicated. I have built it with formatting (the old version). I am facing some problems that were addressed in the formatting2 (the newer version). I want to migrate from the formatting to formatting2.
Syntax wise, What is the best and fastest way to go from formatting to formatting2 keeping the same results?
Can I use java for formatting2? Because the examples I have seen are all in Xtend.
Are there any examples or tutorials for formatting2?
Thanks.
[Updated on: Wed, 24 June 2015 13:18] Report message to a moderator
|
|
|
|
|
Re: Migrating from formatting to formatting2 [message #1699496 is a reply to message #1699472] |
Wed, 24 June 2015 15:08 |
|
here is an example with the new formatter
Model:
elements+=Element*;
Element:
'element' name=ID '{'
features+=Feature*
'}';
terminal SL_COMMENT : '--' !('\n'|'\r')* ('\r'? '\n')?;
Feature: "feature" name=ID;
and here the formatter
class MyDslFormatter extends AbstractFormatter2 {
@Inject extension MyDslGrammarAccess
def dispatch void format(Model model, extension IFormattableDocument document) {
for (Element elements : model.getElements()) {
format(elements, document);
}
}
def dispatch void format(Element element, extension IFormattableDocument document) {
element.prepend[newLine]
val begin = element.regionForKeyword("{").prepend[space = " ";setNewLines(0,0,0)]
.append[newLine; increaseIndentation]
val end = element.regionForKeyword("}")
end.append[newLine; decreaseIndentation]
for (Feature features : element.getFeatures()) {
format(features, document);
}
}
def dispatch void format(Feature element, extension IFormattableDocument document) {
element.prepend[newLine]
}
override createCommentReplacer(IComment comment) {
val ge = comment.grammarElement
if (ge instanceof AbstractRule) {
if ("SL_COMMENT" == ge.name) {
return new SinglelineCodeCommentReplacer(comment, "--") {
override configureWhitespace(WhitespaceReplacer leading, WhitespaceReplacer trailing) {
leading.getFormatting().asFormatter();
}
}
}
}
super.createCommentReplacer(comment)
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.07266 seconds