Skip to content

indentWithTabs ought to preserve C-style multiline comments #1070

@nedtwigg

Description

@nedtwigg

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).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions