-
Notifications
You must be signed in to change notification settings - Fork 473
Convert GJF to compile only dependency #1630
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
nedtwigg
merged 15 commits into
diffplug:main
from
simschla:convert-gfj-to-compile-only-dep
Mar 27, 2023
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8b24b22
use compile-only sourceset for gjf
simschla 30ef0d4
check minimum required formatter version
simschla 7355cb2
add test for min version check + adapt others
simschla 25c4ab3
adapt tests to new required min versions
simschla a7c2242
gfj: bump default version to 1.16.0
simschla e4ec8a5
add gjf conversion to CHANGES.md files
simschla b907a88
adapt min. required GJF versions in READMEs
simschla 94eaa5e
adapt to actual PR number
simschla cb0fd38
adaptions to min-required gjf version
simschla 048947b
adapt more gjf format specifications
simschla b0adcfa
clarify comment
simschla ab982d2
another pinned version to be upgraded
simschla abe30b8
remove obsolete fix code
simschla c65a0cf
revert testGlue as it is no longer needed
simschla 3aea790
formatting cleanup
simschla 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
71 changes: 71 additions & 0 deletions
71
.../googleJavaFormat/java/com/diffplug/spotless/glue/java/GoogleJavaFormatFormatterFunc.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,71 @@ | ||
/* | ||
* Copyright 2023 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.glue.java; | ||
|
||
import static com.diffplug.spotless.glue.java.GoogleJavaFormatUtils.fixWindowsBug; | ||
|
||
import java.util.Objects; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import com.google.googlejavaformat.java.Formatter; | ||
import com.google.googlejavaformat.java.FormatterException; | ||
import com.google.googlejavaformat.java.ImportOrderer; | ||
import com.google.googlejavaformat.java.JavaFormatterOptions; | ||
import com.google.googlejavaformat.java.RemoveUnusedImports; | ||
import com.google.googlejavaformat.java.StringWrapper; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
|
||
public class GoogleJavaFormatFormatterFunc implements FormatterFunc { | ||
|
||
@Nonnull | ||
private final Formatter formatter; | ||
|
||
@Nonnull | ||
private final String version; | ||
@Nonnull | ||
private final JavaFormatterOptions.Style formatterStyle; | ||
private final boolean reflowStrings; | ||
|
||
public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings) { | ||
this.version = Objects.requireNonNull(version); | ||
this.formatterStyle = JavaFormatterOptions.Style.valueOf(Objects.requireNonNull(style)); | ||
this.reflowStrings = reflowStrings; | ||
|
||
this.formatter = new Formatter(JavaFormatterOptions.builder() | ||
.style(formatterStyle) | ||
.build()); | ||
} | ||
|
||
@Override | ||
@Nonnull | ||
public String apply(@Nonnull String input) throws Exception { | ||
String formatted = formatter.formatSource(input); | ||
String removedUnused = RemoveUnusedImports.removeUnusedImports(formatted); | ||
String sortedImports = ImportOrderer.reorderImports(removedUnused, formatterStyle); | ||
String reflowedLongStrings = reflowLongStrings(sortedImports); | ||
return fixWindowsBug(reflowedLongStrings, version); | ||
} | ||
|
||
private String reflowLongStrings(String input) throws FormatterException { | ||
if (reflowStrings) { | ||
return StringWrapper.wrap(input, formatter); | ||
} else { | ||
return input; | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...va/com/diffplug/spotless/glue/java/GoogleJavaFormatRemoveUnusedImporterFormatterFunc.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,43 @@ | ||
/* | ||
* Copyright 2023 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.glue.java; | ||
|
||
import static com.diffplug.spotless.glue.java.GoogleJavaFormatUtils.fixWindowsBug; | ||
|
||
import java.util.Objects; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import com.google.googlejavaformat.java.RemoveUnusedImports; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
|
||
public class GoogleJavaFormatRemoveUnusedImporterFormatterFunc implements FormatterFunc { | ||
|
||
@Nonnull | ||
private final String version; | ||
|
||
public GoogleJavaFormatRemoveUnusedImporterFormatterFunc(@Nonnull String version) { | ||
this.version = Objects.requireNonNull(version); | ||
} | ||
|
||
@Override | ||
@Nonnull | ||
public String apply(@Nonnull String input) throws Exception { | ||
String removedUnused = RemoveUnusedImports.removeUnusedImports(input); | ||
return fixWindowsBug(removedUnused, version); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
lib/src/googleJavaFormat/java/com/diffplug/spotless/glue/java/GoogleJavaFormatUtils.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,54 @@ | ||
/* | ||
* Copyright 2023 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.glue.java; | ||
|
||
import com.diffplug.spotless.LineEnding; | ||
|
||
public class GoogleJavaFormatUtils { | ||
|
||
private static final boolean IS_WINDOWS = LineEnding.PLATFORM_NATIVE.str().equals("\r\n"); | ||
|
||
/** | ||
* google-java-format-1.1's removeUnusedImports does *wacky* stuff on Windows. | ||
* The beauty of normalizing all line endings to unix! | ||
*/ | ||
static String fixWindowsBug(String input, String version) { | ||
if (IS_WINDOWS && version.equals("1.1")) { | ||
int firstImport = input.indexOf("\nimport "); | ||
if (firstImport == 0) { | ||
return input; | ||
} else if (firstImport > 0) { | ||
int numToTrim = 0; | ||
char prevChar; | ||
do { | ||
++numToTrim; | ||
prevChar = input.charAt(firstImport - numToTrim); | ||
} while (Character.isWhitespace(prevChar) && (firstImport - numToTrim) > 0); | ||
if (firstImport - numToTrim == 0) { | ||
// import was the very first line, and we'd like to maintain a one-line gap | ||
++numToTrim; | ||
} else if (prevChar == ';' || prevChar == '/') { | ||
// import came after either license or a package declaration | ||
--numToTrim; | ||
} | ||
if (numToTrim > 0) { | ||
return input.substring(0, firstImport - numToTrim + 2) + input.substring(firstImport + 1); | ||
} | ||
} | ||
} | ||
return input; | ||
} | ||
} |
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.