View | Details | Raw Unified | Return to bug 199265 | Differences between
and this patch

Collapse All | Expand All

(-)formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java (-97 / +89 lines)
Lines 16-21 Link Here
16
import org.eclipse.jdt.core.JavaCore;
16
import org.eclipse.jdt.core.JavaCore;
17
import org.eclipse.jdt.core.compiler.IProblem;
17
import org.eclipse.jdt.core.compiler.IProblem;
18
import org.eclipse.jdt.core.compiler.InvalidInputException;
18
import org.eclipse.jdt.core.compiler.InvalidInputException;
19
import org.eclipse.jdt.core.formatter.CodeFormatter;
19
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
20
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
20
import org.eclipse.jdt.internal.compiler.ASTVisitor;
21
import org.eclipse.jdt.internal.compiler.ASTVisitor;
21
import org.eclipse.jdt.internal.compiler.ast.AND_AND_Expression;
22
import org.eclipse.jdt.internal.compiler.ast.AND_AND_Expression;
Lines 169-175 Link Here
169
		TerminalTokens.TokenNameUNSIGNED_RIGHT_SHIFT,
170
		TerminalTokens.TokenNameUNSIGNED_RIGHT_SHIFT,
170
		TerminalTokens.TokenNameGREATER
171
		TerminalTokens.TokenNameGREATER
171
	};
172
	};
172
	private int chunkKind;
173
	public int lastLocalDeclarationSourceStart;
173
	public int lastLocalDeclarationSourceStart;
174
	private Scanner localScanner;
174
	private Scanner localScanner;
175
	public DefaultCodeFormatterOptions preferences;
175
	public DefaultCodeFormatterOptions preferences;
Lines 261-273 Link Here
261
		messageSend.traverse(builder, scope);
261
		messageSend.traverse(builder, scope);
262
		return builder;
262
		return builder;
263
	}
263
	}
264
	public boolean checkChunkStart(int kind) {
265
		if (this.chunkKind != kind) {
266
			this.chunkKind = kind;
267
			return true;
268
		}
269
		return false;
270
	}
271
264
272
	private boolean commentStartsBlock(int start, int end) {
265
	private boolean commentStartsBlock(int start, int end) {
273
		this.localScanner.resetTo(start, end);
266
		this.localScanner.resetTo(start, end);
Lines 439-447 Link Here
439
					for (int i = 0; i < fragmentsSize - 1; i++) {
432
					for (int i = 0; i < fragmentsSize - 1; i++) {
440
						ASTNode fragment = fragments[i];
433
						ASTNode fragment = fragments[i];
441
						fragment.traverse(this, scope);
434
						fragment.traverse(this, scope);
442
						this.scribe.printTrailingComment();
435
						this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
443
						if (this.scribe.lastNumberOfNewLines == 1) {
436
						if (this.scribe.lastNumberOfNewLines == 1) {
444
							// a new line has been inserted by printTrailingComment()
437
							// a new line has been inserted by printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT)
445
							this.scribe.indentationLevel = binaryExpressionAlignment.breakIndentationLevel;
438
							this.scribe.indentationLevel = binaryExpressionAlignment.breakIndentationLevel;
446
						}
439
						}
447
						if (this.preferences.wrap_before_binary_operator) {
440
						if (this.preferences.wrap_before_binary_operator) {
Lines 460-466 Link Here
460
						}
453
						}
461
					}
454
					}
462
					fragments[fragmentsSize - 1].traverse(this, scope);
455
					fragments[fragmentsSize - 1].traverse(this, scope);
463
					this.scribe.printTrailingComment();
456
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
464
					ok = true;
457
					ok = true;
465
				} catch(AlignmentException e){
458
				} catch(AlignmentException e){
466
					this.scribe.redoAlignment(e);
459
					this.scribe.redoAlignment(e);
Lines 616-631 Link Here
616
609
617
		if (memberAlignment != null) {
610
		if (memberAlignment != null) {
618
			this.scribe.alignFragment(memberAlignment, 2);
611
			this.scribe.alignFragment(memberAlignment, 2);
619
			this.scribe.printTrailingComment();
612
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
620
		} else {
613
		} else {
621
			this.scribe.space();
614
			this.scribe.space();
622
			this.scribe.printTrailingComment();
615
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
623
		}
616
		}
624
	}
617
	}
625
618
626
	private void format(ImportReference importRef, boolean isLast) {
619
	private void format(ImportReference importRef, boolean isLast) {
627
		this.scribe.printNextToken(TerminalTokens.TokenNameimport);
620
		this.scribe.printNextToken(TerminalTokens.TokenNameimport);
628
		this.preferences.number_of_empty_lines_to_preserve = this.preferences.blank_lines_between_import_groups;
621
		if (!isLast) this.scribe.blank_lines_between_import_groups = this.preferences.blank_lines_between_import_groups;
629
		this.scribe.space();
622
		this.scribe.space();
630
		if (importRef.isStatic()) {
623
		if (importRef.isStatic()) {
631
			this.scribe.printNextToken(TerminalTokens.TokenNamestatic);
624
			this.scribe.printNextToken(TerminalTokens.TokenNamestatic);
Lines 641-649 Link Here
641
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
634
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
642
		}
635
		}
643
		if (isLast) {
636
		if (isLast) {
644
			this.scribe.printTrailingComment();
637
			this.scribe.blank_lines_between_import_groups = -1;
638
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.IMPORT_TRAILING_COMMENT);
645
		} else {
639
		} else {
646
			this.scribe.printTrailingComment(this.preferences.blank_lines_between_import_groups);
640
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.NO_TRAILING_COMMENT);
641
			this.scribe.blank_lines_between_import_groups = -1;
647
		}
642
		}
648
		this.scribe.printNewLine();
643
		this.scribe.printNewLine();
649
	}
644
	}
Lines 726-732 Link Here
726
721
727
					if (i != length - 1) {
722
					if (i != length - 1) {
728
						this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_multiple_field_declarations);
723
						this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_multiple_field_declarations);
729
						this.scribe.printTrailingComment();
724
						this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
730
						this.scribe.alignFragment(multiFieldDeclarationsAlignment, i);
725
						this.scribe.alignFragment(multiFieldDeclarationsAlignment, i);
731
726
732
						if (this.preferences.insert_space_after_comma_in_multiple_field_declarations) {
727
						if (this.preferences.insert_space_after_comma_in_multiple_field_declarations) {
Lines 735-741 Link Here
735
					} else {
730
					} else {
736
						this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
731
						this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
737
						this.scribe.alignFragment(fieldAlignment, 2);
732
						this.scribe.alignFragment(fieldAlignment, 2);
738
						this.scribe.printTrailingComment();
733
						this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
739
					}
734
					}
740
				}
735
				}
741
				ok = true;
736
				ok = true;
Lines 1048-1054 Link Here
1048
					for (int i = 0; i < superInterfaceLength; i++) {
1043
					for (int i = 0; i < superInterfaceLength; i++) {
1049
						if (i > 0) {
1044
						if (i > 0) {
1050
							this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_superinterfaces);
1045
							this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_superinterfaces);
1051
							this.scribe.printTrailingComment();
1046
							this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1052
							this.scribe.alignFragment(interfaceAlignment, i+1);
1047
							this.scribe.alignFragment(interfaceAlignment, i+1);
1053
							if (this.preferences.insert_space_after_comma_in_superinterfaces) {
1048
							if (this.preferences.insert_space_after_comma_in_superinterfaces) {
1054
								this.scribe.space();
1049
								this.scribe.space();
Lines 1143-1149 Link Here
1143
									if (this.preferences.insert_space_after_comma_in_enum_declarations) {
1138
									if (this.preferences.insert_space_after_comma_in_enum_declarations) {
1144
										this.scribe.space();
1139
										this.scribe.space();
1145
									}
1140
									}
1146
									this.scribe.printTrailingComment();
1141
									this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1147
									if (fieldDeclaration.initialization instanceof QualifiedAllocationExpression) {
1142
									if (fieldDeclaration.initialization instanceof QualifiedAllocationExpression) {
1148
										this.scribe.printNewLine();
1143
										this.scribe.printNewLine();
1149
									}
1144
									}
Lines 1164-1170 Link Here
1164
						if (this.preferences.insert_space_after_comma_in_enum_declarations) {
1159
						if (this.preferences.insert_space_after_comma_in_enum_declarations) {
1165
							this.scribe.space();
1160
							this.scribe.space();
1166
						}
1161
						}
1167
						this.scribe.printTrailingComment();
1162
						this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1168
						if (fieldDeclaration.initialization instanceof QualifiedAllocationExpression) {
1163
						if (fieldDeclaration.initialization instanceof QualifiedAllocationExpression) {
1169
							this.scribe.printNewLine();
1164
							this.scribe.printNewLine();
1170
						}
1165
						}
Lines 1173-1179 Link Here
1173
			}
1168
			}
1174
			if (isNextToken(TerminalTokens.TokenNameSEMICOLON)) {
1169
			if (isNextToken(TerminalTokens.TokenNameSEMICOLON)) {
1175
				this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
1170
				this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
1176
				this.scribe.printTrailingComment();
1171
				this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1177
				if (hasConstants
1172
				if (hasConstants
1178
						|| ((enumConstantsLength - length) != 0)
1173
						|| ((enumConstantsLength - length) != 0)
1179
						|| typeDeclaration.methods != null
1174
						|| typeDeclaration.methods != null
Lines 1210-1216 Link Here
1210
				}
1205
				}
1211
		}
1206
		}
1212
		this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE);
1207
		this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE);
1213
		this.scribe.printTrailingComment();
1208
		this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1214
		if (class_declaration_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) {
1209
		if (class_declaration_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) {
1215
			this.scribe.unIndent();
1210
			this.scribe.unIndent();
1216
		}
1211
		}
Lines 1305-1311 Link Here
1305
			}
1300
			}
1306
		}
1301
		}
1307
		this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE);
1302
		this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE);
1308
		this.scribe.printTrailingComment();
1303
		this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1309
		if (DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED.equals(block_brace_position)) {
1304
		if (DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED.equals(block_brace_position)) {
1310
			this.scribe.unIndent();
1305
			this.scribe.unIndent();
1311
		}
1306
		}
Lines 1367-1373 Link Here
1367
						for (int j = 0; j < argumentLength; j++) {
1362
						for (int j = 0; j < argumentLength; j++) {
1368
							if (j > 0) {
1363
							if (j > 0) {
1369
								this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_method_invocation_arguments);
1364
								this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_method_invocation_arguments);
1370
								this.scribe.printTrailingComment();
1365
								this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1371
							}
1366
							}
1372
							this.scribe.alignFragment(argumentsAlignment, j);
1367
							this.scribe.alignFragment(argumentsAlignment, j);
1373
							if (j > 0 && this.preferences.insert_space_after_comma_in_method_invocation_arguments) {
1368
							if (j > 0 && this.preferences.insert_space_after_comma_in_method_invocation_arguments) {
Lines 1452-1458 Link Here
1452
								for (int j = 0; j < argumentLength; j++) {
1447
								for (int j = 0; j < argumentLength; j++) {
1453
									if (j > 0) {
1448
									if (j > 0) {
1454
										this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_method_invocation_arguments);
1449
										this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_method_invocation_arguments);
1455
										this.scribe.printTrailingComment();
1450
										this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1456
									}
1451
									}
1457
									this.scribe.alignFragment(argumentsAlignment, j);
1452
									this.scribe.alignFragment(argumentsAlignment, j);
1458
									if (j > 0 && this.preferences.insert_space_after_comma_in_method_invocation_arguments) {
1453
									if (j > 0 && this.preferences.insert_space_after_comma_in_method_invocation_arguments) {
Lines 1532-1538 Link Here
1532
					}
1527
					}
1533
					if (isNextToken(TerminalTokens.TokenNameSEMICOLON)) {
1528
					if (isNextToken(TerminalTokens.TokenNameSEMICOLON)) {
1534
						this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
1529
						this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
1535
						this.scribe.printTrailingComment();
1530
						this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1536
					}
1531
					}
1537
					if (i != max - 1) {
1532
					if (i != max - 1) {
1538
						this.scribe.printNewLine();
1533
						this.scribe.printNewLine();
Lines 1556-1562 Link Here
1556
		while(isNextToken(TerminalTokens.TokenNameSEMICOLON)) {
1551
		while(isNextToken(TerminalTokens.TokenNameSEMICOLON)) {
1557
			this.scribe.printComment();
1552
			this.scribe.printComment();
1558
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
1553
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
1559
			this.scribe.printTrailingComment();
1554
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1560
		}
1555
		}
1561
		if (hasSemiColon && isFirst) {
1556
		if (hasSemiColon && isFirst) {
1562
			this.scribe.printNewLine();
1557
			this.scribe.printNewLine();
Lines 1571-1577 Link Here
1571
		final Statement[] statements = block.statements;
1566
		final Statement[] statements = block.statements;
1572
		statements[0].traverse(this, scope);
1567
		statements[0].traverse(this, scope);
1573
		this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE, true);
1568
		this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE, true);
1574
		this.scribe.printTrailingComment();
1569
		this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1575
	}
1570
	}
1576
1571
1577
	private void formatLeftCurlyBrace(final int line, final String bracePosition) {
1572
	private void formatLeftCurlyBrace(final int line, final String bracePosition) {
Lines 1657-1663 Link Here
1657
			if (insertSpaceAfterComma) {
1652
			if (insertSpaceAfterComma) {
1658
				this.scribe.space();
1653
				this.scribe.space();
1659
			}
1654
			}
1660
			this.scribe.printTrailingComment();
1655
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1661
		}
1656
		}
1662
	}
1657
	}
1663
1658
Lines 1714-1720 Link Here
1714
						for (int i = 0; i < argumentsLength; i++) {
1709
						for (int i = 0; i < argumentsLength; i++) {
1715
							if (i > 0) {
1710
							if (i > 0) {
1716
								this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_method_invocation_arguments);
1711
								this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_method_invocation_arguments);
1717
								this.scribe.printTrailingComment();
1712
								this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1718
							}
1713
							}
1719
							this.scribe.alignFragment(argumentsAlignment, i);
1714
							this.scribe.alignFragment(argumentsAlignment, i);
1720
							if (i > 0 && this.preferences.insert_space_after_comma_in_method_invocation_arguments) {
1715
							if (i > 0 && this.preferences.insert_space_after_comma_in_method_invocation_arguments) {
Lines 1732-1738 Link Here
1732
				for (int i = 0; i < argumentsLength; i++) {
1727
				for (int i = 0; i < argumentsLength; i++) {
1733
					if (i > 0) {
1728
					if (i > 0) {
1734
						this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_method_invocation_arguments);
1729
						this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_method_invocation_arguments);
1735
						this.scribe.printTrailingComment();
1730
						this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1736
					}
1731
					}
1737
					if (i > 0 && this.preferences.insert_space_after_comma_in_method_invocation_arguments) {
1732
					if (i > 0 && this.preferences.insert_space_after_comma_in_method_invocation_arguments) {
1738
						this.scribe.space();
1733
						this.scribe.space();
Lines 1776-1782 Link Here
1776
					for (int i = 0; i < argumentLength; i++) {
1771
					for (int i = 0; i < argumentLength; i++) {
1777
						if (i > 0) {
1772
						if (i > 0) {
1778
							this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, spaceBeforeComma);
1773
							this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, spaceBeforeComma);
1779
							this.scribe.printTrailingComment();
1774
							this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1780
						}
1775
						}
1781
						this.scribe.alignFragment(argumentsAlignment, i);
1776
						this.scribe.alignFragment(argumentsAlignment, i);
1782
						if (i > 0 && spaceAfterComma) {
1777
						if (i > 0 && spaceAfterComma) {
Lines 1830-1836 Link Here
1830
					for (int i = 0; i < argumentLength; i++) {
1825
					for (int i = 0; i < argumentLength; i++) {
1831
						if (i > 0) {
1826
						if (i > 0) {
1832
							this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, spaceBeforeComma);
1827
							this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, spaceBeforeComma);
1833
							this.scribe.printTrailingComment();
1828
							this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1834
						}
1829
						}
1835
						this.scribe.alignFragment(argumentsAlignment, i);
1830
						this.scribe.alignFragment(argumentsAlignment, i);
1836
						if (i > 0 && spaceAfterComma) {
1831
						if (i > 0 && spaceAfterComma) {
Lines 1856-1866 Link Here
1856
			this.scribe.printNewLine();
1851
			this.scribe.printNewLine();
1857
			this.scribe.indent();
1852
			this.scribe.indent();
1858
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
1853
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
1859
			this.scribe.printTrailingComment();
1854
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1860
			this.scribe.unIndent();
1855
			this.scribe.unIndent();
1861
		} else {
1856
		} else {
1862
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
1857
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
1863
			this.scribe.printTrailingComment();
1858
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1864
		}
1859
		}
1865
	}
1860
	}
1866
1861
Lines 1873-1879 Link Here
1873
				this.scribe.indent();
1868
				this.scribe.indent();
1874
			}
1869
			}
1875
			this.scribe.printNextToken(TerminalTokens.TokenNameLBRACE, insertSpaceBeforeBrace);
1870
			this.scribe.printNextToken(TerminalTokens.TokenNameLBRACE, insertSpaceBeforeBrace);
1876
			this.scribe.printTrailingComment();
1871
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1877
	}
1872
	}
1878
	private void formatStatements(BlockScope scope, final Statement[] statements, boolean insertNewLineAfterLastStatement) {
1873
	private void formatStatements(BlockScope scope, final Statement[] statements, boolean insertNewLineAfterLastStatement) {
1879
		int statementsLength = statements.length;
1874
		int statementsLength = statements.length;
Lines 1885-1891 Link Here
1885
			statement.traverse(this, scope);
1880
			statement.traverse(this, scope);
1886
			if (statement instanceof Expression) {
1881
			if (statement instanceof Expression) {
1887
				this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
1882
				this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
1888
				this.scribe.printTrailingComment();
1883
				this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1889
				if (i != statementsLength - 1) {
1884
				if (i != statementsLength - 1) {
1890
					if (!(statement instanceof EmptyStatement) && !(statements[i + 1] instanceof EmptyStatement)) {
1885
					if (!(statement instanceof EmptyStatement) && !(statements[i + 1] instanceof EmptyStatement)) {
1891
						this.scribe.printNewLine();
1886
						this.scribe.printNewLine();
Lines 1903-1909 Link Here
1903
						LocalDeclaration nextLocal = (LocalDeclaration) statements[i + 1];
1898
						LocalDeclaration nextLocal = (LocalDeclaration) statements[i + 1];
1904
						if (currentLocal.declarationSourceStart != nextLocal.declarationSourceStart) {
1899
						if (currentLocal.declarationSourceStart != nextLocal.declarationSourceStart) {
1905
							this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
1900
							this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
1906
							this.scribe.printTrailingComment();
1901
							this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1907
							if (i != statementsLength - 1) {
1902
							if (i != statementsLength - 1) {
1908
								if (!(statement instanceof EmptyStatement) && !(statements[i + 1] instanceof EmptyStatement)) {
1903
								if (!(statement instanceof EmptyStatement) && !(statements[i + 1] instanceof EmptyStatement)) {
1909
									this.scribe.printNewLine();
1904
									this.scribe.printNewLine();
Lines 1914-1920 Link Here
1914
						}
1909
						}
1915
					} else {
1910
					} else {
1916
						this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
1911
						this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
1917
						this.scribe.printTrailingComment();
1912
						this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1918
						if (i != statementsLength - 1) {
1913
						if (i != statementsLength - 1) {
1919
							if (!(statement instanceof EmptyStatement) && !(statements[i + 1] instanceof EmptyStatement)) {
1914
							if (!(statement instanceof EmptyStatement) && !(statements[i + 1] instanceof EmptyStatement)) {
1920
								this.scribe.printNewLine();
1915
								this.scribe.printNewLine();
Lines 1925-1931 Link Here
1925
					}
1920
					}
1926
				} else {
1921
				} else {
1927
					this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
1922
					this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
1928
					this.scribe.printTrailingComment();
1923
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1929
					if (i != statementsLength - 1) {
1924
					if (i != statementsLength - 1) {
1930
						if (!(statement instanceof EmptyStatement) && !(statements[i + 1] instanceof EmptyStatement)) {
1925
						if (!(statement instanceof EmptyStatement) && !(statements[i + 1] instanceof EmptyStatement)) {
1931
							this.scribe.printNewLine();
1926
							this.scribe.printNewLine();
Lines 1969-1975 Link Here
1969
					for (int i = 0; i < thrownExceptionsLength; i++) {
1964
					for (int i = 0; i < thrownExceptionsLength; i++) {
1970
						if (i > 0) {
1965
						if (i > 0) {
1971
							this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, spaceBeforeComma);
1966
							this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, spaceBeforeComma);
1972
							this.scribe.printTrailingComment();
1967
							this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1973
							this.scribe.alignFragment(throwsAlignment, i);
1968
							this.scribe.alignFragment(throwsAlignment, i);
1974
							if (spaceAfterComma) {
1969
							if (spaceAfterComma) {
1975
								this.scribe.space();
1970
								this.scribe.space();
Lines 2047-2053 Link Here
2047
						}
2042
						}
2048
						if (isNextToken(TerminalTokens.TokenNameSEMICOLON)) {
2043
						if (isNextToken(TerminalTokens.TokenNameSEMICOLON)) {
2049
							this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
2044
							this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
2050
							this.scribe.printTrailingComment();
2045
							this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
2051
						}
2046
						}
2052
						this.scribe.printNewLine();
2047
						this.scribe.printNewLine();
2053
						// realign to the proper value
2048
						// realign to the proper value
Lines 2065-2071 Link Here
2065
		} else if (isNextToken(TerminalTokens.TokenNameSEMICOLON)) {
2060
		} else if (isNextToken(TerminalTokens.TokenNameSEMICOLON)) {
2066
			// the only body declaration is an empty declaration (';')
2061
			// the only body declaration is an empty declaration (';')
2067
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
2062
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
2068
			this.scribe.printTrailingComment();
2063
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
2069
		}
2064
		}
2070
		this.scribe.printComment();
2065
		this.scribe.printComment();
2071
		this.scribe.exitMemberAlignment(memberAlignment);
2066
		this.scribe.exitMemberAlignment(memberAlignment);
Lines 2296-2302 Link Here
2296
				case TerminalTokens.TokenNameCOMMENT_JAVADOC :
2291
				case TerminalTokens.TokenNameCOMMENT_JAVADOC :
2297
				case TerminalTokens.TokenNameCOMMENT_BLOCK :
2292
				case TerminalTokens.TokenNameCOMMENT_BLOCK :
2298
				case TerminalTokens.TokenNameCOMMENT_LINE :
2293
				case TerminalTokens.TokenNameCOMMENT_LINE :
2299
	    			this.scribe.printComment(token);
2294
	    			this.scribe.printComment(token, Scribe.NO_TRAILING_COMMENT);
2300
	    			break;
2295
	    			break;
2301
			}
2296
			}
2302
		} catch(InvalidInputException e) {
2297
		} catch(InvalidInputException e) {
Lines 2364-2370 Link Here
2364
					for (int i = 0; i < argumentLength; i++) {
2359
					for (int i = 0; i < argumentLength; i++) {
2365
						if (i > 0) {
2360
						if (i > 0) {
2366
							this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_allocation_expression);
2361
							this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_allocation_expression);
2367
							this.scribe.printTrailingComment();
2362
							this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
2368
						}
2363
						}
2369
						this.scribe.alignFragment(argumentsAlignment, i);
2364
						this.scribe.alignFragment(argumentsAlignment, i);
2370
						if (i > 0 && this.preferences.insert_space_after_comma_in_allocation_expression) {
2365
						if (i > 0 && this.preferences.insert_space_after_comma_in_allocation_expression) {
Lines 2441-2447 Link Here
2441
			defaultValue.traverse(this, (BlockScope) null);
2436
			defaultValue.traverse(this, (BlockScope) null);
2442
		}
2437
		}
2443
		this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
2438
		this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
2444
		this.scribe.printTrailingComment();
2439
		this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
2445
		return false;
2440
		return false;
2446
	}
2441
	}
2447
2442
Lines 2577-2583 Link Here
2577
						expressions[0].traverse(this, scope);
2572
						expressions[0].traverse(this, scope);
2578
						for (int i = 1; i < expressionsLength; i++) {
2573
						for (int i = 1; i < expressionsLength; i++) {
2579
							this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_array_initializer);
2574
							this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_array_initializer);
2580
							this.scribe.printTrailingComment();
2575
							this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
2581
							this.scribe.alignFragment(arrayInitializerAlignment, i);
2576
							this.scribe.alignFragment(arrayInitializerAlignment, i);
2582
							if (this.preferences.insert_space_after_comma_in_array_initializer) {
2577
							if (this.preferences.insert_space_after_comma_in_array_initializer) {
2583
								this.scribe.space();
2578
								this.scribe.space();
Lines 2586-2592 Link Here
2586
							if (i == expressionsLength - 1) {
2581
							if (i == expressionsLength - 1) {
2587
								if (isNextToken(TerminalTokens.TokenNameCOMMA)) {
2582
								if (isNextToken(TerminalTokens.TokenNameCOMMA)) {
2588
									this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_array_initializer);
2583
									this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_array_initializer);
2589
									this.scribe.printTrailingComment();
2584
									this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
2590
								}
2585
								}
2591
							}
2586
							}
2592
						}
2587
						}
Lines 2610-2616 Link Here
2610
				expressions[0].traverse(this, scope);
2605
				expressions[0].traverse(this, scope);
2611
				if (isNextToken(TerminalTokens.TokenNameCOMMA)) {
2606
				if (isNextToken(TerminalTokens.TokenNameCOMMA)) {
2612
					this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_array_initializer);
2607
					this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_array_initializer);
2613
					this.scribe.printTrailingComment();
2608
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
2614
				}
2609
				}
2615
				if (insert_new_line_after_opening_brace) {
2610
				if (insert_new_line_after_opening_brace) {
2616
					this.scribe.unIndent();
2611
					this.scribe.unIndent();
Lines 2632-2645 Link Here
2632
				this.scribe.printNextToken(TerminalTokens.TokenNameLBRACE, this.preferences.insert_space_before_opening_brace_in_array_initializer);
2627
				this.scribe.printNextToken(TerminalTokens.TokenNameLBRACE, this.preferences.insert_space_before_opening_brace_in_array_initializer);
2633
				if (isNextToken(TerminalTokens.TokenNameCOMMA)) {
2628
				if (isNextToken(TerminalTokens.TokenNameCOMMA)) {
2634
					this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_array_initializer);
2629
					this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_array_initializer);
2635
					this.scribe.printTrailingComment();
2630
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
2636
				}
2631
				}
2637
				this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE, this.preferences.insert_space_between_empty_braces_in_array_initializer);
2632
				this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE, this.preferences.insert_space_between_empty_braces_in_array_initializer);
2638
			} else {
2633
			} else {
2639
				formatOpeningBrace(array_initializer_brace_position, this.preferences.insert_space_before_opening_brace_in_array_initializer);
2634
				formatOpeningBrace(array_initializer_brace_position, this.preferences.insert_space_before_opening_brace_in_array_initializer);
2640
				if (isNextToken(TerminalTokens.TokenNameCOMMA)) {
2635
				if (isNextToken(TerminalTokens.TokenNameCOMMA)) {
2641
					this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_array_initializer);
2636
					this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_array_initializer);
2642
					this.scribe.printTrailingComment();
2637
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
2643
				}
2638
				}
2644
				this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE, this.preferences.insert_space_between_empty_braces_in_array_initializer);
2639
				this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE, this.preferences.insert_space_between_empty_braces_in_array_initializer);
2645
				if (array_initializer_brace_position.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) {
2640
				if (array_initializer_brace_position.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) {
Lines 2808-2814 Link Here
2808
			assertStatement.exceptionArgument.traverse(this, scope);
2803
			assertStatement.exceptionArgument.traverse(this, scope);
2809
		}
2804
		}
2810
		this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
2805
		this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
2811
		this.scribe.printTrailingComment();
2806
		this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
2812
		return false;
2807
		return false;
2813
	}
2808
	}
2814
2809
Lines 2906-2912 Link Here
2906
			this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true);
2901
			this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true);
2907
		}
2902
		}
2908
		this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
2903
		this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
2909
		this.scribe.printTrailingComment();
2904
		this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
2910
		return false;
2905
		return false;
2911
	}
2906
	}
2912
2907
Lines 3034-3040 Link Here
3034
			this.scribe.space();
3029
			this.scribe.space();
3035
			this.scribe.printQualifiedReference(compilationUnitDeclaration.currentPackage.sourceEnd, false/*do not expect parenthesis*/);
3030
			this.scribe.printQualifiedReference(compilationUnitDeclaration.currentPackage.sourceEnd, false/*do not expect parenthesis*/);
3036
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3031
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3037
			this.scribe.printTrailingComment();
3032
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3038
			int blankLinesAfterPackage = this.preferences.blank_lines_after_package;
3033
			int blankLinesAfterPackage = this.preferences.blank_lines_after_package;
3039
			if (blankLinesAfterPackage > 0) {
3034
			if (blankLinesAfterPackage > 0) {
3040
				this.scribe.printEmptyLines(blankLinesAfterPackage);
3035
				this.scribe.printEmptyLines(blankLinesAfterPackage);
Lines 3057-3074 Link Here
3057
				}
3052
				}
3058
			}
3053
			}
3059
			int importLength = imports.length;
3054
			int importLength = imports.length;
3060
			int savedNumberOfLineToPreserve = this.preferences.number_of_empty_lines_to_preserve;
3061
			if (importLength != 1) {
3055
			if (importLength != 1) {
3062
				format(imports[0], false);
3056
				format(imports[0], false);
3063
    			for (int i = 1; i < importLength - 1; i++) {
3057
    			for (int i = 1; i < importLength - 1; i++) {
3064
    				format(imports[i], false);
3058
    				format(imports[i], false);
3065
    			}
3059
    			}
3066
    			format(imports[importLength - 1], true);
3060
    			format(imports[importLength - 1], true);
3067
    			this.preferences.number_of_empty_lines_to_preserve = savedNumberOfLineToPreserve;
3068
			} else {
3061
			} else {
3069
				format(imports[0], true);
3062
				format(imports[0], true);
3070
			}
3063
			}
3071
			this.preferences.number_of_empty_lines_to_preserve = savedNumberOfLineToPreserve;
3072
3064
3073
			int blankLinesAfterImports = this.preferences.blank_lines_after_imports;
3065
			int blankLinesAfterImports = this.preferences.blank_lines_after_imports;
3074
			if (blankLinesAfterImports > 0) {
3066
			if (blankLinesAfterImports > 0) {
Lines 3205-3211 Link Here
3205
    				this.scribe.space();
3197
    				this.scribe.space();
3206
    			}
3198
    			}
3207
    			conditionalExpression.valueIfTrue.traverse(this, scope);
3199
    			conditionalExpression.valueIfTrue.traverse(this, scope);
3208
    			this.scribe.printTrailingComment();
3200
    			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3209
    			this.scribe.alignFragment(conditionalExpressionAlignment, 1);
3201
    			this.scribe.alignFragment(conditionalExpressionAlignment, 1);
3210
    			this.scribe.printNextToken(TerminalTokens.TokenNameCOLON, this.preferences.insert_space_before_colon_in_conditional);
3202
    			this.scribe.printNextToken(TerminalTokens.TokenNameCOLON, this.preferences.insert_space_before_colon_in_conditional);
3211
3203
Lines 3241-3247 Link Here
3241
				this.scribe.printIndentationIfNecessary();
3233
				this.scribe.printIndentationIfNecessary();
3242
			}
3234
			}
3243
			this.scribe.scanner.resetTo(constructorDeclaration.declarationSourceEnd + 1, this.scribe.scannerEndPosition - 1);
3235
			this.scribe.scanner.resetTo(constructorDeclaration.declarationSourceEnd + 1, this.scribe.scannerEndPosition - 1);
3244
			this.scribe.printTrailingComment();
3236
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3245
			switch(this.scribe.scanner.source[this.scribe.scanner.currentPosition]) {
3237
			switch(this.scribe.scanner.source[this.scribe.scanner.currentPosition]) {
3246
				case '\n' :
3238
				case '\n' :
3247
					this.scribe.scanner.currentPosition++;
3239
					this.scribe.scanner.currentPosition++;
Lines 3356-3369 Link Here
3356
				}
3348
				}
3357
			}
3349
			}
3358
			this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE);
3350
			this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE);
3359
			this.scribe.printTrailingComment();
3351
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3360
			if (constructor_declaration_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) {
3352
			if (constructor_declaration_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) {
3361
				this.scribe.unIndent();
3353
				this.scribe.unIndent();
3362
			}
3354
			}
3363
		} else {
3355
		} else {
3364
			// no method body
3356
			// no method body
3365
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3357
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3366
			this.scribe.printTrailingComment();
3358
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3367
		}
3359
		}
3368
		return false;
3360
		return false;
3369
	}
3361
	}
Lines 3378-3384 Link Here
3378
			this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true);
3370
			this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true);
3379
		}
3371
		}
3380
		this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3372
		this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3381
		this.scribe.printTrailingComment();
3373
		this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3382
		return false;
3374
		return false;
3383
	}
3375
	}
3384
3376
Lines 3407-3413 Link Here
3407
				action.traverse(this, scope);
3399
				action.traverse(this, scope);
3408
				if (action instanceof Expression) {
3400
				if (action instanceof Expression) {
3409
					this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3401
					this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3410
					this.scribe.printTrailingComment();
3402
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3411
				}
3403
				}
3412
				this.scribe.printNewLine();
3404
				this.scribe.printNewLine();
3413
				this.scribe.unIndent();
3405
				this.scribe.unIndent();
Lines 3433-3439 Link Here
3433
3425
3434
		this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_while);
3426
		this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_while);
3435
		this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3427
		this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3436
		this.scribe.printTrailingComment();
3428
		this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3437
		return false;
3429
		return false;
3438
	}
3430
	}
3439
3431
Lines 3466-3472 Link Here
3466
			this.scribe.printNewLine();
3458
			this.scribe.printNewLine();
3467
		}
3459
		}
3468
		this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3460
		this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3469
		this.scribe.printTrailingComment();
3461
		this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3470
		return false;
3462
		return false;
3471
	}
3463
	}
3472
	// field is an enum constant
3464
	// field is an enum constant
Lines 3519-3525 Link Here
3519
				this.scribe.printNewLine();
3511
				this.scribe.printNewLine();
3520
			}
3512
			}
3521
			this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE);
3513
			this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE);
3522
			this.scribe.printTrailingComment();
3514
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3523
			if (enum_constant_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) {
3515
			if (enum_constant_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) {
3524
				this.scribe.unIndent();
3516
				this.scribe.unIndent();
3525
			}
3517
			}
Lines 3605-3611 Link Here
3605
					for (int i = 0; i < argumentLength; i++) {
3597
					for (int i = 0; i < argumentLength; i++) {
3606
						if (i > 0) {
3598
						if (i > 0) {
3607
							this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_explicit_constructor_call_arguments);
3599
							this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_explicit_constructor_call_arguments);
3608
							this.scribe.printTrailingComment();
3600
							this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3609
						}
3601
						}
3610
						this.scribe.alignFragment(argumentsAlignment, i);
3602
						this.scribe.alignFragment(argumentsAlignment, i);
3611
						if (i > 0 && this.preferences.insert_space_after_comma_in_explicit_constructor_call_arguments) {
3603
						if (i > 0 && this.preferences.insert_space_after_comma_in_explicit_constructor_call_arguments) {
Lines 3624-3630 Link Here
3624
			this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_between_empty_parens_in_method_invocation);
3616
			this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_between_empty_parens_in_method_invocation);
3625
		}
3617
		}
3626
		this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3618
		this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3627
		this.scribe.printTrailingComment();
3619
		this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3628
		return false;
3620
		return false;
3629
	}
3621
	}
3630
	/**
3622
	/**
Lines 3718-3724 Link Here
3718
			}
3710
			}
3719
			if (action instanceof Expression) {
3711
			if (action instanceof Expression) {
3720
				this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3712
				this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3721
				this.scribe.printTrailingComment();
3713
				this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3722
			}
3714
			}
3723
		} else {
3715
		} else {
3724
			/*
3716
			/*
Lines 3754-3760 Link Here
3754
						if (this.preferences.insert_space_after_comma_in_for_inits) {
3746
						if (this.preferences.insert_space_after_comma_in_for_inits) {
3755
							this.scribe.space();
3747
							this.scribe.space();
3756
						}
3748
						}
3757
						this.scribe.printTrailingComment();
3749
						this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3758
					}
3750
					}
3759
				}
3751
				}
3760
			}
3752
			}
Lines 3780-3786 Link Here
3780
					if (this.preferences.insert_space_after_comma_in_for_increments) {
3772
					if (this.preferences.insert_space_after_comma_in_for_increments) {
3781
						this.scribe.space();
3773
						this.scribe.space();
3782
					}
3774
					}
3783
					this.scribe.printTrailingComment();
3775
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3784
				}
3776
				}
3785
			}
3777
			}
3786
		}
3778
		}
Lines 3804-3810 Link Here
3804
			}
3796
			}
3805
			if (action instanceof Expression) {
3797
			if (action instanceof Expression) {
3806
				this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3798
				this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3807
				this.scribe.printTrailingComment();
3799
				this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3808
			}
3800
			}
3809
		} else {
3801
		} else {
3810
			/*
3802
			/*
Lines 3869-3875 Link Here
3869
						thenStatement.traverse(this, scope);
3861
						thenStatement.traverse(this, scope);
3870
						if (thenStatement instanceof Expression) {
3862
						if (thenStatement instanceof Expression) {
3871
							this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3863
							this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3872
							this.scribe.printTrailingComment();
3864
							this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3873
						}
3865
						}
3874
						ok = true;
3866
						ok = true;
3875
					} catch (AlignmentException e) {
3867
					} catch (AlignmentException e) {
Lines 3882-3900 Link Here
3882
				thenStatement.traverse(this, scope);
3874
				thenStatement.traverse(this, scope);
3883
				if (thenStatement instanceof Expression) {
3875
				if (thenStatement instanceof Expression) {
3884
					this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3876
					this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3885
					this.scribe.printTrailingComment();
3877
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3886
				}
3878
				}
3887
				if (elseStatement != null) {
3879
				if (elseStatement != null) {
3888
					this.scribe.printNewLine();
3880
					this.scribe.printNewLine();
3889
				}
3881
				}
3890
			} else {
3882
			} else {
3891
				this.scribe.printTrailingComment();
3883
				this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3892
				this.scribe.printNewLine();
3884
				this.scribe.printNewLine();
3893
				this.scribe.indent();
3885
				this.scribe.indent();
3894
				thenStatement.traverse(this, scope);
3886
				thenStatement.traverse(this, scope);
3895
				if (thenStatement instanceof Expression) {
3887
				if (thenStatement instanceof Expression) {
3896
					this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3888
					this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3897
					this.scribe.printTrailingComment();
3889
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3898
				}
3890
				}
3899
				if (elseStatement != null) {
3891
				if (elseStatement != null) {
3900
					this.scribe.printNewLine();
3892
					this.scribe.printNewLine();
Lines 3926-3932 Link Here
3926
				elseStatement.traverse(this, scope);
3918
				elseStatement.traverse(this, scope);
3927
				if (elseStatement instanceof Expression) {
3919
				if (elseStatement instanceof Expression) {
3928
					this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3920
					this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3929
					this.scribe.printTrailingComment();
3921
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3930
				}
3922
				}
3931
			} else {
3923
			} else {
3932
				this.scribe.printNewLine();
3924
				this.scribe.printNewLine();
Lines 3934-3940 Link Here
3934
				elseStatement.traverse(this, scope);
3926
				elseStatement.traverse(this, scope);
3935
				if (elseStatement instanceof Expression) {
3927
				if (elseStatement instanceof Expression) {
3936
					this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3928
					this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
3937
					this.scribe.printTrailingComment();
3929
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
3938
				}
3930
				}
3939
				this.scribe.unIndent();
3931
				this.scribe.unIndent();
3940
			}
3932
			}
Lines 4011-4017 Link Here
4011
		statement.traverse(this, scope);
4003
		statement.traverse(this, scope);
4012
		if (statement instanceof Expression) {
4004
		if (statement instanceof Expression) {
4013
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
4005
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
4014
			this.scribe.printTrailingComment();
4006
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
4015
		}
4007
		}
4016
		return false;
4008
		return false;
4017
	}
4009
	}
Lines 4125-4131 Link Here
4125
				this.scribe.printIndentationIfNecessary();
4117
				this.scribe.printIndentationIfNecessary();
4126
			}
4118
			}
4127
			this.scribe.scanner.resetTo(methodDeclaration.declarationSourceEnd + 1, this.scribe.scannerEndPosition - 1);
4119
			this.scribe.scanner.resetTo(methodDeclaration.declarationSourceEnd + 1, this.scribe.scannerEndPosition - 1);
4128
			this.scribe.printTrailingComment();
4120
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
4129
			if (!this.scribe.scanner.atEnd()) {
4121
			if (!this.scribe.scanner.atEnd()) {
4130
				switch(this.scribe.scanner.source[this.scribe.scanner.currentPosition]) {
4122
				switch(this.scribe.scanner.source[this.scribe.scanner.currentPosition]) {
4131
					case '\n' :
4123
					case '\n' :
Lines 4255-4268 Link Here
4255
				}
4247
				}
4256
			}
4248
			}
4257
			this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE);
4249
			this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE);
4258
			this.scribe.printTrailingComment();
4250
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
4259
			if (method_declaration_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) {
4251
			if (method_declaration_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) {
4260
				this.scribe.unIndent();
4252
				this.scribe.unIndent();
4261
			}
4253
			}
4262
		} else {
4254
		} else {
4263
			// no method body
4255
			// no method body
4264
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
4256
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
4265
			this.scribe.printTrailingComment();
4257
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
4266
		}
4258
		}
4267
		return false;
4259
		return false;
4268
	}
4260
	}
Lines 4621-4627 Link Here
4621
					for (int i = 0; i < argumentLength; i++) {
4613
					for (int i = 0; i < argumentLength; i++) {
4622
						if (i > 0) {
4614
						if (i > 0) {
4623
							this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_allocation_expression);
4615
							this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_allocation_expression);
4624
							this.scribe.printTrailingComment();
4616
							this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
4625
						}
4617
						}
4626
						this.scribe.alignFragment(argumentsAlignment, i);
4618
						this.scribe.alignFragment(argumentsAlignment, i);
4627
						if (i > 0 && this.preferences.insert_space_after_comma_in_allocation_expression) {
4619
						if (i > 0 && this.preferences.insert_space_after_comma_in_allocation_expression) {
Lines 4768-4774 Link Here
4768
		 * Print the semi-colon
4760
		 * Print the semi-colon
4769
		 */
4761
		 */
4770
		this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
4762
		this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
4771
		this.scribe.printTrailingComment();
4763
		this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
4772
		return false;
4764
		return false;
4773
	}
4765
	}
4774
	public boolean visit(SingleMemberAnnotation annotation, BlockScope scope) {
4766
	public boolean visit(SingleMemberAnnotation annotation, BlockScope scope) {
Lines 4850-4856 Link Here
4850
		}
4842
		}
4851
		this.scribe.checkNLSTag(stringLiteral.sourceStart);
4843
		this.scribe.checkNLSTag(stringLiteral.sourceStart);
4852
		this.scribe.printNextToken(TerminalTokens.TokenNameStringLiteral);
4844
		this.scribe.printNextToken(TerminalTokens.TokenNameStringLiteral);
4853
		this.scribe.printTrailingComment();
4845
		this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
4854
		if (numberOfParens > 0) {
4846
		if (numberOfParens > 0) {
4855
			manageClosingParenthesizedExpression(stringLiteral, numberOfParens);
4847
			manageClosingParenthesizedExpression(stringLiteral, numberOfParens);
4856
		}
4848
		}
Lines 4877-4885 Link Here
4877
				for (int i = 0; i < fragmentsSize - 1; i++) {
4869
				for (int i = 0; i < fragmentsSize - 1; i++) {
4878
					ASTNode fragment = fragments[i];
4870
					ASTNode fragment = fragments[i];
4879
					fragment.traverse(this, scope);
4871
					fragment.traverse(this, scope);
4880
					this.scribe.printTrailingComment();
4872
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
4881
					if (this.scribe.lastNumberOfNewLines == 1) {
4873
					if (this.scribe.lastNumberOfNewLines == 1) {
4882
						// a new line has been inserted by printTrailingComment()
4874
						// a new line has been inserted by printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT)
4883
						this.scribe.indentationLevel = binaryExpressionAlignment.breakIndentationLevel;
4875
						this.scribe.indentationLevel = binaryExpressionAlignment.breakIndentationLevel;
4884
					}
4876
					}
4885
					this.scribe.alignFragment(binaryExpressionAlignment, i);
4877
					this.scribe.alignFragment(binaryExpressionAlignment, i);
Lines 4889-4895 Link Here
4889
					}
4881
					}
4890
				}
4882
				}
4891
				fragments[fragmentsSize - 1].traverse(this, scope);
4883
				fragments[fragmentsSize - 1].traverse(this, scope);
4892
				this.scribe.printTrailingComment();
4884
				this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
4893
				ok = true;
4885
				ok = true;
4894
			} catch(AlignmentException e){
4886
			} catch(AlignmentException e){
4895
				this.scribe.redoAlignment(e);
4887
				this.scribe.redoAlignment(e);
Lines 4959-4965 Link Here
4959
						this.scribe.unIndent();
4951
						this.scribe.unIndent();
4960
					}
4952
					}
4961
					statement.traverse(this, scope);
4953
					statement.traverse(this, scope);
4962
					this.scribe.printTrailingComment();
4954
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
4963
					wasACase = true;
4955
					wasACase = true;
4964
					wasAStatement = false;
4956
					wasAStatement = false;
4965
					if (this.preferences.indent_switchstatements_compare_to_cases) {
4957
					if (this.preferences.indent_switchstatements_compare_to_cases) {
Lines 5017-5023 Link Here
5017
					 * Print the semi-colon
5009
					 * Print the semi-colon
5018
					 */
5010
					 */
5019
					this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
5011
					this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
5020
					this.scribe.printTrailingComment();
5012
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
5021
					this.scribe.printNewLine();
5013
					this.scribe.printNewLine();
5022
				} else if (statement instanceof LocalDeclaration) {
5014
				} else if (statement instanceof LocalDeclaration) {
5023
					LocalDeclaration currentLocal = (LocalDeclaration) statement;
5015
					LocalDeclaration currentLocal = (LocalDeclaration) statement;
Lines 5032-5038 Link Here
5032
								 * Print the semi-colon
5024
								 * Print the semi-colon
5033
								 */
5025
								 */
5034
								this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
5026
								this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
5035
								this.scribe.printTrailingComment();
5027
								this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
5036
								this.scribe.printNewLine();
5028
								this.scribe.printNewLine();
5037
							}
5029
							}
5038
						} else {
5030
						} else {
Lines 5040-5046 Link Here
5040
							 * Print the semi-colon
5032
							 * Print the semi-colon
5041
							 */
5033
							 */
5042
							this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
5034
							this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
5043
							this.scribe.printTrailingComment();
5035
							this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
5044
							this.scribe.printNewLine();
5036
							this.scribe.printNewLine();
5045
						}
5037
						}
5046
					} else {
5038
					} else {
Lines 5048-5054 Link Here
5048
						 * Print the semi-colon
5040
						 * Print the semi-colon
5049
						 */
5041
						 */
5050
						this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
5042
						this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
5051
						this.scribe.printTrailingComment();
5043
						this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
5052
						this.scribe.printNewLine();
5044
						this.scribe.printNewLine();
5053
					}
5045
					}
5054
				} else if (!wasACase) {
5046
				} else if (!wasACase) {
Lines 5066-5072 Link Here
5066
		}
5058
		}
5067
		this.scribe.printNewLine();
5059
		this.scribe.printNewLine();
5068
		this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE);
5060
		this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE);
5069
		this.scribe.printTrailingComment();
5061
		this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
5070
		if (switch_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) {
5062
		if (switch_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) {
5071
			this.scribe.unIndent();
5063
			this.scribe.unIndent();
5072
		}
5064
		}
Lines 5133-5139 Link Here
5133
		 * Print the semi-colon
5125
		 * Print the semi-colon
5134
		 */
5126
		 */
5135
		this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
5127
		this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
5136
		this.scribe.printTrailingComment();
5128
		this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
5137
		return false;
5129
		return false;
5138
	}
5130
	}
5139
5131
Lines 5385-5391 Link Here
5385
				action.traverse(this, scope);
5377
				action.traverse(this, scope);
5386
				if (action instanceof Expression) {
5378
				if (action instanceof Expression) {
5387
					this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
5379
					this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
5388
					this.scribe.printTrailingComment();
5380
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
5389
				}
5381
				}
5390
				this.scribe.unIndent();
5382
				this.scribe.unIndent();
5391
			}
5383
			}
(-)formatter/org/eclipse/jdt/internal/formatter/Scribe.java (-197 / +125 lines)
Lines 96-103 Link Here
96
	public int numberOfIndentations;
96
	public int numberOfIndentations;
97
	private boolean useTabsOnlyForLeadingIndents;
97
	private boolean useTabsOnlyForLeadingIndents;
98
98
99
	/** indent empty lines*/
99
	/** empty lines*/
100
	private final boolean indentEmptyLines;
100
	private final boolean indentEmptyLines;
101
	int blank_lines_between_import_groups = -1;
101
102
102
	/* Comments formatting */
103
	/* Comments formatting */
103
	private static final int INCLUDE_BLOCK_COMMENTS = CodeFormatter.F_INCLUDE_COMMENTS | CodeFormatter.K_MULTI_LINE_COMMENT;
104
	private static final int INCLUDE_BLOCK_COMMENTS = CodeFormatter.F_INCLUDE_COMMENTS | CodeFormatter.K_MULTI_LINE_COMMENT;
Lines 105-110 Link Here
105
	private static final int INCLUDE_LINE_COMMENTS = CodeFormatter.F_INCLUDE_COMMENTS | CodeFormatter.K_SINGLE_LINE_COMMENT;
106
	private static final int INCLUDE_LINE_COMMENTS = CodeFormatter.F_INCLUDE_COMMENTS | CodeFormatter.K_SINGLE_LINE_COMMENT;
106
	private static final int SKIP_FIRST_WHITESPACE_TOKEN = -2;
107
	private static final int SKIP_FIRST_WHITESPACE_TOKEN = -2;
107
	private static final int INVALID_TOKEN = 2000;
108
	private static final int INVALID_TOKEN = 2000;
109
	static final int NO_TRAILING_COMMENT = 0;
110
	static final int BASIC_TRAILING_COMMENT = 1;
111
	static final int IMPORT_TRAILING_COMMENT = 2;
108
	private int formatComments = 0;
112
	private int formatComments = 0;
109
	private int headerEndPosition = -1;
113
	private int headerEndPosition = -1;
110
	String commentIndentation; // indentation requested in comments (usually in javadoc root tags description)
114
	String commentIndentation; // indentation requested in comments (usually in javadoc root tags description)
Lines 1065-1070 Link Here
1065
			}
1069
			}
1066
			return Util.EMPTY_STRING;
1070
			return Util.EMPTY_STRING;
1067
		}
1071
		}
1072
		if (this.blank_lines_between_import_groups >= 0) {
1073
			return getEmptyLines(this.blank_lines_between_import_groups);
1074
		}
1068
		if (this.formatter.preferences.number_of_empty_lines_to_preserve != 0) {
1075
		if (this.formatter.preferences.number_of_empty_lines_to_preserve != 0) {
1069
			int linesToPreserve = Math.min(count, this.formatter.preferences.number_of_empty_lines_to_preserve);
1076
			int linesToPreserve = Math.min(count, this.formatter.preferences.number_of_empty_lines_to_preserve);
1070
			return getEmptyLines(linesToPreserve);
1077
			return getEmptyLines(linesToPreserve);
Lines 1310-1318 Link Here
1310
1317
1311
	private void preserveEmptyLines(int count, int insertPosition) {
1318
	private void preserveEmptyLines(int count, int insertPosition) {
1312
		if (count > 0) {
1319
		if (count > 0) {
1313
			if (this.formatter.preferences.number_of_empty_lines_to_preserve != 0) {
1320
			if (this.blank_lines_between_import_groups >= 0) {
1321
				printEmptyLines(this.blank_lines_between_import_groups, insertPosition);
1322
			} else if (this.formatter.preferences.number_of_empty_lines_to_preserve != 0) {
1314
				int linesToPreserve = Math.min(count, this.formatter.preferences.number_of_empty_lines_to_preserve);
1323
				int linesToPreserve = Math.min(count, this.formatter.preferences.number_of_empty_lines_to_preserve);
1315
				this.printEmptyLines(linesToPreserve, insertPosition);
1324
				printEmptyLines(linesToPreserve, insertPosition);
1316
			} else {
1325
			} else {
1317
				printNewLine(insertPosition);
1326
				printNewLine(insertPosition);
1318
			}
1327
			}
Lines 2052-2064 Link Here
2052
	}
2061
	}
2053
2062
2054
	void printComment() {
2063
	void printComment() {
2055
		printComment(CodeFormatter.K_UNKNOWN);
2064
		printComment(CodeFormatter.K_UNKNOWN, NO_TRAILING_COMMENT);
2056
	}
2065
	}
2057
2066
2058
	/*
2067
	/*
2059
	 * Main method to print and format comments (javadoc, block and single line comments)
2068
	 * Main method to print and format comments (javadoc, block and single line comments)
2060
	 */
2069
	 */
2061
	void printComment(int kind) {
2070
	void printComment(int kind, int trailing) {
2062
		final boolean rejectLineComment = kind  == CodeFormatter.K_MULTI_LINE_COMMENT || kind == CodeFormatter.K_JAVA_DOC;
2071
		final boolean rejectLineComment = kind  == CodeFormatter.K_MULTI_LINE_COMMENT || kind == CodeFormatter.K_JAVA_DOC;
2063
		final boolean rejectBlockComment = kind  == CodeFormatter.K_SINGLE_LINE_COMMENT || kind  == CodeFormatter.K_JAVA_DOC;
2072
		final boolean rejectBlockComment = kind  == CodeFormatter.K_SINGLE_LINE_COMMENT || kind  == CodeFormatter.K_JAVA_DOC;
2064
		final boolean rejectJavadocComment = kind  == CodeFormatter.K_SINGLE_LINE_COMMENT || kind  == CodeFormatter.K_MULTI_LINE_COMMENT;
2073
		final boolean rejectJavadocComment = kind  == CodeFormatter.K_SINGLE_LINE_COMMENT || kind  == CodeFormatter.K_MULTI_LINE_COMMENT;
Lines 2067-2079 Link Here
2067
			int currentTokenStartPosition = this.scanner.currentPosition;
2076
			int currentTokenStartPosition = this.scanner.currentPosition;
2068
			boolean hasComment = false;
2077
			boolean hasComment = false;
2069
			boolean hasLineComment = false;
2078
			boolean hasLineComment = false;
2070
			boolean hasWhitespace = false;
2079
			boolean hasWhitespaces = false;
2071
			int count = 0;
2080
			int lines = 0;
2072
			while ((this.currentToken = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
2081
			while ((this.currentToken = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
2073
				switch(this.currentToken) {
2082
				switch(this.currentToken) {
2074
					case TerminalTokens.TokenNameWHITESPACE :
2083
					case TerminalTokens.TokenNameWHITESPACE :
2075
						char[] whiteSpaces = this.scanner.getCurrentTokenSource();
2084
						char[] whiteSpaces = this.scanner.getCurrentTokenSource();
2076
						count = 0;
2085
						int whitespacesStartPosition = this.scanner.getCurrentTokenStartPosition();
2086
						int whitespacesEndPosition = this.scanner.getCurrentTokenEndPosition();
2087
						lines = 0;
2077
						for (int i = 0, max = whiteSpaces.length; i < max; i++) {
2088
						for (int i = 0, max = whiteSpaces.length; i < max; i++) {
2078
							switch(whiteSpaces[i]) {
2089
							switch(whiteSpaces[i]) {
2079
								case '\r' :
2090
								case '\r' :
Lines 2082-2160 Link Here
2082
											i++;
2093
											i++;
2083
										}
2094
										}
2084
									}
2095
									}
2085
									count++;
2096
									lines++;
2086
									break;
2097
									break;
2087
								case '\n' :
2098
								case '\n' :
2088
									count++;
2099
									lines++;
2089
							}
2100
							}
2090
						}
2101
						}
2091
						if (count == 0) {
2102
						// If following token is a line comment on the same line or the line just after,
2092
							hasWhitespace = true;
2103
						// then it might be not really formatted as a trailing comment
2093
							addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
2104
						boolean realTrailing = trailing > NO_TRAILING_COMMENT;
2094
						} else if (hasLineComment) {
2105
						if (trailing == IMPORT_TRAILING_COMMENT && this.scanner.currentCharacter == '/' && lines <= 1) {
2095
							preserveEmptyLines(count, this.scanner.getCurrentTokenStartPosition());
2106
							int currentPosition = this.scanner.currentPosition;
2096
							addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
2107
							if (this.scanner.getNextToken() == TerminalTokens.TokenNameCOMMENT_LINE) {
2097
						} else if (hasComment) {
2108
								int token = this.scanner.getNextToken();
2098
							if (count == 1) {
2109
								while (token == TerminalTokens.TokenNameCOMMENT_LINE) {
2099
								this.printNewLine(this.scanner.getCurrentTokenStartPosition());
2110
									token = this.scanner.getNextToken();
2100
							} else {
2111
								}
2101
								preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
2112
								if (token == TerminalTokens.TokenNameWHITESPACE) {
2113
									char[] secondWhiteSpaces = this.scanner.getCurrentTokenSource();
2114
									loop: for (int i = 0, max = secondWhiteSpaces.length; i < max; i++) {
2115
										switch(secondWhiteSpaces[i]) {
2116
											case '\r' :
2117
											case '\n' :
2118
												realTrailing = false;
2119
												break loop;
2120
										}
2121
									}
2122
								}
2102
							}
2123
							}
2103
							addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
2124
							this.scanner.resetTo(currentPosition, this.scanner.eofPosition - 1);
2104
						} else if (count != 0 && (!this.formatter.preferences.join_wrapped_lines || this.formatter.preferences.number_of_empty_lines_to_preserve != 0)) {
2125
						}
2105
							addReplaceEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition(), getPreserveEmptyLines(count-1));
2126
						if (realTrailing) {
2127
							// if a line comment is consumed, no other comment can be on the same line after
2128
							if (hasLineComment) {
2129
								if (lines >= 1) {
2130
									currentTokenStartPosition = whitespacesStartPosition;
2131
									preserveEmptyLines(lines, currentTokenStartPosition);
2132
									addDeleteEdit(currentTokenStartPosition, whitespacesEndPosition);
2133
									this.scanner.resetTo(this.scanner.currentPosition, this.scannerEndPosition - 1);
2134
									return;
2135
								}
2136
								this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
2137
								return;
2138
							} 
2139
							// if one or several new lines are consumed, following comments cannot be considered as trailing ones
2140
							if (lines >= 1) {
2141
								if (hasComment) {
2142
									this.printNewLine(whitespacesStartPosition);
2143
								}
2144
								this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
2145
								return;
2146
							}
2147
							// delete consumed white spaces
2148
							hasWhitespaces = true;
2149
							currentTokenStartPosition = this.scanner.currentPosition;
2150
							addDeleteEdit(whitespacesStartPosition, whitespacesEndPosition);
2106
						} else {
2151
						} else {
2107
							addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
2152
							if (lines == 0) {
2153
								hasWhitespaces = true;
2154
								addDeleteEdit(whitespacesStartPosition, whitespacesEndPosition);
2155
							} else if (hasLineComment) {
2156
								currentTokenStartPosition = whitespacesStartPosition;
2157
								preserveEmptyLines(lines, currentTokenStartPosition);
2158
								addDeleteEdit(currentTokenStartPosition, whitespacesEndPosition);
2159
							} else if (hasComment) {
2160
								if (lines == 1) {
2161
									this.printNewLine(whitespacesStartPosition);
2162
								} else {
2163
									preserveEmptyLines(lines - 1, whitespacesStartPosition);
2164
								}
2165
								addDeleteEdit(whitespacesStartPosition, whitespacesEndPosition);
2166
							} else if (lines != 0 && (!this.formatter.preferences.join_wrapped_lines || this.formatter.preferences.number_of_empty_lines_to_preserve != 0)) {
2167
								addReplaceEdit(whitespacesStartPosition, whitespacesEndPosition, getPreserveEmptyLines(lines-1));
2168
							} else {
2169
								addDeleteEdit(whitespacesStartPosition, whitespacesEndPosition);
2170
							}
2108
						}
2171
						}
2109
						currentTokenStartPosition = this.scanner.currentPosition;
2172
						currentTokenStartPosition = this.scanner.currentPosition;
2110
						break;
2173
						break;
2111
					case TerminalTokens.TokenNameCOMMENT_LINE :
2174
					case TerminalTokens.TokenNameCOMMENT_LINE :
2112
						if (rejectLineComment) break;
2175
						if (rejectLineComment) break;
2113
						if (count >= 1) {
2176
						if (lines >= 1) {
2114
							if (count > 1) {
2177
							if (lines > 1) {
2115
								preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
2178
								preserveEmptyLines(lines - 1, this.scanner.getCurrentTokenStartPosition());
2116
							} else if (count == 1) {
2179
							} else if (lines == 1) {
2117
								printNewLine(this.scanner.getCurrentTokenStartPosition());
2180
								printNewLine(this.scanner.getCurrentTokenStartPosition());
2118
							}
2181
							}
2119
						} else if (hasWhitespace) {
2182
						} else if (hasWhitespaces) {
2120
							space();
2183
							space();
2121
						}
2184
						}
2122
						hasWhitespace = false;
2185
						hasWhitespaces = false;
2123
						printLineComment();
2186
						printLineComment();
2124
						currentTokenStartPosition = this.scanner.currentPosition;
2187
						currentTokenStartPosition = this.scanner.currentPosition;
2125
						hasLineComment = true;
2188
						hasLineComment = true;
2126
						count = 0;
2189
						lines = 0;
2127
						break;
2190
						break;
2128
					case TerminalTokens.TokenNameCOMMENT_BLOCK :
2191
					case TerminalTokens.TokenNameCOMMENT_BLOCK :
2192
						if (trailing > NO_TRAILING_COMMENT && lines >= 1) {
2193
							// a block comment on next line means that there's no trailing comment
2194
							this.scanner.resetTo(this.scanner.getCurrentTokenStartPosition(), this.scannerEndPosition - 1);
2195
							return;
2196
						}
2129
						if (rejectBlockComment) break;
2197
						if (rejectBlockComment) break;
2130
						if (count >= 1) {
2198
						if (lines >= 1) {
2131
							if (count > 1) {
2199
							if (lines > 1) {
2132
								preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
2200
								preserveEmptyLines(lines - 1, this.scanner.getCurrentTokenStartPosition());
2133
							} else if (count == 1) {
2201
							} else if (lines == 1) {
2134
								printNewLine(this.scanner.getCurrentTokenStartPosition());
2202
								printNewLine(this.scanner.getCurrentTokenStartPosition());
2135
							}
2203
							}
2136
						} else if (hasWhitespace) {
2204
						} else if (hasWhitespaces) {
2137
							space();
2205
							space();
2138
						}
2206
						}
2139
						hasWhitespace = false;
2207
						hasWhitespaces = false;
2140
						printBlockComment(false);
2208
						printBlockComment(false);
2141
						currentTokenStartPosition = this.scanner.currentPosition;
2209
						currentTokenStartPosition = this.scanner.currentPosition;
2142
						hasLineComment = false;
2210
						hasLineComment = false;
2143
						hasComment = true;
2211
						hasComment = true;
2144
						count = 0;
2212
						lines = 0;
2145
						break;
2213
						break;
2146
					case TerminalTokens.TokenNameCOMMENT_JAVADOC :
2214
					case TerminalTokens.TokenNameCOMMENT_JAVADOC :
2215
						if (trailing > NO_TRAILING_COMMENT) {
2216
							// a javadoc comment should not be considered as a trailing comment
2217
							this.scanner.resetTo(this.scanner.getCurrentTokenStartPosition(), this.scannerEndPosition - 1);
2218
							return;
2219
						}
2147
						if (rejectJavadocComment) break;
2220
						if (rejectJavadocComment) break;
2148
						if (count >= 1) {
2221
						if (lines >= 1) {
2149
							if (count > 1) {
2222
							if (lines > 1) {
2150
								preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
2223
								preserveEmptyLines(lines - 1, this.scanner.getCurrentTokenStartPosition());
2151
							} else if (count == 1) {
2224
							} else if (lines == 1) {
2152
								printNewLine(this.scanner.getCurrentTokenStartPosition());
2225
								printNewLine(this.scanner.getCurrentTokenStartPosition());
2153
							}
2226
							}
2154
						} else if (hasWhitespace) {
2227
						} else if (hasWhitespaces) {
2155
							space();
2228
							space();
2156
						}
2229
						}
2157
						hasWhitespace = false;
2230
						hasWhitespaces = false;
2158
						if (includesJavadocComments()) {
2231
						if (includesJavadocComments()) {
2159
							printJavadocComment(this.scanner.startPosition, this.scanner.currentPosition);
2232
							printJavadocComment(this.scanner.startPosition, this.scanner.currentPosition);
2160
						} else {
2233
						} else {
Lines 2164-2170 Link Here
2164
						currentTokenStartPosition = this.scanner.currentPosition;
2237
						currentTokenStartPosition = this.scanner.currentPosition;
2165
						hasLineComment = false;
2238
						hasLineComment = false;
2166
						hasComment = true;
2239
						hasComment = true;
2167
						count = 0;
2240
						lines = 0;
2168
						break;
2241
						break;
2169
					default :
2242
					default :
2170
						// step back one token
2243
						// step back one token
Lines 2196-2205 Link Here
2196
	    // Print corresponding comment
2269
	    // Print corresponding comment
2197
	    switch (kind) {
2270
	    switch (kind) {
2198
	    	case CodeFormatter.K_SINGLE_LINE_COMMENT:
2271
	    	case CodeFormatter.K_SINGLE_LINE_COMMENT:
2199
			    printComment(kind);
2272
			    printComment(kind, NO_TRAILING_COMMENT);
2200
	    		break;
2273
	    		break;
2201
	    	case CodeFormatter.K_MULTI_LINE_COMMENT:
2274
	    	case CodeFormatter.K_MULTI_LINE_COMMENT:
2202
			    printComment(kind);
2275
			    printComment(kind, NO_TRAILING_COMMENT);
2203
	    		break;
2276
	    		break;
2204
	    	case CodeFormatter.K_JAVA_DOC:
2277
	    	case CodeFormatter.K_JAVA_DOC:
2205
	    		printJavadocComment(start, end);
2278
	    		printJavadocComment(start, end);
Lines 3898-3904 Link Here
3898
				this.formatBrace = true;
3971
				this.formatBrace = true;
3899
		}
3972
		}
3900
		try {
3973
		try {
3901
			printComment(CodeFormatter.K_UNKNOWN);
3974
			printComment(CodeFormatter.K_UNKNOWN, NO_TRAILING_COMMENT);
3902
			try {
3975
			try {
3903
				this.currentToken = this.scanner.getNextToken();
3976
				this.currentToken = this.scanner.getNextToken();
3904
				if (expectedTokenType != this.currentToken) {
3977
				if (expectedTokenType != this.currentToken) {
Lines 3924-3930 Link Here
3924
	}
3997
	}
3925
3998
3926
	public void printNextToken(int[] expectedTokenTypes, boolean considerSpaceIfAny){
3999
	public void printNextToken(int[] expectedTokenTypes, boolean considerSpaceIfAny){
3927
		printComment(CodeFormatter.K_UNKNOWN);
4000
		printComment(CodeFormatter.K_UNKNOWN, NO_TRAILING_COMMENT);
3928
		try {
4001
		try {
3929
			this.currentToken = this.scanner.getNextToken();
4002
			this.currentToken = this.scanner.getNextToken();
3930
			if (Arrays.binarySearch(expectedTokenTypes, this.currentToken) < 0) {
4003
			if (Arrays.binarySearch(expectedTokenTypes, this.currentToken) < 0) {
Lines 3948-3954 Link Here
3948
		int numberOfIdentifiers = 0;
4021
		int numberOfIdentifiers = 0;
3949
		try {
4022
		try {
3950
			do {
4023
			do {
3951
				printComment(CodeFormatter.K_UNKNOWN);
4024
				printComment(CodeFormatter.K_UNKNOWN, NO_TRAILING_COMMENT);
3952
				switch(this.currentToken = this.scanner.getNextToken()) {
4025
				switch(this.currentToken = this.scanner.getNextToken()) {
3953
					case TerminalTokens.TokenNameEOF :
4026
					case TerminalTokens.TokenNameEOF :
3954
						return;
4027
						return;
Lines 3994-4000 Link Here
3994
		int currentTokenStartPosition = this.scanner.currentPosition;
4067
		int currentTokenStartPosition = this.scanner.currentPosition;
3995
		try {
4068
		try {
3996
			do {
4069
			do {
3997
				printComment(CodeFormatter.K_UNKNOWN);
4070
				printComment(CodeFormatter.K_UNKNOWN, NO_TRAILING_COMMENT);
3998
				switch(this.currentToken = this.scanner.getNextToken()) {
4071
				switch(this.currentToken = this.scanner.getNextToken()) {
3999
					case TerminalTokens.TokenNameEOF :
4072
					case TerminalTokens.TokenNameEOF :
4000
						return;
4073
						return;
Lines 4048-4198 Link Here
4048
		}
4121
		}
4049
	}
4122
	}
4050
4123
4051
	public void printTrailingComment(int numberOfNewLinesToInsert) {
4052
		try {
4053
			// if we have a space between two tokens we ensure it will be dumped in the formatted string
4054
			int currentTokenStartPosition = this.scanner.currentPosition;
4055
			boolean hasWhitespaces = false;
4056
			boolean hasLineComment = false;
4057
			while ((this.currentToken = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
4058
				switch(this.currentToken) {
4059
					case TerminalTokens.TokenNameWHITESPACE :
4060
						int count = 0;
4061
						char[] whiteSpaces = this.scanner.getCurrentTokenSource();
4062
						for (int i = 0, max = whiteSpaces.length; i < max; i++) {
4063
							switch(whiteSpaces[i]) {
4064
								case '\r' :
4065
									if ((i + 1) < max) {
4066
										if (whiteSpaces[i + 1] == '\n') {
4067
											i++;
4068
										}
4069
									}
4070
									count++;
4071
									break;
4072
								case '\n' :
4073
									count++;
4074
							}
4075
						}
4076
						if (hasLineComment) {
4077
							if (count >= 1) {
4078
								currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition();
4079
								preserveEmptyLines(numberOfNewLinesToInsert, currentTokenStartPosition);
4080
								addDeleteEdit(currentTokenStartPosition, this.scanner.getCurrentTokenEndPosition());
4081
								this.scanner.resetTo(this.scanner.currentPosition, this.scannerEndPosition - 1);
4082
								return;
4083
							}
4084
							this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
4085
							return;
4086
						} else if (count > 1) {
4087
							this.printEmptyLines(numberOfNewLinesToInsert, this.scanner.getCurrentTokenStartPosition());
4088
							this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
4089
							return;
4090
						} else {
4091
							hasWhitespaces = true;
4092
							currentTokenStartPosition = this.scanner.currentPosition;
4093
							addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
4094
						}
4095
						break;
4096
					case TerminalTokens.TokenNameCOMMENT_LINE :
4097
						if (hasWhitespaces) {
4098
							space();
4099
						}
4100
						printLineComment();
4101
						currentTokenStartPosition = this.scanner.currentPosition;
4102
						hasLineComment = true;
4103
						break;
4104
					case TerminalTokens.TokenNameCOMMENT_BLOCK :
4105
						if (hasWhitespaces) {
4106
							space();
4107
						}
4108
						printBlockComment(false);
4109
						currentTokenStartPosition = this.scanner.currentPosition;
4110
						break;
4111
					default :
4112
						// step back one token
4113
						this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
4114
						return;
4115
				}
4116
			}
4117
		} catch (InvalidInputException e) {
4118
			throw new AbortFormatting(e);
4119
		}
4120
	}
4121
	public void printTrailingComment() {
4122
		try {
4123
			// if we have a space between two tokens we ensure it will be dumped in the formatted string
4124
			int currentTokenStartPosition = this.scanner.currentPosition;
4125
			boolean hasWhitespaces = false;
4126
			boolean hasComment = false;
4127
			boolean hasLineComment = false;
4128
			while ((this.currentToken = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
4129
				switch(this.currentToken) {
4130
					case TerminalTokens.TokenNameWHITESPACE :
4131
						int count = 0;
4132
						char[] whiteSpaces = this.scanner.getCurrentTokenSource();
4133
						for (int i = 0, max = whiteSpaces.length; i < max; i++) {
4134
							switch(whiteSpaces[i]) {
4135
								case '\r' :
4136
									if ((i + 1) < max) {
4137
										if (whiteSpaces[i + 1] == '\n') {
4138
											i++;
4139
										}
4140
									}
4141
									count++;
4142
									break;
4143
								case '\n' :
4144
									count++;
4145
							}
4146
						}
4147
						if (hasLineComment) {
4148
							if (count >= 1) {
4149
								currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition();
4150
								preserveEmptyLines(count, currentTokenStartPosition);
4151
								addDeleteEdit(currentTokenStartPosition, this.scanner.getCurrentTokenEndPosition());
4152
								this.scanner.resetTo(this.scanner.currentPosition, this.scannerEndPosition - 1);
4153
								return;
4154
							}
4155
							this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
4156
							return;
4157
						} else if (count >= 1) {
4158
							if (hasComment) {
4159
								this.printNewLine(this.scanner.getCurrentTokenStartPosition());
4160
							}
4161
							this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
4162
							return;
4163
						} else {
4164
							hasWhitespaces = true;
4165
							currentTokenStartPosition = this.scanner.currentPosition;
4166
							addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
4167
						}
4168
						break;
4169
					case TerminalTokens.TokenNameCOMMENT_LINE :
4170
						if (hasWhitespaces) {
4171
							space();
4172
						}
4173
						printLineComment();
4174
						currentTokenStartPosition = this.scanner.currentPosition;
4175
						hasLineComment = true;
4176
						break;
4177
					case TerminalTokens.TokenNameCOMMENT_BLOCK :
4178
						if (hasWhitespaces) {
4179
							space();
4180
						}
4181
						printBlockComment(false);
4182
						currentTokenStartPosition = this.scanner.currentPosition;
4183
						hasComment = true;
4184
						break;
4185
					default :
4186
						// step back one token
4187
						this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
4188
						return;
4189
				}
4190
			}
4191
		} catch (InvalidInputException e) {
4192
			throw new AbortFormatting(e);
4193
		}
4194
	}
4195
4196
	void redoAlignment(AlignmentException e){
4124
	void redoAlignment(AlignmentException e){
4197
		if (e.relativeDepth > 0) { // if exception targets a distinct context
4125
		if (e.relativeDepth > 0) { // if exception targets a distinct context
4198
			e.relativeDepth--; // record fact that current context got traversed
4126
			e.relativeDepth--; // record fact that current context got traversed
(-)src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java (+142 lines)
Lines 312-317 Link Here
312
}
312
}
313
313
314
/**
314
/**
315
 * @bug 199265: [formatter] 3.3 Code Formatter mis-places commented-out import statements
316
 * @test Ensure that the formatter keep commented import declarations on their lines
317
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=199265"
318
 */
319
public void testBug199265a() throws JavaModelException {
320
	String source =
321
		"import java.util.List;\n" + 
322
		"//import java.util.HashMap;\n" + 
323
		"import java.util.Set;\n" + 
324
		"\n" + 
325
		"public class X01 {\n" + 
326
		"}\n";
327
	formatSource(source);
328
}
329
public void testBug199265b() throws JavaModelException {
330
	String source =
331
		"import java.util.List;\n" + 
332
		"import java.util.Set;\n" + 
333
		"//import java.util.HashMap;\n" + 
334
		"\n" + 
335
		"public class X02 {\n" + 
336
		"}\n";
337
	formatSource(source);
338
}
339
public void testBug199265c1() throws JavaModelException {
340
	String source =
341
		"import java.util.List;\n" + 
342
		"//            CU         snippet\n" + 
343
		"public class X03 {\n" + 
344
		"	List field;\n" + 
345
		"}\n";
346
	formatSource(source,
347
		"import java.util.List;\n" + 
348
		"\n" + 
349
		"//            CU         snippet\n" + 
350
		"public class X03 {\n" + 
351
		"	List field;\n" + 
352
		"}\n"
353
	);
354
}
355
public void testBug199265c2() throws JavaModelException {
356
	this.formatterPrefs.comment_format_header = true;
357
	String source =
358
		"import java.util.List;\n" + 
359
		"//            CU         snippet\n" + 
360
		"public class X03 {\n" + 
361
		"	List field;\n" + 
362
		"}\n";
363
	formatSource(source,
364
		"import java.util.List;\n" + 
365
		"\n" + 
366
		"// CU snippet\n" + 
367
		"public class X03 {\n" + 
368
		"	List field;\n" + 
369
		"}\n"
370
	);
371
}
372
public void testBug199265c3() throws JavaModelException {
373
	String source =
374
		"import java.util.List;\n" + 
375
		"\n" + 
376
		"// line comment\n" + 
377
		"public class X03 {\n" + 
378
		"	List field;\n" + 
379
		"}\n";
380
	formatSource(source);
381
}
382
public void testBug199265_wksp1a() throws JavaModelException {
383
	String source =
384
		"package wksp1;\n" + 
385
		"\n" + 
386
		"import java.util.*;\n" + 
387
		"import java.util.List; // line comment\n" + 
388
		"\n" + 
389
		"/**\n" + 
390
		" * Javadoc comment\n" + 
391
		" */\n" + 
392
		"public class X01 {\n" + 
393
		"\n" + 
394
		"}\n";
395
	formatSource(source);
396
}
397
public void testBug199265_wksp1b() throws JavaModelException {
398
	String source =
399
		"package wksp1;\n" + 
400
		"\n" + 
401
		"import java.util.Map;\n" + 
402
		"\n" + 
403
		"//==========================\n" + 
404
		"// Line comment\n" + 
405
		"//==========================\n" + 
406
		"\n" + 
407
		"/**\n" + 
408
		" * Javadoc comment\n" + 
409
		" */\n" + 
410
		"public class X02 {\n" + 
411
		"\n" + 
412
		"}\n";
413
	formatSource(source);
414
}
415
public void testBug199265_wksp2a() throws JavaModelException {
416
	String source =
417
		"package wksp2;\n" + 
418
		"\n" + 
419
		"import java.util.Map;\n" + 
420
		"\n" + 
421
		"//#if defined(TEST)\n" + 
422
		"import java.util.Vector;\n" + 
423
		"//#else\n" + 
424
		"//##import java.util.Set;\n" + 
425
		"//#endif\n" + 
426
		"\n" + 
427
		"public class X01 {\n" + 
428
		"\n" + 
429
		"}\n";
430
	formatSource(source);
431
}
432
public void testBug199265_wksp3a() throws JavaModelException {
433
	String source =
434
		"package wksp3;\n" + 
435
		"\n" + 
436
		"import java.util.Set;	// comment 1\n" + 
437
		"import java.util.Map;	// comment 2\n" + 
438
		"import java.util.List;	// comment 3\n" + 
439
		"\n" + 
440
		"public class X01 {\n" + 
441
		"\n" + 
442
		"}\n";
443
	formatSource(source,
444
		"package wksp3;\n" + 
445
		"\n" + 
446
		"import java.util.Set; // comment 1\n" + 
447
		"import java.util.Map; // comment 2\n" + 
448
		"import java.util.List; // comment 3\n" + 
449
		"\n" + 
450
		"public class X01 {\n" + 
451
		"\n" + 
452
		"}\n"
453
	);
454
}
455
456
/**
315
 * @bug 208541: [formatter] Formatter does not format whole region/selection
457
 * @bug 208541: [formatter] Formatter does not format whole region/selection
316
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541"
458
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541"
317
 */
459
 */
(-)src/org/eclipse/jdt/core/tests/formatter/FormatterMassiveRegressionTests.java (-37 / +262 lines)
Lines 21-26 Link Here
21
import java.io.InputStreamReader;
21
import java.io.InputStreamReader;
22
import java.io.PrintStream;
22
import java.io.PrintStream;
23
import java.net.URL;
23
import java.net.URL;
24
import java.text.NumberFormat;
24
import java.text.SimpleDateFormat;
25
import java.text.SimpleDateFormat;
25
import java.util.ArrayList;
26
import java.util.ArrayList;
26
import java.util.Date;
27
import java.util.Date;
Lines 33-38 Link Here
33
import junit.framework.Test;
34
import junit.framework.Test;
34
import junit.framework.TestSuite;
35
import junit.framework.TestSuite;
35
36
37
import org.eclipse.core.runtime.CoreException;
36
import org.eclipse.core.runtime.FileLocator;
38
import org.eclipse.core.runtime.FileLocator;
37
import org.eclipse.core.runtime.IPath;
39
import org.eclipse.core.runtime.IPath;
38
import org.eclipse.core.runtime.Path;
40
import org.eclipse.core.runtime.Path;
Lines 45-51 Link Here
45
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter;
47
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter;
46
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions;
48
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions;
47
import org.eclipse.text.edits.TextEdit;
49
import org.eclipse.text.edits.TextEdit;
48
49
import com.ibm.icu.util.StringTokenizer;
50
import com.ibm.icu.util.StringTokenizer;
50
51
51
/**
52
/**
Lines 154-159 Link Here
154
	private final static File INPUT_DIR = new File(System.getProperty("inputDir"));
155
	private final static File INPUT_DIR = new File(System.getProperty("inputDir"));
155
	private static File OUTPUT_DIR; // use static to minimize data consumption
156
	private static File OUTPUT_DIR; // use static to minimize data consumption
156
	private static File WRITE_DIR;
157
	private static File WRITE_DIR;
158
	
159
	// Files
160
	final static String FILES_FILTER = System.getProperty("filesFilter");
161
	final static int FILES_FILTER_KIND;
162
	static {
163
		int kind = 0; // No filter
164
		if (FILES_FILTER != null) {
165
			int length = FILES_FILTER.length();
166
			int idxQM = FILES_FILTER.indexOf('?');
167
			int idxS = FILES_FILTER.indexOf('*');
168
			if (idxQM >= 0 && idxS >= 0) {
169
				kind = 4; // Pure pattern match
170
			} else if (idxQM >= 0) {
171
				while (idxQM < length && FILES_FILTER.charAt(idxQM) == '?') {
172
					idxQM++;
173
				}
174
				if (idxQM == length) {
175
					kind = 3; // Starts with + same length
176
				} else {
177
					kind = 4; // Pure pattern match
178
				}
179
			} else if (idxS >= 0) {
180
				while (idxS < length && FILES_FILTER.charAt(idxQM) == '*') {
181
					idxS++;
182
				}
183
				if (idxS == length) {
184
					kind = 2; // Starts with
185
				} else {
186
					kind = 4; // Pure pattern match
187
				}
188
			} else {
189
				kind = 1; // Equals
190
			}
191
		}
192
		FILES_FILTER_KIND = kind;
193
	}
194
195
	// Log
157
	private static File LOG_FILE;
196
	private static File LOG_FILE;
158
	private static PrintStream LOG_STREAM;
197
	private static PrintStream LOG_STREAM;
159
198
Lines 167-176 Link Here
167
	private static int MAX_FILES, MAX_DIGITS;
206
	private static int MAX_FILES, MAX_DIGITS;
168
207
169
	// Formatting behavior
208
	// Formatting behavior
170
	private final static int FORMAT_REPEAT  = Integer.parseInt(System.getProperty("repeat", "2"));
209
	final static int FORMAT_REPEAT  = Integer.parseInt(System.getProperty("repeat", "2"));
171
	private final static boolean NO_COMMENTS = System.getProperty("no_comments", "false").equals("true");
210
	private final static boolean NO_COMMENTS = System.getProperty("no_comments", "false").equals("true");
172
	private static String JOIN_LINES = System.getProperty("join_lines", null);
211
	private static String JOIN_LINES = System.getProperty("join_lines", null);
173
	private static String BRACES = System.getProperty("braces", null);
212
	private static String BRACES = System.getProperty("braces", null);
213
	
214
	// Time measuring
215
	static class TimeMeasuring {
216
		long[] formatting = new long[FORMAT_REPEAT];
217
		int [] occurences = new int[FORMAT_REPEAT];
218
		int [] null_output = new int[FORMAT_REPEAT];
219
	}
220
	private static TimeMeasuring TIME_MEASURES = new TimeMeasuring();
221
	private static final int ONE_MINUTE = 60000;
222
	private static final long ONE_HOUR = 3600000L;
174
223
175
	// Failures management
224
	// Failures management
176
	int failureIndex;
225
	int failureIndex;
Lines 215-221 Link Here
215
	private static String ECLIPSE_VERSION;
264
	private static String ECLIPSE_VERSION;
216
	private static String ECLIPSE_MILESTONE;
265
	private static String ECLIPSE_MILESTONE;
217
	private static String JDT_CORE_VERSION;
266
	private static String JDT_CORE_VERSION;
218
	private static String PATCH_BUG;
267
	private static String PATCH_BUG, PATCH_VERSION;
219
	private static boolean JDT_CORE_HEAD;
268
	private static boolean JDT_CORE_HEAD;
220
	private final static IPath[] EXPECTED_FAILURES = INPUT_DIR.getPath().indexOf("v34") < 0
269
	private final static IPath[] EXPECTED_FAILURES = INPUT_DIR.getPath().indexOf("v34") < 0
221
		? new IPath[] {
270
		? new IPath[] {
Lines 287-293 Link Here
287
		// Get files from input dir
336
		// Get files from input dir
288
		FileFilter filter = new FileFilter() {
337
		FileFilter filter = new FileFilter() {
289
			public boolean accept(File pathname) {
338
			public boolean accept(File pathname) {
290
	            return pathname.isDirectory() || pathname.getPath().endsWith(".java");
339
				String path = pathname.getPath();
340
				boolean accept = pathname.isDirectory() || path.endsWith(".java");
341
				if (accept) {
342
					switch (FILES_FILTER_KIND) {
343
						case 1: // Equals
344
							accept = path.equals(FILES_FILTER);
345
							break;
346
						case 2: // Starts with
347
							accept = path.startsWith(FILES_FILTER);
348
							break;
349
						case 3: // Starts with + same length
350
							accept = path.startsWith(FILES_FILTER) && path.length() == FILES_FILTER.length();
351
							break;
352
						case 4: // Pattern
353
							accept = path.matches(FILES_FILTER);
354
							break;
355
					}
356
				}
357
				return accept;
291
            }
358
            }
292
		};
359
		};
293
		File[] allFiles = ModelTestsUtil.getAllFiles(INPUT_DIR, filter);
360
		File[] allFiles = ModelTestsUtil.getAllFiles(INPUT_DIR, filter);
Lines 300-307 Link Here
300
367
301
		// Init directories
368
		// Init directories
302
		boolean clean = initDirectories(buffer);
369
		boolean clean = initDirectories(buffer);
303
		buffer.append("Comparison: "+CAN_COMPARE);
370
		buffer.append("Compare vs: ");
304
		buffer.append(LINE_SEPARATOR);
371
		if (CAN_COMPARE) {
372
			if (clean) {
373
				buffer.append(JDT_CORE_VERSION);
374
			} else {
375
				File versionFile = new File(OUTPUT_DIR, "version.txt");
376
				if (versionFile.exists()) {
377
					buffer.append(Util.fileContent(versionFile.getAbsolutePath()));
378
				} else {
379
					buffer.append("???");
380
				}
381
			}
382
		} else {
383
			buffer.append("none");
384
		}
385
//		buffer.append(LINE_SEPARATOR);
305
386
306
		// Write logs
387
		// Write logs
307
		System.out.println(buffer.toString());
388
		System.out.println(buffer.toString());
Lines 385-391 Link Here
385
				clean = true;
466
				clean = true;
386
			}
467
			}
387
		}
468
		}
388
		if (!OUTPUT_DIR.exists() && !clean) {
469
		if (clean) {
470
			if (PATCH_BUG != null || JDT_CORE_HEAD) {
471
				throw new RuntimeException("Reference can only be updated using a version (i.e. with a closed buildnotes_jdt-core.html)!");
472
			}
473
		} else if (!OUTPUT_DIR.exists()) {
389
			System.err.println("            WARNING: The output directory "+OUTPUT_DIR+" does not exist...");
474
			System.err.println("            WARNING: The output directory "+OUTPUT_DIR+" does not exist...");
390
			System.err.println("            => NO comparison could be done!");
475
			System.err.println("            => NO comparison could be done!");
391
			CAN_COMPARE = false;
476
			CAN_COMPARE = false;
Lines 398-404 Link Here
398
	}
483
	}
399
484
400
	// Get log dir
485
	// Get log dir
401
	setLogDir(buffer);
486
	try {
487
		setLogDir(buffer);
488
	} catch (CoreException e) {
489
		e.printStackTrace();
490
	}
402
491
403
	// Get write dir
492
	// Get write dir
404
	String wdir = System.getProperty("writeDir"); //$NON-NLS-1$
493
	String wdir = System.getProperty("writeDir"); //$NON-NLS-1$
Lines 417-425 Link Here
417
	return clean;
506
	return clean;
418
}
507
}
419
508
420
private static void setLogDir(StringBuffer buffer) {
509
private static void setLogDir(StringBuffer buffer) throws CoreException {
421
510
422
	// Compute log dir
511
	// Compute log dir
512
//	IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
513
//	IProject project = root.getProject("Bugs");
514
//	IFolder rootFolder = project.getFolder("Formatter");
423
	File logDir = new File(System.getProperty("logDir"));
515
	File logDir = new File(System.getProperty("logDir"));
424
	if (!logDir.exists()) {
516
	if (!logDir.exists()) {
425
		return;
517
		return;
Lines 427-470 Link Here
427
519
428
	// Compute log sub-directories depending on profiles
520
	// Compute log sub-directories depending on profiles
429
	logDir = new File(logDir, ECLIPSE_VERSION);
521
	logDir = new File(logDir, ECLIPSE_VERSION);
522
//	IFolder folder = rootFolder.getFolder(ECLIPSE_VERSION);
523
//	if (!folder.exists()) folder.create(true, true, null);
430
	if (PATCH_BUG != null) {
524
	if (PATCH_BUG != null) {
431
		logDir = new File(logDir, "tests");
525
		logDir = new File(logDir, "tests");
432
		logDir = new File(logDir, PATCH_BUG);
526
		logDir = new File(logDir, PATCH_BUG);
527
		logDir = new File(logDir, PATCH_VERSION);
528
//		folder = folder.getFolder("tests");
529
//		if (!folder.exists()) folder.create(true, true, null);
530
//		folder = folder.getFolder(PATCH_BUG);
531
//		if (!folder.exists()) folder.create(true, true, null);
532
//		folder = folder.getFolder(PATCH_VERSION);
533
//		if (!folder.exists()) folder.create(true, true, null);
433
	} else if (JDT_CORE_HEAD) {
534
	} else if (JDT_CORE_HEAD) {
434
		logDir = new File(logDir, "HEAD");
535
		logDir = new File(logDir, "HEAD");
536
//		folder = folder.getFolder("HEAD");
537
//		if (!folder.exists()) folder.create(true, true, null);
435
	} else {
538
	} else {
436
		logDir = new File(logDir, ECLIPSE_MILESTONE);
539
		logDir = new File(logDir, ECLIPSE_MILESTONE);
437
		logDir = new File(logDir, JDT_CORE_VERSION);
540
		logDir = new File(logDir, JDT_CORE_VERSION);
541
//		folder = folder.getFolder(ECLIPSE_MILESTONE);
542
//		if (!folder.exists()) folder.create(true, true, null);
543
//		folder = folder.getFolder(JDT_CORE_VERSION);
544
//		if (!folder.exists()) folder.create(true, true, null);
438
	}
545
	}
439
	if (NO_COMMENTS || BRACES != null || JOIN_LINES != null) {
546
	if (NO_COMMENTS || BRACES != null || JOIN_LINES != null) {
440
		logDir = new File(logDir, "profiles");
547
		logDir = new File(logDir, "profiles");
548
//		folder = folder.getFolder("profiles");
549
//		if (!folder.exists()) folder.create(true, true, null);
441
		if (JOIN_LINES != null) {
550
		if (JOIN_LINES != null) {
442
			logDir = new File(new File(logDir, "join_lines"), JOIN_LINES);
551
			logDir = new File(new File(logDir, "join_lines"), JOIN_LINES);
552
//			folder = folder.getFolder("join_lines");
553
//			if (!folder.exists()) folder.create(true, true, null);
554
//			folder = folder.getFolder(JOIN_LINES);
555
//			if (!folder.exists()) folder.create(true, true, null);
443
		}
556
		}
444
		if (NO_COMMENTS) {
557
		if (NO_COMMENTS) {
445
			logDir = new File(logDir, "no_comments");
558
			logDir = new File(logDir, "no_comments");
559
//			folder = folder.getFolder("no_comments");
560
//			if (!folder.exists()) folder.create(true, true, null);
446
		}
561
		}
447
		if (BRACES != null) {
562
		if (BRACES != null) {
448
			logDir = new File(new File(logDir, "braces"), BRACES);
563
			logDir = new File(new File(logDir, "braces"), BRACES);
564
//			folder = folder.getFolder("braces");
565
//			if (!folder.exists()) folder.create(true, true, null);
566
//			folder = folder.getFolder(BRACES);
567
//			if (!folder.exists()) folder.create(true, true, null);
449
		}
568
		}
450
	}
569
	}
451
	
570
	
452
	// Create log stream
571
	// Create log stream
453
	logDir.mkdirs();
572
	logDir.mkdirs();
454
	String logFileName = INPUT_DIR.getName().replaceAll("\\.", "")+".txt";
573
	String filePrefix = INPUT_DIR.getName().replaceAll("\\.", "");
574
	String logFileName = filePrefix+".txt";
455
	LOG_FILE = new File(logDir, logFileName);
575
	LOG_FILE = new File(logDir, logFileName);
576
	if (LOG_FILE.exists()) {
577
		File saveDir = new File(logDir, "save");
578
		saveDir.mkdir();
579
		int i=0;
580
		while (true) {
581
			String newFileName = filePrefix+"_";
582
			if (i<10) newFileName += "0";
583
			newFileName += i+".txt";
584
			File renamedFile = new File(saveDir, newFileName);
585
			if (LOG_FILE.renameTo(renamedFile)) break;
586
			i++;
587
		}
588
	}
589
//	LOG_RESOURCE = folder.getFile(logFileName);
456
	buffer.append("Log file  : ");
590
	buffer.append("Log file  : ");
457
	buffer.append(LOG_FILE);
591
	buffer.append(LOG_FILE);
592
//	buffer.append(LOG_RESOURCE.getFullPath());
458
	buffer.append(LINE_SEPARATOR);
593
	buffer.append(LINE_SEPARATOR);
459
	try {
594
	try {
460
		if (LOG_FILE.exists()) {
461
			Util.delete(LOG_FILE);
462
		}
463
		LOG_STREAM = new PrintStream(new BufferedOutputStream(new FileOutputStream(LOG_FILE)));
595
		LOG_STREAM = new PrintStream(new BufferedOutputStream(new FileOutputStream(LOG_FILE)));
596
		LOG_STREAM.flush();
464
	}
597
	}
465
	catch (FileNotFoundException fnfe) {
598
	catch (FileNotFoundException fnfe) {
466
		System.err.println("Can't create log file"+LOG_FILE); //$NON-NLS-1$
599
		System.err.println("Can't create log file"+LOG_FILE); //$NON-NLS-1$
467
	}
600
	}
601
//	if (LOG_RESOURCE.exists()) {
602
//		Util.delete(LOG_RESOURCE);
603
//	}
604
//	LOG_BUFFER = new StringBuffer();
468
}
605
}
469
private static void setOutputDir(String dir, StringBuffer buffer) {
606
private static void setOutputDir(String dir, StringBuffer buffer) {
470
607
Lines 546-551 Link Here
546
					patch_line = line;
683
					patch_line = line;
547
					StringTokenizer tokenizer = new StringTokenizer(line);
684
					StringTokenizer tokenizer = new StringTokenizer(line);
548
					tokenizer.nextToken(); // 'Patch'
685
					tokenizer.nextToken(); // 'Patch'
686
					PATCH_VERSION = tokenizer.nextToken();
549
					while (tokenizer.hasMoreTokens()) {
687
					while (tokenizer.hasMoreTokens()) {
550
						PATCH_BUG = tokenizer.nextToken();
688
						PATCH_BUG = tokenizer.nextToken();
551
					}
689
					}
Lines 553-559 Link Here
553
						Integer.parseInt(PATCH_BUG);
691
						Integer.parseInt(PATCH_BUG);
554
					}
692
					}
555
					catch (NumberFormatException nfe) {
693
					catch (NumberFormatException nfe) {
556
						System.err.println("Invalid patch bug number noticed in JDT/Core buildnotes:"+PATCH_BUG);
694
						System.err.println("Invalid patch bug number noticed in JDT/Core buildnotes: "+PATCH_BUG);
557
					}
695
					}
558
				}
696
				}
559
				if (!JDT_CORE_HEAD) break;
697
				if (!JDT_CORE_HEAD) break;
Lines 625-631 Link Here
625
	StringBuffer name = new StringBuffer(super.getName());
763
	StringBuffer name = new StringBuffer(super.getName());
626
	if (this.testIndex >= 0) {
764
	if (this.testIndex >= 0) {
627
		int n = this.testIndex == 0 ? 0 : (int) (Math.log(this.testIndex)/Math.log(10));
765
		int n = this.testIndex == 0 ? 0 : (int) (Math.log(this.testIndex)/Math.log(10));
628
		for (int i=n; i<MAX_DIGITS-1; i++) {
766
		for (int i=n; i<MAX_DIGITS; i++) {
629
			name.append('0');
767
			name.append('0');
630
		}
768
		}
631
		name.append(this.testIndex);
769
		name.append(this.testIndex);
Lines 692-740 Link Here
692
 */
830
 */
693
public void tearDownSuite() throws Exception {
831
public void tearDownSuite() throws Exception {
694
832
833
	// Display time measures
834
	StringBuffer buffer1 = new StringBuffer();
835
	buffer1.append("Time measures:").append(LINE_SEPARATOR);
836
	buffer1.append("	- first format:").append(LINE_SEPARATOR);
837
	buffer1.append("		+ elapsed = "+timeString(TIME_MEASURES.formatting[0])).append(LINE_SEPARATOR);
838
	buffer1.append("		+ occurrences = "+TIME_MEASURES.occurences[0]).append(LINE_SEPARATOR);
839
	buffer1.append("		+ null output = "+TIME_MEASURES.null_output[0]).append(LINE_SEPARATOR);
840
	buffer1.append("	- repeated format:").append(LINE_SEPARATOR);
841
	for (int i=1; i<FORMAT_REPEAT; i++) {
842
		buffer1.append("	   n° "+(i+1)).append(LINE_SEPARATOR);
843
		buffer1.append("		+ elapsed = "+timeString(TIME_MEASURES.formatting[i])).append(LINE_SEPARATOR);
844
		buffer1.append("		+ occurrences = "+TIME_MEASURES.occurences[i]).append(LINE_SEPARATOR);
845
		buffer1.append("		+ null output = "+TIME_MEASURES.null_output[i]).append(LINE_SEPARATOR);
846
	}
847
	buffer1.append(LINE_SEPARATOR);
848
695
	// Display stored failures
849
	// Display stored failures
696
	StringBuffer buffer = new StringBuffer(LINE_SEPARATOR);
697
	int max = FAILURES.length;
850
	int max = FAILURES.length;
698
	for (int i=0; i<max; i++) {
851
	for (int i=0; i<max; i++) {
699
		List failures = FAILURES[i].failures;
852
		List failures = FAILURES[i].failures;
700
		int size = failures.size();
853
		int size = failures.size();
701
		if (size > 0) {
854
		if (size > 0) {
702
			buffer.append(size);
855
			buffer1.append(size);
703
			buffer.append(" file");
856
			buffer1.append(" file");
704
			if (size == 1) {
857
			if (size == 1) {
705
				buffer.append(" has ");
858
				buffer1.append(" has ");
706
			} else {
859
			} else {
707
				buffer.append("s have ");
860
				buffer1.append("s have ");
708
			}
861
			}
709
			buffer.append(FAILURES[i]);
862
			buffer1.append(FAILURES[i]);
710
			buffer.append('!');
863
			buffer1.append('!');
711
			buffer.append(LINE_SEPARATOR);
864
			buffer1.append(LINE_SEPARATOR);
712
		}
865
		}
713
	}
866
	}
714
	buffer.append(LINE_SEPARATOR);
867
	buffer1.append(LINE_SEPARATOR);
868
	StringBuffer buffer2 = new StringBuffer(LINE_SEPARATOR);
715
	for (int i=0; i<max; i++) {
869
	for (int i=0; i<max; i++) {
716
		List failures = FAILURES[i].failures;
870
		List failures = FAILURES[i].failures;
717
		int size = failures.size();
871
		int size = failures.size();
718
		if (size > 0) {
872
		if (size > 0) {
719
			buffer.append("List of file(s) with ");
873
			buffer2.append("List of file(s) with ");
720
			buffer.append(FAILURES[i]);
874
			buffer2.append(FAILURES[i]);
721
			buffer.append(':');
875
			buffer2.append(':');
722
			buffer.append(LINE_SEPARATOR);
876
			buffer2.append(LINE_SEPARATOR);
723
			for (int j=0; j<size; j++) {
877
			for (int j=0; j<size; j++) {
724
				buffer.append("	- ");
878
				buffer2.append("	- ");
725
				buffer.append(failures.get(j));
879
				buffer2.append(failures.get(j));
726
				buffer.append(LINE_SEPARATOR);
880
				buffer2.append(LINE_SEPARATOR);
727
			}
881
			}
728
		}
882
		}
729
	}
883
	}
730
884
731
	// Log failures
885
	// Log failures
886
	System.out.println(buffer1.toString());
732
	if (LOG_STREAM == null) {
887
	if (LOG_STREAM == null) {
733
		System.out.println(buffer.toString());
888
		System.out.println(buffer2.toString());
734
	} else {
889
	} else {
735
		LOG_STREAM.print(buffer.toString());
890
		LOG_STREAM.print(buffer1.toString());
891
		LOG_STREAM.print(buffer2.toString());
736
		LOG_STREAM.close();
892
		LOG_STREAM.close();
737
	}
893
	}
894
//	LOG_BUFFER.append(buffer1.toString());
895
//	LOG_BUFFER.append(buffer2.toString());
896
//	InputStream stream= new InputStream() {
897
//		private Reader reader= new StringReader(LOG_BUFFER.toString());
898
//		public int read() throws IOException {
899
//			return this.reader.read();
900
//		}
901
//	};
902
//	if (LOG_RESOURCE.exists()) {
903
//		LOG_RESOURCE.setContents(
904
//			stream,
905
//			IResource.FORCE | IResource.KEEP_HISTORY,
906
//			null);
907
//	} else {
908
//		LOG_RESOURCE.create(stream, IResource.FORCE, null);
909
//	}
738
}
910
}
739
911
740
/*
912
/*
Lines 928-945 Link Here
928
*/
1100
*/
929
1101
930
String runFormatter(CodeFormatter codeFormatter, String source, int kind, int indentationLevel, int offset, int length, String lineSeparator, boolean repeat) {
1102
String runFormatter(CodeFormatter codeFormatter, String source, int kind, int indentationLevel, int offset, int length, String lineSeparator, boolean repeat) {
1103
	long timeStart = System.currentTimeMillis();
931
	TextEdit edit = codeFormatter.format(kind, source, offset, length, indentationLevel, lineSeparator);
1104
	TextEdit edit = codeFormatter.format(kind, source, offset, length, indentationLevel, lineSeparator);
1105
	if (FAILURES != null) { // Comparison has started
1106
		TIME_MEASURES.formatting[0] += System.currentTimeMillis() - timeStart;
1107
		TIME_MEASURES.occurences[0]++;
1108
		if (edit == null) TIME_MEASURES.null_output[0]++;
1109
	}
932
	if (edit == null) {
1110
	if (edit == null) {
933
		this.failureIndex = NO_OUTPUT_FAILURE;
1111
		this.failureIndex = NO_OUTPUT_FAILURE;
934
		throw new AssertionFailedError("Formatted source should not be null!");
1112
		throw new AssertionFailedError("Formatted source should not be null!");
935
	}
1113
	}
936
	String initialResult = org.eclipse.jdt.internal.core.util.Util.editedString(source, edit);
1114
	String initialResult = org.eclipse.jdt.internal.core.util.Util.editedString(source, edit);
937
1115
938
	int count = 1;
1116
	int count = 0;
939
	String result = initialResult;
1117
	String result = initialResult;
940
	String previousResult = result;
1118
	String previousResult = result;
941
	while (count++ < FORMAT_REPEAT) {
1119
	while (++count < FORMAT_REPEAT) {
1120
		timeStart = System.currentTimeMillis();
942
		edit = codeFormatter.format(kind, result, 0, result.length(), indentationLevel, lineSeparator);
1121
		edit = codeFormatter.format(kind, result, 0, result.length(), indentationLevel, lineSeparator);
1122
		if (FAILURES != null) { // Comparison has started
1123
			TIME_MEASURES.formatting[count] += System.currentTimeMillis() - timeStart;
1124
			TIME_MEASURES.occurences[count]++;
1125
			if (edit == null) TIME_MEASURES.null_output[count]++;
1126
		}
943
		if (edit == null) return null;
1127
		if (edit == null) return null;
944
		previousResult = result;
1128
		previousResult = result;
945
		result = org.eclipse.jdt.internal.core.util.Util.editedString(result, edit);
1129
		result = org.eclipse.jdt.internal.core.util.Util.editedString(result, edit);
Lines 977-983 Link Here
977
		assertSourceEquals(counterString+" formatting is different from first one!", previousResult, result);
1161
		assertSourceEquals(counterString+" formatting is different from first one!", previousResult, result);
978
		*/
1162
		*/
979
		if (!isExpectedFailure()) {
1163
		if (!isExpectedFailure()) {
980
			String counterString = counterToString(count-1);
1164
			String counterString = counterToString(count);
981
			try {
1165
			try {
982
				assertSourceEquals(counterString+" formatting is different from first one!", previousResult, result);
1166
				assertSourceEquals(counterString+" formatting is different from first one!", previousResult, result);
983
			}
1167
			}
Lines 994-999 Link Here
994
	return initialResult;
1178
	return initialResult;
995
}
1179
}
996
1180
1181
/**
1182
 * Returns a string to display the given time as a duration
1183
 * formatted as:
1184
 *	<ul>
1185
 *	<li>"XXXms" if the duration is less than 0.1s (e.g. "543ms")</li>
1186
 *	<li>"X.YYs" if the duration is less than 1s (e.g. "5.43s")</li>
1187
 *	<li>"XX.Ys" if the duration is less than 1mn (e.g. "54.3s")</li>
1188
 *	<li>"XXmn XXs" if the duration is less than 1h (e.g. "54mn 3s")</li>
1189
 *	<li>"XXh XXmn XXs" if the duration is over than 1h (e.g. "5h 4mn 3s")</li>
1190
 *	</ul>
1191
 *
1192
 * @param time The time to format as a long.
1193
 * @return The formatted string.
1194
 */
1195
public String timeString(long time) {
1196
	NumberFormat format = NumberFormat.getInstance();
1197
	format.setMaximumFractionDigits(3);
1198
	StringBuffer buffer = new StringBuffer();
1199
	if (time == 0) {
1200
		// print nothing
1201
	} else {
1202
		long h = time / ONE_HOUR;
1203
		if (h > 0) buffer.append(h).append("h "); //$NON-NLS-1$
1204
		long remaining = time % ONE_HOUR;
1205
		long m = remaining / ONE_MINUTE;
1206
		if (h > 0 || m > 0) buffer.append(m).append("mn "); //$NON-NLS-1$
1207
		remaining = remaining % ONE_MINUTE;
1208
		if ((remaining % 1000) == 0) {
1209
			buffer.append(remaining/1000);
1210
		} else {
1211
			buffer.append(format.format(remaining/1000.0));
1212
		}
1213
		buffer.append("s"); //$NON-NLS-1$
1214
	}
1215
	return buffer.toString();
1216
}
1217
997
/*
1218
/*
998
 * Test to delete the output directory.
1219
 * Test to delete the output directory.
999
 */
1220
 */
Lines 1001-1012 Link Here
1001
	Util.delete(OUTPUT_DIR);
1222
	Util.delete(OUTPUT_DIR);
1002
}
1223
}
1003
1224
1004
1005
/*
1225
/*
1006
 * Test to fill the output directory with reference.
1226
 * Test to fill the output directory with reference.
1007
 */
1227
 */
1008
public void testMakeReferences() throws IOException, Exception {
1228
public void testMakeReferences() throws IOException, Exception {
1009
1229
1230
	// Dump the version
1231
	File versionFile = new Path(OUTPUT_DIR.getPath()).append("version.txt").toFile();
1232
	OUTPUT_DIR.mkdirs();
1233
	Util.writeToFile(JDT_CORE_VERSION, versionFile.getAbsolutePath());
1234
1010
	// Format each file of the input dir and write the result to the output directory
1235
	// Format each file of the input dir and write the result to the output directory
1011
	assertNotNull("We should have got input files from "+INPUT_DIR, this.inputFiles);
1236
	assertNotNull("We should have got input files from "+INPUT_DIR, this.inputFiles);
1012
	DefaultCodeFormatter codeFormatter = codeFormatter();
1237
	DefaultCodeFormatter codeFormatter = codeFormatter();

Return to bug 199265