@@ -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