diff --git a/CHANGES.md b/CHANGES.md index c67d650b44..be10219e07 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Fixed +* Update IndentStep to allow leading space on multiline comments ([#1072](https://github.com/diffplug/spotless/pull/1072)). ## [2.21.1] - 2022-01-06 ### Changed diff --git a/lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java b/lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java index e50a45b82e..4281df736c 100644 --- a/lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java +++ b/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. @@ -100,6 +100,9 @@ String format(String raw) { ++contentStart; } + // detect potential multi-line comments + boolean mightBeMultiLineComment = (contentStart < raw.length()) && (raw.charAt(contentStart) == '*'); + // add the leading space in a canonical way if (numSpaces > 0) { switch (state.type) { @@ -112,6 +115,9 @@ String format(String raw) { for (int i = 0; i < numSpaces / state.numSpacesPerTab; ++i) { builder.append('\t'); } + if (mightBeMultiLineComment && (numSpaces % state.numSpacesPerTab == 1)) { + builder.append(' '); + } break; default: throw new IllegalArgumentException("Unexpected enum " + state.type); diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 144ee057d6..1f8c3fa489 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Fixed +* Update IndentStep to allow leading space on multiline comments ([#1072](https://github.com/diffplug/spotless/pull/1072)). ## [6.1.1] - 2022-01-06 ### Fixed diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index a9709b9d27..2c81d28eef 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Fixed +* Update IndentStep to allow leading space on multiline comments ([#1072](https://github.com/diffplug/spotless/pull/1072)). ## [2.19.0] - 2022-01-06 ### Added diff --git a/testlib/src/main/resources/indent/IndentedWithSpace.test b/testlib/src/main/resources/indent/IndentedWithSpace.test index 7fd1449575..582b0a9aca 100644 --- a/testlib/src/main/resources/indent/IndentedWithSpace.test +++ b/testlib/src/main/resources/indent/IndentedWithSpace.test @@ -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 example = Integer::parseInt; example.andThen(val -> { @@ -21,7 +26,11 @@ public class Java8Test { throw new Exception(); } } - + + /** Test enum + * with weirdly formatted javadoc + * which IndentStep should not change + */ public enum SimpleEnum { A, B, C; } diff --git a/testlib/src/main/resources/indent/IndentedWithTab.test b/testlib/src/main/resources/indent/IndentedWithTab.test index 83f1b7f793..9124dfa92f 100644 --- a/testlib/src/main/resources/indent/IndentedWithTab.test +++ b/testlib/src/main/resources/indent/IndentedWithTab.test @@ -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 example = Integer::parseInt; example.andThen(val -> { @@ -21,7 +26,11 @@ public class Java8Test { throw new Exception(); } } - + + /** Test enum + * with weirdly formatted javadoc + * which IndentStep should not change + */ public enum SimpleEnum { A, B, C; } diff --git a/testlib/src/test/java/com/diffplug/spotless/generic/IndentStepTest.java b/testlib/src/test/java/com/diffplug/spotless/generic/IndentStepTest.java index 33ca5a8262..c7df4639e3 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/IndentStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/IndentStepTest.java @@ -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. @@ -23,6 +23,7 @@ import com.diffplug.spotless.FormatterStep; import com.diffplug.spotless.ResourceHarness; import com.diffplug.spotless.SerializableEqualityTester; +import com.diffplug.spotless.StepHarness; class IndentStepTest extends ResourceHarness { @Test @@ -82,4 +83,13 @@ protected FormatterStep create() { } }.testEquals(); } + + @Test + void allowLeadingSpaceIfMultiLineComment() throws Exception { + StepHarness.forStep(IndentStep.Type.TAB.create(4)) + .testUnaffected("* test") + .testUnaffected(" * test") + .testUnaffected("\t* test") + .test(" * test", "\t* test"); + } }