Skip to content

Commit 26027bb

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 26027bb

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,24 @@ 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
80+
String newText = document.getText();
81+
if (!newText.equals(origText)) {
82+
return;
83+
}
84+
7585
document.setText(text);
7686
};
7787
}

0 commit comments

Comments
 (0)