diff --git a/eclipse_plugin/src/com/google/googlejavaformat/java/GoogleJavaFormatter.java b/eclipse_plugin/src/com/google/googlejavaformat/java/GoogleJavaFormatter.java index 945c068bd..c300514be 100644 --- a/eclipse_plugin/src/com/google/googlejavaformat/java/GoogleJavaFormatter.java +++ b/eclipse_plugin/src/com/google/googlejavaformat/java/GoogleJavaFormatter.java @@ -82,10 +82,16 @@ private TextEdit formatInternal(int kind, String source, IRegion[] regions, int default: throw new IllegalArgumentException(String.format("Unknown snippet kind: %d", kind)); } - return editFromReplacements( + List replacements = new SnippetFormatter() .format( - snippetKind, source, rangesFromRegions(regions), initialIndent, includeComments)); + snippetKind, source, rangesFromRegions(regions), initialIndent, includeComments); + if (idempotent(source, regions, replacements)) { + // Do not create edits if there's no diff. + return null; + } + // Convert replacements to text edits. + return editFromReplacements(replacements); } catch (IllegalArgumentException | FormatterException exception) { // Do not format on errors. return null; @@ -100,6 +106,28 @@ private List> rangesFromRegions(IRegion[] regions) { return ranges; } + /** @return {@code true} if input and output texts are equal, else {@code false}. */ + private boolean idempotent(String source, IRegion[] regions, List replacements) { + // This implementation only checks for single replacement. + if (replacements.size() == 1) { + Replacement replacement = replacements.get(0); + String output = replacement.getReplacementString(); + // Entire source case: input = output, nothing changed. + if (output.equals(source)) { + return true; + } + // Single region and single replacement case: if they are equal, nothing changed. + if (regions.length == 1) { + Range range = replacement.getReplaceRange(); + String snippet = source.substring(range.lowerEndpoint(), range.upperEndpoint()); + if (output.equals(snippet)) { + return true; + } + } + } + return false; + } + private TextEdit editFromReplacements(List replacements) { // Split the replacements that cross line boundaries. TextEdit edit = new MultiTextEdit();