Skip to content

Commit 7a180ba

Browse files
committed
revert back to the naive implementation, note the reason
1 parent b2fb695 commit 7a180ba

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

idea-plugin/src/main/java/com/palantir/javaformat/intellij/PalantirJavaFormatFormattingService.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,24 @@ public void run() {
8888
return;
8989
}
9090

91+
String preFormatText = request.getDocumentText();
92+
9193
try {
92-
List<Replacement> replacements =
93-
formatterService.get().getFormatReplacements(request.getDocumentText(), toRanges(request));
94-
95-
// The Javadoc of onTextReady API says that you should set it to null when the
96-
// document is unchanged. But an even better version is to simply not attempt
97-
// to format a document that is already formatted
98-
if (replacements.isEmpty()) {
99-
return;
94+
// Note: I attempted to implement this using getFormatReplacements and elide an update when
95+
// there were no replacements. There is a bug in the underlying formatter where it _always_
96+
// returns a change. Thus we must perform a content aware diff / branching.
97+
String formattedText = applyReplacements(
98+
request.getDocumentText(),
99+
formatterService.get().getFormatReplacements(request.getDocumentText(), toRanges(request)));
100+
101+
// The Javadoc of this API says that you should set it to null when the document is unchanged
102+
// We should not be trying to format a document that is already formatted
103+
if (preFormatText.equals(formattedText)) {
104+
request.onTextReady(null);
105+
} else {
106+
request.onTextReady(formattedText);
100107
}
101108

102-
request.onTextReady(applyReplacements(request.getDocumentText(), replacements));
103109
} catch (FormatterException e) {
104110
request.onError(
105111
Notifications.PARSING_ERROR_TITLE,

0 commit comments

Comments
 (0)