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

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/parser/Scanner.java (-24 / +14 lines)
Lines 3664-3699 Link Here
3664
	if (this.currentPosition <= 0)
3664
	if (this.currentPosition <= 0)
3665
		return "NOT started!\n\n"+ new String(this.source); //$NON-NLS-1$
3665
		return "NOT started!\n\n"+ new String(this.source); //$NON-NLS-1$
3666
3666
3667
	char front[] = new char[this.startPosition];
3667
	StringBuffer buffer = new StringBuffer();
3668
	System.arraycopy(this.source, 0, front, 0, this.startPosition);
3668
	if (this.startPosition < 1000) {
3669
		buffer.append(this.source, 0, this.startPosition);
3670
	} else {
3671
		buffer.append("<source beginning>\n...\n"); //$NON-NLS-1$
3672
		int line = Util.getLineNumber(this.startPosition-1000, this.lineEnds, 0, this.lineEnds.length);
3673
		int lineStart = getLineStart(line);
3674
		buffer.append(this.source, lineStart, this.startPosition-lineStart);
3675
	}
3669
3676
3677
	buffer.append("\n===============================\nStarts here -->"); //$NON-NLS-1$
3670
	int middleLength = (this.currentPosition - 1) - this.startPosition + 1;
3678
	int middleLength = (this.currentPosition - 1) - this.startPosition + 1;
3671
	char middle[];
3672
	if (middleLength > -1) {
3679
	if (middleLength > -1) {
3673
		middle = new char[middleLength];
3680
		buffer.append(this.source, this.startPosition, middleLength);
3674
		System.arraycopy(
3675
			this.source,
3676
			this.startPosition,
3677
			middle,
3678
			0,
3679
			middleLength);
3680
	} else {
3681
		middle = CharOperation.NO_CHAR;
3682
	}
3681
	}
3682
	buffer.append("<-- Ends here\n===============================\n"); //$NON-NLS-1$
3683
3683
3684
	char end[] = new char[this.eofPosition - (this.currentPosition - 1)];
3684
	buffer.append(this.source, (this.currentPosition - 1) + 1, this.eofPosition - (this.currentPosition - 1) - 1);
3685
	System.arraycopy(
3686
		this.source,
3687
		(this.currentPosition - 1) + 1,
3688
		end,
3689
		0,
3690
		this.eofPosition - (this.currentPosition - 1) - 1);
3691
3685
3692
	return new String(front)
3686
	return buffer.toString();
3693
		+ "\n===============================\nStarts here -->" //$NON-NLS-1$
3694
		+ new String(middle)
3695
		+ "<-- Ends here\n===============================\n" //$NON-NLS-1$
3696
		+ new String(end);
3697
}
3687
}
3698
public String toStringAction(int act) {
3688
public String toStringAction(int act) {
3699
	switch (act) {
3689
	switch (act) {
(-)formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java (-3 / +3 lines)
Lines 1868-1874 Link Here
1868
				this.scribe.indent();
1868
				this.scribe.indent();
1869
			}
1869
			}
1870
			this.scribe.printNextToken(TerminalTokens.TokenNameLBRACE, insertSpaceBeforeBrace);
1870
			this.scribe.printNextToken(TerminalTokens.TokenNameLBRACE, insertSpaceBeforeBrace);
1871
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
1871
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.UNMODIFIABLE_TRAILING_COMMENT);
1872
	}
1872
	}
1873
	private void formatStatements(BlockScope scope, final Statement[] statements, boolean insertNewLineAfterLastStatement) {
1873
	private void formatStatements(BlockScope scope, final Statement[] statements, boolean insertNewLineAfterLastStatement) {
1874
		int statementsLength = statements.length;
1874
		int statementsLength = statements.length;
Lines 4254-4260 Link Here
4254
		} else {
4254
		} else {
4255
			// no method body
4255
			// no method body
4256
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
4256
			this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
4257
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
4257
			this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.COMPLEX_TRAILING_COMMENT);
4258
		}
4258
		}
4259
		return false;
4259
		return false;
4260
	}
4260
	}
Lines 4951-4962 Link Here
4951
						this.scribe.unIndent();
4951
						this.scribe.unIndent();
4952
					}
4952
					}
4953
					statement.traverse(this, scope);
4953
					statement.traverse(this, scope);
4954
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
4955
					wasACase = true;
4954
					wasACase = true;
4956
					wasAStatement = false;
4955
					wasAStatement = false;
4957
					if (this.preferences.indent_switchstatements_compare_to_cases) {
4956
					if (this.preferences.indent_switchstatements_compare_to_cases) {
4958
						this.scribe.indent();
4957
						this.scribe.indent();
4959
					}
4958
					}
4959
					this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.COMPLEX_TRAILING_COMMENT);
4960
				} else if (statement instanceof BreakStatement) {
4960
				} else if (statement instanceof BreakStatement) {
4961
					if (this.preferences.indent_breaks_compare_to_cases) {
4961
					if (this.preferences.indent_breaks_compare_to_cases) {
4962
						if (wasAStatement && !this.preferences.indent_switchstatements_compare_to_cases) {
4962
						if (wasAStatement && !this.preferences.indent_switchstatements_compare_to_cases) {
(-)formatter/org/eclipse/jdt/internal/formatter/Location.java (-1 / +1 lines)
Lines 43-49 Link Here
43
		this.outputColumn = scribe.column;
43
		this.outputColumn = scribe.column;
44
		this.outputLine = scribe.line;
44
		this.outputLine = scribe.line;
45
		this.inputOffset = sourceRestart;
45
		this.inputOffset = sourceRestart;
46
		this.inputColumn = scribe.getCurrentIndentation(sourceRestart);
46
		this.inputColumn = scribe.getCurrentColumn(sourceRestart);
47
		this.outputIndentationLevel = scribe.indentationLevel;
47
		this.outputIndentationLevel = scribe.indentationLevel;
48
		this.lastNumberOfNewLines = scribe.lastNumberOfNewLines;
48
		this.lastNumberOfNewLines = scribe.lastNumberOfNewLines;
49
		this.needSpace = scribe.needSpace;
49
		this.needSpace = scribe.needSpace;
(-)formatter/org/eclipse/jdt/internal/formatter/Scribe.java (-45 / +192 lines)
Lines 106-117 Link Here
106
	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;
107
	private static final int SKIP_FIRST_WHITESPACE_TOKEN = -2;
107
	private static final int SKIP_FIRST_WHITESPACE_TOKEN = -2;
108
	private static final int INVALID_TOKEN = 2000;
108
	private static final int INVALID_TOKEN = 2000;
109
	static final int NO_TRAILING_COMMENT = 0;
109
	static final int NO_TRAILING_COMMENT = 0x0000;
110
	static final int BASIC_TRAILING_COMMENT = 1;
110
	static final int BASIC_TRAILING_COMMENT = 0x0100;
111
	static final int IMPORT_TRAILING_COMMENT = 2;
111
	static final int COMPLEX_TRAILING_COMMENT = 0x0200;
112
	static final int IMPORT_TRAILING_COMMENT = COMPLEX_TRAILING_COMMENT | 0x0001;
113
	static final int UNMODIFIABLE_TRAILING_COMMENT = 0x0400;
112
	private int formatComments = 0;
114
	private int formatComments = 0;
113
	private int headerEndPosition = -1;
115
	private int headerEndPosition = -1;
114
	String commentIndentation; // indentation requested in comments (usually in javadoc root tags description)
116
	String commentIndentation; // indentation requested in comments (usually in javadoc root tags description)
117
	// Class to store previous line comment information
118
	class LineComment {
119
		boolean contiguous = false;
120
		int currentColumn, indentation;
121
		char[] leadingSpaces;
122
	}
123
	final LineComment lastLineComment = new LineComment();
124
115
125
116
	// New way to format javadoc
126
	// New way to format javadoc
117
	private FormatterCommentParser formatterCommentParser; // specialized parser to format comments
127
	private FormatterCommentParser formatterCommentParser; // specialized parser to format comments
Lines 787-796 Link Here
787
		return null;
797
		return null;
788
	}
798
	}
789
799
790
	private int getCurrentCommentOffset(int start) {
800
	private int getCurrentCommentColumn(int start) {
791
		int linePtr = -Arrays.binarySearch(this.lineEnds, start);
801
		int linePtr = -Arrays.binarySearch(this.lineEnds, start);
792
		int offset = 0;
802
		int commentColumn = 1;
793
		int beginningOfLine = getLineEnd(linePtr - 1);
803
		int beginningOfLine = getLineEnd(linePtr - 1)+1;
794
		if (beginningOfLine == -1) {
804
		if (beginningOfLine == -1) {
795
			beginningOfLine = 0;
805
			beginningOfLine = 0;
796
		}
806
		}
Lines 800-834 Link Here
800
		// find the position of the beginning of the line containing the comment
810
		// find the position of the beginning of the line containing the comment
801
		while (beginningOfLine > currentStartPosition) {
811
		while (beginningOfLine > currentStartPosition) {
802
			if (linePtr > 0) {
812
			if (linePtr > 0) {
803
				beginningOfLine = getLineEnd(--linePtr);
813
				beginningOfLine = getLineEnd(--linePtr)+1;
804
			} else {
814
			} else {
805
				beginningOfLine = 0;
815
				beginningOfLine = 0;
806
				break;
816
				break;
807
			}
817
			}
808
		}
818
		}
809
		for (int i = currentStartPosition - 1; i >= beginningOfLine ; i--) {
819
		for (int i=beginningOfLine; i < currentStartPosition ; i++) {
810
			char currentCharacter = source[i];
820
			char currentCharacter = source[i];
811
			switch (currentCharacter) {
821
			switch (currentCharacter) {
812
				case '\t' :
822
				case '\t' :
813
					offset += this.tabLength;
823
					if (this.tabLength != 0) {
824
						int reminder = commentColumn % this.tabLength;
825
						if (reminder == 0) {
826
							commentColumn += this.tabLength;
827
						} else {
828
							commentColumn = ((commentColumn / this.tabLength) + 1) * this.tabLength + 1;
829
						}
830
					}
814
					break;
831
					break;
815
				case '\r' :
832
				case '\r' :
816
				case '\n' :
833
				case '\n' :
834
					commentColumn = 0;
817
					break;
835
					break;
818
				default:
836
				default:
819
					offset++;
837
					commentColumn++;
820
					break;
838
					break;
821
			}
839
			}
822
		}
840
		}
823
		return offset;
841
		return commentColumn;
824
	}
842
	}
825
843
826
	int getCurrentIndentation(int start) {
844
	int getCurrentColumn(char[] whitespaces) {
845
		int length = whitespaces.length;
846
		if (this.tabLength == 0) return length;
847
		int currentColumn = 1;
848
		for (int i=0; i<length; i++) {
849
			char ch = whitespaces[i];
850
			switch (ch) {
851
				case '\t' :
852
					int reminder = (currentColumn-1) % this.tabLength;
853
					if (reminder == 0) {
854
						currentColumn += this.tabLength;
855
					} else {
856
						currentColumn = ((currentColumn / this.tabLength) + 1) * this.tabLength + 1;
857
					}
858
					break;
859
				case '\r' :
860
				case '\n' :
861
					currentColumn = 0;
862
					break;
863
				default:
864
					currentColumn++;
865
					break;
866
			}
867
		}
868
		return currentColumn;
869
	}
870
871
	int getCurrentColumn(int start) {
827
		int linePtr = Arrays.binarySearch(this.lineEnds, start);
872
		int linePtr = Arrays.binarySearch(this.lineEnds, start);
828
		if (linePtr < 0) {
873
		if (linePtr < 0) {
829
			linePtr = -linePtr - 1;
874
			linePtr = -linePtr - 1;
830
		}
875
		}
831
		int offset = 0;
876
		int currentColumn = 1;
832
		int beginningOfLine = getLineEnd(linePtr)+1;
877
		int beginningOfLine = getLineEnd(linePtr)+1;
833
		if (beginningOfLine == -1) {
878
		if (beginningOfLine == -1) {
834
			beginningOfLine = 0;
879
			beginningOfLine = 0;
Lines 839-857 Link Here
839
			char currentCharacter = source[i];
884
			char currentCharacter = source[i];
840
			switch (currentCharacter) {
885
			switch (currentCharacter) {
841
				case '\t' :
886
				case '\t' :
842
					offset += this.tabLength;
887
					if (this.tabLength != 0) {
888
						int reminder = (currentColumn-1) % this.tabLength;
889
						if (reminder == 0) {
890
							currentColumn += this.tabLength;
891
						} else {
892
							currentColumn = ((currentColumn / this.tabLength) + 1) * this.tabLength + 1;
893
						}
894
					}
843
					break;
895
					break;
844
				case '\r' :
896
				case '\r' :
845
				case '\n' :
897
				case '\n' :
898
					currentColumn = 0;
846
					break;
899
					break;
847
				case ' ':
900
				case ' ':
848
					offset++;
901
					currentColumn++;
849
					break;
902
					break;
850
				default:
903
				default:
851
					return offset;
904
					return currentColumn;
852
			}
905
			}
853
		}
906
		}
854
		return offset;
907
		return currentColumn;
855
	}
908
	}
856
909
857
	public String getEmptyLines(int linesNumber) {
910
	public String getEmptyLines(int linesNumber) {
Lines 1009-1015 Link Here
1009
					StringBuffer buffer = new StringBuffer(getNewLine());
1062
					StringBuffer buffer = new StringBuffer(getNewLine());
1010
					
1063
					
1011
					// Look for current indentation
1064
					// Look for current indentation
1012
					int currentColumn = getCurrentIndentation(this.scanner.currentPosition);
1065
					int currentIndentation = getCurrentColumn(this.scanner.currentPosition) - 1;
1013
					
1066
					
1014
					// Determine whether the alignment indentation can be used or not
1067
					// Determine whether the alignment indentation can be used or not
1015
					// So far, the best algorithm is to use it when
1068
					// So far, the best algorithm is to use it when
Lines 1036-1049 Link Here
1036
					}
1089
					}
1037
					
1090
					
1038
					// Use the current indentation if over the computed indentation
1091
					// Use the current indentation if over the computed indentation
1039
					if (this.indentationLevel < currentColumn) {
1092
					if (this.indentationLevel < currentIndentation) {
1040
						this.indentationLevel = currentColumn;
1093
						this.indentationLevel = currentIndentation;
1041
					}
1094
					}
1042
					
1095
					
1043
					// Debug
1096
					// Debug
1044
					if (DefaultCodeFormatter.DEBUG) {
1097
					if (DefaultCodeFormatter.DEBUG) {
1045
						System.out.println(" - format brace = "+this.formatBrace); //$NON-NLS-1$
1098
						System.out.println(" - format brace = "+this.formatBrace); //$NON-NLS-1$
1046
						System.out.println(" - current column = "+currentColumn); //$NON-NLS-1$
1099
						System.out.println(" - current column = "+(currentIndentation+1)); //$NON-NLS-1$
1047
						System.out.println(" - current position = "+this.scanner.currentPosition); //$NON-NLS-1$
1100
						System.out.println(" - current position = "+this.scanner.currentPosition); //$NON-NLS-1$
1048
						System.out.print(" - current line = "); //$NON-NLS-1$
1101
						System.out.print(" - current line = "); //$NON-NLS-1$
1049
						int linePtr = Arrays.binarySearch(this.lineEnds, this.scanner.currentPosition);
1102
						int linePtr = Arrays.binarySearch(this.lineEnds, this.scanner.currentPosition);
Lines 1387-1393 Link Here
1387
		if ((commentColumn-1) > this.indentationLevel) {
1440
		if ((commentColumn-1) > this.indentationLevel) {
1388
			this.indentationLevel = commentColumn-1;
1441
			this.indentationLevel = commentColumn-1;
1389
		}
1442
		}
1390
		int currentCommentOffset = onFirstColumn ? 0 : getCurrentCommentOffset(start);
1443
		int currentCommentIndentation = onFirstColumn ? 0 : getCurrentCommentColumn(start) - 1;
1391
		boolean formatComment = (isJavadoc && (this.formatComments & CodeFormatter.K_JAVA_DOC) != 0) || (!isJavadoc && (this.formatComments & CodeFormatter.K_MULTI_LINE_COMMENT) != 0);
1444
		boolean formatComment = (isJavadoc && (this.formatComments & CodeFormatter.K_JAVA_DOC) != 0) || (!isJavadoc && (this.formatComments & CodeFormatter.K_MULTI_LINE_COMMENT) != 0);
1392
1445
1393
		try {
1446
		try {
Lines 1441-1459 Link Here
1441
							} else {
1494
							} else {
1442
								if (ScannerHelper.isWhitespace((char) currentCharacter)) {
1495
								if (ScannerHelper.isWhitespace((char) currentCharacter)) {
1443
									int previousStartPosition = this.scanner.currentPosition;
1496
									int previousStartPosition = this.scanner.currentPosition;
1444
									int count = 0;
1497
									int currentIndentation = 0;
1445
									loop: while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) {
1498
									loop: while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) {
1446
										if (count >= currentCommentOffset) {
1499
										if (currentIndentation >= currentCommentIndentation) {
1447
											break loop;
1500
											break loop;
1448
										}
1501
										}
1449
										previousStart = nextCharacterStart;
1502
										previousStart = nextCharacterStart;
1450
										previousStartPosition = this.scanner.currentPosition;
1503
										previousStartPosition = this.scanner.currentPosition;
1451
										switch(currentCharacter) {
1504
										switch(currentCharacter) {
1452
											case '\t' :
1505
											case '\t' :
1453
												count += this.tabLength;
1506
												if (this.tabLength != 0) {
1507
													int reminder = currentIndentation % this.tabLength;
1508
													if (reminder == 0) {
1509
														currentIndentation += this.tabLength;
1510
													} else {
1511
														currentIndentation = ((currentIndentation / this.tabLength) + 1) * this.tabLength;
1512
													}
1513
												}
1454
												break;
1514
												break;
1455
											default :
1515
											default :
1456
												count ++;
1516
												currentIndentation ++;
1457
										}
1517
										}
1458
										currentCharacter = this.scanner.getNextChar();
1518
										currentCharacter = this.scanner.getNextChar();
1459
										nextCharacterStart = this.scanner.currentPosition;
1519
										nextCharacterStart = this.scanner.currentPosition;
Lines 2108-2134 Link Here
2108
						// If following token is a line comment on the same line or the line just after,
2168
						// If following token is a line comment on the same line or the line just after,
2109
						// then it might be not really formatted as a trailing comment
2169
						// then it might be not really formatted as a trailing comment
2110
						boolean realTrailing = trailing > NO_TRAILING_COMMENT;
2170
						boolean realTrailing = trailing > NO_TRAILING_COMMENT;
2111
						if (trailing == IMPORT_TRAILING_COMMENT && this.scanner.currentCharacter == '/' && lines <= 1) {
2171
						if (realTrailing && this.scanner.currentCharacter == '/' && (lines == 0 || (lines == 1 && !hasLineComment && trailing == IMPORT_TRAILING_COMMENT))) {
2112
							int currentPosition = this.scanner.currentPosition;
2172
							// sometimes changing the trailing may not be the best idea
2113
							if (this.scanner.getNextToken() == TerminalTokens.TokenNameCOMMENT_LINE) {
2173
							// for complex trailing comment, it's basically a good idea
2114
								int token = this.scanner.getNextToken();
2174
							boolean canChangeTrailing = (trailing & COMPLEX_TRAILING_COMMENT) != 0;
2115
								while (token == TerminalTokens.TokenNameCOMMENT_LINE) {
2175
							// for basic trailing comment preceded by a line comment, then it depends on the comments relative position
2116
									token = this.scanner.getNextToken();
2176
							// when following comment column (after having been rounded) is below the preceding one,
2177
							// then it becomes not a good idea to change the trailing flag
2178
							if (trailing == BASIC_TRAILING_COMMENT && hasLineComment) {
2179
								int currentCommentColumn = getCurrentColumn(whiteSpaces);
2180
								int lastCommentColumn = this.lastLineComment.currentColumn;
2181
								if (this.tabLength > 0) {
2182
									if ((currentCommentColumn % this.tabLength) == 0) {
2183
										lastCommentColumn = (lastCommentColumn / this.tabLength) * this.tabLength;
2184
									} else {
2185
										currentCommentColumn = ((currentCommentColumn / this.tabLength) + 1) * this.tabLength;
2186
									}
2117
								}
2187
								}
2118
								if (token == TerminalTokens.TokenNameWHITESPACE) {
2188
								canChangeTrailing = currentCommentColumn >= lastCommentColumn;
2119
									char[] secondWhiteSpaces = this.scanner.getCurrentTokenSource();
2189
							}
2120
									loop: for (int i = 0, max = secondWhiteSpaces.length; i < max; i++) {
2190
							// if the trailing can be change, then look at the following tokens
2121
										switch(secondWhiteSpaces[i]) {
2191
							if (canChangeTrailing) {
2122
											case '\r' :
2192
								int currentPosition = this.scanner.currentPosition;
2123
											case '\n' :
2193
								if (this.scanner.getNextToken() == TerminalTokens.TokenNameCOMMENT_LINE) {
2194
									realTrailing = !hasLineComment;
2195
									switch (this.scanner.getNextToken()) {
2196
										case TerminalTokens.TokenNameCOMMENT_LINE:
2197
											// at least two contiguous line comments
2198
											// the formatter should not consider comments as trailing ones
2199
											realTrailing = false;
2200
											break;
2201
										case TerminalTokens.TokenNameWHITESPACE:
2202
											if (this.scanner.getNextToken() == TerminalTokens.TokenNameCOMMENT_LINE) {
2203
												// at least two contiguous line comments
2204
												// the formatter should not consider comments as trailing ones
2124
												realTrailing = false;
2205
												realTrailing = false;
2125
												break loop;
2206
											}
2126
										}
2207
											break;
2127
									}
2208
									}
2128
								}
2209
								}
2210
								this.scanner.resetTo(currentPosition, this.scanner.eofPosition - 1);
2129
							}
2211
							}
2130
							this.scanner.resetTo(currentPosition, this.scanner.eofPosition - 1);
2131
						}
2212
						}
2213
						// Look whether comments line may be contiguous or not
2214
						// Note that when preceding token is a comment line, then only one line
2215
						// is enough to have an empty line as the line end is included in the comment line...
2216
						// If comments are contiguous, store the white spaces to be able to compute the current comment indentation
2217
						if (lines > 1 || (lines == 1 && hasLineComment)) {
2218
							this.lastLineComment.contiguous = false;
2219
						} else {
2220
							this.lastLineComment.leadingSpaces = whiteSpaces;
2221
						}
2222
						// Strategy to consume spaces and eventually leave at this stage
2223
						// depends on the fact that a trailing comment is expected or not
2132
						if (realTrailing) {
2224
						if (realTrailing) {
2133
							// if a line comment is consumed, no other comment can be on the same line after
2225
							// if a line comment is consumed, no other comment can be on the same line after
2134
							if (hasLineComment) {
2226
							if (hasLineComment) {
Lines 2200-2205 Link Here
2200
							this.scanner.resetTo(this.scanner.getCurrentTokenStartPosition(), this.scannerEndPosition - 1);
2292
							this.scanner.resetTo(this.scanner.getCurrentTokenStartPosition(), this.scannerEndPosition - 1);
2201
							return;
2293
							return;
2202
						}
2294
						}
2295
						this.lastLineComment.contiguous = false;
2203
						if (rejectBlockComment) break;
2296
						if (rejectBlockComment) break;
2204
						if (lines >= 1) {
2297
						if (lines >= 1) {
2205
							if (lines > 1) {
2298
							if (lines > 1) {
Lines 2223-2228 Link Here
2223
							this.scanner.resetTo(this.scanner.getCurrentTokenStartPosition(), this.scannerEndPosition - 1);
2316
							this.scanner.resetTo(this.scanner.getCurrentTokenStartPosition(), this.scannerEndPosition - 1);
2224
							return;
2317
							return;
2225
						}
2318
						}
2319
						this.lastLineComment.contiguous = false;
2226
						if (rejectJavadocComment) break;
2320
						if (rejectJavadocComment) break;
2227
						if (lines >= 1) {
2321
						if (lines >= 1) {
2228
							if (lines > 1) {
2322
							if (lines > 1) {
Lines 2246-2251 Link Here
2246
						lines = 0;
2340
						lines = 0;
2247
						break;
2341
						break;
2248
					default :
2342
					default :
2343
						this.lastLineComment.contiguous = false;
2249
						// step back one token
2344
						// step back one token
2250
						this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
2345
						this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
2251
						return;
2346
						return;
Lines 2300-2311 Link Here
2300
    	int start = currentTokenStartPosition;
2395
    	int start = currentTokenStartPosition;
2301
    	int nextCharacterStart = currentTokenStartPosition;
2396
    	int nextCharacterStart = currentTokenStartPosition;
2302
2397
2303
    	if (this.indentationLevel != 0) {
2398
    	// Print comment line indentation
2304
    		if (!this.formatter.preferences.never_indent_line_comments_on_first_column
2399
    	int commentIndentationLevel;
2305
    				|| !isOnFirstColumn(start)) {
2400
    	if (this.indentationLevel == 0) {
2306
    			printIndentationIfNecessary();
2401
    		commentIndentationLevel = this.column - 1;
2402
    	} else {
2403
    		if (this.formatter.preferences.never_indent_line_comments_on_first_column &&
2404
    			isOnFirstColumn(start)) {
2405
	   			commentIndentationLevel = this.column - 1;
2406
    		} else {
2407
    			// Indentation may be specific for contiguous comment
2408
    			// see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=293300
2409
				if (this.lastLineComment.contiguous) {
2410
					// The leading spaces have been set while looping in the printComment(int) method
2411
					int currentCommentIndentation = getCurrentColumn(this.lastLineComment.leadingSpaces);
2412
					// Keep the current comment indentation when over the previous contiguous line comment
2413
					// and the previous comment has not been reindented
2414
					int lastCommentColumn = this.lastLineComment.currentColumn;
2415
					if (this.tabLength > 0) {
2416
						if ((currentCommentIndentation % this.tabLength) == 0) {
2417
							lastCommentColumn = (lastCommentColumn / this.tabLength) * this.tabLength;
2418
						} else {
2419
							currentCommentIndentation = ((currentCommentIndentation / this.tabLength) + 1) * this.tabLength;
2420
						}
2421
					}
2422
					if (currentCommentIndentation >= lastCommentColumn && this.lastLineComment.indentation != this.indentationLevel) {
2423
						int currentIndentationLevel = this.indentationLevel;
2424
						this.indentationLevel = this.lastLineComment.indentation ;
2425
						printIndentationIfNecessary();
2426
						this.indentationLevel = currentIndentationLevel;
2427
			   			commentIndentationLevel = this.lastLineComment.indentation ;
2428
					} else {
2429
						printIndentationIfNecessary();
2430
			   			commentIndentationLevel = this.column - 1;
2431
					}
2432
				} else {
2433
					if (this.currentAlignment != null && this.currentAlignment.name.equals("array_initializer") && //$NON-NLS-1$
2434
						this.indentationLevel < this.currentAlignment.breakIndentationLevel)
2435
					{
2436
						int currentIndentationLevel = this.indentationLevel;
2437
						this.indentationLevel = this.currentAlignment.breakIndentationLevel;
2438
		    			printIndentationIfNecessary();
2439
						this.indentationLevel = currentIndentationLevel;
2440
			   			commentIndentationLevel = this.currentAlignment.breakIndentationLevel;
2441
					} else {
2442
		    			printIndentationIfNecessary();
2443
			   			commentIndentationLevel = this.column - 1;
2444
					}
2445
				}
2307
    		}
2446
    		}
2308
    	}
2447
    	}
2448
    	
2449
    	// Store line comment information
2450
   		this.lastLineComment.contiguous = true;
2451
		this.lastLineComment.currentColumn = getCurrentCommentColumn(currentTokenStartPosition);
2452
		this.lastLineComment.indentation = commentIndentationLevel;
2453
		
2454
		// Add pending space if necessary
2309
    	if (this.pendingSpace) {
2455
    	if (this.pendingSpace) {
2310
    		addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$
2456
    		addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$
2311
    	}
2457
    	}
Lines 3983-3988 Link Here
3983
		this.needSpace = false;
4129
		this.needSpace = false;
3984
		this.pendingSpace = false;
4130
		this.pendingSpace = false;
3985
		this.preserveLineBreakIndentation = false;
4131
		this.preserveLineBreakIndentation = false;
4132
		this.lastLineComment.contiguous = false;
3986
	}
4133
	}
3987
4134
3988
	public void printNextToken(int expectedTokenType){
4135
	public void printNextToken(int expectedTokenType){
(-)src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java (-5 / +1315 lines)
Lines 334-340 Link Here
334
		"\n" + 
334
		"\n" + 
335
		"public class X02 {\n" + 
335
		"public class X02 {\n" + 
336
		"}\n";
336
		"}\n";
337
	formatSource(source);
337
	formatSource(source,
338
		"import java.util.List;\n" + 
339
		"import java.util.Set;\n" + 
340
		"\n" + 
341
		"//import java.util.HashMap;\n" + 
342
		"\n" + 
343
		"public class X02 {\n" + 
344
		"}\n"
345
	);
338
}
346
}
339
public void testBug199265c1() throws JavaModelException {
347
public void testBug199265c1() throws JavaModelException {
340
	String source =
348
	String source =
Lines 379-384 Link Here
379
		"}\n";
387
		"}\n";
380
	formatSource(source);
388
	formatSource(source);
381
}
389
}
390
public void testBug199265d1() throws JavaModelException {
391
	String source =
392
		"import java.util.Set; // trailing comment\n" + 
393
		"// line comment\n" + 
394
		"import java.util.Map; // trailing comment\n" + 
395
		"// line comment\n" + 
396
		"public class X04 {\n" + 
397
		"\n" + 
398
		"}\n";
399
	formatSource(source,
400
		"import java.util.Set; // trailing comment\n" + 
401
		"// line comment\n" + 
402
		"import java.util.Map; // trailing comment\n" + 
403
		"// line comment\n" + 
404
		"\n" + 
405
		"public class X04 {\n" + 
406
		"\n" + 
407
		"}\n"
408
	);
409
}
410
public void testBug199265d2() throws JavaModelException {
411
	String source =
412
		"import java.util.Set; // trailing comment\n" + 
413
		"// line comment\n" + 
414
		"import java.util.Map; // trailing comment\n" + 
415
		"// line comment\n" + 
416
		"\n" + 
417
		"public class X04 {\n" + 
418
		"\n" + 
419
		"}\n";
420
	formatSource(source);
421
}
422
public void testBug199265d3() throws JavaModelException {
423
	String source =
424
		"import java.util.Set; // trailing comment\n" + 
425
		"	// line comment\n" + 
426
		"import java.util.Map; // trailing comment\n" + 
427
		"	// line comment\n" + 
428
		"public class X04 {\n" + 
429
		"\n" + 
430
		"}\n";
431
	formatSource(source,
432
		"import java.util.Set; // trailing comment\n" + 
433
		"// line comment\n" + 
434
		"import java.util.Map; // trailing comment\n" + 
435
		"// line comment\n" + 
436
		"\n" + 
437
		"public class X04 {\n" + 
438
		"\n" + 
439
		"}\n"
440
	);
441
}
382
public void testBug199265_wksp1a() throws JavaModelException {
442
public void testBug199265_wksp1a() throws JavaModelException {
383
	String source =
443
	String source =
384
		"package wksp1;\n" + 
444
		"package wksp1;\n" + 
Lines 837-843 Link Here
837
	String source =
897
	String source =
838
		"package massive;\n" +
898
		"package massive;\n" +
839
		"\n" +
899
		"\n" +
840
		"public class X05b\n" +
900
		"public class X05\n" +
841
		"{\n" +
901
		"{\n" +
842
		"\n" +
902
		"\n" +
843
		"    public void foo() throws NullPointerException {\n" +
903
		"    public void foo() throws NullPointerException {\n" +
Lines 859-865 Link Here
859
	formatSource(source,
919
	formatSource(source,
860
		"package massive;\n" +
920
		"package massive;\n" +
861
		"\n" +
921
		"\n" +
862
		"public class X05b {\n" +
922
		"public class X05 {\n" +
863
		"\n" +
923
		"\n" +
864
		"	public void foo() throws NullPointerException {\n" +
924
		"	public void foo() throws NullPointerException {\n" +
865
		"\n" +
925
		"\n" +
Lines 888-894 Link Here
888
	String source =
948
	String source =
889
		"package massive;\n" +
949
		"package massive;\n" +
890
		"\n" +
950
		"\n" +
891
		"public class X05b\n" +
951
		"public class X05\n" +
892
		"{\n" +
952
		"{\n" +
893
		"\n" +
953
		"\n" +
894
		"    public void foo() throws NullPointerException {\n" +
954
		"    public void foo() throws NullPointerException {\n" +
Lines 910-916 Link Here
910
	formatSource(source,
970
	formatSource(source,
911
		"package massive;\n" +
971
		"package massive;\n" +
912
		"\n" +
972
		"\n" +
913
		"public class X05b\n" +
973
		"public class X05\n" +
914
		"{\n" +
974
		"{\n" +
915
		"\n" +
975
		"\n" +
916
		"	public void foo() throws NullPointerException\n" +
976
		"	public void foo() throws NullPointerException\n" +
Lines 1555-1560 Link Here
1555
}
1615
}
1556
1616
1557
/**
1617
/**
1618
 * @bug 293300: [formatter] The formatter is still unstable in certain circumstances
1619
 * @test Verify that formatting twice a compilation unit does not produce different output
1620
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=293300"
1621
 */
1622
public void testBug293300_wksp1_01() {
1623
	String source = 
1624
		"package wksp1;\n" + 
1625
		"\n" + 
1626
		"public class X01 {\n" + 
1627
		"\n" + 
1628
		"	boolean foo(int test, int value) {\n" + 
1629
		"		// This comment may also be impacted after having been split in several lines. Furthermore, it\'s also important to verify that the algorithm works when the comment is split into several lines. It\'s a common use case that it may works for 1, 2 but not for 3 iterations...\n" + 
1630
		"		if (test == 0) {\n" + 
1631
		"			// skip\n" + 
1632
		"		} else if (Math.sqrt(Math.pow(test, 2)) > 10) // This is the offending comment after having been split into several lines\n" + 
1633
		"			return false;\n" + 
1634
		"		return true;\n" + 
1635
		"	}\n" + 
1636
		"}\n";
1637
	formatSource(source,
1638
		"package wksp1;\n" + 
1639
		"\n" + 
1640
		"public class X01 {\n" + 
1641
		"\n" + 
1642
		"	boolean foo(int test, int value) {\n" + 
1643
		"		// This comment may also be impacted after having been split in several\n" + 
1644
		"		// lines. Furthermore, it\'s also important to verify that the algorithm\n" + 
1645
		"		// works when the comment is split into several lines. It\'s a common use\n" + 
1646
		"		// case that it may works for 1, 2 but not for 3 iterations...\n" + 
1647
		"		if (test == 0) {\n" + 
1648
		"			// skip\n" + 
1649
		"		} else if (Math.sqrt(Math.pow(test, 2)) > 10) // This is the offending\n" + 
1650
		"														// comment after having\n" + 
1651
		"														// been split into\n" + 
1652
		"														// several lines\n" + 
1653
		"			return false;\n" + 
1654
		"		return true;\n" + 
1655
		"	}\n" + 
1656
		"}\n"
1657
	);
1658
}
1659
public void testBug293300_wkps1_02() {
1660
	String source = 
1661
		"package wksp1;\n" + 
1662
		"\n" + 
1663
		"public class X02 {\n" + 
1664
		"	String field;\n" + 
1665
		"	 public X02(String test) {\n" + 
1666
		"		field= test.toLowerCase();\n" + 
1667
		"		try {\n" + 
1668
		"			testWhetherItWorksOrNot(test); // This comment will be split and should not involve instability\n" + 
1669
		"		} catch (Exception e) {\n" + 
1670
		"			return;\n" + 
1671
		"		}\n" + 
1672
		"	 }\n" + 
1673
		"	private void testWhetherItWorksOrNot(String test) {\n" + 
1674
		"	}\n" + 
1675
		"}\n";
1676
	formatSource(source,
1677
		"package wksp1;\n" + 
1678
		"\n" + 
1679
		"public class X02 {\n" + 
1680
		"	String field;\n" + 
1681
		"\n" + 
1682
		"	public X02(String test) {\n" + 
1683
		"		field = test.toLowerCase();\n" + 
1684
		"		try {\n" + 
1685
		"			testWhetherItWorksOrNot(test); // This comment will be split and\n" + 
1686
		"											// should not involve instability\n" + 
1687
		"		} catch (Exception e) {\n" + 
1688
		"			return;\n" + 
1689
		"		}\n" + 
1690
		"	}\n" + 
1691
		"\n" + 
1692
		"	private void testWhetherItWorksOrNot(String test) {\n" + 
1693
		"	}\n" + 
1694
		"}\n"
1695
	);
1696
}
1697
public void testBug293300_wkps1_03() {
1698
	String source = 
1699
		"package wksp1;\n" + 
1700
		"\n" + 
1701
		"public class X03 {\n" + 
1702
		"public static final native int foo(\n" + 
1703
		"	int firstParameter,\n" + 
1704
		"	int secondParameter,\n" + 
1705
		"	int[] param3);        //When a long comment is placed here with at least one line to follow,\n" + 
1706
		"						  //    the second line may be difficult to be formatted correctly\n" + 
1707
		"public static final native int bar();\n" + 
1708
		"\n" + 
1709
		"}\n";
1710
	formatSource(source,
1711
		"package wksp1;\n" + 
1712
		"\n" + 
1713
		"public class X03 {\n" + 
1714
		"	public static final native int foo(int firstParameter, int secondParameter,\n" + 
1715
		"			int[] param3); // When a long comment is placed here with at least\n" + 
1716
		"							// one line to follow,\n" + 
1717
		"							// the second line may be difficult to be formatted\n" + 
1718
		"							// correctly\n" + 
1719
		"\n" + 
1720
		"	public static final native int bar();\n" + 
1721
		"\n" + 
1722
		"}\n"
1723
	);
1724
}
1725
public void testBug293300_wkps1_04() {
1726
	String source = 
1727
		"package wksp1;\n" + 
1728
		"\n" + 
1729
		"interface Y04_____________________________ {\n" + 
1730
		"}\n" + 
1731
		"\n" + 
1732
		"public interface X04 extends Y04_____________________________ { // modifier constant\n" + 
1733
		"	// those constants are depending upon ClassFileConstants (relying that classfiles only use the 16 lower bits)\n" + 
1734
		"	final int AccDefault = 0;\n" + 
1735
		"}\n";
1736
	formatSource(source,
1737
		"package wksp1;\n" + 
1738
		"\n" + 
1739
		"interface Y04_____________________________ {\n" + 
1740
		"}\n" + 
1741
		"\n" + 
1742
		"public interface X04 extends Y04_____________________________ { // modifier\n" + 
1743
		"																// constant\n" + 
1744
		"	// those constants are depending upon ClassFileConstants (relying that\n" + 
1745
		"	// classfiles only use the 16 lower bits)\n" + 
1746
		"	final int AccDefault = 0;\n" + 
1747
		"}\n"
1748
	);
1749
}
1750
public void testBug293300_wksp2_01() {
1751
	String source = 
1752
		"package wksp2;\n" + 
1753
		"\n" + 
1754
		"public class X01 {\n" + 
1755
		"\n" + 
1756
		"	protected String foo(String[] tests) {\n" + 
1757
		"		String result = null;\n" + 
1758
		"		for (int i = 0; i < tests.length; i++) {\n" + 
1759
		"			String test = tests[i];\n" + 
1760
		"			if (test.startsWith(\"test\")) { //$NON-NLS-1$\n" + 
1761
		"				//we got the malformed tree exception here\n" + 
1762
		"				result = test;\n" + 
1763
		"			}\n" + 
1764
		"		}\n" + 
1765
		"		return result;\n" + 
1766
		"	}\n" + 
1767
		"\n" + 
1768
		"}\n";
1769
	formatSource(source,
1770
		"package wksp2;\n" + 
1771
		"\n" + 
1772
		"public class X01 {\n" + 
1773
		"\n" + 
1774
		"	protected String foo(String[] tests) {\n" + 
1775
		"		String result = null;\n" + 
1776
		"		for (int i = 0; i < tests.length; i++) {\n" + 
1777
		"			String test = tests[i];\n" + 
1778
		"			if (test.startsWith(\"test\")) { //$NON-NLS-1$\n" + 
1779
		"				// we got the malformed tree exception here\n" + 
1780
		"				result = test;\n" + 
1781
		"			}\n" + 
1782
		"		}\n" + 
1783
		"		return result;\n" + 
1784
		"	}\n" + 
1785
		"\n" + 
1786
		"}\n"
1787
	);
1788
}
1789
public void testBug293300_wksp2_02() {
1790
	String source = 
1791
		"package wksp2;\n" + 
1792
		"\n" + 
1793
		"public class X02 {\n" + 
1794
		"\n" + 
1795
		"\n" + 
1796
		"	public void foo(int kind) {\n" + 
1797
		"		switch (kind) {\n" + 
1798
		"			case 0 :\n" + 
1799
		"				break;\n" + 
1800
		"			case 1 :\n" + 
1801
		"				//the first formatting looks strange on this already splitted\n" + 
1802
		"				// comment\n" + 
1803
		"				if (true)\n" + 
1804
		"					return;\n" + 
1805
		"			//fall through\n" + 
1806
		"			default:\n" + 
1807
		"				if (kind < 0)\n" + 
1808
		"					return;\n" + 
1809
		"				break;\n" + 
1810
		"		}\n" + 
1811
		"	}\n" + 
1812
		"\n" + 
1813
		"}\n";
1814
	formatSource(source,
1815
		"package wksp2;\n" + 
1816
		"\n" + 
1817
		"public class X02 {\n" + 
1818
		"\n" + 
1819
		"	public void foo(int kind) {\n" + 
1820
		"		switch (kind) {\n" + 
1821
		"		case 0:\n" + 
1822
		"			break;\n" + 
1823
		"		case 1:\n" + 
1824
		"			// the first formatting looks strange on this already splitted\n" + 
1825
		"			// comment\n" + 
1826
		"			if (true)\n" + 
1827
		"				return;\n" + 
1828
		"			// fall through\n" + 
1829
		"		default:\n" + 
1830
		"			if (kind < 0)\n" + 
1831
		"				return;\n" + 
1832
		"			break;\n" + 
1833
		"		}\n" + 
1834
		"	}\n" + 
1835
		"\n" + 
1836
		"}\n"
1837
	);
1838
}
1839
public void testBug293300_wksp2_03() {
1840
	String source = 
1841
		"package wksp2;\n" + 
1842
		"\n" + 
1843
		"public class X03 {\n" + 
1844
		"	public byte[] foo(byte value) {\n" + 
1845
		"		byte[] result = new byte[10];\n" + 
1846
		"		int valTest = 0;\n" + 
1847
		"		switch (value) {\n" + 
1848
		"			case 1 :\n" + 
1849
		"				for (int j = 10; j >= 0; j--) {\n" + 
1850
		"					result[j] = (byte) (valTest & 0xff); // Bottom 8\n" + 
1851
		"					// bits\n" + 
1852
		"					valTest = valTest >>> 2;\n" + 
1853
		"				}\n" + 
1854
		"				break;\n" + 
1855
		"		}\n" + 
1856
		"		return result;\n" + 
1857
		"	}\n" + 
1858
		"}\n";
1859
	formatSource(source,
1860
		"package wksp2;\n" + 
1861
		"\n" + 
1862
		"public class X03 {\n" + 
1863
		"	public byte[] foo(byte value) {\n" + 
1864
		"		byte[] result = new byte[10];\n" + 
1865
		"		int valTest = 0;\n" + 
1866
		"		switch (value) {\n" + 
1867
		"		case 1:\n" + 
1868
		"			for (int j = 10; j >= 0; j--) {\n" + 
1869
		"				result[j] = (byte) (valTest & 0xff); // Bottom 8\n" + 
1870
		"				// bits\n" + 
1871
		"				valTest = valTest >>> 2;\n" + 
1872
		"			}\n" + 
1873
		"			break;\n" + 
1874
		"		}\n" + 
1875
		"		return result;\n" + 
1876
		"	}\n" + 
1877
		"}\n"
1878
	);
1879
}
1880
public void testBug293300_wksp2_04() {
1881
	String source = 
1882
		"package wksp2;\n" + 
1883
		"\n" + 
1884
		"public class X04 {\n" + 
1885
		"\n" + 
1886
		"	void foo() {\n" + 
1887
		"		int lastDiagonal[]= new int[1000000 + 1]; // this line comments configuration\n" + 
1888
		"		// may screw up the formatter to know which one\n" + 
1889
		"		int origin= 1000000 / 2; // needs to stay at its current indentation or not\n" + 
1890
		"	}\n" + 
1891
		"}\n";
1892
	formatSource(source,
1893
		"package wksp2;\n" + 
1894
		"\n" + 
1895
		"public class X04 {\n" + 
1896
		"\n" + 
1897
		"	void foo() {\n" + 
1898
		"		int lastDiagonal[] = new int[1000000 + 1]; // this line comments\n" + 
1899
		"													// configuration\n" + 
1900
		"		// may screw up the formatter to know which one\n" + 
1901
		"		int origin = 1000000 / 2; // needs to stay at its current indentation or\n" + 
1902
		"									// not\n" + 
1903
		"	}\n" + 
1904
		"}\n"
1905
	);
1906
}
1907
private static final String EXPECTED_OUTPUT_WKSP2E1 =
1908
	"package wksp2;\n" + 
1909
	"\n" + 
1910
	"public class X05 {\n" + 
1911
	"	void foo(int val) {\n" + 
1912
	"		try {\n" + 
1913
	"			loop: for (int i = 0; i < 10; i++) {\n" + 
1914
	"				switch (val) {\n" + 
1915
	"				case 1:\n" + 
1916
	"					if (i == 0) {\n" + 
1917
	"						if (true) {\n" + 
1918
	"							val++;\n" + 
1919
	"						} // these comments\n" + 
1920
	"							// may be wrongly\n" + 
1921
	"							// realigned\n" + 
1922
	"							// by the formatter\n" + 
1923
	"\n" + 
1924
	"						// other comment\n" + 
1925
	"						val--;\n" + 
1926
	"						continue loop;\n" + 
1927
	"					}\n" + 
1928
	"				default:\n" + 
1929
	"					throw new IllegalArgumentException();\n" + 
1930
	"				}\n" + 
1931
	"			}\n" + 
1932
	"		} finally {\n" + 
1933
	"		}\n" + 
1934
	"	}\n" + 
1935
	"}\n";
1936
public void testBug293300_wksp2_05() {
1937
	String source = 
1938
		"package wksp2;\n" + 
1939
		"\n" + 
1940
		"public class X05 {\n" + 
1941
		"	void foo(int val) {\n" + 
1942
		"		try {\n" + 
1943
		"			loop: for (int i=0; i<10; i++) {\n" + 
1944
		"				switch (val) {\n" + 
1945
		"					case 1 :\n" + 
1946
		"						if (i==0) {\n" + 
1947
		"							if (true) {\n" + 
1948
		"								val++;\n" + 
1949
		"							} //these comments\n" + 
1950
		"							// may be wrongly\n" + 
1951
		"							// realigned\n" + 
1952
		"							// by the formatter\n" + 
1953
		"\n" + 
1954
		"							// other comment\n" + 
1955
		"							val--;\n" + 
1956
		"							continue loop;\n" + 
1957
		"						}\n" + 
1958
		"					default :\n" + 
1959
		"						throw new IllegalArgumentException();\n" + 
1960
		"				}\n" + 
1961
		"			}\n" + 
1962
		"		}\n" + 
1963
		"		finally {\n" + 
1964
		"		}\n" + 
1965
		"	}\n" + 
1966
		"}\n";
1967
	formatSource(source, EXPECTED_OUTPUT_WKSP2E1);
1968
}
1969
public void testBug293300_wksp2_05b() {
1970
	String source = 
1971
		"package wksp2;\n" + 
1972
		"\n" + 
1973
		"public class X05 {\n" + 
1974
		"	void foo(int val) {\n" + 
1975
		"		try {\n" + 
1976
		"			loop: for (int i=0; i<10; i++) {\n" + 
1977
		"				switch (val) {\n" + 
1978
		"					case 1 :\n" + 
1979
		"						if (i==0) {\n" + 
1980
		"							if (true) {\n" + 
1981
		"								val++;\n" + 
1982
		"							} //these comments\n" + 
1983
		"							 // may be wrongly\n" + 
1984
		"							 // realigned\n" + 
1985
		"							 // by the formatter\n" + 
1986
		"\n" + 
1987
		"							// other comment\n" + 
1988
		"							val--;\n" + 
1989
		"							continue loop;\n" + 
1990
		"						}\n" + 
1991
		"					default :\n" + 
1992
		"						throw new IllegalArgumentException();\n" + 
1993
		"				}\n" + 
1994
		"			}\n" + 
1995
		"		}\n" + 
1996
		"		finally {\n" + 
1997
		"		}\n" + 
1998
		"	}\n" + 
1999
		"}\n";
2000
	formatSource(source, EXPECTED_OUTPUT_WKSP2E1);
2001
}
2002
private static final String EXPECTED_OUTPUT_WKSP2E3 =
2003
	"package wksp2;\n" + 
2004
	"\n" + 
2005
	"public class X05 {\n" + 
2006
	"	void foo(int val) {\n" + 
2007
	"		try {\n" + 
2008
	"			loop: for (int i = 0; i < 10; i++) {\n" + 
2009
	"				switch (val) {\n" + 
2010
	"				case 1:\n" + 
2011
	"					if (i == 0) {\n" + 
2012
	"						if (true) {\n" + 
2013
	"							val++;\n" + 
2014
	"						} // these comments\n" + 
2015
	"							// may be wrongly\n" + 
2016
	"							// realigned\n" + 
2017
	"							// by the formatter\n" + 
2018
	"\n" + 
2019
	"						// other comment\n" + 
2020
	"						val--;\n" + 
2021
	"						continue loop;\n" + 
2022
	"					}\n" + 
2023
	"				default:\n" + 
2024
	"					throw new IllegalArgumentException();\n" + 
2025
	"				}\n" + 
2026
	"			}\n" + 
2027
	"		} finally {\n" + 
2028
	"		}\n" + 
2029
	"	}\n" + 
2030
	"}\n";
2031
public void testBug293300_wksp2_05c() {
2032
	String source = 
2033
		"package wksp2;\n" + 
2034
		"\n" + 
2035
		"public class X05 {\n" + 
2036
		"	void foo(int val) {\n" + 
2037
		"		try {\n" + 
2038
		"			loop: for (int i=0; i<10; i++) {\n" + 
2039
		"				switch (val) {\n" + 
2040
		"					case 1 :\n" + 
2041
		"						if (i==0) {\n" + 
2042
		"							if (true) {\n" + 
2043
		"								val++;\n" + 
2044
		"							} //these comments\n" + 
2045
		"							  // may be wrongly\n" + 
2046
		"							  // realigned\n" + 
2047
		"							  // by the formatter\n" + 
2048
		"\n" + 
2049
		"							// other comment\n" + 
2050
		"							val--;\n" + 
2051
		"							continue loop;\n" + 
2052
		"						}\n" + 
2053
		"					default :\n" + 
2054
		"						throw new IllegalArgumentException();\n" + 
2055
		"				}\n" + 
2056
		"			}\n" + 
2057
		"		}\n" + 
2058
		"		finally {\n" + 
2059
		"		}\n" + 
2060
		"	}\n" + 
2061
		"}\n";
2062
	formatSource(source, EXPECTED_OUTPUT_WKSP2E3);
2063
}
2064
public void testBug293300_wksp2_05d() {
2065
	String source = 
2066
		"package wksp2;\n" + 
2067
		"\n" + 
2068
		"public class X05 {\n" + 
2069
		"	void foo(int val) {\n" + 
2070
		"		try {\n" + 
2071
		"			loop: for (int i=0; i<10; i++) {\n" + 
2072
		"				switch (val) {\n" + 
2073
		"					case 1 :\n" + 
2074
		"						if (i==0) {\n" + 
2075
		"							if (true) {\n" + 
2076
		"								val++;\n" + 
2077
		"							} //these comments\n" + 
2078
		"							   // may be wrongly\n" + 
2079
		"							   // realigned\n" + 
2080
		"							   // by the formatter\n" + 
2081
		"\n" + 
2082
		"							// other comment\n" + 
2083
		"							val--;\n" + 
2084
		"							continue loop;\n" + 
2085
		"						}\n" + 
2086
		"					default :\n" + 
2087
		"						throw new IllegalArgumentException();\n" + 
2088
		"				}\n" + 
2089
		"			}\n" + 
2090
		"		}\n" + 
2091
		"		finally {\n" + 
2092
		"		}\n" + 
2093
		"	}\n" + 
2094
		"}\n";
2095
	formatSource(source, EXPECTED_OUTPUT_WKSP2E3);
2096
}
2097
public void testBug293300_wksp2_05e() {
2098
	String source = 
2099
		"package wksp2;\n" + 
2100
		"\n" + 
2101
		"public class X05 {\n" + 
2102
		"	void foo(int val) {\n" + 
2103
		"		try {\n" + 
2104
		"			loop: for (int i=0; i<10; i++) {\n" + 
2105
		"				switch (val) {\n" + 
2106
		"					case 1 :\n" + 
2107
		"						if (i==0) {\n" + 
2108
		"							if (true) {\n" + 
2109
		"								val++;\n" + 
2110
		"							} //these comments\n" + 
2111
		"								// may be wrongly\n" + 
2112
		"								// realigned\n" + 
2113
		"								// by the formatter\n" + 
2114
		"\n" + 
2115
		"							// other comment\n" + 
2116
		"							val--;\n" + 
2117
		"							continue loop;\n" + 
2118
		"						}\n" + 
2119
		"					default :\n" + 
2120
		"						throw new IllegalArgumentException();\n" + 
2121
		"				}\n" + 
2122
		"			}\n" + 
2123
		"		}\n" + 
2124
		"		finally {\n" + 
2125
		"		}\n" + 
2126
		"	}\n" + 
2127
		"}\n";
2128
	formatSource(source, EXPECTED_OUTPUT_WKSP2E3);
2129
}
2130
private static final String EXPECTED_OUTPUT_WKSP2E1_SPACES =
2131
	"package wksp2;\n" + 
2132
	"\n" + 
2133
	"public class X05 {\n" + 
2134
	"    void foo(int val) {\n" + 
2135
	"        try {\n" + 
2136
	"            loop: for (int i = 0; i < 10; i++) {\n" + 
2137
	"                switch (val) {\n" + 
2138
	"                case 1:\n" + 
2139
	"                    if (i == 0) {\n" + 
2140
	"                        if (true) {\n" + 
2141
	"                            val++;\n" + 
2142
	"                        } // these comments\n" + 
2143
	"                          // may be wrongly\n" + 
2144
	"                          // realigned\n" + 
2145
	"                          // by the formatter\n" + 
2146
	"\n" + 
2147
	"                        // other comment\n" + 
2148
	"                        val--;\n" + 
2149
	"                        continue loop;\n" + 
2150
	"                    }\n" + 
2151
	"                default:\n" + 
2152
	"                    throw new IllegalArgumentException();\n" + 
2153
	"                }\n" + 
2154
	"            }\n" + 
2155
	"        } finally {\n" + 
2156
	"        }\n" + 
2157
	"    }\n" + 
2158
	"}\n";
2159
public void testBug293300_wksp2_05_spaces() {
2160
	this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE;
2161
	String source = 
2162
		"package wksp2;\n" + 
2163
		"\n" + 
2164
		"public class X05 {\n" + 
2165
		"	void foo(int val) {\n" + 
2166
		"		try {\n" + 
2167
		"			loop: for (int i=0; i<10; i++) {\n" + 
2168
		"				switch (val) {\n" + 
2169
		"					case 1 :\n" + 
2170
		"						if (i==0) {\n" + 
2171
		"							if (true) {\n" + 
2172
		"								val++;\n" + 
2173
		"							} //these comments\n" + 
2174
		"							// may be wrongly\n" + 
2175
		"							// realigned\n" + 
2176
		"							// by the formatter\n" + 
2177
		"\n" + 
2178
		"							// other comment\n" + 
2179
		"							val--;\n" + 
2180
		"							continue loop;\n" + 
2181
		"						}\n" + 
2182
		"					default :\n" + 
2183
		"						throw new IllegalArgumentException();\n" + 
2184
		"				}\n" + 
2185
		"			}\n" + 
2186
		"		}\n" + 
2187
		"		finally {\n" + 
2188
		"		}\n" + 
2189
		"	}\n" + 
2190
		"}\n";
2191
	formatSource(source, EXPECTED_OUTPUT_WKSP2E1_SPACES);
2192
}
2193
public void testBug293300_wksp2_05b_spaces() {
2194
	this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE;
2195
	String source = 
2196
		"package wksp2;\n" + 
2197
		"\n" + 
2198
		"public class X05 {\n" + 
2199
		"	void foo(int val) {\n" + 
2200
		"		try {\n" + 
2201
		"			loop: for (int i=0; i<10; i++) {\n" + 
2202
		"				switch (val) {\n" + 
2203
		"					case 1 :\n" + 
2204
		"						if (i==0) {\n" + 
2205
		"							if (true) {\n" + 
2206
		"								val++;\n" + 
2207
		"							} //these comments\n" + 
2208
		"							 // may be wrongly\n" + 
2209
		"							 // realigned\n" + 
2210
		"							 // by the formatter\n" + 
2211
		"\n" + 
2212
		"							// other comment\n" + 
2213
		"							val--;\n" + 
2214
		"							continue loop;\n" + 
2215
		"						}\n" + 
2216
		"					default :\n" + 
2217
		"						throw new IllegalArgumentException();\n" + 
2218
		"				}\n" + 
2219
		"			}\n" + 
2220
		"		}\n" + 
2221
		"		finally {\n" + 
2222
		"		}\n" + 
2223
		"	}\n" + 
2224
		"}\n";
2225
	formatSource(source, EXPECTED_OUTPUT_WKSP2E1_SPACES);
2226
}
2227
private static final String EXPECTED_OUTPUT_WKSP2E3_SPACES =
2228
	"package wksp2;\n" + 
2229
	"\n" + 
2230
	"public class X05 {\n" + 
2231
	"    void foo(int val) {\n" + 
2232
	"        try {\n" + 
2233
	"            loop: for (int i = 0; i < 10; i++) {\n" + 
2234
	"                switch (val) {\n" + 
2235
	"                case 1:\n" + 
2236
	"                    if (i == 0) {\n" + 
2237
	"                        if (true) {\n" + 
2238
	"                            val++;\n" + 
2239
	"                        } // these comments\n" + 
2240
	"                          // may be wrongly\n" + 
2241
	"                          // realigned\n" + 
2242
	"                          // by the formatter\n" + 
2243
	"\n" + 
2244
	"                        // other comment\n" + 
2245
	"                        val--;\n" + 
2246
	"                        continue loop;\n" + 
2247
	"                    }\n" + 
2248
	"                default:\n" + 
2249
	"                    throw new IllegalArgumentException();\n" + 
2250
	"                }\n" + 
2251
	"            }\n" + 
2252
	"        } finally {\n" + 
2253
	"        }\n" + 
2254
	"    }\n" + 
2255
	"}\n";
2256
public void testBug293300_wksp2_05c_spaces() {
2257
	this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE;
2258
	String source = 
2259
		"package wksp2;\n" + 
2260
		"\n" + 
2261
		"public class X05 {\n" + 
2262
		"	void foo(int val) {\n" + 
2263
		"		try {\n" + 
2264
		"			loop: for (int i=0; i<10; i++) {\n" + 
2265
		"				switch (val) {\n" + 
2266
		"					case 1 :\n" + 
2267
		"						if (i==0) {\n" + 
2268
		"							if (true) {\n" + 
2269
		"								val++;\n" + 
2270
		"							} //these comments\n" + 
2271
		"							  // may be wrongly\n" + 
2272
		"							  // realigned\n" + 
2273
		"							  // by the formatter\n" + 
2274
		"\n" + 
2275
		"							// other comment\n" + 
2276
		"							val--;\n" + 
2277
		"							continue loop;\n" + 
2278
		"						}\n" + 
2279
		"					default :\n" + 
2280
		"						throw new IllegalArgumentException();\n" + 
2281
		"				}\n" + 
2282
		"			}\n" + 
2283
		"		}\n" + 
2284
		"		finally {\n" + 
2285
		"		}\n" + 
2286
		"	}\n" + 
2287
		"}\n";
2288
	formatSource(source, EXPECTED_OUTPUT_WKSP2E3_SPACES);
2289
}
2290
public void testBug293300_wksp2_05d_spaces() {
2291
	this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE;
2292
	String source = 
2293
		"package wksp2;\n" + 
2294
		"\n" + 
2295
		"public class X05 {\n" + 
2296
		"	void foo(int val) {\n" + 
2297
		"		try {\n" + 
2298
		"			loop: for (int i=0; i<10; i++) {\n" + 
2299
		"				switch (val) {\n" + 
2300
		"					case 1 :\n" + 
2301
		"						if (i==0) {\n" + 
2302
		"							if (true) {\n" + 
2303
		"								val++;\n" + 
2304
		"							} //these comments\n" + 
2305
		"							   // may be wrongly\n" + 
2306
		"							   // realigned\n" + 
2307
		"							   // by the formatter\n" + 
2308
		"\n" + 
2309
		"							// other comment\n" + 
2310
		"							val--;\n" + 
2311
		"							continue loop;\n" + 
2312
		"						}\n" + 
2313
		"					default :\n" + 
2314
		"						throw new IllegalArgumentException();\n" + 
2315
		"				}\n" + 
2316
		"			}\n" + 
2317
		"		}\n" + 
2318
		"		finally {\n" + 
2319
		"		}\n" + 
2320
		"	}\n" + 
2321
		"}\n";
2322
	formatSource(source, EXPECTED_OUTPUT_WKSP2E3_SPACES);
2323
}
2324
public void testBug293300_wksp2_05e_spaces() {
2325
	this.formatterPrefs.tab_char = DefaultCodeFormatterOptions.SPACE;
2326
	String source = 
2327
		"package wksp2;\n" + 
2328
		"\n" + 
2329
		"public class X05 {\n" + 
2330
		"	void foo(int val) {\n" + 
2331
		"		try {\n" + 
2332
		"			loop: for (int i=0; i<10; i++) {\n" + 
2333
		"				switch (val) {\n" + 
2334
		"					case 1 :\n" + 
2335
		"						if (i==0) {\n" + 
2336
		"							if (true) {\n" + 
2337
		"								val++;\n" + 
2338
		"							} //these comments\n" + 
2339
		"								// may be wrongly\n" + 
2340
		"								// realigned\n" + 
2341
		"								// by the formatter\n" + 
2342
		"\n" + 
2343
		"							// other comment\n" + 
2344
		"							val--;\n" + 
2345
		"							continue loop;\n" + 
2346
		"						}\n" + 
2347
		"					default :\n" + 
2348
		"						throw new IllegalArgumentException();\n" + 
2349
		"				}\n" + 
2350
		"			}\n" + 
2351
		"		}\n" + 
2352
		"		finally {\n" + 
2353
		"		}\n" + 
2354
		"	}\n" + 
2355
		"}\n";
2356
	formatSource(source, EXPECTED_OUTPUT_WKSP2E3_SPACES);
2357
}
2358
public void testBug293300_wksp_06() {
2359
	String source = 
2360
		"package wksp2;\n" + 
2361
		"\n" + 
2362
		"public class X06 {\n" + 
2363
		"public static final native int foo(\n" + 
2364
		"	String field,        //First field\n" + 
2365
		"	int[] array);        //This comment may cause trouble for the formatter, especially if there\'s another\n" + 
2366
		"						  //    line below  \n" + 
2367
		"public static final native int bar();\n" + 
2368
		"}\n";
2369
	formatSource(source,
2370
		"package wksp2;\n" + 
2371
		"\n" + 
2372
		"public class X06 {\n" + 
2373
		"	public static final native int foo(String field, // First field\n" + 
2374
		"			int[] array); // This comment may cause trouble for the formatter,\n" + 
2375
		"							// especially if there\'s another\n" + 
2376
		"							// line below\n" + 
2377
		"\n" + 
2378
		"	public static final native int bar();\n" + 
2379
		"}\n"
2380
	);
2381
}
2382
public void testBug293300_wksp_07() {
2383
	String source = 
2384
		"package wksp2;\n" + 
2385
		"\n" + 
2386
		"public class X07 {\n" + 
2387
		"	void foo(boolean test) {\n" + 
2388
		"		if (test) {\n" + 
2389
		"			while (true) {\n" + 
2390
		"				try {\n" + 
2391
		"					try {\n" + 
2392
		"					} finally {\n" + 
2393
		"						if (true) {\n" + 
2394
		"							try {\n" + 
2395
		"								toString();\n" + 
2396
		"							} catch (Exception e) {\n" + 
2397
		"							} // nothing\n" + 
2398
		"						}\n" + 
2399
		"					} // first comment which does not move\n" + 
2400
		"\n" + 
2401
		"					// second comment which should not move\n" + 
2402
		"					toString();\n" + 
2403
		"				} catch (Exception e) {\n" + 
2404
		"				}\n" + 
2405
		"\n" + 
2406
		"			} // last comment\n" + 
2407
		"\n" + 
2408
		"		}\n" + 
2409
		"\n" + 
2410
		"		return;\n" + 
2411
		"	}\n" + 
2412
		"}\n";
2413
	formatSource(source);
2414
}
2415
public void testBug293300_wksp2_08() {
2416
	String source = 
2417
		"package wksp2;\n" + 
2418
		"\n" + 
2419
		"public class X08 {\n" + 
2420
		"int foo(int x) {\n" + 
2421
		"    while (x < 0) {\n" + 
2422
		"        switch (x) {\n" + 
2423
		"        \n" + 
2424
		"        }\n" + 
2425
		"    } // end while\n" + 
2426
		"\n" + 
2427
		"        // fill in output parameter\n" + 
2428
		"    if(x > 10)\n" + 
2429
		"        x = 1;\n" + 
2430
		"\n" + 
2431
		"        // return the value\n" + 
2432
		"    return x;\n" + 
2433
		"    }\n" + 
2434
		"}\n";
2435
	formatSource(source,
2436
		"package wksp2;\n" + 
2437
		"\n" + 
2438
		"public class X08 {\n" + 
2439
		"	int foo(int x) {\n" + 
2440
		"		while (x < 0) {\n" + 
2441
		"			switch (x) {\n" + 
2442
		"\n" + 
2443
		"			}\n" + 
2444
		"		} // end while\n" + 
2445
		"\n" + 
2446
		"		// fill in output parameter\n" + 
2447
		"		if (x > 10)\n" + 
2448
		"			x = 1;\n" + 
2449
		"\n" + 
2450
		"		// return the value\n" + 
2451
		"		return x;\n" + 
2452
		"	}\n" + 
2453
		"}\n"
2454
	);
2455
}
2456
public void testBug293300_wksp2_08b() {
2457
	String source = 
2458
		"package wksp2;\n" + 
2459
		"\n" + 
2460
		"public class X08 {\n" + 
2461
		"int foo(int x) {\n" + 
2462
		"    while (x < 0) {\n" + 
2463
		"        switch (x) {\n" + 
2464
		"        \n" + 
2465
		"        }\n" + 
2466
		"    } /* end while */\n" + 
2467
		"\n" + 
2468
		"        // fill in output parameter\n" + 
2469
		"    if(x > 10)\n" + 
2470
		"        x = 1;\n" + 
2471
		"\n" + 
2472
		"        // return the value\n" + 
2473
		"    return x;\n" + 
2474
		"    }\n" + 
2475
		"}\n";
2476
	formatSource(source,
2477
		"package wksp2;\n" + 
2478
		"\n" + 
2479
		"public class X08 {\n" + 
2480
		"	int foo(int x) {\n" + 
2481
		"		while (x < 0) {\n" + 
2482
		"			switch (x) {\n" + 
2483
		"\n" + 
2484
		"			}\n" + 
2485
		"		} /* end while */\n" + 
2486
		"\n" + 
2487
		"		// fill in output parameter\n" + 
2488
		"		if (x > 10)\n" + 
2489
		"			x = 1;\n" + 
2490
		"\n" + 
2491
		"		// return the value\n" + 
2492
		"		return x;\n" + 
2493
		"	}\n" + 
2494
		"}\n"
2495
	);
2496
}
2497
public void testBug293300_wksp2_08c() {
2498
	String source = 
2499
		"package wksp2;\n" + 
2500
		"\n" + 
2501
		"public class X08 {\n" + 
2502
		"int foo(int x) {\n" + 
2503
		"    while (x < 0) {\n" + 
2504
		"        switch (x) {\n" + 
2505
		"        \n" + 
2506
		"        }\n" + 
2507
		"    } /** end while */\n" + 
2508
		"\n" + 
2509
		"        // fill in output parameter\n" + 
2510
		"    if(x > 10)\n" + 
2511
		"        x = 1;\n" + 
2512
		"\n" + 
2513
		"        // return the value\n" + 
2514
		"    return x;\n" + 
2515
		"    }\n" + 
2516
		"}\n";
2517
	formatSource(source,
2518
		"package wksp2;\n" + 
2519
		"\n" + 
2520
		"public class X08 {\n" + 
2521
		"	int foo(int x) {\n" + 
2522
		"		while (x < 0) {\n" + 
2523
		"			switch (x) {\n" + 
2524
		"\n" + 
2525
		"			}\n" + 
2526
		"		}\n" + 
2527
		"		/** end while */\n" + 
2528
		"\n" + 
2529
		"		// fill in output parameter\n" + 
2530
		"		if (x > 10)\n" + 
2531
		"			x = 1;\n" + 
2532
		"\n" + 
2533
		"		// return the value\n" + 
2534
		"		return x;\n" + 
2535
		"	}\n" + 
2536
		"}\n"
2537
	);
2538
}
2539
public void testBug293300_wksp2_09() {
2540
	String source = 
2541
		"package wksp2;\n" + 
2542
		"\n" + 
2543
		"public class X09 {\n" + 
2544
		"void foo(int param) {\n" + 
2545
		"        int local = param - 10000; // first comment\n" + 
2546
		"                                    // on several lines\n" + 
2547
		"        // following unrelated comment\n" + 
2548
		"        // also on several lines\n" + 
2549
		"        int value = param + 10000;\n" + 
2550
		"}\n" + 
2551
		"}\n";
2552
	formatSource(source,
2553
		"package wksp2;\n" + 
2554
		"\n" + 
2555
		"public class X09 {\n" + 
2556
		"	void foo(int param) {\n" + 
2557
		"		int local = param - 10000; // first comment\n" + 
2558
		"									// on several lines\n" + 
2559
		"		// following unrelated comment\n" + 
2560
		"		// also on several lines\n" + 
2561
		"		int value = param + 10000;\n" + 
2562
		"	}\n" + 
2563
		"}\n"
2564
	);
2565
}
2566
public void testBug293300_wksp2_10() {
2567
	String source = 
2568
		"package wksp2;\n" + 
2569
		"\n" + 
2570
		"public class X10 {\n" + 
2571
		"\n" + 
2572
		"    private  String           field;          //  Trailing comment of the field\n" + 
2573
		"                                               //  This comment was not well formatted\n" + 
2574
		"                                               //  as an unexpected line was inserted after the first one\n" + 
2575
		"\n" + 
2576
		"    // -------------------------------\n" + 
2577
		"    X10()  {}\n" + 
2578
		"}\n";
2579
	formatSource(source,
2580
		"package wksp2;\n" + 
2581
		"\n" + 
2582
		"public class X10 {\n" + 
2583
		"\n" + 
2584
		"	private String field; // Trailing comment of the field\n" + 
2585
		"							// This comment was not well formatted\n" + 
2586
		"							// as an unexpected line was inserted after the\n" + 
2587
		"							// first one\n" + 
2588
		"\n" + 
2589
		"	// -------------------------------\n" + 
2590
		"	X10() {\n" + 
2591
		"	}\n" + 
2592
		"}\n"
2593
	);
2594
}
2595
public void testBug293300_wksp2_11() {
2596
	String source = 
2597
		"package wksp2;\n" + 
2598
		"\n" + 
2599
		"public abstract class X11 {\n" + 
2600
		"\n" + 
2601
		"    // [NEW] \n" + 
2602
		"    /**\n" + 
2603
		"     * Comment foo\n" + 
2604
		"     */\n" + 
2605
		"    public abstract StringBuffer foo();\n" + 
2606
		"//#if defined(TEST)\n" + 
2607
		"//#else\n" + 
2608
		"//#endif\n" + 
2609
		"\n" + 
2610
		"    // [NEW]\n" + 
2611
		"    /**\n" + 
2612
		"     * Comment foo2\n" + 
2613
		"     */\n" + 
2614
		"    public abstract StringBuffer foo2();\n" + 
2615
		"    // [NEW]\n" + 
2616
		"    /**\n" + 
2617
		"     * Comment foo3\n" + 
2618
		"     */\n" + 
2619
		"    public abstract StringBuffer foo3();\n" + 
2620
		"\n" + 
2621
		"}\n";
2622
	formatSource(source,
2623
		"package wksp2;\n" + 
2624
		"\n" + 
2625
		"public abstract class X11 {\n" + 
2626
		"\n" + 
2627
		"	// [NEW]\n" + 
2628
		"	/**\n" + 
2629
		"	 * Comment foo\n" + 
2630
		"	 */\n" + 
2631
		"	public abstract StringBuffer foo();\n" + 
2632
		"\n" + 
2633
		"	// #if defined(TEST)\n" + 
2634
		"	// #else\n" + 
2635
		"	// #endif\n" + 
2636
		"\n" + 
2637
		"	// [NEW]\n" + 
2638
		"	/**\n" + 
2639
		"	 * Comment foo2\n" + 
2640
		"	 */\n" + 
2641
		"	public abstract StringBuffer foo2();\n" + 
2642
		"\n" + 
2643
		"	// [NEW]\n" + 
2644
		"	/**\n" + 
2645
		"	 * Comment foo3\n" + 
2646
		"	 */\n" + 
2647
		"	public abstract StringBuffer foo3();\n" + 
2648
		"\n" + 
2649
		"}\n"
2650
	);
2651
}
2652
public void testBug293300_wksp2_12a() {
2653
	String source = 
2654
		"package wksp2;\n" + 
2655
		"\n" + 
2656
		"public class X12 {\n" + 
2657
		"\n" + 
2658
		"\n" + 
2659
		"	private boolean sampleField = false;   //trailing comment of the field which\n" + 
2660
		" 	                                      //was wrongly formatted in previous\n" + 
2661
		"	                                      //version as an unexpected empty lines was\n" + 
2662
		"	                                      //inserted after the second comment line...\n" + 
2663
		"\n" + 
2664
		"\n" + 
2665
		"	/**\n" + 
2666
		"	    Javadoc comment\n" + 
2667
		"	*/\n" + 
2668
		"	public X12() {}\n" + 
2669
		"}\n";
2670
	formatSource(source,
2671
		"package wksp2;\n" + 
2672
		"\n" + 
2673
		"public class X12 {\n" + 
2674
		"\n" + 
2675
		"	private boolean sampleField = false; // trailing comment of the field which\n" + 
2676
		"											// was wrongly formatted in previous\n" + 
2677
		"											// version as an unexpected empty\n" + 
2678
		"											// lines was\n" + 
2679
		"											// inserted after the second comment\n" + 
2680
		"											// line...\n" + 
2681
		"\n" + 
2682
		"	/**\n" + 
2683
		"	 * Javadoc comment\n" + 
2684
		"	 */\n" + 
2685
		"	public X12() {\n" + 
2686
		"	}\n" + 
2687
		"}\n"
2688
	);
2689
}
2690
public void testBug293300_wksp2_12b() {
2691
	String source = 
2692
		"package wksp2;\n" + 
2693
		"\n" + 
2694
		"public class X12 {\n" + 
2695
		"\n" + 
2696
		"\n" + 
2697
		"	private boolean sampleField = false;   //trailing comment of the field which\n" + 
2698
		" 	                                       //was wrongly formatted in previous\n" + 
2699
		"	                                       //version as an unexpected empty lines was\n" + 
2700
		"	                                       //inserted after the second comment line...\n" + 
2701
		"\n" + 
2702
		"\n" + 
2703
		"	/**\n" + 
2704
		"	    Javadoc comment\n" + 
2705
		"	*/\n" + 
2706
		"	public X12() {}\n" + 
2707
		"}\n";
2708
	formatSource(source,
2709
		"package wksp2;\n" + 
2710
		"\n" + 
2711
		"public class X12 {\n" + 
2712
		"\n" + 
2713
		"	private boolean sampleField = false; // trailing comment of the field which\n" + 
2714
		"											// was wrongly formatted in previous\n" + 
2715
		"											// version as an unexpected empty\n" + 
2716
		"											// lines was\n" + 
2717
		"											// inserted after the second comment\n" + 
2718
		"											// line...\n" + 
2719
		"\n" + 
2720
		"	/**\n" + 
2721
		"	 * Javadoc comment\n" + 
2722
		"	 */\n" + 
2723
		"	public X12() {\n" + 
2724
		"	}\n" + 
2725
		"}\n"
2726
	);
2727
}
2728
public void testBug293300_wksp2_13() {
2729
	String source = 
2730
		"package wksp2;\n" + 
2731
		"\n" + 
2732
		"public class X13 {\n" + 
2733
		"void foo(int x) {\n" + 
2734
		"	switch (x) {\n" + 
2735
		"		default : // regular object ref\n" + 
2736
		"//				if (compileTimeType.isRawType() && runtimeTimeType.isBoundParameterizedType()) {\n" + 
2737
		"//				    scope.problemReporter().unsafeRawExpression(this, compileTimeType, runtimeTimeType);\n" + 
2738
		"//				}\n" + 
2739
		"	}\n" + 
2740
		"}\n" + 
2741
		"}\n";
2742
	formatSource(source,
2743
		"package wksp2;\n" + 
2744
		"\n" + 
2745
		"public class X13 {\n" + 
2746
		"	void foo(int x) {\n" + 
2747
		"		switch (x) {\n" + 
2748
		"		default: // regular object ref\n" + 
2749
		"			// if (compileTimeType.isRawType() &&\n" + 
2750
		"			// runtimeTimeType.isBoundParameterizedType()) {\n" + 
2751
		"			// scope.problemReporter().unsafeRawExpression(this,\n" + 
2752
		"			// compileTimeType, runtimeTimeType);\n" + 
2753
		"			// }\n" + 
2754
		"		}\n" + 
2755
		"	}\n" + 
2756
		"}\n"
2757
	);
2758
}
2759
public void testBug293300_wksp2_14() {
2760
	String source = 
2761
		"package wksp2;\n" + 
2762
		"\n" + 
2763
		"public interface X14 {\n" + 
2764
		"void foo();\n" + 
2765
		"// line 1\n" + 
2766
		"// line 2\n" + 
2767
		"void bar();\n" + 
2768
		"}\n";
2769
	formatSource(source,
2770
		"package wksp2;\n" + 
2771
		"\n" + 
2772
		"public interface X14 {\n" + 
2773
		"	void foo();\n" + 
2774
		"\n" + 
2775
		"	// line 1\n" + 
2776
		"	// line 2\n" + 
2777
		"	void bar();\n" + 
2778
		"}\n"
2779
	);
2780
}
2781
// TODO (frederic) try to fix the formatter instability in the following test case
2782
public void _testBug293300_wksp2_15a() {
2783
	String source = 
2784
		"package wksp2;\n" + 
2785
		"\n" + 
2786
		"public class X15 {\n" + 
2787
		"	void foo(int[] params) {\n" + 
2788
		"		if (params.length > 0) { // trailing comment formatted in several lines...\n" + 
2789
		"//			int length = params == null ? : 0 params.length; // this commented lined causes troubles for the formatter but only if the comment starts at column 1...\n" + 
2790
		"			for (int i=0; i<params.length; i++) {\n" + 
2791
		"			}\n" + 
2792
		"		}\n" + 
2793
		"	}\n" + 
2794
		"}\n";
2795
	formatSource(source,
2796
		"\n" + 
2797
		"public class X15 {\n" + 
2798
		"	void foo(int[] params) {\n" + 
2799
		"		if (params.length > 0) { // trailing comment formatted in several\n" + 
2800
		"									// lines...\n" + 
2801
		"			// int length = params == null ? : 0 params.length; // this\n" + 
2802
		"			// commented\n" + 
2803
		"			// lined causes troubles for the formatter but only if the comment\n" + 
2804
		"			// starts at column 1...\n" + 
2805
		"			for (int i = 0; i < params.length; i++) {\n" + 
2806
		"			}\n" + 
2807
		"		}\n" + 
2808
		"	}\n" + 
2809
		"}\n"
2810
	);
2811
}
2812
public void testBug293300_wksp2_15b() {
2813
	String source = 
2814
		"package wksp2;\n" + 
2815
		"\n" + 
2816
		"public class X15 {\n" + 
2817
		"	void foo(int[] params) {\n" + 
2818
		"		if (params.length > 0) { // trailing comment formatted in several lines...\n" + 
2819
		"			// int length = params == null ? : 0 params.length; // this commented lined does not cause troubles for the formatter when the comments is not on column 1...\n" + 
2820
		"			for (int i=0; i<params.length; i++) {\n" + 
2821
		"			}\n" + 
2822
		"		}\n" + 
2823
		"	}\n" + 
2824
		"}\n";
2825
	formatSource(source,
2826
		"package wksp2;\n" + 
2827
		"\n" + 
2828
		"public class X15 {\n" + 
2829
		"	void foo(int[] params) {\n" + 
2830
		"		if (params.length > 0) { // trailing comment formatted in several\n" + 
2831
		"									// lines...\n" + 
2832
		"			// int length = params == null ? : 0 params.length; // this\n" + 
2833
		"			// commented lined does not cause troubles for the formatter when\n" + 
2834
		"			// the comments is not on column 1...\n" + 
2835
		"			for (int i = 0; i < params.length; i++) {\n" + 
2836
		"			}\n" + 
2837
		"		}\n" + 
2838
		"	}\n" + 
2839
		"}\n"
2840
	);
2841
}
2842
public void testBug293300_wksp3_01() {
2843
	String source = 
2844
		"package wksp3;\n" + 
2845
		"\n" + 
2846
		"public class X01 {\n" + 
2847
		"static String[] constant = {\n" + 
2848
		"// comment\n" + 
2849
		"\"first\",\n" + 
2850
		"// comment\n" + 
2851
		"\"second\",\n" + 
2852
		"};\n" + 
2853
		"}\n";
2854
	formatSource(source,
2855
		"package wksp3;\n" + 
2856
		"\n" + 
2857
		"public class X01 {\n" + 
2858
		"	static String[] constant = {\n" + 
2859
		"			// comment\n" + 
2860
		"			\"first\",\n" + 
2861
		"			// comment\n" + 
2862
		"			\"second\", };\n" + 
2863
		"}\n"
2864
	);
2865
}
2866
2867
/**
1558
 * @bug 293496:  [formatter] 'insert_space_before_opening_brace_in_array_initializer' preference may be reset in certain circumstances
2868
 * @bug 293496:  [formatter] 'insert_space_before_opening_brace_in_array_initializer' preference may be reset in certain circumstances
1559
 * @test Verify that non ArithmeticException occurs when using tab size = 0
2869
 * @test Verify that non ArithmeticException occurs when using tab size = 0
1560
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=293496"
2870
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=293496"

Return to bug 293300