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

(-)formatter/org/eclipse/jdt/internal/formatter/Scribe.java (-21 / +43 lines)
Lines 2793-2816 Link Here
2793
			    	}
2793
			    	}
2794
    				int lastColumn = this.column + length;
2794
    				int lastColumn = this.column + length;
2795
	    			while (!this.scanner.atEnd()) {
2795
	    			while (!this.scanner.atEnd()) {
2796
	    				int token = this.scanner.getNextToken();
2796
	    				try {
2797
	    				switch (token) {
2797
		    				int token = this.scanner.getNextToken();
2798
	    					case TerminalTokens.TokenNameWHITESPACE:
2798
		    				switch (token) {
2799
	    						if (CharOperation.indexOf('\n', this.scanner.source, this.scanner.startPosition, this.scanner.currentPosition) >= 0) {
2799
		    					case TerminalTokens.TokenNameWHITESPACE:
2800
	    							return newLines;
2800
		    						if (CharOperation.indexOf('\n', this.scanner.source, this.scanner.startPosition, this.scanner.currentPosition) >= 0) {
2801
	    						}
2801
		    							return newLines;
2802
	    						length = 1;
2802
		    						}
2803
	    						break;
2803
		    						length = 1;
2804
	    					case TerminalTokens.TokenNameMULTIPLY:
2804
		    						break;
2805
	    						if (newLine) {
2805
		    					case TerminalTokens.TokenNameMULTIPLY:
2806
	    							newLine = false;
2806
		    						if (newLine) {
2807
	    							continue;
2807
		    							newLine = false;
2808
	    						}
2808
		    							continue;
2809
	    						length = 1;
2809
		    						}
2810
	    						break;
2810
		    						length = 1;
2811
	    					default:
2811
		    						break;
2812
				    			length = (this.scanner.atEnd() ? this.scanner.eofPosition : this.scanner.currentPosition) - this.scanner.startPosition;
2812
		    					default:
2813
	    						break;
2813
					    			length = (this.scanner.atEnd() ? this.scanner.eofPosition : this.scanner.currentPosition) - this.scanner.startPosition;
2814
		    						break;
2815
		    				}
2816
	    				}
2817
	    				catch (InvalidInputException iie) {
2818
	    					// maybe an unterminated string or comment
2819
			    			length = (this.scanner.atEnd() ? this.scanner.eofPosition : this.scanner.currentPosition) - this.scanner.startPosition;
2814
	    				}
2820
	    				}
2815
	    				lastColumn += length;
2821
	    				lastColumn += length;
2816
	    				if (lastColumn > maxColumn) {
2822
	    				if (lastColumn > maxColumn) {
Lines 3008-3014 Link Here
3008
	    			}
3014
	    			}
3009
	    			textLength += (this.scanner.atEnd() ? this.scanner.eofPosition : this.scanner.currentPosition) - this.scanner.startPosition;
3015
	    			textLength += (this.scanner.atEnd() ? this.scanner.eofPosition : this.scanner.currentPosition) - this.scanner.startPosition;
3010
                } catch (InvalidInputException e) {
3016
                } catch (InvalidInputException e) {
3011
	                return textLength;
3017
   					// maybe an unterminated string or comment
3018
	    			textLength += (this.scanner.atEnd() ? this.scanner.eofPosition : this.scanner.currentPosition) - this.scanner.startPosition;
3012
                }
3019
                }
3013
			}
3020
			}
3014
			return textLength;
3021
			return textLength;
Lines 3167-3172 Link Here
3167
			int linePtr = this.scanner.linePtr;
3174
			int linePtr = this.scanner.linePtr;
3168
			int lineCount = 0;
3175
			int lineCount = 0;
3169
			int start = textStartPosition;
3176
			int start = textStartPosition;
3177
			boolean endsOnMultiply = false;
3170
			while (!this.scanner.atEnd()) {
3178
			while (!this.scanner.atEnd()) {
3171
				switch (this.scanner.getNextToken()) {
3179
				switch (this.scanner.getNextToken()) {
3172
					case TerminalTokens.TokenNameMULTIPLY:
3180
					case TerminalTokens.TokenNameMULTIPLY:
Lines 3224-3229 Link Here
3224
						// next start is just after the current token
3232
						// next start is just after the current token
3225
						start = this.scanner.currentPosition;
3233
						start = this.scanner.currentPosition;
3226
						linePtr = this.scanner.linePtr;
3234
						linePtr = this.scanner.linePtr;
3235
						endsOnMultiply = true;
3236
						break;
3237
					default:
3238
						endsOnMultiply = false;
3227
						break;
3239
						break;
3228
				}
3240
				}
3229
			}
3241
			}
Lines 3277-3282 Link Here
3277
					} else {
3289
					} else {
3278
						output.append(buffer);
3290
						output.append(buffer);
3279
					}
3291
					}
3292
					this.needSpace = false;
3293
				} else if (endsOnMultiply) {
3294
					if (output == null) {
3295
						addInsertEdit(textEndPosition+1, " "); //$NON-NLS-1$
3296
					} else {
3297
						output.append(' ');
3298
					}
3299
					this.needSpace = false;
3280
				}
3300
				}
3281
				this.column++;
3301
				this.column++;
3282
			}
3302
			}
Lines 3427-3433 Link Here
3427
				}
3447
				}
3428
			} else {
3448
			} else {
3429
				if (idx > 0 && linesAfter > 0) {
3449
				if (idx > 0 && linesAfter > 0) {
3430
					printJavadocGapLines(previousEnd+1, nextStart, linesAfter, clearBlankLines, false, buffer);
3450
					printJavadocGapLines(previousEnd+1, nextStart-1, linesAfter, clearBlankLines, false, buffer);
3431
					textOnNewLine = true;
3451
					textOnNewLine = true;
3432
				}
3452
				}
3433
				boolean needIndentation = textOnNewLine;
3453
				boolean needIndentation = textOnNewLine;
Lines 3506-3514 Link Here
3506
						// Format gap lines before code
3526
						// Format gap lines before code
3507
						int newLines = linesGap;
3527
						int newLines = linesGap;
3508
						if (newLines == 0) newLines=1;
3528
						if (newLines == 0) newLines=1;
3529
						this.needSpace = needLeadingSpace;
3509
						printJavadocGapLines(end+1, nextStart-1, newLines, false/* clear first blank lines inside <pre> tag as done by old formatter */, false, null);
3530
						printJavadocGapLines(end+1, nextStart-1, newLines, false/* clear first blank lines inside <pre> tag as done by old formatter */, false, null);
3510
						if (needLeadingSpace) {
3531
						if (this.needSpace) {
3511
							addInsertEdit(nextStart, " "); //$NON-NLS-1$
3532
							addInsertEdit(nextStart, " "); //$NON-NLS-1$
3533
							this.needSpace = false;
3512
						}
3534
						}
3513
						// Format the code
3535
						// Format the code
3514
						printCodeSnippet(nextStart, codeEnd);
3536
						printCodeSnippet(nextStart, codeEnd);
(-)src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java (+177 lines)
Lines 1799-1802 Link Here
1799
		"}\n"
1799
		"}\n"
1800
	);
1800
	);
1801
}
1801
}
1802
1803
/**
1804
 * @bug 295175: [formatter] Missing space before a string at the beginning of a javadoc comment
1805
 * @test Verify that space is well inserted before the leading string
1806
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=295175"
1807
 */
1808
public void testBug295175a() {
1809
	String source = 
1810
		"public class X {\n" + 
1811
		"/**\n" + 
1812
		" * <p>\n" + 
1813
		" * \"String\", this string may be not well formatted in certain circumstances,\n" + 
1814
		" * typically after bug 294529 has been fixed...\n" + 
1815
		" */\n" + 
1816
		"void foo() {}\n" + 
1817
		"}\n";
1818
	formatSource(source,
1819
		"public class X {\n" + 
1820
		"	/**\n" + 
1821
		"	 * <p>\n" + 
1822
		"	 * \"String\", this string may be not well formatted in certain circumstances,\n" + 
1823
		"	 * typically after bug 294529 has been fixed...\n" + 
1824
		"	 */\n" + 
1825
		"	void foo() {\n" + 
1826
		"	}\n" + 
1827
		"}\n"
1828
	);
1829
}
1830
public void testBug295175b() {
1831
	String source = 
1832
		"package wksp2;\n" + 
1833
		"\n" + 
1834
		"public interface X {\n" + 
1835
		"\n" + 
1836
		"    /**\n" + 
1837
		"     * <P>\n" + 
1838
		"     * <BR>\n" + 
1839
		"	 *<B>NOTE</B><BR>\n" + 
1840
		"	 * Formatter can miss a space before the previous B tag...\n" + 
1841
		"     **/\n" + 
1842
		"	void foo();\n" + 
1843
		"}\n";
1844
	formatSource(source,
1845
		"package wksp2;\n" + 
1846
		"\n" + 
1847
		"public interface X {\n" + 
1848
		"\n" + 
1849
		"	/**\n" + 
1850
		"	 * <P>\n" + 
1851
		"	 * <BR>\n" + 
1852
		"	 * <B>NOTE</B><BR>\n" + 
1853
		"	 * Formatter can miss a space before the previous B tag...\n" + 
1854
		"	 **/\n" + 
1855
		"	void foo();\n" + 
1856
		"}\n"
1857
	);
1858
}
1859
public void testBug295175c() {
1860
	String source = 
1861
		"package wksp2;\n" + 
1862
		"\n" + 
1863
		"public interface X {\n" + 
1864
		"\n" + 
1865
		"    /**\n" + 
1866
		"     * <P>Following p tag can miss a space before after formatting\n" + 
1867
		"     *<p>\n" + 
1868
		"     * end of comment.\n" + 
1869
		"     **/\n" + 
1870
		"	void foo();\n" + 
1871
		"}\n";
1872
	formatSource(source,
1873
		"package wksp2;\n" + 
1874
		"\n" + 
1875
		"public interface X {\n" + 
1876
		"\n" + 
1877
		"	/**\n" + 
1878
		"	 * <P>\n" + 
1879
		"	 * Following p tag can miss a space before after formatting\n" + 
1880
		"	 * <p>\n" + 
1881
		"	 * end of comment.\n" + 
1882
		"	 **/\n" + 
1883
		"	void foo();\n" + 
1884
		"}\n"
1885
	);
1886
}
1887
public void testBug295175d() {
1888
	String source = 
1889
		"package wksp2;\n" + 
1890
		"\n" + 
1891
		"public interface X {\n" + 
1892
		"\n" + 
1893
		"    /**\n" + 
1894
		"     * <p>Following p tag can miss a space before after formatting\n" + 
1895
		"     *\n" + 
1896
		"     *<p>\n" + 
1897
		"     * <BR>\n" + 
1898
		"	 *<B>NOTE</B><BR>\n" + 
1899
		"	 * Formatter can miss a space before the previous B tag...\n" + 
1900
		"     **/\n" + 
1901
		"	void foo();\n" + 
1902
		"}\n";
1903
	formatSource(source,
1904
		"package wksp2;\n" + 
1905
		"\n" + 
1906
		"public interface X {\n" + 
1907
		"\n" + 
1908
		"	/**\n" + 
1909
		"	 * <p>\n" + 
1910
		"	 * Following p tag can miss a space before after formatting\n" + 
1911
		"	 * \n" + 
1912
		"	 * <p>\n" + 
1913
		"	 * <BR>\n" + 
1914
		"	 * <B>NOTE</B><BR>\n" + 
1915
		"	 * Formatter can miss a space before the previous B tag...\n" + 
1916
		"	 **/\n" + 
1917
		"	void foo();\n" + 
1918
		"}\n"
1919
	);
1920
}
1921
public void testBug295175e() {
1922
	String source = 
1923
		"package wksp3;\n" + 
1924
		"\n" + 
1925
		"public class X01 {\n" + 
1926
		"    /** \n" + 
1927
		"     * In this peculiar config <code>true</code>, the comment is not___ \n" + 
1928
		"     * really well formatted. The problem is that the first_ code tag\n" + 
1929
		"     * here_______ <code>/*</code> and <code>*&#47;</code> go at the end of the previous line\n" + 
1930
		"     * instead of staying on the 3rd one... \n" + 
1931
		"     */\n" + 
1932
		"    void foo() {}\n" + 
1933
		"}\n";
1934
	formatSource(source,
1935
		"package wksp3;\n" + 
1936
		"\n" + 
1937
		"public class X01 {\n" + 
1938
		"	/**\n" + 
1939
		"	 * In this peculiar config <code>true</code>, the comment is not___ really\n" + 
1940
		"	 * well formatted. The problem is that the first_ code tag here_______\n" + 
1941
		"	 * <code>/*</code> and <code>*&#47;</code> go at the end of the previous\n" + 
1942
		"	 * line instead of staying on the 3rd one...\n" + 
1943
		"	 */\n" + 
1944
		"	void foo() {\n" + 
1945
		"	}\n" + 
1946
		"}\n"
1947
	);
1948
}
1949
public void testBug295175f() {
1950
	String source = 
1951
		"package wksp1;\n" + 
1952
		"\n" + 
1953
		"public class X01 {\n" + 
1954
		"\n" + 
1955
		"	/**\n" + 
1956
		"	 * Finds the deepest <code>IJavaElement</code> in the hierarchy of\n" + 
1957
		"	 * <code>elt</elt>'s children (including <code>elt</code> itself)\n" + 
1958
		"	 * which has a source range that encloses <code>position</code>\n" + 
1959
		"	 * according to <code>mapper</code>.\n" + 
1960
		"	 */\n" + 
1961
		"	void foo() {}\n" + 
1962
		"}\n";
1963
	formatSource(source,
1964
		"package wksp1;\n" + 
1965
		"\n" + 
1966
		"public class X01 {\n" + 
1967
		"\n" + 
1968
		"	/**\n" + 
1969
		"	 * Finds the deepest <code>IJavaElement</code> in the hierarchy of\n" + 
1970
		"	 * <code>elt</elt>\'s children (including <code>elt</code> itself) which has\n" + 
1971
		"	 * a source range that encloses <code>position</code> according to\n" + 
1972
		"	 * <code>mapper</code>.\n" + 
1973
		"	 */\n" + 
1974
		"	void foo() {\n" + 
1975
		"	}\n" + 
1976
		"}\n"
1977
	);
1978
}
1802
}
1979
}

Return to bug 295175