WithMarkup (char c) { StringBuilder sb = new StringBuilder(toString()); int[] offsets = markupOffsets(); for (int i = offsets.length - 1; i >= 0; i--) { sb.insert(offsets[i], c); } return new WithMarkup() { @Override public int offsetToOriginal(int offsetWithMarkup) { int result = offsetWithMarkup; for (int offset : offsets) { if (offset >= result) break; result--; } return result; } @Override public int length() { return sb.length(); } @Override public char charAt(int index) { return sb.charAt(index); } @NotNull @Override public CharSequence subSequence(int start, int end) { return sb.subSequence(start, end); } @Override public String toString() { return sb.toString(); } }; }