protected int getSearchStartIndex ( final @ NonNull Spanned text , int cursor ) { if ( cursor < 0 || cursor > text . length ( ) ) { cursor = 0 ; } // Get index of the end of the last span before the cursor (or 0 if does not exist) 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 ; } } // Get the index of the start of the line String textString = text . toString ( ) . substring ( 0 , cursor ) ; int lineStartIndex = 0 ; if ( textString . contains ( mConfig . LINE_SEPARATOR ) ) { lineStartIndex = textString . lastIndexOf ( mConfig . LINE_SEPARATOR ) + 1 ; } // Return whichever is closer before to the cursor return Math . max ( closestToCursor , lineStartIndex ) ; }
Returns the index of the end of the last span before the cursor or the start of the current line if there are no spans before the cursor .