Skip to content

Commit 89f7902

Browse files
committed
Fix java.lang.RuntimeException: Document is locked by write PSI operations errors
1 parent 81cb908 commit 89f7902

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,24 @@ 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+
return () -> {
68+
if (documentManager.isDocumentBlockedByPsi(document)) {
69+
documentManager.doPostponedOperationsAndUnblockDocument(document);
70+
String newText = document.getText();
71+
if (!newText.equals(origText)) {
72+
return;
73+
}
74+
}
75+
document.setText(text);
76+
};
6977
}
7078
}

0 commit comments

Comments
 (0)