Buggy code:
private Image starImage ; private static final String OVERDUE = "OVERDUE" ; private static final String TODAY = "TODAY" ; private static final String TOMORROW = "TOMORROW" ; private static final String WEEK = "WEEK" ; private static final String OTHERS = "OTHERS" ; private static final int STARTING_INDEX = 2 ; private static final int WHITESPACES = 1 ; private View view ; private Parser parser ; private Logic logic ; private Storage storage ; public Controller ( ) throws Exception { } private void displayNotification ( Notification notify ) { } private void displayCategory ( ) { } private void displayList ( ArrayList < Task > list ) throws Exception { } private void displayHelp ( LinkedList < String > items ) { } private void addKeyListener ( ) { } private void timer ( ) { } public String getCurrentDate ( ) { } public String getCurrentTime ( ) { } public String getCurrentDay ( ) { } public String getHeaderFormat ( Date date ) { } public int getCurrentYear ( ) { } private void inputToHint ( ) { } private void inputToNormal ( ) { } private boolean isTextEmpty ( StyledText t ) { } private int calculateDiffInDay ( Calendar startDate , Calendar endDate ) { } private boolean insertIntoTable ( String key , boolean week ) { if ( putIntoDays . containsKey ( key ) ) { if ( week ) { TableItem headerItem = new TableItem ( view . getMainTable ( ) , SWT . NONE ) ; headerItem . setText ( key ) ; headerItem . setFont ( normalFont ) ; headerItem . setForeground ( orangeColor ) ; } ArrayList < Task > tempArrList = putIntoDays . get ( key ) ; ArrayList < Task > lastTasks = Logic . getLastTasksNoRemove ( ) ; for ( int i = 0 ; i < ( tempArrList . size ( ) ) ; i ++ ) { final TableItem mainItem = new TableItem ( view . getMainTable ( ) , SWT . NONE ) ; String whiteSpaces = "" ; if ( ( tempArrList . get ( i ) . getPriority ( ) ) == 1 ) { mainItem . setData ( object ) ; whiteSpaces = "<seq2seq4repair_space>" ; } if ( lastTasks != null ) { for ( int k = 0 ; k < ( lastTasks . size ( ) ) ; k ++ ) { if ( ( lastTasks . get ( k ) . getId ( ) ) == ( tempArrList . get ( i ) . getId ( ) ) ) { mainItem . setBackground ( newColor ) ; mainItem . setFont ( boldFont ) ; } } } String text = whiteSpaces + ( tempArrList . get ( i ) . getUserFormat ( ) ) ; if ( week ) { text = ( ( whiteSpaces + ( tempArrList . get ( i ) . getUserFormatNoDate ( ) ) ) + ( Keywords . SPACE_STRING ) ) + ( tempArrList . get ( i ) . getDisplayTimeRange ( ) ) ; } final TextLayout textLayout = new TextLayout ( Display . getCurrent ( ) ) ; textLayout . setText ( text ) ; TextStyle styleDescription = new TextStyle ( View . normalFont , null , null ) ; TextStyle styleDate = new TextStyle ( View . boldFont , View . dateColor , null ) ; TextStyle styleCategory = new TextStyle ( View . normalFont , View . redColor , null ) ; if ( ( tempArrList . get ( i ) . getDatetimes ( ) . get ( 0 ) ) != null ) { int seperatingIndex = ( whiteSpaces . length ( ) ) + ( tempArrList . get ( i ) . getUserFormatNoDate ( ) . length ( ) ) ; textLayout . setStyle ( styleDescription , 0 , seperatingIndex ) ; if ( week ) { textLayout . setStyle ( styleDate , seperatingIndex , text . length ( ) ) ; } else { textLayout . setStyle ( styleDate , ( seperatingIndex + 3 ) , text . length ( ) ) ; } } else { textLayout . setStyle ( styleDescription , 0 , text . length ( ) ) ; } for ( int z = 0 ; z < ( tempArrList . get ( i ) . getCategories ( ) . size ( ) ) ; z ++ ) { String tempCat = tempArrList . get ( i ) . getCategories ( ) . get ( z ) ; <START_BUG> textLayout . setStyle ( styleCategory , ( ( text . indexOf ( tempCat ) ) - ( CATEGORY_PREPEND . length ( ) ) ) , ( ( text . indexOf ( tempCat ) ) + ( tempCat . length ( ) ) ) ) ; <END_BUG> } view . getMainTable ( ) . addListener ( PaintItem , new Listener ( ) { public void handleEvent ( Event event ) { if ( event . item . equals ( mainItem ) ) { textLayout . draw ( event . gc , event . x , event . y ) ; } } } ) ; final Rectangle textLayoutBounds = textLayout . getBounds ( ) ; view . getMainTable ( ) . addListener ( MeasureItem , new Listener ( ) { public void handleEvent ( Event e ) { if ( e . item . equals ( mainItem ) ) { e . width = ( textLayoutBounds . width ) + 2 ; e . height = ( textLayoutBounds . height ) + 2 ; } } } ) ; } return true ; } return false ; } private void insertToHashMap ( String key , Task value ) { } private void readFileLocation ( ) throws IOException { } public void writePathToFile ( ) { } public void setAutoCompleteContent ( String tempInput ) { } public void setAutoComplete ( ) { } public View getView ( ) { } }

Fix:
textLayout . setStyle ( styleCategory , ( ( text . lastIndexOf ( tempCat ) ) - ( CATEGORY_PREPEND . length ( ) ) ) , ( ( text . lastIndexOf ( tempCat ) ) + ( tempCat . length ( ) ) ) ) ;