Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

public class MarkdownFormatter {
private final @NonNull AssetManager mAssetManager;
private String mPrevText;
private List<MarkdownRange> mPrevMarkdownRanges;
private MarkdownStyle mPrevMarkdownStyle;

public MarkdownFormatter(@NonNull AssetManager assetManager) {
mAssetManager = assetManager;
Expand All @@ -24,8 +27,19 @@ public void format(@NonNull SpannableStringBuilder ssb, @NonNull List<MarkdownRa
try {
Systrace.beginSection(0, "format");
Objects.requireNonNull(markdownStyle, "mMarkdownStyle is null");

String text = ssb.toString();
if (text.equals(mPrevText) && markdownRanges == mPrevMarkdownRanges && markdownStyle == mPrevMarkdownStyle) {
// Use shallow comparison of markdown ranges and markdown style
// to optimistically skip removing and applying the same spans
return;
}
Comment on lines +32 to +36
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there anything that could alter the styles in between format passes? I'm not sure how it will behave with the commit hook when the text doesn't change but the node is cloned.


removeSpans(ssb);
applyRanges(ssb, markdownRanges, markdownStyle);
mPrevText = text;
mPrevMarkdownRanges = markdownRanges;
mPrevMarkdownStyle = markdownStyle;
} finally {
Systrace.endSection(0);
}
Expand Down