Skip to content

Commit

Permalink
Prevent edit if nothing was formatted (#120)
Browse files Browse the repository at this point in the history
Handle trivial cases, where input equals output, to prevent dirty flags in active editors.
  • Loading branch information
sormuras authored and cushon committed Feb 9, 2017
1 parent 760a13c commit 34e9581
Showing 1 changed file with 30 additions and 2 deletions.
Expand Up @@ -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<Replacement> 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;
Expand All @@ -100,6 +106,28 @@ private List<Range<Integer>> 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<Replacement> 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<Integer> range = replacement.getReplaceRange();
String snippet = source.substring(range.lowerEndpoint(), range.upperEndpoint());
if (output.equals(snippet)) {
return true;
}
}
}
return false;
}

private TextEdit editFromReplacements(List<Replacement> replacements) {
// Split the replacements that cross line boundaries.
TextEdit edit = new MultiTextEdit();
Expand Down

0 comments on commit 34e9581

Please sign in to comment.