Skip to content

Commit 28b199c

Browse files
facboygoogle-java-format Team
authored andcommitted
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=#960 from facboy:master e0925a2 PiperOrigin-RevId: 563163224
1 parent 4ebb6f5 commit 28b199c

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

idea_plugin/build.gradle.kts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
plugins { id("org.jetbrains.intellij") version "1.13.3" }
17+
plugins { id("org.jetbrains.intellij") version "1.15.0" }
1818

1919
apply(plugin = "org.jetbrains.intellij")
2020

2121
apply(plugin = "java")
2222

2323
repositories { mavenCentral() }
2424

25-
val googleJavaFormatVersion = "1.16.0"
25+
val googleJavaFormatVersion = "1.17.0"
2626

2727
java {
2828
sourceCompatibility = JavaVersion.VERSION_11
@@ -37,7 +37,7 @@ intellij {
3737

3838
tasks {
3939
patchPluginXml {
40-
version.set("${googleJavaFormatVersion}.2")
40+
version.set("${googleJavaFormatVersion}.0")
4141
sinceBuild.set("213")
4242
untilBuild.set("")
4343
}
@@ -49,12 +49,12 @@ tasks {
4949

5050
withType<Test>().configureEach {
5151
jvmArgs(
52-
"--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
53-
"--add-exports jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
54-
"--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
55-
"--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
56-
"--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
57-
"--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
52+
"--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
53+
"--add-exports", "jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
54+
"--add-exports", "jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
55+
"--add-exports", "jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
56+
"--add-exports", "jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
57+
"--add-exports", "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
5858
)
5959
}
6060
}

idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,36 @@ public boolean supports(@NotNull PsiFile file) {
5555

5656
JavaFormatterOptions.Style style = GoogleJavaFormatSettings.getInstance(project).getStyle();
5757

58+
final String origText = document.getText();
5859
String text;
5960
try {
60-
text =
61-
ImportOrderer.reorderImports(
62-
RemoveUnusedImports.removeUnusedImports(document.getText()), style);
61+
text = ImportOrderer.reorderImports(RemoveUnusedImports.removeUnusedImports(origText), style);
6362
} catch (FormatterException e) {
6463
Notifications.displayParsingErrorNotification(project, file.getName());
6564
return Runnables.doNothing();
6665
}
6766

68-
return () -> document.setText(text);
67+
/* pointless to change document text if it hasn't changed, plus this can interfere with
68+
e.g. GoogleJavaFormattingService's output, i.e. it can overwrite the results from the main
69+
formatter. */
70+
if (text.equals(origText)) {
71+
return Runnables.doNothing();
72+
}
73+
74+
return () -> {
75+
if (documentManager.isDocumentBlockedByPsi(document)) {
76+
documentManager.doPostponedOperationsAndUnblockDocument(document);
77+
}
78+
79+
/* similarly to above, don't overwrite new document text if it has changed - we use
80+
getCharsSequence() as we should have `writeAction()` (which I think means effectively a
81+
write-lock) and it saves calling getText(), which apparently is expensive. */
82+
CharSequence newText = document.getCharsSequence();
83+
if (CharSequence.compare(origText, newText) != 0) {
84+
return;
85+
}
86+
87+
document.setText(text);
88+
};
6989
}
7090
}

0 commit comments

Comments
 (0)