public String format ( String comment , int indentLevel , CommentLocation commentLocation ) { // Return if there's nothing to do if ( comment == null ) { return null ; } final StringBuilder indentString = new StringBuilder ( ) ; for ( int i = 0 ; i < indentLevel ; i ++ ) { indentString . append ( " " ) ; } // Comment ends with newline boolean endsWithNewline = ( comment . endsWith ( "\r\n" ) || comment . endsWith ( "\n" ) ) ; List < String > matchList = new ArrayList < String > ( ) ; Matcher regexMatcher = commentFormattingRegex . matcher ( comment ) ; while ( regexMatcher . find ( ) ) { matchList . add ( regexMatcher . group ( ) ) ; } StringBuilder builder = new StringBuilder ( ) ; for ( int i = 0 ; i < matchList . size ( ) ; i ++ ) { String indent ; String newline ; if ( commentLocation . equals ( CommentLocation . END ) ) { indent = " " ; newline = "" ; } else { indent = indentString . toString ( ) ; newline = "\n" ; } // We need to handle the last newline if ( i == 0 ) { builder . append ( indent + matchList . get ( i ) . trim ( ) + newline ) ; } else if ( i == matchList . size ( ) - 1 ) { builder . append ( indent + " " + matchList . get ( i ) . trim ( ) + ( endsWithNewline ? newline : "" ) ) ; } else { builder . append ( indent + " " + matchList . get ( i ) . trim ( ) + newline ) ; } } return builder . toString ( ) ; }
Format a given comment string with the indent level specified .