View | Details | Raw Unified | Return to bug 297225
Collapse All | Expand All

(-)formatter/org/eclipse/jdt/internal/formatter/Location.java (-1 / +2 lines)
Lines 18-23 Link Here
18
public class Location {
18
public class Location {
19
19
20
	public int inputOffset;
20
	public int inputOffset;
21
	/** deprecated */
21
	public int inputColumn;
22
	public int inputColumn;
22
	public int outputLine;
23
	public int outputLine;
23
	public int outputColumn;
24
	public int outputColumn;
Lines 43-49 Link Here
43
		this.outputColumn = scribe.column;
44
		this.outputColumn = scribe.column;
44
		this.outputLine = scribe.line;
45
		this.outputLine = scribe.line;
45
		this.inputOffset = sourceRestart;
46
		this.inputOffset = sourceRestart;
46
		this.inputColumn = scribe.getCurrentColumn(sourceRestart);
47
		this.inputColumn = scribe.getCurrentIndentation(sourceRestart) + 1;
47
		this.outputIndentationLevel = scribe.indentationLevel;
48
		this.outputIndentationLevel = scribe.indentationLevel;
48
		this.lastNumberOfNewLines = scribe.lastNumberOfNewLines;
49
		this.lastNumberOfNewLines = scribe.lastNumberOfNewLines;
49
		this.needSpace = scribe.needSpace;
50
		this.needSpace = scribe.needSpace;
(-)formatter/org/eclipse/jdt/internal/formatter/Scribe.java (-39 / +39 lines)
Lines 117-123 Link Here
117
	// Class to store previous line comment information
117
	// Class to store previous line comment information
118
	class LineComment {
118
	class LineComment {
119
		boolean contiguous = false;
119
		boolean contiguous = false;
120
		int currentColumn, indentation;
120
		int currentIndentation, indentation;
121
		int lines;
121
		int lines;
122
		char[] leadingSpaces;
122
		char[] leadingSpaces;
123
	}
123
	}
Lines 798-806 Link Here
798
		return null;
798
		return null;
799
	}
799
	}
800
800
801
	private int getCurrentCommentColumn(int start) {
801
	private int getCurrentCommentIndentation(int start) {
802
		int linePtr = -Arrays.binarySearch(this.lineEnds, start);
802
		int linePtr = -Arrays.binarySearch(this.lineEnds, start);
803
		int commentColumn = 1;
803
		int indentation = 0;
804
		int beginningOfLine = getLineEnd(linePtr - 1)+1;
804
		int beginningOfLine = getLineEnd(linePtr - 1)+1;
805
		if (beginningOfLine == -1) {
805
		if (beginningOfLine == -1) {
806
			beginningOfLine = 0;
806
			beginningOfLine = 0;
Lines 822-880 Link Here
822
			switch (currentCharacter) {
822
			switch (currentCharacter) {
823
				case '\t' :
823
				case '\t' :
824
					if (this.tabLength != 0) {
824
					if (this.tabLength != 0) {
825
						int reminder = commentColumn % this.tabLength;
825
						int reminder = indentation % this.tabLength;
826
						if (reminder == 0) {
826
						if (reminder == 0) {
827
							commentColumn += this.tabLength;
827
							indentation += this.tabLength;
828
						} else {
828
						} else {
829
							commentColumn = ((commentColumn / this.tabLength) + 1) * this.tabLength + 1;
829
							indentation = ((indentation / this.tabLength) + 1) * this.tabLength;
830
						}
830
						}
831
					}
831
					}
832
					break;
832
					break;
833
				case '\r' :
833
				case '\r' :
834
				case '\n' :
834
				case '\n' :
835
					commentColumn = 0;
835
					indentation = 0;
836
					break;
836
					break;
837
				default:
837
				default:
838
					commentColumn++;
838
					indentation++;
839
					break;
839
					break;
840
			}
840
			}
841
		}
841
		}
842
		return commentColumn;
842
		return indentation;
843
	}
843
	}
844
844
845
	int getCurrentColumn(char[] whitespaces) {
845
	int getCurrentIndentation(char[] whitespaces) {
846
		int length = whitespaces.length;
846
		int length = whitespaces.length;
847
		if (this.tabLength == 0) return length;
847
		if (this.tabLength == 0) return length;
848
		int currentColumn = 1;
848
		int indentation = 0;
849
		for (int i=0; i<length; i++) {
849
		for (int i=0; i<length; i++) {
850
			char ch = whitespaces[i];
850
			char ch = whitespaces[i];
851
			switch (ch) {
851
			switch (ch) {
852
				case '\t' :
852
				case '\t' :
853
					int reminder = (currentColumn-1) % this.tabLength;
853
					int reminder = indentation % this.tabLength;
854
					if (reminder == 0) {
854
					if (reminder == 0) {
855
						currentColumn += this.tabLength;
855
						indentation += this.tabLength;
856
					} else {
856
					} else {
857
						currentColumn = ((currentColumn / this.tabLength) + 1) * this.tabLength + 1;
857
						indentation = ((indentation / this.tabLength) + 1) * this.tabLength;
858
					}
858
					}
859
					break;
859
					break;
860
				case '\r' :
860
				case '\r' :
861
				case '\n' :
861
				case '\n' :
862
					currentColumn = 0;
862
					indentation = 0;
863
					break;
863
					break;
864
				default:
864
				default:
865
					currentColumn++;
865
					indentation++;
866
					break;
866
					break;
867
			}
867
			}
868
		}
868
		}
869
		return currentColumn;
869
		return indentation;
870
	}
870
	}
871
871
872
	int getCurrentColumn(int start) {
872
	int getCurrentIndentation(int start) {
873
		int linePtr = Arrays.binarySearch(this.lineEnds, start);
873
		int linePtr = Arrays.binarySearch(this.lineEnds, start);
874
		if (linePtr < 0) {
874
		if (linePtr < 0) {
875
			linePtr = -linePtr - 1;
875
			linePtr = -linePtr - 1;
876
		}
876
		}
877
		int currentColumn = 1;
877
		int indentation = 0;
878
		int beginningOfLine = getLineEnd(linePtr)+1;
878
		int beginningOfLine = getLineEnd(linePtr)+1;
879
		if (beginningOfLine == -1) {
879
		if (beginningOfLine == -1) {
880
			beginningOfLine = 0;
880
			beginningOfLine = 0;
Lines 886-911 Link Here
886
			switch (currentCharacter) {
886
			switch (currentCharacter) {
887
				case '\t' :
887
				case '\t' :
888
					if (this.tabLength != 0) {
888
					if (this.tabLength != 0) {
889
						int reminder = (currentColumn-1) % this.tabLength;
889
						int reminder = indentation % this.tabLength;
890
						if (reminder == 0) {
890
						if (reminder == 0) {
891
							currentColumn += this.tabLength;
891
							indentation += this.tabLength;
892
						} else {
892
						} else {
893
							currentColumn = ((currentColumn / this.tabLength) + 1) * this.tabLength + 1;
893
							indentation = ((indentation / this.tabLength) + 1) * this.tabLength;
894
						}
894
						}
895
					}
895
					}
896
					break;
896
					break;
897
				case '\r' :
897
				case '\r' :
898
				case '\n' :
898
				case '\n' :
899
					currentColumn = 0;
899
					indentation = 0;
900
					break;
900
					break;
901
				case ' ':
901
				case ' ':
902
					currentColumn++;
902
					indentation++;
903
					break;
903
					break;
904
				default:
904
				default:
905
					return currentColumn;
905
					return indentation;
906
			}
906
			}
907
		}
907
		}
908
		return currentColumn;
908
		return indentation;
909
	}
909
	}
910
910
911
	public String getEmptyLines(int linesNumber) {
911
	public String getEmptyLines(int linesNumber) {
Lines 1063-1069 Link Here
1063
					StringBuffer buffer = new StringBuffer(getNewLine());
1063
					StringBuffer buffer = new StringBuffer(getNewLine());
1064
					
1064
					
1065
					// Look for current indentation
1065
					// Look for current indentation
1066
					int currentIndentation = getCurrentColumn(this.scanner.currentPosition) - 1;
1066
					int currentIndentation = getCurrentIndentation(this.scanner.currentPosition);
1067
					
1067
					
1068
					// Determine whether the alignment indentation can be used or not
1068
					// Determine whether the alignment indentation can be used or not
1069
					// So far, the best algorithm is to use it when
1069
					// So far, the best algorithm is to use it when
Lines 1441-1447 Link Here
1441
		if ((commentColumn-1) > this.indentationLevel) {
1441
		if ((commentColumn-1) > this.indentationLevel) {
1442
			this.indentationLevel = commentColumn-1;
1442
			this.indentationLevel = commentColumn-1;
1443
		}
1443
		}
1444
		int currentCommentIndentation = onFirstColumn ? 0 : getCurrentCommentColumn(start) - 1;
1444
		int currentCommentIndentation = onFirstColumn ? 0 : getCurrentCommentIndentation(start);
1445
		boolean formatComment = (isJavadoc && (this.formatComments & CodeFormatter.K_JAVA_DOC) != 0) || (!isJavadoc && (this.formatComments & CodeFormatter.K_MULTI_LINE_COMMENT) != 0);
1445
		boolean formatComment = (isJavadoc && (this.formatComments & CodeFormatter.K_JAVA_DOC) != 0) || (!isJavadoc && (this.formatComments & CodeFormatter.K_MULTI_LINE_COMMENT) != 0);
1446
1446
1447
		try {
1447
		try {
Lines 2177-2192 Link Here
2177
							// when following comment column (after having been rounded) is below the preceding one,
2177
							// when following comment column (after having been rounded) is below the preceding one,
2178
							// then it becomes not a good idea to change the trailing flag
2178
							// then it becomes not a good idea to change the trailing flag
2179
							if (trailing == BASIC_TRAILING_COMMENT && hasLineComment) {
2179
							if (trailing == BASIC_TRAILING_COMMENT && hasLineComment) {
2180
								int currentCommentColumn = getCurrentColumn(whiteSpaces);
2180
								int currentCommentIndentation = getCurrentIndentation(whiteSpaces);
2181
								int lastCommentColumn = this.lastLineComment.currentColumn;
2181
								int lastCommentIndentation = this.lastLineComment.currentIndentation;
2182
								if (this.tabLength > 0) {
2182
								if (this.tabLength > 0) {
2183
									if ((currentCommentColumn % this.tabLength) == 0) {
2183
									if ((currentCommentIndentation % this.tabLength) == 0) {
2184
										lastCommentColumn = (lastCommentColumn / this.tabLength) * this.tabLength;
2184
										lastCommentIndentation = (lastCommentIndentation / this.tabLength) * this.tabLength;
2185
									} else {
2185
									} else {
2186
										currentCommentColumn = ((currentCommentColumn / this.tabLength) + 1) * this.tabLength;
2186
										currentCommentIndentation = ((currentCommentIndentation / this.tabLength) + 1) * this.tabLength;
2187
									}
2187
									}
2188
								}
2188
								}
2189
								canChangeTrailing = currentCommentColumn >= lastCommentColumn;
2189
								canChangeTrailing = currentCommentIndentation >= lastCommentIndentation;
2190
							}
2190
							}
2191
							// if the trailing can be change, then look at the following tokens
2191
							// if the trailing can be change, then look at the following tokens
2192
							if (canChangeTrailing) {
2192
							if (canChangeTrailing) {
Lines 2409-2426 Link Here
2409
    			// see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=293300
2409
    			// see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=293300
2410
				if (this.lastLineComment.contiguous) {
2410
				if (this.lastLineComment.contiguous) {
2411
					// The leading spaces have been set while looping in the printComment(int) method
2411
					// The leading spaces have been set while looping in the printComment(int) method
2412
					int currentCommentIndentation = getCurrentColumn(this.lastLineComment.leadingSpaces);
2412
					int currentCommentIndentation = getCurrentIndentation(this.lastLineComment.leadingSpaces);
2413
					// Keep the current comment indentation when over the previous contiguous line comment
2413
					// Keep the current comment indentation when over the previous contiguous line comment
2414
					// and the previous comment has not been reindented
2414
					// and the previous comment has not been reindented
2415
					int lastCommentColumn = this.lastLineComment.currentColumn;
2415
					int lastCommentIndentation = this.lastLineComment.currentIndentation;
2416
					if (this.tabLength > 0) {
2416
					if (this.tabLength > 0) {
2417
						if ((currentCommentIndentation % this.tabLength) == 0) {
2417
						if ((currentCommentIndentation % this.tabLength) == 0) {
2418
							lastCommentColumn = (lastCommentColumn / this.tabLength) * this.tabLength;
2418
							lastCommentIndentation = (lastCommentIndentation / this.tabLength) * this.tabLength;
2419
						} else {
2419
						} else {
2420
							currentCommentIndentation = ((currentCommentIndentation / this.tabLength) + 1) * this.tabLength;
2420
							currentCommentIndentation = ((currentCommentIndentation / this.tabLength) + 1) * this.tabLength;
2421
						}
2421
						}
2422
					}
2422
					}
2423
					if (currentCommentIndentation >= lastCommentColumn && this.lastLineComment.indentation != this.indentationLevel) {
2423
					if (currentCommentIndentation >= lastCommentIndentation && this.lastLineComment.indentation != this.indentationLevel) {
2424
						int currentIndentationLevel = this.indentationLevel;
2424
						int currentIndentationLevel = this.indentationLevel;
2425
						this.indentationLevel = this.lastLineComment.indentation ;
2425
						this.indentationLevel = this.lastLineComment.indentation ;
2426
						printIndentationIfNecessary();
2426
						printIndentationIfNecessary();
Lines 2450-2456 Link Here
2450
    	
2450
    	
2451
    	// Store line comment information
2451
    	// Store line comment information
2452
   		this.lastLineComment.contiguous = true;
2452
   		this.lastLineComment.contiguous = true;
2453
		this.lastLineComment.currentColumn = getCurrentCommentColumn(currentTokenStartPosition);
2453
		this.lastLineComment.currentIndentation = getCurrentCommentIndentation(currentTokenStartPosition);
2454
		this.lastLineComment.indentation = commentIndentationLevel;
2454
		this.lastLineComment.indentation = commentIndentationLevel;
2455
		
2455
		
2456
		// Add pending space if necessary
2456
		// Add pending space if necessary
(-)src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java (+30 lines)
Lines 3465-3468 Link Here
3465
		"}\n"
3465
		"}\n"
3466
	);
3466
	);
3467
}
3467
}
3468
3469
/**
3470
 * @bug 297225: [formatter] Indentation may be still wrong in certain circumstances after formatting
3471
 * @test Verify that comment indentation is correct when there's a mix of tab and spaces in
3472
 * 		existing indentation and all comments formatting is off.
3473
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=297225"
3474
 */
3475
public void testBug297225() {
3476
	this.formatterPrefs.comment_format_line_comment = false;
3477
	this.formatterPrefs.comment_format_block_comment = false;
3478
	this.formatterPrefs.comment_format_javadoc_comment = false;
3479
	String source = 
3480
		"public class X01 {\n" + 
3481
		"   	\n" + 
3482
		"   	/**\n" + 
3483
		"   	 * The foo method\n" + 
3484
		"   	 */\n" + 
3485
		"	void foo() {}\n" + 
3486
		"}\n";
3487
	formatSource(source,
3488
		"public class X01 {\n" + 
3489
		"\n" + 
3490
		"	/**\n" + 
3491
		"	 * The foo method\n" + 
3492
		"	 */\n" + 
3493
		"	void foo() {\n" + 
3494
		"	}\n" + 
3495
		"}\n"
3496
	);
3497
}
3468
}
3498
}

Return to bug 297225