TL;DR: if text.length > MAX_MENTION_QUERY_LENGTH, the mentions will not working if the MAX_MENTION_QUERY_LENGTH's char in original text is already attributed.
In HKWMentionsPluginV2, when controlCharactersToPrepend is set, it'll trim the parentTextView's text to be a substring of last MAX_MENTION_QUERY_LENGTH chars when trying to find the last unattributed control char. However, the location is offset-ed (offset = originalText.length - MAX_MENTION_QUERY_LENGTH) when passed to mentionAttributeAtLocation to determine whether the char at location in original text is already attributed. Before this fix, the location's value range is [0, trimmedText.length] while it should be [offset, trimmedText.length + offset]. Thus we need to add back the offset if the string is trimmed.
Example:
Original text: <-- 200 chars, all attributed -->
When adding a control char: <-- 200 chars, all attributed --> @
Trimmed text: <-- 99 chars, all attributed --> @
Use the trimmed text to find the last unattributed control char in original string:
<----- 200 chars, all attributed -----> @
<--- 99 chars ---> @ <- before fix
⬆️
< offset ><--- 99 chars ---> @ <- after fix
⬆️
Now it will think the last control char is attributed, but actually it's not.
TL;DR: if
text.length > MAX_MENTION_QUERY_LENGTH
, the mentions will not working if theMAX_MENTION_QUERY_LENGTH
's char in original text is already attributed.In
HKWMentionsPluginV2
, whencontrolCharactersToPrepend
is set, it'll trim theparentTextView
's text to be a substring of lastMAX_MENTION_QUERY_LENGTH
chars when trying to find the last unattributed control char. However, thelocation
is offset-ed (offset = originalText.length - MAX_MENTION_QUERY_LENGTH
) when passed tomentionAttributeAtLocation
to determine whether the char atlocation
in original text is already attributed. Before this fix, thelocation
's value range is[0, trimmedText.length]
while it should be[offset, trimmedText.length + offset]
. Thus we need to add back the offset if the string is trimmed.Example:
<-- 200 chars, all attributed -->
<-- 200 chars, all attributed --> @
<-- 99 chars, all attributed --> @
Now it will think the last control char is attributed, but actually it's not.