I am using FQN rule ID ('.' ID)*
and it works but it allows to type e.g. p1.p2. p3
(I want that behavior but after formatting I want it to be p1.p2.p3
).
Is it possible to eliminate these whitespaces in formatter?
I am using FQN rule ID ('.' ID)*
and it works but it allows to type e.g. p1.p2. p3
(I want that behavior but after formatting I want it to be p1.p2.p3
).
Is it possible to eliminate these whitespaces in formatter?
Thanks to Christian Dietrich for providing answer here.
I modified it for this problem. Keep in mind that TAB is treated differently from spaces and also there are differences between Windows and Unix new lines. On Windows new line consists of '\r\n' (CR LF in ascii table) and on Unix new line consists of '\n' (LF in ascii table).
For the given grammar:
Model:
greetings+=Greeting*;
Greeting:
'Hello' name=FQN '!';
FQN:
ID ('.' ID)*;
Formatter looks like this:
def dispatch void format(Greeting model, extension IFormattableDocument document) {
model.prepend[newLine]
val region = model.regionFor.feature(MyDslPackage.Literals.GREETING__NAME)
val r = new AbstractTextReplacer(document, region) {
override createReplacements(ITextReplacerContext it) {
var text = region.text.replace("\t", " ").replace("\n", " ")
if (System.getProperty("os.name").toLowerCase().contains("win")) {
text = text.replace("\r", " ")
}
val offset = region.offset
var int index = text.indexOf(" ");
while (index >=0) {
it.addReplacement(region.textRegionAccess.rewriter.createReplacement(offset + index, 1, "")
index = text.indexOf(" ", index + 1);
}
it
}
}
addReplacer(r) }