1 11 package org.eclipse.jdt.internal.formatter.align; 12 13 import org.eclipse.jdt.internal.formatter.Location2; 14 import org.eclipse.jdt.internal.formatter.Scribe2; 15 16 21 public class Alignment2 { 22 23 public String name; 25 26 public Alignment2 enclosing; 28 29 public Location2 location; 31 32 public int fragmentIndex; 34 public int fragmentCount; 35 public int[] fragmentIndentations; 36 public boolean needRedoColumnAlignment; 37 38 public int chunkStartIndex; 40 public int chunkKind; 41 42 public int originalIndentationLevel; 44 public int breakIndentationLevel; 45 public int shiftBreakIndentationLevel; 46 public int[] fragmentBreaks; 47 public boolean wasSplit; 48 49 public Scribe2 scribe; 50 51 54 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; 58 60 64 public static final int M_COMPACT_SPLIT = 16; 66 71 public static final int M_COMPACT_FIRST_BREAK_SPLIT = 32; 73 80 public static final int M_ONE_PER_LINE_SPLIT = 32+16; 82 90 public static final int M_NEXT_SHIFTED_SPLIT = 64; 92 98 public static final int M_NEXT_PER_LINE_SPLIT = 64+16; 100 103 111 public static final int M_MULTICOLUMN = 256; 113 public static final int M_NO_ALIGNMENT = 0; 114 115 public int mode; 116 117 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; 118 119 public static final int R_OUTERMOST = 1; 121 public static final int R_INNERMOST = 2; 122 public int tieBreakRule; 123 124 public static int NONE = 0; 126 public static int BREAK = 1; 127 128 public static final int CHUNK_FIELD = 1; 130 public static final int CHUNK_METHOD = 2; 131 public static final int CHUNK_TYPE = 3; 132 public static final int CHUNK_ENUM = 4; 133 134 public Alignment2(String name, int mode, int tieBreakRule, Scribe2 scribe, int fragmentCount, int sourceRestart, int continuationIndent){ 136 137 this.name = name; 138 this.location = new Location2(scribe, sourceRestart); 139 this.mode = mode; 140 this.tieBreakRule = tieBreakRule; 141 this.fragmentCount = fragmentCount; 142 this.scribe = scribe; 143 this.originalIndentationLevel = this.scribe.indentationLevel; 144 this.wasSplit = false; 145 146 final int indentSize = this.scribe.indentationSize; 148 int currentColumn = this.location.outputColumn; 149 if (currentColumn == 1) { 150 currentColumn = this.location.outputIndentationLevel + 1; 151 } 152 153 if ((mode & M_INDENT_ON_COLUMN) != 0) { 154 this.breakIndentationLevel = this.scribe.getNextIndentationLevel(currentColumn); 156 if (this.breakIndentationLevel == this.location.outputIndentationLevel) { 157 this.breakIndentationLevel += (continuationIndent * indentSize); 158 } 159 } else if ((mode & M_INDENT_BY_ONE) != 0) { 160 this.breakIndentationLevel = this.location.outputIndentationLevel + indentSize; 162 } else { 163 this.breakIndentationLevel = this.location.outputIndentationLevel + continuationIndent * indentSize; 164 } 165 this.shiftBreakIndentationLevel = this.breakIndentationLevel + indentSize; 166 167 this.fragmentIndentations = new int[this.fragmentCount]; 168 this.fragmentBreaks = new int[this.fragmentCount]; 169 170 if ((this.mode & M_FORCE) != 0) { 172 couldBreak(); 173 } 174 } 175 176 public boolean checkChunkStart(int kind, int startIndex, int sourceRestart) { 177 if (this.chunkKind != kind) { 178 this.chunkKind = kind; 179 180 if (startIndex != this.chunkStartIndex) { 182 this.chunkStartIndex = startIndex; 183 this.location.update(this.scribe, sourceRestart); 184 reset(); 185 } 186 return true; 187 } 188 return false; 189 } 190 191 public void checkColumn() { 192 if ((this.mode & M_MULTICOLUMN) != 0) { 193 int currentIndentation = this.scribe.getNextIndentationLevel(this.scribe.column+(this.scribe.needSpace ? 1 : 0)); 194 int fragmentIndentation = this.fragmentIndentations[this.fragmentIndex]; 195 if (currentIndentation > fragmentIndentation) { 196 this.fragmentIndentations[this.fragmentIndex] = currentIndentation; 197 if (fragmentIndentation != 0) { 198 for (int i = this.fragmentIndex+1; i < this.fragmentCount; i++) { 199 this.fragmentIndentations[i] = 0; 200 } 201 this.needRedoColumnAlignment = true; 202 } 203 } 204 if (this.needRedoColumnAlignment && this.fragmentIndex == this.fragmentCount-1) { 207 this.needRedoColumnAlignment = false; 212 int relativeDepth = 0; 213 Alignment2 targetAlignment = this.scribe.memberAlignment; 214 while (targetAlignment != null){ 215 if (targetAlignment == this){ 216 throw new AlignmentException(AlignmentException.ALIGN_TOO_SMALL, relativeDepth); 217 } 218 targetAlignment = targetAlignment.enclosing; 219 relativeDepth++; 220 } 221 } 222 } 223 } 224 225 public boolean couldBreak(){ 226 int i; 227 switch(mode & SPLIT_MASK){ 228 229 234 case M_COMPACT_FIRST_BREAK_SPLIT : 235 if (this.fragmentBreaks[0] == NONE) { 236 this.fragmentBreaks[0] = BREAK; 237 this.fragmentIndentations[0] = this.breakIndentationLevel; 238 return wasSplit = true; 239 } 240 i = this.fragmentIndex; 241 do { 242 if (this.fragmentBreaks[i] == NONE) { 243 this.fragmentBreaks[i] = BREAK; 244 this.fragmentIndentations[i] = this.breakIndentationLevel; 245 return wasSplit = true; 246 } 247 } while (--i >= 0); 248 break; 249 253 case M_COMPACT_SPLIT : 254 i = this.fragmentIndex; 255 do { 256 if (this.fragmentBreaks[i] == NONE) { 257 this.fragmentBreaks[i] = BREAK; 258 this.fragmentIndentations[i] = this.breakIndentationLevel; 259 return wasSplit = true; 260 } 261 } while (--i >= 0); 262 break; 263 264 270 case M_NEXT_SHIFTED_SPLIT : 271 if (this.fragmentBreaks[0] == NONE) { 272 this.fragmentBreaks[0] = BREAK; 273 this.fragmentIndentations[0] = this.breakIndentationLevel; 274 for (i = 1; i < this.fragmentCount; i++){ 275 this.fragmentBreaks[i] = BREAK; 276 this.fragmentIndentations[i] = this.shiftBreakIndentationLevel; 277 } 278 return wasSplit = true; 279 } 280 break; 281 282 288 case M_ONE_PER_LINE_SPLIT : 289 if (this.fragmentBreaks[0] == NONE) { 290 for (i = 0; i < this.fragmentCount; i++){ 291 this.fragmentBreaks[i] = BREAK; 292 this.fragmentIndentations[i] = this.breakIndentationLevel; 293 } 294 return wasSplit = true; 295 } 296 301 case M_NEXT_PER_LINE_SPLIT : 302 if (this.fragmentBreaks[0] == NONE) { 303 if (this.fragmentCount > 1 304 && this.fragmentBreaks[1] == NONE) { 305 if ((this.mode & M_INDENT_ON_COLUMN) != 0) { 306 this.fragmentIndentations[0] = this.breakIndentationLevel; 307 } 308 for (i = 1; i < this.fragmentCount; i++) { 309 this.fragmentBreaks[i] = BREAK; 310 this.fragmentIndentations[i] = this.breakIndentationLevel; 311 } 312 return wasSplit = true; 313 } 314 } 315 break; 316 } 317 return false; } 319 320 public Alignment2 getAlignment(String targetName) { 321 322 if (targetName.equals(this.name)) return this; 323 if (this.enclosing == null) return null; 324 325 return this.enclosing.getAlignment(targetName); 326 } 327 328 public void performFragmentEffect(){ 330 if ((this.mode & M_MULTICOLUMN) == 0) { 331 switch(this.mode & SPLIT_MASK) { 332 case Alignment2.M_COMPACT_SPLIT : 333 case Alignment2.M_COMPACT_FIRST_BREAK_SPLIT : 334 case Alignment2.M_NEXT_PER_LINE_SPLIT : 335 case Alignment2.M_NEXT_SHIFTED_SPLIT : 336 case Alignment2.M_ONE_PER_LINE_SPLIT : 337 break; 338 default: 339 return; 340 } 341 } 342 343 if (this.fragmentBreaks[this.fragmentIndex] == BREAK) { 344 this.scribe.printNewLine(); 345 } 346 if (this.fragmentIndentations[this.fragmentIndex] > 0) { 347 this.scribe.indentationLevel = this.fragmentIndentations[this.fragmentIndex]; 348 } 349 } 350 351 public void reset() { 353 354 if (fragmentCount > 0){ 355 this.fragmentIndentations = new int[this.fragmentCount]; 356 this.fragmentBreaks = new int[this.fragmentCount]; 357 } 358 359 if ((mode & M_FORCE) != 0) { 361 couldBreak(); 362 } 363 } 364 365 public void toFragmentsString(StringBuffer buffer){ 366 } 368 369 public String toString() { 370 StringBuffer buffer = new StringBuffer (10); 371 buffer 372 .append(getClass().getName()) 373 .append(':') 374 .append("<name: ") .append(this.name) 376 .append(">"); if (this.enclosing != null) { 378 buffer 379 .append("<enclosingName: ") .append(this.enclosing.name) 381 .append('>'); 382 } 383 buffer.append('\n'); 384 385 for (int i = 0; i < this.fragmentCount; i++){ 386 buffer 387 .append(" - fragment ") .append(i) 389 .append(": ") .append("<break: ") .append(this.fragmentBreaks[i] > 0 ? "YES" : "NO") .append(">") .append("<indent: ") .append(this.fragmentIndentations[i]) 395 .append(">\n"); } 397 buffer.append('\n'); 398 return buffer.toString(); 399 } 400 401 public void update() { 402 for (int i = 1; i < this.fragmentCount; i++){ 403 if (this.fragmentBreaks[i] == BREAK) { 404 this.fragmentIndentations[i] = this.breakIndentationLevel; 405 } 406 } 407 } 408 409 public boolean isWrapped() { 410 for (int i = 0, max = this.fragmentCount; i < max; i++) { 411 if (this.fragmentBreaks[i] == BREAK) { 412 return true; 413 } 414 } 415 return false; 416 } 417 } 418 | Popular Tags |