MentionsEditable extends SpannableStringBuilder implements Parcelable { @NonNull @Override public SpannableStringBuilder replace(int start, int end, CharSequence tb, int tbstart, int tbend) { if (start == end && start - tbend - 1 >= 0 && tb.length() > 1) { String insertString = tb.subSequence(tbstart, tbend).toString(); int prevStart = start - tbend - 1; int prevEnd = start - 1; String prevString = subSequence(prevStart, prevEnd).toString(); MentionSpan[] prevSpans = getSpans(prevStart, prevEnd, MentionSpan.class); if (insertString.equals(prevString) && prevSpans.length > 0) { return super.replace(start - 1, start, "", 0, 0); } } return super.replace(start, end, tb, tbstart, tbend); } MentionsEditable(@NonNull CharSequence text);  MentionsEditable(@NonNull CharSequence text, int start, int end);  MentionsEditable(@NonNull Parcel in); @Override void setSpan(Object what, int start, int end, int flags); @NonNull @Override SpannableStringBuilder replace(int start, int end, CharSequence tb, int tbstart, int tbend); @NonNull MentionsEditable trim(); @NonNull List<MentionSpan> getMentionSpans(); @Nullable MentionSpan getMentionSpanAtOffset(int index); @Nullable MentionSpan getMentionSpanStartingAt(int index); @Nullable MentionSpan getMentionSpanEndingAt(int index); @Override int describeContents(); @Override void writeToParcel(Parcel dest, int flags); static final Parcelable.Creator<MentionsEditable> CREATOR; }
@Test public void testMapToDeleteCharacterInsteadOfAppendWithoutMentionSpan() { mEditable = new MentionsEditable("Hello World"); mEditable.replace(11, 11, "Worl"); assertEquals("Hello WorldWorl", mEditable.toString()); }