You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: CONTRIBUTING.md
+14-16
Original file line number
Diff line number
Diff line change
@@ -10,21 +10,20 @@ In order to use and combine `FormatterStep`, you first create a `Formatter`, whi
10
10
11
11
- an encoding
12
12
- a list of `FormatterStep`
13
-
- a line endings policy (`LineEnding.GIT_ATTRIBUTES` is almost always the best choice)
13
+
- a line endings policy (`LineEnding.GIT_ATTRIBUTES_FAST_ALLSAME` is almost always the best choice)
14
14
15
-
Once you have an instance of `Formatter`, you can call `boolean isClean(File)`, or `void applyTo(File)` to either check or apply formatting to a file. Spotless will then:
15
+
Once you have an instance of `Formatter`, you can call `DirtyState.of(Formatter, File)`. Under the hood, Spotless will:
16
16
17
17
- parse the raw bytes into a String according to the encoding
18
18
- normalize its line endings to `\n`
19
19
- pass the unix string to each `FormatterStep` one after the other
20
+
- check for idempotence problems, and repeatedly apply the steps until the [result is stable](PADDEDCELL.md).
20
21
- apply line endings according to the policy
21
22
22
23
You can also use lower-level methods like `String compute(String unix, File file)` if you'd like to do lower-level processing.
23
24
24
25
All `FormatterStep` implement `Serializable`, `equals`, and `hashCode`, so build systems that support up-to-date checks can easily and correctly determine if any actions need to be taken.
25
26
26
-
Spotless also provides `PaddedCell`, which makes it easy to diagnose and correct idempotence problems.
27
-
28
27
## Project layout
29
28
30
29
For the folders below in monospace text, they are published on MavenCentral at the coordinate `com.diffplug.spotless:spotless-${FOLDER_NAME}`. The other folders are dev infrastructure.
@@ -39,15 +38,16 @@ For the folders below in monospace text, they are published on MavenCentral at t
39
38
40
39
## How to add a new FormatterStep
41
40
42
-
The easiest way to create a FormatterStep is `FormatterStep createNeverUpToDate(String name, FormatterFunc function)`, which you can use like this:
41
+
The easiest way to create a FormatterStep is to just create `class FooStep implements FormatterStep`. It has one abstract method which is the formatting function, and you're ready to tinker. To work with the build plugins, this class will need to
You can use `StepHarness` (if you don't care about the `File` argument) or `StepHarnessWithFile` to test. The harness will roundtrip serialize your step, check that it's equal to itself, and then perform all tests on the roundtripped step.
47
47
48
-
This creates a step which will fail up-to-date checks (it is equal only to itself), and will use the function you passed in to do the formatting pass.
48
+
## Implementing equality in terms of serialization
49
49
50
-
To create a step which can handle up-to-date checks properly, use the method `<State extends Serializable> FormatterStep create(String name, State state, Function<State, FormatterFunc> stateToFormatter)`. Here's an example:
50
+
Spotless has infrastructure which uses the serialized form of your step to implement equality for you. Here is an example:
51
51
52
52
```java
53
53
publicfinalclassReplaceStep {
@@ -62,10 +62,10 @@ public final class ReplaceStep {
@@ -82,8 +82,6 @@ The `FormatterStep` created above implements `equals` and `hashCode` based on th
82
82
Oftentimes, a rule's state will be expensive to compute. `EclipseFormatterStep`, for example, depends on a formatting file. Ideally, we would like to only pay the cost of the I/O needed to load that file if we have to - we'd like to create the FormatterStep now but load its state lazily at the last possible moment. For this purpose, each of the `FormatterStep.create` methods has a lazy counterpart. Here are their signatures:
83
83
84
84
```java
85
-
FormatterStep createNeverUpToDate (String name, FormatterFunc function )
@@ -101,7 +99,7 @@ Here's a checklist for creating a new step for Spotless:
101
99
102
100
### Serialization roundtrip
103
101
104
-
In order to support Gradle's configuration cache, all `FormatterStep` must be round-trip serializable. This is a bit tricky because step equality is based on the serialized form of the state, and `transient`is used to take absolute paths out of the equality check. To make this work, roundtrip compatible steps actually have *two* states:
102
+
In order to support Gradle's configuration cache, all `FormatterStep` must be round-trip serializable. This is a bit tricky because step equality is based on the serialized form of the state, and `transient`can be used to take absolute paths out of the equality check. To make this work, roundtrip compatible steps can actually have *two* states:
105
103
106
104
-`RoundtripState` which must be roundtrip serializable but has no equality constraints
107
105
-`FileSignature.Promised` for settings files and `JarState.Promised` for the classpath
0 commit comments