Skip to content

Commit fa29183

Browse files
author
sergeykuchin
committed
added forbidden chars list to avoid highlighting start char when new line symbol or space and etc is following it
1 parent 9e33cd9 commit fa29183

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

hashtag-helper/src/main/java/com/volokh/danylo/hashtaghelper/HashTagHelper.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
public final class HashTagHelper implements ClickableForegroundColorSpan.OnHashTagClickListener {
3030

3131
private static final Character NEW_LINE = '\n';
32+
private static final Character CARRIAGE_RETURN = '\r';
33+
private static final Character SPACE = ' ';
3234

3335
/**
3436
* If this is not null then all of the symbols in the List will be considered as valid symbols of hashtag
@@ -59,6 +61,8 @@ public final class HashTagHelper implements ClickableForegroundColorSpan.OnHashT
5961

6062
private OnHashTagClickListener mOnHashTagClickListener;
6163

64+
private final ArrayList<Character> mForbiddenCharacters = new ArrayList<>();
65+
6266
public static final class Creator {
6367

6468
private Creator() {
@@ -122,6 +126,8 @@ private HashTagHelper(
122126
@Nullable Class<? extends ClickableForegroundColorSpan> characterStyle
123127
) {
124128

129+
addForbiddenCharactersToList();
130+
125131
if (characterStyle == null) {
126132
mCharacterStyle = ClickableForegroundColorSpan.class;
127133
} else {
@@ -141,6 +147,12 @@ private HashTagHelper(
141147
}
142148
}
143149

150+
private void addForbiddenCharactersToList() {
151+
mForbiddenCharacters.add(NEW_LINE);
152+
mForbiddenCharacters.add(SPACE);
153+
mForbiddenCharacters.add(CARRIAGE_RETURN);
154+
}
155+
144156
public void handle(TextView textView) {
145157
if (mTextView == null) {
146158
mTextView = textView;
@@ -187,7 +199,7 @@ private void setColorsToAllHashTags(CharSequence text) {
187199
char sign = trimmedText.charAt(index);
188200
char nextSign = trimmedText.charAt(index + 1);
189201
int nextNotLetterDigitCharIndex = index + 1; // we assume it is next. if if was not changed by findNextValidHashTagChar then index will be incremented by 1
190-
if (mStartChars.contains(sign) && !mStartChars.contains(nextSign) && nextSign != NEW_LINE) {
202+
if (mStartChars.contains(sign) && !mStartChars.contains(nextSign) && !mForbiddenCharacters.contains(nextSign)) {
191203
startIndexOfNextHashSign = index;
192204

193205
nextNotLetterDigitCharIndex = findNextValidHashTagChar(trimmedText, startIndexOfNextHashSign);

0 commit comments

Comments
 (0)