TextUtils { static int fixSelectionOffset( String fullText, int selectionOffset ) { if( selectionOffset < 0 || selectionOffset >= fullText.length()) selectionOffset = 0; int lineBreakIndex = selectionOffset - 1; for( ; lineBreakIndex >= 0; lineBreakIndex -- ) { char c = fullText.charAt( lineBreakIndex ); if( isLineBreak( c )) { lineBreakIndex ++; break; } } return lineBreakIndex < 0 ? 0 : lineBreakIndex; } private  TextUtils(); static String removeComments( String line ); static boolean isLineBreak( char c ); static SelectionRange findRegionFromSelection( String fullText, int selectionOffset, int selectionLength );  }
@Test public void testFixSelectionOffset() { String text = "something not too long"; Assert.assertEquals( 0, TextUtils.fixSelectionOffset( text, -2 )); Assert.assertEquals( 0, TextUtils.fixSelectionOffset( text, 500 )); Assert.assertEquals( 0, TextUtils.fixSelectionOffset( text, 2 )); Assert.assertEquals( 0, TextUtils.fixSelectionOffset( text, 0 )); text = " something not too long"; Assert.assertEquals( 0, TextUtils.fixSelectionOffset( text, 0 )); Assert.assertEquals( 0, TextUtils.fixSelectionOffset( text, 1 )); Assert.assertEquals( 0, TextUtils.fixSelectionOffset( text, 2 )); Assert.assertEquals( 0, TextUtils.fixSelectionOffset( text, 3 )); text = "\nline 1\r\nline 2"; Assert.assertEquals( 9, TextUtils.fixSelectionOffset( text, 9 )); Assert.assertEquals( 9, TextUtils.fixSelectionOffset( text, 10 )); Assert.assertEquals( 9, TextUtils.fixSelectionOffset( text, 11 )); Assert.assertEquals( 9, TextUtils.fixSelectionOffset( text, 12 )); Assert.assertEquals( 0, TextUtils.fixSelectionOffset( text, 0 )); Assert.assertEquals( 1, TextUtils.fixSelectionOffset( text, 1 )); Assert.assertEquals( 1, TextUtils.fixSelectionOffset( text, 2 )); Assert.assertEquals( 1, TextUtils.fixSelectionOffset( text, 3 )); }