Skip to content

Commit 1804930

Browse files
authored
feat: clarify indent format option api (#2350 fixes #794)
2 parents 263609e + 629fc32 commit 1804930

File tree

5 files changed

+166
-5
lines changed

5 files changed

+166
-5
lines changed

plugin-gradle/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1111
### Fixed
1212
* You can now use `removeUnusedImports` and `googleJavaFormat` at the same time again. (fixes [#2159](https://github.com/diffplug/spotless/issues/2159))
1313
* The default list of type annotations used by `formatAnnotations` now includes Jakarta Validation's `Valid` and constraints validations (fixes [#2334](https://github.com/diffplug/spotless/issues/2334))
14+
* `indentWith[Spaces|Tabs]` has been deprecated in favor of `leadingTabsToSpaces` and `leadingSpacesToTabs`. ([#2350](https://github.com/diffplug/spotless/pull/2350) fixes [#794](https://github.com/diffplug/spotless/issues/794))
1415

1516
## [7.0.0.BETA4] - 2024-10-24
1617
### Added

plugin-gradle/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ spotless {
109109
110110
// define the steps to apply to those files
111111
trimTrailingWhitespace()
112-
indentWithTabs() // or spaces. Takes an integer argument if you don't like 4
112+
leadingSpacesToTabs() // or leadingTabsToSpaces. Takes an integer argument if you don't like 4
113113
endWithNewline()
114114
}
115115
java {

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java

+37-4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import org.gradle.api.plugins.BasePlugin;
4848
import org.gradle.api.tasks.TaskProvider;
4949
import org.gradle.util.GradleVersion;
50+
import org.slf4j.Logger;
51+
import org.slf4j.LoggerFactory;
5052

5153
import com.diffplug.common.base.Preconditions;
5254
import com.diffplug.spotless.FormatterFunc;
@@ -77,6 +79,9 @@
7779

7880
/** Adds a {@code spotless{Name}Check} and {@code spotless{Name}Apply} task. */
7981
public class FormatExtension {
82+
83+
private static final Logger logger = LoggerFactory.getLogger(FormatExtension.class);
84+
8085
final SpotlessExtension spotless;
8186
final List<Action<FormatExtension>> lazyActions = new ArrayList<>();
8287

@@ -505,25 +510,53 @@ public void endWithNewline() {
505510
}
506511

507512
/** Ensures that the files are indented using spaces. */
513+
public void leadingTabsToSpaces(int spacesPerTab) {
514+
addStep(IndentStep.Type.SPACE.create(spacesPerTab));
515+
}
516+
517+
@Deprecated
508518
public void indentWithSpaces(int numSpacesPerTab) {
509-
addStep(IndentStep.Type.SPACE.create(numSpacesPerTab));
519+
logDeprecation("indentWithSpaces", "leadingTabsToSpaces");
520+
leadingTabsToSpaces(numSpacesPerTab);
510521
}
511522

512523
/** Ensures that the files are indented using spaces. */
513-
public void indentWithSpaces() {
524+
public void leadingTabsToSpaces() {
514525
addStep(IndentStep.Type.SPACE.create());
515526
}
516527

528+
@Deprecated
529+
public void indentWithSpaces() {
530+
logDeprecation("indentWithSpaces", "leadingTabsToSpaces");
531+
leadingTabsToSpaces();
532+
}
533+
517534
/** Ensures that the files are indented using tabs. */
535+
public void leadingSpacesToTabs(int spacesPerTab) {
536+
addStep(IndentStep.Type.TAB.create(spacesPerTab));
537+
}
538+
539+
@Deprecated
518540
public void indentWithTabs(int tabToSpaces) {
519-
addStep(IndentStep.Type.TAB.create(tabToSpaces));
541+
logDeprecation("indentWithTabs", "leadingSpacesToTabs");
542+
leadingSpacesToTabs(tabToSpaces);
520543
}
521544

522545
/** Ensures that the files are indented using tabs. */
523-
public void indentWithTabs() {
546+
public void leadingSpacesToTabs() {
524547
addStep(IndentStep.Type.TAB.create());
525548
}
526549

550+
@Deprecated
551+
public void indentWithTabs() {
552+
logDeprecation("indentWithTabs", "leadingSpacesToTabs");
553+
leadingSpacesToTabs();
554+
}
555+
556+
private static void logDeprecation(String methodName, String replacement) {
557+
logger.warn("'{}' is deprecated, use '{}' in your gradle build script instead.", methodName, replacement);
558+
}
559+
527560
/** Ensures formatting of files via native binary. */
528561
public void nativeCmd(String name, String pathToExe, List<String> arguments) {
529562
addStep(NativeCmdStep.create(name, new File(pathToExe), arguments));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2021-2024 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.spotless;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import java.io.IOException;
21+
import java.util.stream.Stream;
22+
23+
import org.gradle.testkit.runner.BuildResult;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.Arguments;
26+
import org.junit.jupiter.params.provider.MethodSource;
27+
import org.junit.jupiter.params.provider.ValueSource;
28+
29+
class IndentIntegrationTest extends GradleIntegrationHarness {
30+
31+
@ParameterizedTest
32+
@ValueSource(strings = {"indentWithSpaces", "indentWithTabs"})
33+
void oldIndentApiLogsDeprecationWarning(String indentationMethodName) throws IOException {
34+
BuildResult result = runIndentFormatter(indentationMethodName);
35+
assertThat(result.getOutput()).containsPattern(".*" + indentationMethodName + ".*deprecated.*");
36+
}
37+
38+
@ParameterizedTest
39+
@ValueSource(strings = {"leadingTabsToSpaces", "leadingSpacesToTabs"})
40+
void newIndentApiDoesNotLogDeprecationWarning(String indentationMethodName) throws IOException {
41+
BuildResult result = runIndentFormatter(indentationMethodName);
42+
assertThat(result.getOutput()).doesNotContainPattern(".*" + indentationMethodName + ".*deprecated.*");
43+
}
44+
45+
@ParameterizedTest(name = "{0}")
46+
@MethodSource("indentationCombinations")
47+
void indentationCombinations(String testName, String indentationMethodName, String actualResource, String expectedResultResource) throws IOException {
48+
runIndentFormatter(indentationMethodName, actualResource);
49+
assertFile("test.txt").sameAsResource(expectedResultResource);
50+
}
51+
52+
private static Stream<Arguments> indentationCombinations() {
53+
return Stream.of(
54+
// new API
55+
Arguments.of("tabsToTabs", "leadingSpacesToTabs", "indent/IndentedWithTab.test", "indent/IndentedWithTab.test"),
56+
Arguments.of("spacesToSpaces", "leadingTabsToSpaces", "indent/IndentedWithSpace.test", "indent/IndentedWithSpace.test"),
57+
Arguments.of("spacesToTabs", "leadingSpacesToTabs", "indent/IndentedWithSpace.test", "indent/IndentedWithTab.test"),
58+
Arguments.of("tabsToSpaces", "leadingTabsToSpaces", "indent/IndentedWithTab.test", "indent/IndentedWithSpace.test"),
59+
Arguments.of("mixedToTabs", "leadingSpacesToTabs", "indent/IndentedMixed.test", "indent/IndentedWithTab.test"),
60+
Arguments.of("mixedToSpaces", "leadingTabsToSpaces", "indent/IndentedMixed.test", "indent/IndentedWithSpace.test"),
61+
// legacy API
62+
Arguments.of("legacy: tabsToTabs", "indentWithTabs", "indent/IndentedWithTab.test", "indent/IndentedWithTab.test"),
63+
Arguments.of("legacy: spacesToSpaces", "indentWithSpaces", "indent/IndentedWithSpace.test", "indent/IndentedWithSpace.test"),
64+
Arguments.of("legacy: spacesToTabs", "indentWithTabs", "indent/IndentedWithSpace.test", "indent/IndentedWithTab.test"),
65+
Arguments.of("legacy: tabsToSpaces", "indentWithSpaces", "indent/IndentedWithTab.test", "indent/IndentedWithSpace.test"),
66+
Arguments.of("legacy: mixedToTabs", "indentWithTabs", "indent/IndentedMixed.test", "indent/IndentedWithTab.test"),
67+
Arguments.of("legacy: mixedToSpaces", "indentWithSpaces", "indent/IndentedMixed.test", "indent/IndentedWithSpace.test"));
68+
}
69+
70+
private BuildResult runIndentFormatter(String indentationMethodName) throws IOException {
71+
return runIndentFormatter(indentationMethodName, "indent/IndentedMixed.test");
72+
}
73+
74+
private BuildResult runIndentFormatter(String indentationMethodName, String resourceFile) throws IOException {
75+
setFile("build.gradle").toLines(
76+
"plugins {",
77+
" id 'com.diffplug.spotless'",
78+
"}",
79+
"spotless {",
80+
" format 'test', {",
81+
" target '**/*.txt'",
82+
" " + indentationMethodName + "()",
83+
" }",
84+
"}");
85+
setFile("test.txt").toResource(resourceFile);
86+
BuildResult result = gradleRunner().withArguments("spotlessApply").build();
87+
return result;
88+
}
89+
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.github.youribonnaffe.gradle.format;
2+
3+
import java.util.function.Function;
4+
5+
/**
6+
* Test class.
7+
*/
8+
public class Java8Test {
9+
/**
10+
* Test method.
11+
*/
12+
public void doStuff() throws Exception {
13+
Function<String, Integer> example = Integer::parseInt;
14+
example.andThen(val -> {
15+
return val + 2;
16+
} );
17+
SimpleEnum val = SimpleEnum.A;
18+
switch (val) {
19+
case A:
20+
break;
21+
case B:
22+
break;
23+
case C:
24+
break;
25+
default:
26+
throw new Exception();
27+
}
28+
}
29+
30+
/** Test enum
31+
* with weirdly formatted javadoc
32+
* which IndentStep should not change
33+
*/
34+
public enum SimpleEnum {
35+
A, B, C;
36+
}
37+
}

0 commit comments

Comments
 (0)