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

(-)src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java (+170 lines)
Lines 10568-10571 Link Here
10568
		DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences);
10568
		DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences);
10569
		runTest(codeFormatter, "test719", "A.java", CodeFormatter.K_COMPILATION_UNIT, false);//$NON-NLS-1$ //$NON-NLS-2$
10569
		runTest(codeFormatter, "test719", "A.java", CodeFormatter.K_COMPILATION_UNIT, false);//$NON-NLS-1$ //$NON-NLS-2$
10570
	}
10570
	}
10571
10572
/**
10573
 * @bug 198074: [formatter] the code formatter doesn't respect my new lines
10574
 * @test Ensure that the formatter keep line breaks wrapping set by users in the code
10575
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=198074"
10576
 */
10577
public void testBug198074() throws JavaModelException {
10578
	this.formatterPrefs.preserve_existing_line_breaks = true;
10579
	String source = 
10580
		"public class Test {\n" + 
10581
		"\n" + 
10582
		"	void foo() {\n" + 
10583
		"String x = \"select x \"\n" + 
10584
		"         + \"from y \"\n" + 
10585
		"         + \"where z=a\";\n" + 
10586
		"	}\n" + 
10587
		"}\n";
10588
	formatSource(source,
10589
		"public class Test {\n" + 
10590
		"\n" + 
10591
		"	void foo() {\n" + 
10592
		"		String x = \"select x \"\n" + 
10593
		"				+ \"from y \"\n" + 
10594
		"				+ \"where z=a\";\n" + 
10595
		"	}\n" + 
10596
		"}\n"
10597
	);
10598
}
10599
// another test case put in bug's comment 1
10600
public void testBug198074_c1() throws JavaModelException {
10601
	this.formatterPrefs.preserve_existing_line_breaks = true;
10602
	String source = 
10603
		"public class Test {\n" + 
10604
		"\n" + 
10605
		"	String foo(boolean enabled) {\n" + 
10606
		"if (enabled)\n" + 
10607
		"{\n" + 
10608
		"   // we need x\n" + 
10609
		"   // we need a select\n" + 
10610
		"   return \"select x \"\n" + 
10611
		"   + \"from X\";}\n" + 
10612
		"	return null;}\n" + 
10613
		"}\n";
10614
	formatSource(source,
10615
		"public class Test {\n" + 
10616
		"\n" + 
10617
		"	String foo(boolean enabled) {\n" + 
10618
		"		if (enabled) {\n" + 
10619
		"			// we need x\n" + 
10620
		"			// we need a select\n" + 
10621
		"			return \"select x \"\n" + 
10622
		"					+ \"from X\";\n" + 
10623
		"		}\n" + 
10624
		"		return null;\n" + 
10625
		"	}\n" + 
10626
		"}\n"
10627
	);
10628
}
10629
// another test case put in bug's comment 3
10630
public void testBug198074_c3() throws JavaModelException {
10631
	this.formatterPrefs.preserve_existing_line_breaks = true;
10632
	String source = 
10633
		"public class Test {\n" + 
10634
		"\n" + 
10635
		"public String toString() {\n" + 
10636
		"        return \"YAD01: \"\n" + 
10637
		"        + \" nommbr=\'\"+getName()+\"\'\"\n" + 
10638
		"        + \" nomgrp=\'\"+getService().getArgtbl()+\"\'\"\n" + 
10639
		"        + \" typmbr=\'\"+getMemberType().getArgument()+\"\'\"\n" + 
10640
		"        + \" srcpat=\'\"+getPhysicalPath()+\"\'\"\n" + 
10641
		"        + \" nommdl=\'\"+getModel()+\"\'\"\n" + 
10642
		"        ;\n" + 
10643
		"}\n" + 
10644
		"}\n";
10645
	formatSource(source,
10646
		"public class Test {\n" + 
10647
		"\n" + 
10648
		"	public String toString() {\n" + 
10649
		"		return \"YAD01: \"\n" + 
10650
		"				+ \" nommbr=\'\" + getName() + \"\'\"\n" + 
10651
		"				+ \" nomgrp=\'\" + getService().getArgtbl() + \"\'\"\n" + 
10652
		"				+ \" typmbr=\'\" + getMemberType().getArgument() + \"\'\"\n" + 
10653
		"				+ \" srcpat=\'\" + getPhysicalPath() + \"\'\"\n" + 
10654
		"				+ \" nommdl=\'\" + getModel() + \"\'\";\n" + 
10655
		"	}\n" + 
10656
		"}\n"
10657
	);
10658
}
10659
// duplicate bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=201022
10660
public void testBug201022() throws JavaModelException {
10661
	this.formatterPrefs.preserve_existing_line_breaks = true;
10662
	String source = 
10663
		"public class Test {\n" + 
10664
		"\n" + 
10665
		"	void foo() {\n" + 
10666
		"    String sQuery =\n" + 
10667
		"        \"select * \" +\n" + 
10668
		"        \"from person p, address a \" +\n" + 
10669
		"        \"where p.person_id = a.person_id \" +\n" + 
10670
		"        \"and p.person_id = ?\";\n" + 
10671
		"	}\n" + 
10672
		"}\n";
10673
	formatSource(source,
10674
		"public class Test {\n" + 
10675
		"\n" + 
10676
		"	void foo() {\n" + 
10677
		"		String sQuery =\n" + 
10678
		"				\"select * \" +\n" + 
10679
		"				\"from person p, address a \" +\n" + 
10680
		"				\"where p.person_id = a.person_id \" +\n" + 
10681
		"				\"and p.person_id = ?\";\n" + 
10682
		"	}\n" + 
10683
		"}\n"
10684
	);
10685
}
10686
// duplicate bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541
10687
public void testBug208541() throws JavaModelException {
10688
	this.formatterPrefs.preserve_existing_line_breaks = true;
10689
	String source = 
10690
		"public class MyTest {\n" + 
10691
		"\n" + 
10692
		"    public void testname() throws Exception {\n" + 
10693
		"        int i = 5, j = 6, k = 7;\n" + 
10694
		"        if (new String().length() != 0 &&\n" + 
10695
		"                (i < j && j < k)) {\n" + 
10696
		"\n" + 
10697
		"        }\n" + 
10698
		"    }\n" + 
10699
		"}\n";
10700
	formatSource(source,
10701
		"public class MyTest {\n" + 
10702
		"\n" + 
10703
		"	public void testname() throws Exception {\n" + 
10704
		"		int i = 5, j = 6, k = 7;\n" + 
10705
		"		if (new String().length() != 0 &&\n" + 
10706
		"				(i < j && j < k)) {\n" + 
10707
		"\n" + 
10708
		"		}\n" + 
10709
		"	}\n" + 
10710
		"}\n"
10711
	);
10712
}
10713
// duplicate bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=213700
10714
public void testBug213700() throws JavaModelException {
10715
	this.formatterPrefs.preserve_existing_line_breaks = true;
10716
	String source = 
10717
		"public class Test {\n" + 
10718
		"\n" + 
10719
		"	void foo() {\n" + 
10720
		"		int a=0, b=0, c=0, d=0, e=0, f=0, g=0, h=0, i=0;\n" + 
10721
		"if( (a == b && b == c) &&\n" + 
10722
		"    (d == e) &&\n" + 
10723
		"    (f == g && h == i) \n" + 
10724
		"    ){\n" + 
10725
		"}\n" + 
10726
		"	}\n" + 
10727
		"}\n";
10728
	formatSource(source,
10729
		"public class Test {\n" + 
10730
		"\n" + 
10731
		"	void foo() {\n" + 
10732
		"		int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, i = 0;\n" + 
10733
		"		if ((a == b && b == c) &&\n" + 
10734
		"				(d == e) &&\n" + 
10735
		"				(f == g && h == i)) {\n" + 
10736
		"		}\n" + 
10737
		"	}\n" + 
10738
		"}\n"
10739
	);
10740
}
10571
}
10741
}
(-)formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java (+10 lines)
Lines 3115-3120 Link Here
3115
	public static final String FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE = JavaCore.PLUGIN_ID + ".formatter.number_of_empty_lines_to_preserve";	//$NON-NLS-1$
3115
	public static final String FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE = JavaCore.PLUGIN_ID + ".formatter.number_of_empty_lines_to_preserve";	//$NON-NLS-1$
3116
	/**
3116
	/**
3117
	 * <pre>
3117
	 * <pre>
3118
	 * FORMATTER / Option to specify whether the formatter should preserve existing line breaks or not
3119
	 *     - option id:         "org.eclipse.jdt.core.formatter.preserve_existing_line_breaks"
3120
	 *     - possible values:   { TRUE, FALSE }
3121
	 *     - default:           FALSE
3122
	 * </pre>
3123
	 * @since 3.5
3124
	 */
3125
	public static final String FORMATTER_PRESERVE_EXISTING_LINE_BREAKS = JavaCore.PLUGIN_ID + ".formatter.preserve_existing_line_breaks";	//$NON-NLS-1$
3126
	/**
3127
	 * <pre>
3118
	 * FORMATTER / Option to specify whether or not empty statement should be on a new line
3128
	 * FORMATTER / Option to specify whether or not empty statement should be on a new line
3119
	 *     - option id:         "org.eclipse.jdt.core.formatter.put_empty_statement_on_new_line"
3129
	 *     - option id:         "org.eclipse.jdt.core.formatter.put_empty_statement_on_new_line"
3120
	 *     - possible values:   { TRUE, FALSE }
3130
	 *     - possible values:   { TRUE, FALSE }
(-)formatter/org/eclipse/jdt/internal/formatter/Scribe.java (-8 / +21 lines)
Lines 928-942 Link Here
928
		return indent;
928
		return indent;
929
	}
929
	}
930
930
931
	/*
932
	 * Preserve empty lines depending on given count and preferences.
933
	 */
931
	private String getPreserveEmptyLines(int count) {
934
	private String getPreserveEmptyLines(int count) {
932
		if (count > 0) {
935
		if (count == 0) {
933
			if (this.formatter.preferences.number_of_empty_lines_to_preserve != 0) {
936
			// preserve line breaks in wrapping if specified
934
				int linesToPreserve = Math.min(count, this.formatter.preferences.number_of_empty_lines_to_preserve);
937
			// see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=198074
935
				return getEmptyLines(linesToPreserve);
938
			if (this.currentAlignment != null && this.formatter.preferences.preserve_existing_line_breaks) {
939
				int savedIndentation = this.indentationLevel;
940
				StringBuffer buffer = new StringBuffer(getNewLine());
941
				this.indentationLevel = this.currentAlignment.breakIndentationLevel;
942
				printIndentationIfNecessary(buffer);
943
				this.indentationLevel = savedIndentation;
944
				return buffer.toString();
936
			}
945
			}
937
			return getNewLine();
946
			return Util.EMPTY_STRING;
947
		}
948
		if (this.formatter.preferences.number_of_empty_lines_to_preserve != 0) {
949
			int linesToPreserve = Math.min(count, this.formatter.preferences.number_of_empty_lines_to_preserve);
950
			return getEmptyLines(linesToPreserve);
938
		}
951
		}
939
		return Util.EMPTY_STRING;
952
		return getNewLine();
940
	}
953
	}
941
954
942
	private IRegion getAdaptedRegionAt(int offset) {
955
	private IRegion getAdaptedRegionAt(int offset) {
Lines 1921-1928 Link Here
1921
						} else if (hasLineComment) {
1934
						} else if (hasLineComment) {
1922
							preserveEmptyLines(count, this.scanner.getCurrentTokenStartPosition());
1935
							preserveEmptyLines(count, this.scanner.getCurrentTokenStartPosition());
1923
							addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
1936
							addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
1924
						} else if (count != 0 && this.formatter.preferences.number_of_empty_lines_to_preserve != 0) {
1937
						} else if (count != 0 && (this.formatter.preferences.preserve_existing_line_breaks || this.formatter.preferences.number_of_empty_lines_to_preserve != 0)) {
1925
							addReplaceEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition(), getPreserveEmptyLines(count - 1));
1938
							addReplaceEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition(), getPreserveEmptyLines(count-1));
1926
						} else {
1939
						} else {
1927
							addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
1940
							addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
1928
						}
1941
						}
(-)formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java (+8 lines)
Lines 302-307 Link Here
302
	public boolean never_indent_block_comments_on_first_column;
302
	public boolean never_indent_block_comments_on_first_column;
303
	public boolean never_indent_line_comments_on_first_column;
303
	public boolean never_indent_line_comments_on_first_column;
304
	public int number_of_empty_lines_to_preserve;
304
	public int number_of_empty_lines_to_preserve;
305
	public boolean preserve_existing_line_breaks;
305
	public boolean put_empty_statement_on_new_line;
306
	public boolean put_empty_statement_on_new_line;
306
	public int tab_size;
307
	public int tab_size;
307
	public final char filling_space = ' ';
308
	public final char filling_space = ' ';
Lines 579-584 Link Here
579
		options.put(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_BLOCK_COMMENTS_ON_FIRST_COLUMN, this.never_indent_block_comments_on_first_column ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
580
		options.put(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_BLOCK_COMMENTS_ON_FIRST_COLUMN, this.never_indent_block_comments_on_first_column ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
580
		options.put(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN, this.never_indent_line_comments_on_first_column ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
581
		options.put(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN, this.never_indent_line_comments_on_first_column ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
581
		options.put(DefaultCodeFormatterConstants.FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE, Integer.toString(this.number_of_empty_lines_to_preserve));
582
		options.put(DefaultCodeFormatterConstants.FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE, Integer.toString(this.number_of_empty_lines_to_preserve));
583
		options.put(DefaultCodeFormatterConstants.FORMATTER_PRESERVE_EXISTING_LINE_BREAKS, this.preserve_existing_line_breaks ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
582
		options.put(DefaultCodeFormatterConstants.FORMATTER_PUT_EMPTY_STATEMENT_ON_NEW_LINE, this.put_empty_statement_on_new_line ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
584
		options.put(DefaultCodeFormatterConstants.FORMATTER_PUT_EMPTY_STATEMENT_ON_NEW_LINE, this.put_empty_statement_on_new_line ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
583
		options.put(DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT, Integer.toString(this.page_width));
585
		options.put(DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT, Integer.toString(this.page_width));
584
		switch(this.tab_char) {
586
		switch(this.tab_char) {
Lines 1846-1851 Link Here
1846
				this.number_of_empty_lines_to_preserve = 0;
1848
				this.number_of_empty_lines_to_preserve = 0;
1847
			}
1849
			}
1848
		}
1850
		}
1851
		final Object preserveExistingLineBreaksOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_PRESERVE_EXISTING_LINE_BREAKS);
1852
		if (preserveExistingLineBreaksOption != null) {
1853
			this.preserve_existing_line_breaks = DefaultCodeFormatterConstants.TRUE.equals(preserveExistingLineBreaksOption);
1854
		}
1849
		final Object putEmptyStatementOnNewLineOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_PUT_EMPTY_STATEMENT_ON_NEW_LINE);
1855
		final Object putEmptyStatementOnNewLineOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_PUT_EMPTY_STATEMENT_ON_NEW_LINE);
1850
		if (putEmptyStatementOnNewLineOption != null) {
1856
		if (putEmptyStatementOnNewLineOption != null) {
1851
			this.put_empty_statement_on_new_line = DefaultCodeFormatterConstants.TRUE.equals(putEmptyStatementOnNewLineOption);
1857
			this.put_empty_statement_on_new_line = DefaultCodeFormatterConstants.TRUE.equals(putEmptyStatementOnNewLineOption);
Lines 2185-2190 Link Here
2185
		this.never_indent_block_comments_on_first_column = false;
2191
		this.never_indent_block_comments_on_first_column = false;
2186
		this.never_indent_line_comments_on_first_column = false;
2192
		this.never_indent_line_comments_on_first_column = false;
2187
		this.number_of_empty_lines_to_preserve = 1;
2193
		this.number_of_empty_lines_to_preserve = 1;
2194
		this.preserve_existing_line_breaks = false;
2188
		this.put_empty_statement_on_new_line = false;
2195
		this.put_empty_statement_on_new_line = false;
2189
		this.tab_size = 4;
2196
		this.tab_size = 4;
2190
		this.page_width = 80;
2197
		this.page_width = 80;
Lines 2449-2454 Link Here
2449
		this.never_indent_block_comments_on_first_column = false;
2456
		this.never_indent_block_comments_on_first_column = false;
2450
		this.never_indent_line_comments_on_first_column = false;
2457
		this.never_indent_line_comments_on_first_column = false;
2451
		this.number_of_empty_lines_to_preserve = 1;
2458
		this.number_of_empty_lines_to_preserve = 1;
2459
		this.preserve_existing_line_breaks = false;
2452
		this.put_empty_statement_on_new_line = true;
2460
		this.put_empty_statement_on_new_line = true;
2453
		this.tab_size = 8;
2461
		this.tab_size = 8;
2454
		this.page_width = 80;
2462
		this.page_width = 80;

Return to bug 198074