From 4eb2a4da83d3929842263fed4fb50d9f4fa73ad7 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Wed, 26 Jul 2023 18:14:02 -0700 Subject: [PATCH 01/10] Update Guava version https://github.com/google/google-java-format/issues/950 PiperOrigin-RevId: 551370402 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 243da54d..1fe2803e 100644 --- a/pom.xml +++ b/pom.xml @@ -86,7 +86,7 @@ UTF-8 1.8 - 31.1-jre + 32.1.1-jre 1.1.3 3.21.2 2.16 From 4ebb6f5a00d0f933dcc68ad8673e740700fcf436 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Sun, 6 Aug 2023 09:31:43 -0700 Subject: [PATCH 02/10] Update ci.yml Update latest STS from 19 to 20, add 22-ea --- .github/workflows/ci.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 716abcc0..3b735636 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,22 +29,22 @@ jobs: fail-fast: false matrix: os: [ ubuntu-latest ] - java: [ 19, 17, 11 ] + java: [ 20, 17, 11 ] experimental: [ false ] include: # Only test on macos and windows with a single recent JDK to avoid a # combinatorial explosion of test configurations. - os: macos-latest - java: 19 + java: 20 experimental: false - os: windows-latest - java: 19 + java: 20 experimental: false - os: ubuntu-latest - java: 20-ea + java: 21-ea experimental: true - os: ubuntu-latest - java: 21-ea + java: 22-ea experimental: true runs-on: ${{ matrix.os }} continue-on-error: ${{ matrix.experimental }} From 28b199c54957bb10d203bb5d7a0e3ed82603f5e8 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Wed, 6 Sep 2023 11:23:56 -0700 Subject: [PATCH 03/10] Fix `java.lang.RuntimeException: Document is locked by write PSI operations` errors Also update to `google-java-format` 1.17.0 Fixes #960 COPYBARA_INTEGRATE_REVIEW=https://github.com/google/google-java-format/pull/960 from facboy:master e0925a2e7d7c94e84a1c1fd6bb95ff9ca1b66c79 PiperOrigin-RevId: 563163224 --- idea_plugin/build.gradle.kts | 18 ++++++------ .../GoogleJavaFormatImportOptimizer.java | 28 ++++++++++++++++--- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/idea_plugin/build.gradle.kts b/idea_plugin/build.gradle.kts index 474c24d7..0aec5a7b 100644 --- a/idea_plugin/build.gradle.kts +++ b/idea_plugin/build.gradle.kts @@ -14,7 +14,7 @@ * limitations under the License. */ -plugins { id("org.jetbrains.intellij") version "1.13.3" } +plugins { id("org.jetbrains.intellij") version "1.15.0" } apply(plugin = "org.jetbrains.intellij") @@ -22,7 +22,7 @@ apply(plugin = "java") repositories { mavenCentral() } -val googleJavaFormatVersion = "1.16.0" +val googleJavaFormatVersion = "1.17.0" java { sourceCompatibility = JavaVersion.VERSION_11 @@ -37,7 +37,7 @@ intellij { tasks { patchPluginXml { - version.set("${googleJavaFormatVersion}.2") + version.set("${googleJavaFormatVersion}.0") sinceBuild.set("213") untilBuild.set("") } @@ -49,12 +49,12 @@ tasks { withType().configureEach { jvmArgs( - "--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", - "--add-exports jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED", - "--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED", - "--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED", - "--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED", - "--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED", + "--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", + "--add-exports", "jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED", + "--add-exports", "jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED", + "--add-exports", "jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED", + "--add-exports", "jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED", + "--add-exports", "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED", ) } } diff --git a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java index 498c8852..bad03457 100644 --- a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java +++ b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java @@ -55,16 +55,36 @@ public boolean supports(@NotNull PsiFile file) { JavaFormatterOptions.Style style = GoogleJavaFormatSettings.getInstance(project).getStyle(); + final String origText = document.getText(); String text; try { - text = - ImportOrderer.reorderImports( - RemoveUnusedImports.removeUnusedImports(document.getText()), style); + text = ImportOrderer.reorderImports(RemoveUnusedImports.removeUnusedImports(origText), style); } catch (FormatterException e) { Notifications.displayParsingErrorNotification(project, file.getName()); return Runnables.doNothing(); } - return () -> document.setText(text); + /* pointless to change document text if it hasn't changed, plus this can interfere with + e.g. GoogleJavaFormattingService's output, i.e. it can overwrite the results from the main + formatter. */ + if (text.equals(origText)) { + return Runnables.doNothing(); + } + + return () -> { + if (documentManager.isDocumentBlockedByPsi(document)) { + documentManager.doPostponedOperationsAndUnblockDocument(document); + } + + /* similarly to above, don't overwrite new document text if it has changed - we use + getCharsSequence() as we should have `writeAction()` (which I think means effectively a + write-lock) and it saves calling getText(), which apparently is expensive. */ + CharSequence newText = document.getCharsSequence(); + if (CharSequence.compare(origText, newText) != 0) { + return; + } + + document.setText(text); + }; } } From 9f2cd6827d016a3b55b7f9f39e054a69b0deb168 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Wed, 6 Sep 2023 12:25:02 -0700 Subject: [PATCH 04/10] Make google-java-format friendlier to TSAN This code does deliberate racy initialization of some memoized values, and there is a static final instance of the `Space` subclass that ends up being shared across multiple threads. Tested: sponge/1777b644-2dd8-420b-ad06-b4f17c893d8f PiperOrigin-RevId: 563181626 --- .../java/com/google/googlejavaformat/Doc.java | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/core/src/main/java/com/google/googlejavaformat/Doc.java b/core/src/main/java/com/google/googlejavaformat/Doc.java index d638ddb4..e755bc67 100644 --- a/core/src/main/java/com/google/googlejavaformat/Doc.java +++ b/core/src/main/java/com/google/googlejavaformat/Doc.java @@ -19,6 +19,7 @@ import static java.lang.Math.max; import com.google.common.base.MoreObjects; +import com.google.common.base.Suppliers; import com.google.common.collect.DiscreteDomain; import com.google.common.collect.Iterators; import com.google.common.collect.Range; @@ -26,6 +27,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.function.Supplier; /** * {@link com.google.googlejavaformat.java.JavaInputAstVisitor JavaInputAstVisitor} outputs a @@ -102,16 +104,13 @@ public String toString() { private static final DiscreteDomain INTEGERS = DiscreteDomain.integers(); // Memoized width; Float.POSITIVE_INFINITY if contains forced breaks. - private boolean widthComputed = false; - private float width = 0.0F; + private final Supplier width = Suppliers.memoize(this::computeWidth); // Memoized flat; not defined (and never computed) if contains forced breaks. - private boolean flatComputed = false; - private String flat = ""; + private final Supplier flat = Suppliers.memoize(this::computeFlat); // Memoized Range. - private boolean rangeComputed = false; - private Range range = EMPTY_RANGE; + private final Supplier> range = Suppliers.memoize(this::computeRange); /** * Return the width of a {@code Doc}, or {@code Float.POSITIVE_INFINITY} if it must be broken. @@ -119,11 +118,7 @@ public String toString() { * @return the width */ final float getWidth() { - if (!widthComputed) { - width = computeWidth(); - widthComputed = true; - } - return width; + return width.get(); } /** @@ -133,11 +128,7 @@ final float getWidth() { * @return the flat-string value */ final String getFlat() { - if (!flatComputed) { - flat = computeFlat(); - flatComputed = true; - } - return flat; + return flat.get(); } /** @@ -146,11 +137,7 @@ final String getFlat() { * @return the {@code Doc}'s {@link Range} */ final Range range() { - if (!rangeComputed) { - range = computeRange(); - rangeComputed = true; - } - return range; + return range.get(); } /** From 915b78fb245aba6fa8236c2e8c928f5d91a73295 Mon Sep 17 00:00:00 2001 From: Michael Plump Date: Thu, 7 Sep 2023 08:31:10 -0700 Subject: [PATCH 05/10] Update the release notes for the IntelliJ plugin. (Also updated a dependency and tweaked the comment formatting.) PiperOrigin-RevId: 563439879 --- idea_plugin/build.gradle.kts | 2 +- .../intellij/GoogleJavaFormatImportOptimizer.java | 12 ++++++------ idea_plugin/src/main/resources/META-INF/plugin.xml | 3 +++ 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/idea_plugin/build.gradle.kts b/idea_plugin/build.gradle.kts index 0aec5a7b..5e965823 100644 --- a/idea_plugin/build.gradle.kts +++ b/idea_plugin/build.gradle.kts @@ -62,5 +62,5 @@ tasks { dependencies { implementation("com.google.googlejavaformat:google-java-format:${googleJavaFormatVersion}") testImplementation("junit:junit:4.13.2") - testImplementation("com.google.truth:truth:1.1.3") + testImplementation("com.google.truth:truth:1.1.5") } diff --git a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java index bad03457..42512421 100644 --- a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java +++ b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java @@ -64,9 +64,9 @@ public boolean supports(@NotNull PsiFile file) { return Runnables.doNothing(); } - /* pointless to change document text if it hasn't changed, plus this can interfere with - e.g. GoogleJavaFormattingService's output, i.e. it can overwrite the results from the main - formatter. */ + // pointless to change document text if it hasn't changed, plus this can interfere with + // e.g. GoogleJavaFormattingService's output, i.e. it can overwrite the results from the main + // formatter. if (text.equals(origText)) { return Runnables.doNothing(); } @@ -76,9 +76,9 @@ public boolean supports(@NotNull PsiFile file) { documentManager.doPostponedOperationsAndUnblockDocument(document); } - /* similarly to above, don't overwrite new document text if it has changed - we use - getCharsSequence() as we should have `writeAction()` (which I think means effectively a - write-lock) and it saves calling getText(), which apparently is expensive. */ + // similarly to above, don't overwrite new document text if it has changed - we use + // getCharsSequence() as we should have `writeAction()` (which I think means effectively a + // write-lock) and it saves calling getText(), which apparently is expensive. CharSequence newText = document.getCharsSequence(); if (CharSequence.compare(origText, newText) != 0) { return; diff --git a/idea_plugin/src/main/resources/META-INF/plugin.xml b/idea_plugin/src/main/resources/META-INF/plugin.xml index 42011a20..1b1e67ff 100644 --- a/idea_plugin/src/main/resources/META-INF/plugin.xml +++ b/idea_plugin/src/main/resources/META-INF/plugin.xml @@ -35,6 +35,9 @@ ]]> +
1.17.0.0
+
Updated to use google-java-format 1.17.0.
+
Fixed "Document is locked" errors (Thanks, @facboy!)
1.16.0.2
Disable AD_HOC_FORMATTING, which should stop the formatter from running so often when it wasn't specifically requested.
1.16.0.1
From d0e44e5d6120dfab4f4833dc4331986d726a2ce1 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Thu, 7 Sep 2023 08:57:02 -0700 Subject: [PATCH 06/10] Follow-up to https://github.com/google/google-java-format/commit/9f2cd6827d016a3b55b7f9f39e054a69b0deb168 Use `com.google.common.base.Supplier`, for compatibility with clients that are using the Android version of Guava. Fixes e.g. ``` Exception in thread "main" java.lang.IncompatibleClassChangeError: Class com.google.common.base.Suppliers$NonSerializableMemoizingSupplier does not implement the requested interface java.util.function.Supplier ``` PiperOrigin-RevId: 563446857 --- core/src/main/java/com/google/googlejavaformat/Doc.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/googlejavaformat/Doc.java b/core/src/main/java/com/google/googlejavaformat/Doc.java index e755bc67..cab68855 100644 --- a/core/src/main/java/com/google/googlejavaformat/Doc.java +++ b/core/src/main/java/com/google/googlejavaformat/Doc.java @@ -19,6 +19,7 @@ import static java.lang.Math.max; import com.google.common.base.MoreObjects; +import com.google.common.base.Supplier; import com.google.common.base.Suppliers; import com.google.common.collect.DiscreteDomain; import com.google.common.collect.Iterators; @@ -27,7 +28,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; -import java.util.function.Supplier; /** * {@link com.google.googlejavaformat.java.JavaInputAstVisitor JavaInputAstVisitor} outputs a From 1fe678942551b446d589c0fe0ad5319ccf17ffa3 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Tue, 12 Sep 2023 07:03:59 -0700 Subject: [PATCH 07/10] Handle `final var` lambda variables The start position of `final` variable is apparently after the token for `final`, this works around that by inlining a call to `visitVariable` and dropping the `sync` call that asserts the start position of the current node matches the next token. Fixes https://github.com/google/google-java-format/issues/959 PiperOrigin-RevId: 564710136 --- .../google/googlejavaformat/java/JavaInputAstVisitor.java | 5 ++++- .../com/google/googlejavaformat/java/testdata/I959.input | 5 +++++ .../com/google/googlejavaformat/java/testdata/I959.output | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.input create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.output 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 394d396d..89c944c5 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -1250,7 +1250,10 @@ public Void visitLambdaExpression(LambdaExpressionTree node, Void unused) { token(","); builder.breakOp(" "); } - scan(parameter, null); + visitVariables( + ImmutableList.of(parameter), + DeclarationKind.NONE, + fieldAnnotationDirection(parameter.getModifiers())); first = false; } if (parens) { diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.input new file mode 100644 index 00000000..0660079d --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.input @@ -0,0 +1,5 @@ +class I959 { + public void test() { + new File(".").listFiles((final var dir, final var name) -> true); + } +} \ No newline at end of file diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.output new file mode 100644 index 00000000..76a07f48 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I959.output @@ -0,0 +1,5 @@ +class I959 { + public void test() { + new File(".").listFiles((final var dir, final var name) -> true); + } +} From 4ade3471b7978af553f425bfc28b36c425e12a67 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Thu, 21 Sep 2023 08:44:47 -0700 Subject: [PATCH 08/10] Update ci.yml for JDK 21 release --- .github/workflows/ci.yml | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3b735636..8018665a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,20 +29,17 @@ jobs: fail-fast: false matrix: os: [ ubuntu-latest ] - java: [ 20, 17, 11 ] + java: [ 21, 17, 11 ] experimental: [ false ] include: # Only test on macos and windows with a single recent JDK to avoid a # combinatorial explosion of test configurations. - os: macos-latest - java: 20 + java: 21 experimental: false - os: windows-latest - java: 20 + java: 21 experimental: false - - os: ubuntu-latest - java: 21-ea - experimental: true - os: ubuntu-latest java: 22-ea experimental: true From c47ebbb6207e412d012a2a65e2ce5ee679902997 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Fri, 29 Sep 2023 17:44:09 -0700 Subject: [PATCH 09/10] Update ci.yml --- .github/workflows/ci.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8018665a..ee1174df 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -41,7 +41,7 @@ jobs: java: 21 experimental: false - os: ubuntu-latest - java: 22-ea + java: EA experimental: true runs-on: ${{ matrix.os }} continue-on-error: ${{ matrix.experimental }} @@ -52,7 +52,15 @@ jobs: access_token: ${{ github.token }} - name: 'Check out repository' uses: actions/checkout@v2 + - name: 'Set up JDK ${{ matrix.java }} from jdk.java.net' + if: ${{ matrix.java == 'EA' }} + uses: oracle-actions/setup-java@v1 + with: + website: jdk.java.net + release: ${{ matrix.java }} + cache: 'maven' - name: 'Set up JDK ${{ matrix.java }}' + if: ${{ matrix.java != 'EA' }} uses: actions/setup-java@v2 with: java-version: ${{ matrix.java }} From 11839c023c5153a14dc56b54fefe26dada3e0907 Mon Sep 17 00:00:00 2001 From: cushon Date: Mon, 2 Oct 2023 20:59:12 +0000 Subject: [PATCH 10/10] Release google-java-format 1.18.0 --- core/pom.xml | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 038e4eda..102d5fc4 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -22,7 +22,7 @@ com.google.googlejavaformat google-java-format-parent - HEAD-SNAPSHOT + 1.18.0 google-java-format diff --git a/pom.xml b/pom.xml index 1fe2803e..fef8c3d1 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ com.google.googlejavaformat google-java-format-parent pom - HEAD-SNAPSHOT + 1.18.0 core