<s> package org . eclipse . jdt . internal . formatter . comment ; import java . util . Map ; import org . eclipse . text . edits . TextEdit ; import org . eclipse . core . runtime . Assert ; import org . eclipse . jface . text . BadLocationException ; import org . eclipse . jface . text . BadPositionCategoryException ; import org . eclipse . jface . text . DefaultPositionUpdater ; import org . eclipse . jface . text . Document ; import org . eclipse . jface . text . Position ; import org . eclipse . jdt . core . ToolFactory ; import org . eclipse . jdt . internal . core . util . Util ; public class CommentFormatterUtil { public static String evaluateFormatterEdit ( String string , TextEdit edit , Position [ ] positions ) { try { Document doc = createDocument ( string , positions ) ; edit . apply ( doc , 0 ) ; if ( positions != null ) { for ( int i = 0 ; i < positions . length ; i ++ ) { Assert . isTrue ( ! positions [ i ] . isDeleted , "" ) ; } } return doc . get ( ) ; } catch ( BadLocationException e ) { log ( e ) ; Assert . isTrue ( false , "" + e . getMessage ( ) ) ; } return null ; } public static TextEdit format2 ( int kind , String string , int indentationLevel , String lineSeparator , Map options ) { int length = string . length ( ) ; if ( 0 < 0 || length < 0 || 0 + length > string . length ( ) ) { throw new IllegalArgumentException ( "" + 0 + ", length: " + length + "" + string . length ( ) ) ; } return ToolFactory . createCodeFormatter ( options ) . format ( kind , string , 0 , length , indentationLevel , lineSeparator ) ; } private static Document createDocument ( String content , Position [ ] positions ) throws IllegalArgumentException { Document doc = new Document ( content ) ; try { if ( positions != null ) { final String POS_CATEGORY = "myCategory" ; doc . addPositionCategory ( POS_CATEGORY ) ; doc . addPositionUpdater ( new DefaultPositionUpdater ( POS_CATEGORY ) { protected boolean notDeleted ( ) { if ( this . fOffset < this . fPosition . offset && ( this . fPosition . offset + this . fPosition . length < this . fOffset + this . fLength ) ) { this . fPosition . offset = this . fOffset + this . fLength ; return false ; } return true ; } } ) ; for ( int i = 0 ; i < positions . length ; i ++ ) { try { doc . addPosition ( POS_CATEGORY , positions [ i ] ) ; } catch ( BadLocationException e ) { throw new IllegalArgumentException ( "" + positions [ i ] . offset + ", length: " + positions [ i ] . length + "" + content . length ( ) ) ; } } } } catch ( BadPositionCategoryException cannotHappen ) { } return doc ; } public static void log ( Throwable t ) { Util . log ( t , "" ) ; } } </s>