From 81cb908bb5dab25179a18d866f96a4be0af824ec Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Sun, 27 Aug 2023 11:03:59 +0100 Subject: [PATCH 1/3] Update to `google-java-format` 1.17.0 --- idea_plugin/build.gradle.kts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/idea_plugin/build.gradle.kts b/idea_plugin/build.gradle.kts index 474c24d79..0aec5a7b6 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", ) } } From 89f790262e90808ed2d45cf27d643eb0ffebd62f Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Sun, 27 Aug 2023 11:04:13 +0100 Subject: [PATCH 2/3] Fix `java.lang.RuntimeException: Document is locked by write PSI operations` errors --- .../GoogleJavaFormatImportOptimizer.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) 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 498c88526..6fda8befd 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,24 @@ 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); + return () -> { + if (documentManager.isDocumentBlockedByPsi(document)) { + documentManager.doPostponedOperationsAndUnblockDocument(document); + String newText = document.getText(); + if (!newText.equals(origText)) { + return; + } + } + document.setText(text); + }; } } From e0925a2e7d7c94e84a1c1fd6bb95ff9ca1b66c79 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Fri, 1 Sep 2023 15:15:56 +0100 Subject: [PATCH 3/3] `GoogleJavaFormatImportOptimizer`: do not overwrite `document.text` if formatter results are unchanged, or if `document.text` has changed This seemed to be responsible for some of the issues with the formatter seemingly not formatting a file. --- .../GoogleJavaFormatImportOptimizer.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) 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 6fda8befd..bad03457a 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,14 +64,26 @@ 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. */ + if (text.equals(origText)) { + return Runnables.doNothing(); + } + return () -> { if (documentManager.isDocumentBlockedByPsi(document)) { documentManager.doPostponedOperationsAndUnblockDocument(document); - String newText = document.getText(); - if (!newText.equals(origText)) { - return; - } } + + /* 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); }; }