Skip to content

Commit e0925a2

Browse files
committed
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.
1 parent 89f7902 commit e0925a2

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,26 @@ public boolean supports(@NotNull PsiFile file) {
6464
return Runnables.doNothing();
6565
}
6666

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+
6774
return () -> {
6875
if (documentManager.isDocumentBlockedByPsi(document)) {
6976
documentManager.doPostponedOperationsAndUnblockDocument(document);
70-
String newText = document.getText();
71-
if (!newText.equals(origText)) {
72-
return;
73-
}
7477
}
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+
7587
document.setText(text);
7688
};
7789
}

0 commit comments

Comments
 (0)