diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d2ce2beae..1db537e39 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -83,16 +83,18 @@ jobs: build-native-image: name: "Build GraalVM native-image on ${{ matrix.os }}" runs-on: ${{ matrix.os }} + permissions: + contents: write needs: build-maven-jars strategy: matrix: # Use "oldest" available ubuntu-* instead of -latest, # see https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners/about-github-hosted-runners#standard-github-hosted-runners-for-public-repositories; # due to https://github.com/google/google-java-format/issues/1072. - os: [ubuntu-20.04, macos-latest, windows-latest] + os: [ubuntu-20.04, ubuntu-22.04-arm, macos-latest, windows-latest] env: # NB: Must keep the keys in this inline JSON below in line with the os: above! - SUFFIX: ${{fromJson('{"ubuntu-20.04":"linux-x86-64", "macos-latest":"darwin-arm64", "windows-latest":"windows-x86-64"}')[matrix.os]}} + SUFFIX: ${{fromJson('{"ubuntu-20.04":"linux-x86-64", "ubuntu-22.04-arm":"linux-arm64", "macos-latest":"darwin-arm64", "windows-latest":"windows-x86-64"}')[matrix.os]}} EXTENSION: ${{ matrix.os == 'windows-latest' && '.exe' || '' }} steps: - name: "Check out repository" diff --git a/.mvn/jvm.config b/.mvn/jvm.config index 504456f90..2e8f1d41f 100644 --- a/.mvn/jvm.config +++ b/.mvn/jvm.config @@ -8,3 +8,6 @@ --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED + +-Djdk.xml.maxGeneralEntitySizeLimit=0 +-Djdk.xml.totalEntitySizeLimit=0 \ No newline at end of file diff --git a/README.md b/README.md index 83a524bbd..d3836efb2 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,11 @@ and run it with: java -jar /path/to/google-java-format-${GJF_VERSION?}-all-deps.jar [files...] ``` +Note that it uses the `jdk.compiler` module to parse the Java source code. The +`java` binary version used must therefore be from a JDK (not JRE) with a version +equal to or newer than the Java language version of the files being formatted. +An alternative is to use the available GraalVM based native binaries instead. + The formatter can act on whole files, on limited lines (`--lines`), on specific offsets (`--offset`), passing through to standard-out (default) or altered in-place (`--replace`). diff --git a/core/pom.xml b/core/pom.xml index 5b6bbb4d7..ad18413ab 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -22,7 +22,7 @@ com.google.googlejavaformat google-java-format-parent - HEAD-SNAPSHOT + 1.26.0 google-java-format diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java index dcaa930e4..f21f86a5c 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -42,6 +42,7 @@ import static com.sun.source.tree.Tree.Kind.STRING_LITERAL; import static com.sun.source.tree.Tree.Kind.UNION_TYPE; import static com.sun.source.tree.Tree.Kind.VARIABLE; +import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; import com.google.auto.value.AutoOneOf; @@ -71,6 +72,7 @@ import com.google.googlejavaformat.FormattingError; import com.google.googlejavaformat.Indent; import com.google.googlejavaformat.Input; +import com.google.googlejavaformat.Newlines; import com.google.googlejavaformat.Op; import com.google.googlejavaformat.OpenOp; import com.google.googlejavaformat.OpsBuilder; @@ -1753,6 +1755,23 @@ public Void visitMemberSelect(MemberSelectTree node, Void unused) { public Void visitLiteral(LiteralTree node, Void unused) { sync(node); String sourceForNode = getSourceForNode(node, getCurrentPath()); + if (sourceForNode.startsWith("\"\"\"")) { + String separator = Newlines.guessLineSeparator(sourceForNode); + ImmutableList initialLines = sourceForNode.lines().collect(toImmutableList()); + String stripped = initialLines.stream().skip(1).collect(joining(separator)).stripIndent(); + // Use the last line of the text block to determine if it is deindented to column 0, by + // comparing the length of the line in the input source with the length after processing + // the text block contents with stripIndent(). + boolean deindent = + getLast(initialLines).stripTrailing().length() + == Streams.findLast(stripped.lines()).orElseThrow().stripTrailing().length(); + if (deindent) { + Indent indent = Indent.Const.make(Integer.MIN_VALUE / indentMultiplier, indentMultiplier); + builder.breakOp(indent); + } + token(sourceForNode); + return null; + } if (isUnaryMinusLiteral(sourceForNode)) { token("-"); sourceForNode = sourceForNode.substring(1).trim(); diff --git a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java index 5d10da388..27450d82f 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java +++ b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java @@ -188,15 +188,10 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) { private void indentTextBlocks( TreeRangeMap replacements, List textBlocks) { for (Tree tree : textBlocks) { - int startPosition = getStartPosition(tree); + int startPosition = lineMap.getStartPosition(lineMap.getLineNumber(getStartPosition(tree))); int endPosition = getEndPosition(unit, tree); String text = input.substring(startPosition, endPosition); - int lineStartPosition = lineMap.getStartPosition(lineMap.getLineNumber(startPosition)); - int startColumn = - CharMatcher.whitespace() - .negate() - .indexIn(input.substring(lineStartPosition, endPosition)) - + 1; + int leadingWhitespace = CharMatcher.whitespace().negate().indexIn(text); // Find the source code of the text block with incidental whitespace removed. // The first line of the text block is always """, and it does not affect incidental @@ -204,17 +199,13 @@ private void indentTextBlocks( ImmutableList initialLines = text.lines().collect(toImmutableList()); String stripped = initialLines.stream().skip(1).collect(joining(separator)).stripIndent(); ImmutableList lines = stripped.lines().collect(toImmutableList()); - int deindent = + boolean deindent = getLast(initialLines).stripTrailing().length() - - getLast(lines).stripTrailing().length(); + == getLast(lines).stripTrailing().length(); - String prefix = - (deindent == 0 - || lines.stream().anyMatch(x -> x.length() + startColumn - 1 > columnLimit)) - ? "" - : " ".repeat(startColumn - 1); + String prefix = deindent ? "" : " ".repeat(leadingWhitespace); - StringBuilder output = new StringBuilder(initialLines.get(0).stripLeading()); + StringBuilder output = new StringBuilder(prefix).append(initialLines.get(0).stripLeading()); for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); String trimmed = line.stripTrailing(); diff --git a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java index b83be477b..6a73da185 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java @@ -48,20 +48,6 @@ public class FormatterIntegrationTest { private static final ImmutableMultimap VERSIONED_TESTS = ImmutableMultimap.builder() - .putAll( - 14, - "I477", - "Records", - "RSLs", - "Var", - "ExpressionSwitch", - "I574", - "I594", - "SwitchComment", - "B380299722") - .putAll(15, "I603") - .putAll(16, "I588", "Sealed") - .putAll(17, "I683", "I684", "I696") .putAll( 21, "SwitchGuardClause", diff --git a/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java b/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java index 339ed1340..a9ceeacb6 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java @@ -63,7 +63,7 @@ public void textBlock() throws Exception { " private String myString;", " private ReproBug() {", " String str =", - " \"\"\"", + "\"\"\"", "{\"sourceEndpoint\":\"ri.something.1-1.object-internal.1\",\"targetEndpoint" + "\":\"ri.something.1-1.object-internal.2\",\"typeId\":\"typeId\"}\\", "\"\"\";", diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B361077825.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B361077825.output index 62344bd54..c93942a5d 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B361077825.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B361077825.output @@ -10,6 +10,6 @@ class T { """; String c = """ - # No implicit input file, because they can only be created outside a symbolic macro, -"""; + # No implicit input file, because they can only be created outside a symbolic macro, + """; } diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/RSLs.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/RSLs.input index 6eaa28558..002b3653a 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/RSLs.input +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/RSLs.input @@ -40,6 +40,11 @@ class RSLs { lorem ipsum """; + String l = + """ + foo +bar + baz"""; { f(""" lorem diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/RSLs.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/RSLs.output index 6e9a3ae5b..af4a8a6fc 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/RSLs.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/RSLs.output @@ -43,14 +43,20 @@ class RSLs { """; String j = """ -lorem -one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt -ipsum -"""; + lorem + one long incredibly unbroken sentence moving from topic to topic so that no one had a chance to interrupt + ipsum + """; String k = - """ +""" lorem ipsum +"""; + String l = +""" + foo +bar + baz\ """; { @@ -85,10 +91,11 @@ ipsum bar """; String t = - """ +""" foo """ - + """ + + +""" bar """; String u = diff --git a/eclipse_plugin/META-INF/MANIFEST.MF b/eclipse_plugin/META-INF/MANIFEST.MF index 913245393..62d8b0135 100644 --- a/eclipse_plugin/META-INF/MANIFEST.MF +++ b/eclipse_plugin/META-INF/MANIFEST.MF @@ -3,7 +3,7 @@ Bundle-ManifestVersion: 2 Bundle-Name: google-java-format Bundle-SymbolicName: google-java-format-eclipse-plugin;singleton:=true Bundle-Vendor: Google -Bundle-Version: 1.13.0 +Bundle-Version: 1.26.0 Bundle-RequiredExecutionEnvironment: JavaSE-11 Require-Bundle: org.eclipse.jdt.core;bundle-version="3.10.0", org.eclipse.jface, diff --git a/eclipse_plugin/pom.xml b/eclipse_plugin/pom.xml index b2c6e368a..33b23d6be 100644 --- a/eclipse_plugin/pom.xml +++ b/eclipse_plugin/pom.xml @@ -22,7 +22,7 @@ com.google.googlejavaformat google-java-format-eclipse-plugin eclipse-plugin - 1.13.0 + 1.26.0 Google Java Format Plugin for Eclipse 4.5+ diff --git a/idea_plugin/build.gradle.kts b/idea_plugin/build.gradle.kts index b2405603c..d99451333 100644 --- a/idea_plugin/build.gradle.kts +++ b/idea_plugin/build.gradle.kts @@ -30,7 +30,7 @@ repositories { } // https://github.com/google/google-java-format/releases -val googleJavaFormatVersion = "1.25.1" +val googleJavaFormatVersion = "1.25.2" java { sourceCompatibility = JavaVersion.VERSION_17 diff --git a/idea_plugin/src/main/resources/META-INF/plugin.xml b/idea_plugin/src/main/resources/META-INF/plugin.xml index edede3151..f7188f929 100644 --- a/idea_plugin/src/main/resources/META-INF/plugin.xml +++ b/idea_plugin/src/main/resources/META-INF/plugin.xml @@ -35,6 +35,8 @@ ]]> +
1.25.2.0
+
Updated to use google-java-format 1.25.2.
1.25.1.0
Updated to use google-java-format 1.25.1.
1.25.0.0
diff --git a/pom.xml b/pom.xml index f6efb7b7d..dc86c4b6e 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ com.google.googlejavaformat google-java-format-parent pom - HEAD-SNAPSHOT + 1.26.0 core @@ -89,7 +89,7 @@ 32.1.3-jre 1.4.0 1.0.0 - 2.36.0 + 2.37.0 1.9 1.0.1 3.6.3