WordTokenizer implements Tokenizer { protected int getSearchStartIndex(final @NonNull Spanned text, int cursor) { if (cursor < 0 || cursor > text.length()) { cursor = 0; } MentionSpan[] spans = text.getSpans(0, text.length(), MentionSpan.class); int closestToCursor = 0; for (MentionSpan span : spans) { int end = text.getSpanEnd(span); if (end > closestToCursor && end <= cursor) { closestToCursor = end; } } String textString = text.toString().substring(0, cursor); int lineStartIndex = 0; if (textString.contains(mConfig.LINE_SEPARATOR)) { lineStartIndex = textString.lastIndexOf(mConfig.LINE_SEPARATOR) + 1; } return Math.max(closestToCursor, lineStartIndex); } WordTokenizer();  WordTokenizer(final @NonNull WordTokenizerConfig config); @Override int findTokenStart(final @NonNull Spanned text, final int cursor); @Override int findTokenEnd(final @NonNull Spanned text, final int cursor); boolean isValidMention(final @NonNull Spanned text, final int start, final int end); @Override @NonNull Spanned terminateToken(final @NonNull Spanned text); boolean isExplicitChar(final char c); boolean isWordBreakingChar(final char c); boolean isExplicit(final @NonNull CharSequence text, final int cursor); char getExplicitChar(final @NonNull CharSequence text, final int cursor); boolean containsExplicitChar(final @NonNull CharSequence input); boolean containsWordBreakingChar(final @NonNull CharSequence input); boolean onlyLettersOrDigits(final @NonNull CharSequence input, final int numCharsToCheck, final int start);  }
@Test public void testGetStartIndex() { setTestSpan("Shoulong Li", 14); Spanned text = mRichEditor.getText(); assertEquals(0, mTokenizer.getSearchStartIndex(text, 0)); assertEquals(0, mTokenizer.getSearchStartIndex(text, 6)); assertEquals(0, mTokenizer.getSearchStartIndex(text, 14)); assertEquals(25, mTokenizer.getSearchStartIndex(text, 26)); assertEquals(25, mTokenizer.getSearchStartIndex(text, 30)); assertEquals(25, mTokenizer.getSearchStartIndex(text, 35)); assertEquals(25, mTokenizer.getSearchStartIndex(text, 40)); }
@Test public void testGetStartIndexWithMultipleLines() { String ls = System.getProperty("line.separator"); String textString = String.format("Test %smentions Shoulong Li a%snd @Nathan Hi %s@Mi", ls, ls, ls); mRichEditor.setText(textString); setTestSpan("Shoulong Li", 15); Spanned text = mRichEditor.getText(); assertEquals(0, mTokenizer.getSearchStartIndex(text, 0)); assertEquals(0, mTokenizer.getSearchStartIndex(text, 5)); assertEquals(6, mTokenizer.getSearchStartIndex(text, 6)); assertEquals(6, mTokenizer.getSearchStartIndex(text, 14)); assertEquals(6, mTokenizer.getSearchStartIndex(text, 15)); assertEquals(26, mTokenizer.getSearchStartIndex(text, 28)); assertEquals(29, mTokenizer.getSearchStartIndex(text, 29)); assertEquals(29, mTokenizer.getSearchStartIndex(text, 30)); assertEquals(29, mTokenizer.getSearchStartIndex(text, 32)); assertEquals(29, mTokenizer.getSearchStartIndex(text, 33)); assertEquals(29, mTokenizer.getSearchStartIndex(text, 43)); assertEquals(44, mTokenizer.getSearchStartIndex(text, 44)); assertEquals(44, mTokenizer.getSearchStartIndex(text, 45)); assertEquals(44, mTokenizer.getSearchStartIndex(text, 47)); }