<s> package org . eclipse . jdt . internal . formatter . align ; import org . eclipse . core . runtime . Assert ; import org . eclipse . jdt . internal . formatter . Location ; import org . eclipse . jdt . internal . formatter . Scribe ; public class Alignment { public int kind ; public static final int ALLOCATION = 1 ; public static final int ANNOTATION_MEMBERS_VALUE_PAIRS = 2 ; public static final int ARRAY_INITIALIZER = 3 ; public static final int ASSIGNMENT = 4 ; public static final int BINARY_EXPRESSION = 5 ; public static final int CASCADING_MESSAGE_SEND = 6 ; public static final int COMPACT_IF = 7 ; public static final int COMPOUND_ASSIGNMENT = 8 ; public static final int CONDITIONAL_EXPRESSION = 9 ; public static final int ENUM_CONSTANTS = 10 ; public static final int ENUM_CONSTANTS_ARGUMENTS = 11 ; public static final int EXPLICIT_CONSTRUCTOR_CALL = 12 ; public static final int FIELD_DECLARATION_ASSIGNMENT = 13 ; public static final int LOCAL_DECLARATION_ASSIGNMENT = 14 ; public static final int MESSAGE_ARGUMENTS = 15 ; public static final int MESSAGE_SEND = 16 ; public static final int METHOD_ARGUMENTS = 17 ; public static final int METHOD_DECLARATION = 18 ; public static final int MULTIPLE_FIELD = 19 ; public static final int SUPER_CLASS = 20 ; public static final int SUPER_INTERFACES = 21 ; public static final int THROWS = 22 ; public static final int TYPE_MEMBERS = 23 ; public static final int STRING_CONCATENATION = 24 ; public static final int TRY_RESOURCES = 25 ; public static final int MULTI_CATCH = 26 ; public String name ; public static final String [ ] NAMES = { "" , "allocation" , "" , "" , "" , "" , "" , "compactIf" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "superclass" , "" , "throws" , "typeMembers" , "" , "tryResources" , "" , } ; public Alignment enclosing ; public Location location ; public int fragmentIndex ; public int fragmentCount ; public int [ ] fragmentIndentations ; public boolean needRedoColumnAlignment ; public int chunkStartIndex ; public int chunkKind ; public int originalIndentationLevel ; public int breakIndentationLevel ; public int shiftBreakIndentationLevel ; public int [ ] fragmentBreaks ; public boolean wasSplit ; public boolean blockAlign = false ; public boolean tooLong = false ; public Scribe scribe ; private boolean reset = false ; public static final int M_FORCE = 1 ; public static final int M_INDENT_ON_COLUMN = 2 ; public static final int M_INDENT_BY_ONE = 4 ; public static final int M_COMPACT_SPLIT = 16 ; public static final int M_COMPACT_FIRST_BREAK_SPLIT = 32 ; public static final int M_ONE_PER_LINE_SPLIT = 32 + 16 ; public static final int M_NEXT_SHIFTED_SPLIT = 64 ; public static final int M_NEXT_PER_LINE_SPLIT = 64 + 16 ; public static final int M_MULTICOLUMN = 256 ; public static final int M_NO_ALIGNMENT = 0 ; public int mode ; public static final int SPLIT_MASK = M_ONE_PER_LINE_SPLIT | M_NEXT_SHIFTED_SPLIT | M_COMPACT_SPLIT | M_COMPACT_FIRST_BREAK_SPLIT | M_NEXT_PER_LINE_SPLIT ; public static final int R_OUTERMOST = 1 ; public static final int R_INNERMOST = 2 ; public int tieBreakRule ; public int startingColumn = - 1 ; public static final int NONE = 0 ; public static final int BREAK = 1 ; public static final int CHUNK_FIELD = 1 ; public static final int CHUNK_METHOD = 2 ; public static final int CHUNK_TYPE = 3 ; public static final int CHUNK_ENUM = 4 ; public Alignment ( int kind , int mode , int tieBreakRule , Scribe scribe , int fragmentCount , int sourceRestart , int continuationIndent ) { Assert . isTrue ( kind >= ALLOCATION && kind <= MULTI_CATCH ) ; this . kind = kind ; this . name = NAMES [ kind ] ; this . location = new Location ( scribe , sourceRestart ) ; this . mode = mode ; this . tieBreakRule = tieBreakRule ; this . fragmentCount = fragmentCount ; this . scribe = scribe ; this . originalIndentationLevel = this . scribe . indentationLevel ; this . wasSplit = false ; final int indentSize = this . scribe . indentationSize ; int currentColumn = this . location . outputColumn ; if ( currentColumn == 1 ) { currentColumn = this . location . outputIndentationLevel + 1 ; } if ( ( mode & M_INDENT_ON_COLUMN ) != 0 ) { this . breakIndentationLevel = this . scribe . getNextIndentationLevel ( currentColumn ) ; if ( this . breakIndentationLevel == this . location . outputIndentationLevel ) { this . breakIndentationLevel += ( continuationIndent * indentSize ) ; } } else if ( ( mode & M_INDENT_BY_ONE ) != 0 ) { this . breakIndentationLevel = this . location . outputIndentationLevel + indentSize ; } else { this . breakIndentationLevel = this . location . outputIndentationLevel + continuationIndent * indentSize ; } this . shiftBreakIndentationLevel = this . breakIndentationLevel + indentSize ; this . fragmentIndentations = new int [ this . fragmentCount ] ; this . fragmentBreaks = new int [ this . fragmentCount ] ; if ( ( this . mode & M_FORCE ) != 0 ) { couldBreak ( ) ; } } public boolean checkChunkStart ( int chunk , int startIndex , int sourceRestart ) { if ( this . chunkKind != chunk ) { this . chunkKind = chunk ; if ( startIndex != this . chunkStartIndex ) { this . chunkStartIndex = startIndex ; this . location . update ( this . scribe , sourceRestart ) ; reset ( ) ; } return true ; } return false ; } public void checkColumn ( ) { if ( ( this . mode & M_MULTICOLUMN ) != 0 && this . fragmentCount > 0 ) { int currentIndentation = this . scribe . getNextIndentationLevel ( this . scribe . column + ( this . scribe . needSpace ? 1 : 0 ) ) ; int fragmentIndentation = this . fragmentIndentations [ this . fragmentIndex ] ; if ( currentIndentation > fragmentIndentation ) { this . fragmentIndentations [ this . fragmentIndex ] = currentIndentation ; if ( fragmentIndentation != 0 ) { for ( int i = this . fragmentIndex + 1 ; i < this . fragmentCount ; i ++ ) { this . fragmentIndentations [ i ] = 0 ; } this . needRedoColumnAlignment = true ; } } if ( this . needRedoColumnAlignment && this . fragmentIndex == this . fragmentCount - 1 ) { this . needRedoColumnAlignment = false ; int relativeDepth = 0 ; Alignment targetAlignment = this . scribe . memberAlignment ; while ( targetAlignment != null ) { if ( targetAlignment == this ) { throw new AlignmentException ( AlignmentException . ALIGN_TOO_SMALL , relativeDepth ) ; } targetAlignment = targetAlignment . enclosing ; relativeDepth ++ ; } } } } public int depth ( ) { int depth = 0 ; Alignment current = this . enclosing ; while ( current != null ) { depth ++ ; current = current . enclosing ; } return depth ; } public boolean canAlign ( ) { if ( this . tooLong ) { return true ; } boolean canAlign = true ; Alignment enclosingAlignment = this . enclosing ; while ( enclosingAlignment != null ) { switch ( enclosingAlignment . kind ) { case Alignment . ALLOCATION : case Alignment . MESSAGE_ARGUMENTS : if ( enclosingAlignment . isWrapped ( ) && ( enclosingAlignment . fragmentIndex > 0 || enclosingAlignment . fragmentCount < 2 ) ) { return ! this . blockAlign ; } if ( enclosingAlignment . tooLong ) { return true ; } canAlign = false ; break ; case Alignment . MESSAGE_SEND : switch ( this . kind ) { case Alignment . ALLOCATION : case Alignment . MESSAGE_ARGUMENTS : case Alignment . MESSAGE_SEND : Alignment superEnclosingAlignment = enclosingAlignment . enclosing ; while ( superEnclosingAlignment != null ) { switch ( superEnclosingAlignment . kind ) { case Alignment . ALLOCATION : case Alignment . MESSAGE_ARGUMENTS : case Alignment . MESSAGE_SEND : if ( this . scribe . nlsTagCounter == 0 ) { enclosingAlignment . blockAlign = true ; } return ! this . blockAlign ; } superEnclosingAlignment = superEnclosingAlignment . enclosing ; } break ; } return ! this . blockAlign ; } enclosingAlignment = enclosingAlignment . enclosing ; } return canAlign && ! this . blockAlign ; } public boolean couldBreak ( ) { if ( this . fragmentCount == 0 ) return false ; int i ; switch ( this . mode & SPLIT_MASK ) { case M_COMPACT_FIRST_BREAK_SPLIT : if ( this . fragmentBreaks [ 0 ] == NONE ) { this . fragmentBreaks [ 0 ] = BREAK ; this . fragmentIndentations [ 0 ] = this . breakIndentationLevel ; return this . wasSplit = true ; } i = this . fragmentIndex ; do { if ( this . fragmentBreaks [ i ] == NONE ) { this . fragmentBreaks [ i ] = BREAK ; this . fragmentIndentations [ i ] = this . breakIndentationLevel ; return this . wasSplit = true ; } } while ( -- i >= 0 ) ; break ; case M_COMPACT_SPLIT : i = this . fragmentIndex ; do { if ( this . fragmentBreaks [ i ] == NONE ) { this . fragmentBreaks [ i ] = BREAK ; this . fragmentIndentations [ i ] = this . breakIndentationLevel ; return this . wasSplit = true ; } } while ( -- i >= 0 ) ; break ; case M_NEXT_SHIFTED_SPLIT : if ( this . fragmentBreaks [ 0 ] == NONE ) { this . fragmentBreaks [ 0 ] = BREAK ; this . fragmentIndentations [ 0 ] = this . breakIndentationLevel ; for ( i = 1 ; i < this . fragmentCount ; i ++ ) { this . fragmentBreaks [ i ] = BREAK ; this . fragmentIndentations [ i ] = this . shiftBreakIndentationLevel ; } return this . wasSplit = true ; } break ; case M_ONE_PER_LINE_SPLIT : if ( this . fragmentBreaks [ 0 ] == NONE ) { for ( i = 0 ; i < this . fragmentCount ; i ++ ) { this . fragmentBreaks [ i ] = BREAK ; this . fragmentIndentations [ i ] = this . breakIndentationLevel ; } return this . wasSplit = true ; } break ; case M_NEXT_PER_LINE_SPLIT : if ( this . fragmentBreaks [ 0 ] == NONE ) { if ( this . fragmentCount > 1 && this . fragmentBreaks [ 1 ] == NONE ) { if ( ( this . mode & M_INDENT_ON_COLUMN ) != 0 ) { this . fragmentIndentations [ 0 ] = this . breakIndentationLevel ; } for ( i = 1 ; i < this . fragmentCount ; i ++ ) { this . fragmentBreaks [ i ] = BREAK ; this . fragmentIndentations [ i ] = this . breakIndentationLevel ; } return this . wasSplit = true ; } } break ; } return false ; } public boolean isWrapped ( ) { if ( this . fragmentCount == 0 ) return false ; return this . fragmentBreaks [ this . fragmentIndex ] == BREAK ; } public int wrappedIndex ( ) { for ( int i = 0 , max = this . fragmentCount ; i < max ; i ++ ) { if ( this . fragmentBreaks [ i ] == BREAK ) { return i ; } } return - 1 ; } public void performFragmentEffect ( ) { if ( this . fragmentCount == 0 ) return ; if ( ( this . mode & M_MULTICOLUMN ) == 0 ) { switch ( this . mode & SPLIT_MASK ) { case Alignment . M_COMPACT_SPLIT : case Alignment . M_COMPACT_FIRST_BREAK_SPLIT : case Alignment . M_NEXT_PER_LINE_SPLIT : case Alignment . M_NEXT_SHIFTED_SPLIT : case Alignment . M_ONE_PER_LINE_SPLIT : break ; default : return ; } } int fragmentIndentation = this . fragmentIndentations [ this . fragmentIndex ] ; if ( this . startingColumn < 0 || ( fragmentIndentation + 1 ) < this . startingColumn ) { if ( this . fragmentBreaks [ this . fragmentIndex ] == BREAK ) { this . scribe . printNewLine ( ) ; } if ( fragmentIndentation > 0 ) { this . scribe . indentationLevel = fragmentIndentation ; } } } public void reset ( ) { this . wasSplit = false ; if ( this . fragmentCount > 0 ) { this . fragmentIndentations = new int [ this . fragmentCount ] ; this . fragmentBreaks = new int [ this . fragmentCount ] ; } if ( ( this . mode & M_FORCE ) != 0 ) { couldBreak ( ) ; } this . reset = true ; } public void toFragmentsString ( StringBuffer buffer ) { } public String toString ( ) { StringBuffer buffer = new StringBuffer ( 10 ) ; return toString ( buffer , - 1 ) ; } public String toString ( StringBuffer buffer , int level ) { StringBuffer indentation = new StringBuffer ( ) ; for ( int i = 0 ; i < level ; i ++ ) { indentation . append ( '\t' ) ; } buffer . append ( indentation ) ; buffer . append ( "<kind: " ) . append ( this . kind ) . append ( "> " ) ; buffer . append ( "<name: " ) . append ( this . name ) . append ( ">n" ) ; buffer . append ( indentation ) ; buffer . append ( "<depth=" ) . append ( depth ( ) ) . append ( "" ) . append ( this . breakIndentationLevel ) . append ( "" ) . append ( this . shiftBreakIndentationLevel ) . append ( ">n" ) ; buffer . append ( indentation ) ; buffer . append ( "<location=" ) . append ( this . location . toString ( ) ) . append ( ">n" ) ; buffer . append ( indentation ) . append ( "<fragments:n" ) ; for ( int i = 0 ; i < this . fragmentCount ; i ++ ) { buffer . append ( indentation ) . append ( " - " ) . append ( i ) . append ( ": " ) . append ( "<break: " ) . append ( this . fragmentBreaks [ i ] > 0 ? "YES" : "NO" ) . append ( ">" ) . append ( "<indent: " ) . append ( this . fragmentIndentations [ i ] ) . append ( ">n" ) ; } buffer . append ( indentation ) . append ( ">n" ) ; if ( this . enclosing != null && level >= 0 ) { buffer . append ( indentation ) . append ( "" ) ; this . enclosing . toString ( buffer , level + 1 ) ; buffer . append ( indentation ) . append ( ">n" ) ; } return buffer . toString ( ) ; } public void update ( ) { for ( int i = 1 ; i < this . fragmentCount ; i ++ ) { if ( this . fragmentBreaks [ i ] == BREAK ) { this . fragmentIndentations [ i ] = this . breakIndentationLevel ; } } } public boolean wasReset ( ) { return this . reset ; } } </s>