As noted here, our indentWithTabs space has some surprising behavior when used with C-style multiline comments, for example:
/**
* A simple greeting task.
*/
abstract class GreetingTask extends DefaultTask {}
Gets errored out because Spotless dumps the leading spaces
/**
-·*·A·simple·greeting·task.
-·*/
+*·A·simple·greeting·task.
+*/
A hard limitation is that the indentWithTabs step can't know what language it is indenting. But it is pretty easy to detect that the leading whitespace of a line was terminated by a *, and then note that the indentation had one more space than expected, and just preserve that space.
You would detect that the first non-whitespace character is a * here to set boolean mightBeMultilineComment = true/false.
|
char c; |
|
while (contentStart < raw.length() && isSpaceOrTab(c = raw.charAt(contentStart))) { |
|
switch (c) { |
|
case ' ': |
|
++numSpaces; |
|
break; |
|
case '\t': |
|
numSpaces += state.numSpacesPerTab; |
|
break; |
|
default: |
|
throw new IllegalArgumentException("Unexpected char " + c); |
|
} |
|
++contentStart; |
|
} |
|
|
And then do some remainder math here to add an extra space if necessary.
|
case TAB: |
|
for (int i = 0; i < numSpaces / state.numSpacesPerTab; ++i) { |
|
builder.append('\t'); |
|
} |
|
break; |
I don't plan to implement this, but I'm happy to merge a PR for this (current test is here).
As noted here, our
indentWithTabsspace has some surprising behavior when used with C-style multiline comments, for example:Gets errored out because Spotless dumps the leading spaces
A hard limitation is that the
indentWithTabsstep can't know what language it is indenting. But it is pretty easy to detect that the leading whitespace of a line was terminated by a*, and then note that the indentation had one more space than expected, and just preserve that space.You would detect that the first non-whitespace character is a
*here to setboolean mightBeMultilineComment = true/false.spotless/lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java
Lines 88 to 102 in b3a6e36
And then do some remainder math here to add an extra space if necessary.
spotless/lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java
Lines 111 to 115 in b3a6e36
I don't plan to implement this, but I'm happy to merge a PR for this (current test is here).