From fcfe6722907d42a14fd88ee62f40c4862993b636 Mon Sep 17 00:00:00 2001 From: Vedran Pavic Date: Thu, 6 Jan 2022 18:27:15 +0100 Subject: [PATCH 1/4] Update `IndentStep` to allow leading space on multi-line comments 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 --- .../com/diffplug/spotless/generic/IndentStep.java | 8 +++++++- .../main/resources/indent/IndentedWithSpace.test | 13 +++++++++++-- .../src/main/resources/indent/IndentedWithTab.test | 13 +++++++++++-- .../diffplug/spotless/generic/IndentStepTest.java | 12 +++++++++++- 4 files changed, 40 insertions(+), 6 deletions(-) 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/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..93695a6e13 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"); + } } From 1499e7e796eed43f8a40b502139255caf2888bbe Mon Sep 17 00:00:00 2001 From: Vedran Pavic Date: Thu, 6 Jan 2022 18:35:06 +0100 Subject: [PATCH 2/4] Update changelogs --- CHANGES.md | 2 ++ plugin-gradle/CHANGES.md | 2 ++ plugin-maven/CHANGES.md | 2 ++ 3 files changed, 6 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index c67d650b44..1f0ca8086b 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 multi-line comments ([#1072](https://github.com/diffplug/spotless/pull/1072)). ## [2.21.1] - 2022-01-06 ### Changed diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 144ee057d6..9bb86ebc17 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 multi-line 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..2ef7c9c04b 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 multi-line comments ([#1072](https://github.com/diffplug/spotless/pull/1072)). ## [2.19.0] - 2022-01-06 ### Added From cc2aee0e62914c558ece7010ba31c32903edcb46 Mon Sep 17 00:00:00 2001 From: Vedran Pavic Date: Thu, 6 Jan 2022 20:30:34 +0100 Subject: [PATCH 3/4] Fix code style --- .../com/diffplug/spotless/generic/IndentStepTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 93695a6e13..c7df4639e3 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/IndentStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/IndentStepTest.java @@ -87,9 +87,9 @@ protected FormatterStep create() { @Test void allowLeadingSpaceIfMultiLineComment() throws Exception { StepHarness.forStep(IndentStep.Type.TAB.create(4)) - .testUnaffected("* test") - .testUnaffected(" * test") - .testUnaffected("\t* test") - .test(" * test", "\t* test"); + .testUnaffected("* test") + .testUnaffected(" * test") + .testUnaffected("\t* test") + .test(" * test", "\t* test"); } } From d5c3d65da8e6bcf7ac367ab751889b941bd8b7a0 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 6 Jan 2022 15:57:12 -0800 Subject: [PATCH 4/4] Minor changelog change just because CircleCI is jammed. --- CHANGES.md | 2 +- plugin-gradle/CHANGES.md | 2 +- plugin-maven/CHANGES.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 1f0ca8086b..be10219e07 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,7 +11,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Fixed -* Update IndentStep to allow leading space on multi-line comments ([#1072](https://github.com/diffplug/spotless/pull/1072)). +* 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/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 9bb86ebc17..1f8c3fa489 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -4,7 +4,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Fixed -* Update IndentStep to allow leading space on multi-line comments ([#1072](https://github.com/diffplug/spotless/pull/1072)). +* 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 2ef7c9c04b..2c81d28eef 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -4,7 +4,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Fixed -* Update IndentStep to allow leading space on multi-line comments ([#1072](https://github.com/diffplug/spotless/pull/1072)). +* Update IndentStep to allow leading space on multiline comments ([#1072](https://github.com/diffplug/spotless/pull/1072)). ## [2.19.0] - 2022-01-06 ### Added