Skip to content

Fix java.lang.RuntimeException: Document is locked by write PSI operations errors #960

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions idea_plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
* 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")

apply(plugin = "java")

repositories { mavenCentral() }

val googleJavaFormatVersion = "1.16.0"
val googleJavaFormatVersion = "1.17.0"

java {
sourceCompatibility = JavaVersion.VERSION_11
Expand All @@ -37,7 +37,7 @@ intellij {

tasks {
patchPluginXml {
version.set("${googleJavaFormatVersion}.2")
version.set("${googleJavaFormatVersion}.0")
sinceBuild.set("213")
untilBuild.set("")
}
Expand All @@ -49,12 +49,12 @@ tasks {

withType<Test>().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",
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}
}