Skip to content

Commit

Permalink
Update IndentStep to allow leading space on multi-line comments
Browse files Browse the repository at this point in the history
At present, `indentWithTabs` doesn't handle leading space on multi-line comments and reports errors on properly formatted code.

This commit updates `IndentStep` to add support for detecting multi-line comments and allowing a single leading space for such lines.

Closes #1070
  • Loading branch information
vpavic committed Jan 6, 2022
1 parent e98684a commit 1b01d73
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
14 changes: 13 additions & 1 deletion lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -100,6 +100,15 @@ String format(String raw) {
++contentStart;
}

// detect multi-line comments
boolean multiLineComment = false;
if (contentStart < raw.length() && raw.charAt(contentStart) == '*') {
multiLineComment = true;
if (contentStart == lineStart) {
++numSpaces; // multi-line comment without a leading space
}
}

// add the leading space in a canonical way
if (numSpaces > 0) {
switch (state.type) {
Expand All @@ -112,6 +121,9 @@ String format(String raw) {
for (int i = 0; i < numSpaces / state.numSpacesPerTab; ++i) {
builder.append('\t');
}
if (multiLineComment) {
builder.append(' ');
}
break;
default:
throw new IllegalArgumentException("Unexpected enum " + state.type);
Expand Down
12 changes: 10 additions & 2 deletions testlib/src/main/resources/indent/IndentedWithSpace.test
Expand Up @@ -2,8 +2,13 @@ package com.github.youribonnaffe.gradle.format;

import java.util.function.Function;


/**
* Test class.
*/
public class Java8Test {
/**
* Test method.
*/
public void doStuff() throws Exception {
Function<String, Integer> example = Integer::parseInt;
example.andThen(val -> {
Expand All @@ -21,7 +26,10 @@ public class Java8Test {
throw new Exception();
}
}


/**
* Test enum.
*/
public enum SimpleEnum {
A, B, C;
}
Expand Down
12 changes: 10 additions & 2 deletions testlib/src/main/resources/indent/IndentedWithTab.test
Expand Up @@ -2,8 +2,13 @@ package com.github.youribonnaffe.gradle.format;

import java.util.function.Function;


/**
* Test class.
*/
public class Java8Test {
/**
* Test method.
*/
public void doStuff() throws Exception {
Function<String, Integer> example = Integer::parseInt;
example.andThen(val -> {
Expand All @@ -21,7 +26,10 @@ public class Java8Test {
throw new Exception();
}
}


/**
* Test enum.
*/
public enum SimpleEnum {
A, B, C;
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -82,4 +82,19 @@ protected FormatterStep create() {
}
}.testEquals();
}

@Test
void allowLeadingSpaceIfMultiLineComment() {
FormatterStep indent = IndentStep.Type.TAB.create(4);
Assertions.assertAll("class multi-line comments",
() -> Assertions.assertEquals(" * test", indent.format("* test", new File(""))),
() -> Assertions.assertEquals(" * test", indent.format(" * test", new File(""))),
() -> Assertions.assertEquals(" * test", indent.format(" * test", new File(""))),
() -> Assertions.assertEquals(" * test", indent.format(" * test", new File(""))));
Assertions.assertAll("method multi-line comments",
() -> Assertions.assertEquals("\t * test", indent.format("\t* test", new File(""))),
() -> Assertions.assertEquals("\t * test", indent.format("\t * test", new File(""))),
() -> Assertions.assertEquals("\t * test", indent.format("\t * test", new File(""))),
() -> Assertions.assertEquals("\t * test", indent.format("\t * test", new File(""))));
}
}

0 comments on commit 1b01d73

Please sign in to comment.