-
Couldn't load subscription status.
- Fork 489
Linting - breaking changes to internal API to prepare #2148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
48e744d
Remove crufty and internally-unused methods from `Formatter`.
nedtwigg 5843830
Moved `PaddedCell.DirtyState` to its own top-level class with new met…
nedtwigg 856ab9f
Removed `rootDir` property `Formatter` and its builder because it was…
nedtwigg b7f559a
Mark the tests which are broken as part of the lint refactor.
nedtwigg 7bd9c59
Remove the name property from Formatter, and route it in a different …
nedtwigg c6cfb6d
Remove the `FormatExceptionPolicy` from the `Formatter` because it wi…
nedtwigg baf0f11
And now the tests that do and don't work have shuffled once again.
nedtwigg 9347888
Reduce the size of the diff.
nedtwigg 5f159a0
No reason for DirtyState.Calculation to be public API.
nedtwigg baea8c9
In fact there wasn't any reason for the DirtyState.Calculation class …
nedtwigg 4f5bf51
Update contributor docs a bit.
nedtwigg 33444a7
Update changelog.
nedtwigg e1a9acb
Merge branch 'main' into feat/prepare-for-lint-take-2
nedtwigg 5822660
spotlessApply
nedtwigg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...c/test/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStepSpecialCaseTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
lib/src/main/java/com/diffplug/spotless/DirtyState.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * Copyright 2022-2024 DiffPlug | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.diffplug.spotless; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.nio.file.Files; | ||
| import java.util.Arrays; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * The clean/dirty state of a single file. Intended use: | ||
| * - {@link #isClean()} means that the file is is clean, and there's nothing else to say | ||
| * - {@link #didNotConverge()} means that we were unable to determine a clean state | ||
| * - once you've tested the above conditions and you know that it's a dirty file with a converged state, | ||
| * then you can call {@link #writeCanonicalTo(OutputStream)} to get the canonical form of the given file. | ||
| */ | ||
| public class DirtyState { | ||
| @Nullable | ||
| private final byte[] canonicalBytes; | ||
|
|
||
| DirtyState(@Nullable byte[] canonicalBytes) { | ||
| this.canonicalBytes = canonicalBytes; | ||
| } | ||
|
|
||
| public boolean isClean() { | ||
| return this == isClean; | ||
| } | ||
|
|
||
| public boolean didNotConverge() { | ||
| return this == didNotConverge; | ||
| } | ||
|
|
||
| private byte[] canonicalBytes() { | ||
| if (canonicalBytes == null) { | ||
| throw new IllegalStateException("First make sure that {@code !isClean()} and {@code !didNotConverge()}"); | ||
| } | ||
| return canonicalBytes; | ||
| } | ||
|
|
||
| public void writeCanonicalTo(File file) throws IOException { | ||
| Files.write(file.toPath(), canonicalBytes()); | ||
| } | ||
|
|
||
| public void writeCanonicalTo(OutputStream out) throws IOException { | ||
| out.write(canonicalBytes()); | ||
| } | ||
|
|
||
| /** Returns the DirtyState which corresponds to {@code isClean()}. */ | ||
| public static DirtyState clean() { | ||
| return isClean; | ||
| } | ||
|
|
||
| static final DirtyState didNotConverge = new DirtyState(null); | ||
| static final DirtyState isClean = new DirtyState(null); | ||
|
|
||
| public static DirtyState of(Formatter formatter, File file) throws IOException { | ||
| return of(formatter, file, Files.readAllBytes(file.toPath())); | ||
| } | ||
|
|
||
| public static DirtyState of(Formatter formatter, File file, byte[] rawBytes) { | ||
| String raw = new String(rawBytes, formatter.getEncoding()); | ||
| // check that all characters were encodable | ||
| String encodingError = EncodingErrorMsg.msg(raw, rawBytes, formatter.getEncoding()); | ||
| if (encodingError != null) { | ||
| throw new IllegalArgumentException(encodingError); | ||
| } | ||
|
|
||
| String rawUnix = LineEnding.toUnix(raw); | ||
|
|
||
| // enforce the format | ||
| String formattedUnix = formatter.compute(rawUnix, file); | ||
| // convert the line endings if necessary | ||
| String formatted = formatter.computeLineEndings(formattedUnix, file); | ||
|
|
||
| // if F(input) == input, then the formatter is well-behaving and the input is clean | ||
| byte[] formattedBytes = formatted.getBytes(formatter.getEncoding()); | ||
| if (Arrays.equals(rawBytes, formattedBytes)) { | ||
| return isClean; | ||
| } | ||
|
|
||
| // F(input) != input, so we'll do a padded check | ||
| String doubleFormattedUnix = formatter.compute(formattedUnix, file); | ||
| if (doubleFormattedUnix.equals(formattedUnix)) { | ||
| // most dirty files are idempotent-dirty, so this is a quick-short circuit for that common case | ||
| return new DirtyState(formattedBytes); | ||
| } | ||
|
|
||
| PaddedCell cell = PaddedCell.check(formatter, file, rawUnix); | ||
| if (!cell.isResolvable()) { | ||
| return didNotConverge; | ||
| } | ||
|
|
||
| // get the canonical bytes | ||
| String canonicalUnix = cell.canonical(); | ||
| String canonical = formatter.computeLineEndings(canonicalUnix, file); | ||
| byte[] canonicalBytes = canonical.getBytes(formatter.getEncoding()); | ||
| if (!Arrays.equals(rawBytes, canonicalBytes)) { | ||
| // and write them to disk if needed | ||
| return new DirtyState(canonicalBytes); | ||
| } else { | ||
| return isClean; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.