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

Collapse All | Expand All

(-)buildnotes_jdt-core.html (+167 lines)
Lines 48-53 Link Here
48
<br>Project org.eclipse.jdt.core v_A38
48
<br>Project org.eclipse.jdt.core v_A38
49
(<a href="http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jdt.core/?only_with_tag=v_A38">cvs</a>).
49
(<a href="http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jdt.core/?only_with_tag=v_A38">cvs</a>).
50
<h2>What's new in this drop</h2>
50
<h2>What's new in this drop</h2>
51
<ul>
52
<li>
53
Added two new preferences to disable and enable the formatter in a section of the code.
54
<p>
55
These new preferences are controlled with the options:</p>
56
<code>DefaultCodeFormatterConstants.FORMATTER_DISABLING_TAGS</code>
57
<code>DefaultCodeFormatterConstants.FORMATTER_ENABLING_TAGS</code>
58
<pre>
59
/**
60
 * FORMATTER / Option to define tags used to disable formatting from the beginning of the comment including one of these tags.
61
 *     - option id:         "org.eclipse.jdt.core.formatter.disabling_tags"
62
 *     - default:           <code>null</code>
63
 * 
64
 * Note that:
65
 * 
66
 * 1. As soon as the formatter encounter a disabling tag, it stops to format the
67
 *    code just after the comment including the tag. If it was already inactive, the
68
 *    tag is simply ignored.
69
 *    Hence, nesting disabling and enabling tags has no specific effect.
70
 *    For example, in the following snippet:
71
 *     class X {
72
 *     // disable-formatter-1
73
 *     void foo1() {}
74
 *     // disable-formatter-2
75
 *     void foo2() {}
76
 *     // enable-formatter-2
77
 *     void bar1() {}
78
 *     // enable-formatter-1
79
 *     void bar2() {}
80
 *     }
81
 * 
82
 *    the second disabling tag and the second enabling tag will be ignored by the
83
 *    formatter, which will format it the same than:
84
 *     class X {
85
 *     // disable-formatter-1
86
 *     void foo1() {}
87
 *     void foo2() {}
88
 *     // enable-formatter-2
89
 *     void bar1() {}
90
 *     void bar2() {}
91
 *     }
92
 * 
93
 * 2. If no enabling tag is found by the formatter after a disabling tag, then the
94
 *    end of the snippet won't be formatted.
95
 *    For example, to disable the formatting of the entire content of a compilation
96
 *    unit, just put a disabling tag at the beginning of the code:
97
 *     // disable-formatter
98
 *     class X {
99
 *     void foo1() {}
100
 *     void foo2() {}
101
 *     void bar1() {}
102
 *     void bar2() {}
103
 *     }
104
 * 
105
 *    In this case, the formatter will not change the content of the class X.
106
 * 
107
 * 3. The tag cannot include new line characters but it can have white spaces.
108
 *    E.g. "<b>format: off</b>" is a valid disabling tag
109
 *
110
 * 4. If a mix of disabling and enabling tags is done in the same comment, then
111
 *    the formatter will take into account the last encountered tag in the comment.
112
 *    For example, in the following snippet, the formatter will be disabled after
113
 *    the comment:
114
 *     class X {
115
 *     &#47;&#42;
116
 *     &#42; This is a comment with a mix of disabling and enabling tags:
117
 *     &#42; disable-formatter-1
118
 *     &#42; enable-formatter-1
119
 *     &#42; disable-formatter-2
120
 *     &#42; The formatter will stop to format after this comment...
121
 *     &#42;&#47;
122
 *     void foo() {}
123
 *     void bar() {}
124
 *     }
125
 * 
126
 * @see #FORMATTER_ENABLING_TAGS
127
 * @since 3.6
128
 */
129
130
/**
131
 * FORMATTER / Option to define tags used to enable formatting after the end of the comment including one of these tags.
132
 *     - option id:         "org.eclipse.jdt.core.formatter.enabling_tags"
133
 *     - default:           <code>null</code>
134
 * 
135
 * Note that:
136
 * 
137
 * 1. As soon as the formatter encounter an enabling tag, it starts to format
138
 *    the code just after the comment including the tag. If it was already active,
139
 *    the tag is simply ignored.
140
 *    Hence, nesting disabling and enabling tags has no specific effect.
141
 *    For example, in the following snippet:
142
 *     class X {
143
 *     // disable-formatter-1
144
 *     void foo1() {}
145
 *     // disable-formatter-2
146
 *     void foo2() {}
147
 *     // enable-formatter-2
148
 *     void bar1() {}
149
 *     // enable-formatter-1
150
 *     void bar2() {}
151
 *     }
152
 * 
153
 *    the second disabling tag and the second enabling tag will be ignored by the
154
 *    formatter, which will format it the same than:
155
 *     class X {
156
 *     // disable-formatter-1
157
 *     void foo1() {}
158
 *     void foo2() {}
159
 *     // enable-formatter-2
160
 *     void bar1() {}
161
 *     void bar2() {}
162
 *     }
163
 * 
164
 * 2. The tag cannot include new line characters but it can have white spaces.
165
 *    E.g. "<b>format: on</b>" is a valid enabling tag
166
 * 
167
 * 3. If a mix of disabling and enabling tags is done in the same comment, then
168
 *    the formatter will take into account the last encountered tag in the comment.
169
 *    For example, in the following snippet, the formatter will be disabled after
170
 *    the comment:
171
 *     class X {
172
 *     &#47;&#42;
173
 *     &#42; This is a comment with a mix of disabling and enabling tags:
174
 *     &#42; disable-formatter-1
175
 *     &#42; enable-formatter-1
176
 *     &#42; disable-formatter-2
177
 *     &#42; The formatter will stop to format after this comment...
178
 *     &#42;&#47;
179
 *     void foo() {}
180
 *     void bar() {}
181
 *     }
182
 * 
183
 * @see #FORMATTER_DISABLING_TAGS
184
 * @since 3.6
185
 */
186
</pre>
187
<p>For example, the following snippet:</p>
188
<pre>
189
public class Test {
190
/* disable-formatter */
191
void     foo(    )      {	
192
				//      unformatted       area  	  
193
}
194
/* enable-formatter */
195
void     bar(    )      {	
196
				//      formatted       area  	  
197
}
198
}
199
</pre>
200
formatted with disabling tags = &quot;disable-formatter&quot; and enabling tags
201
= &quot;enable-formatter&quot; produces the following output:
202
<pre>
203
public class Test {
204
205
/* disable-formatter *	
206
void     foo(    )      {
207
				//      unformatted       area  	  
208
}
209
/* enable-formatter *
210
	void bar() {
211
		// formatted area
212
	}
213
}
214
</pre>
215
See bug <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=27079">27079</a> for more details.
216
</li>
217
</ul>
51
218
52
<h3>Problem Reports Fixed</h3>
219
<h3>Problem Reports Fixed</h3>
53
220
(-)formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java (+145 lines)
Lines 856-861 Link Here
856
	public static final String FORMATTER_CONTINUATION_INDENTATION_FOR_ARRAY_INITIALIZER = JavaCore.PLUGIN_ID + ".formatter.continuation_indentation_for_array_initializer";	//$NON-NLS-1$
856
	public static final String FORMATTER_CONTINUATION_INDENTATION_FOR_ARRAY_INITIALIZER = JavaCore.PLUGIN_ID + ".formatter.continuation_indentation_for_array_initializer";	//$NON-NLS-1$
857
	/**
857
	/**
858
	 * <pre>
858
	 * <pre>
859
	 * FORMATTER / Option to define tags used to disable formatting from the beginning of the comment including one of these tags.
860
	 *     - option id:         "org.eclipse.jdt.core.formatter.disabling_tags"
861
	 *     - default:           <code>null</code>
862
	 * </pre>
863
	 * <p>
864
	 * Note that:
865
	 * <ol>
866
	 * <li>As soon as the formatter encounter a disabling tag, it stops to format the
867
	 * code just after the comment including the tag. If it was already inactive, the
868
	 * tag is simply ignored.
869
	 * <p>Hence, nesting disabling and enabling tags has no specific effect.</p>
870
	 * For example, in the following snippet:
871
	 * <pre>
872
	 * class X {
873
	 * // disable-formatter-1
874
	 * void foo1() {}
875
	 * // disable-formatter-2
876
	 * void foo2() {}
877
	 * // enable-formatter-2
878
	 * void bar1() {}
879
	 * // enable-formatter-1
880
	 * void bar2() {}
881
	 * }
882
	 * </pre>
883
	 * the second disabling tag and the second enabling tag will be ignored by the
884
	 * formatter, which will format it the same than:
885
	 * <pre>
886
	 * class X {
887
	 * // disable-formatter-1
888
	 * void foo1() {}
889
	 * void foo2() {}
890
	 * // enable-formatter-2
891
	 * void bar1() {}
892
	 * void bar2() {}
893
	 * }
894
	 * </pre>
895
	 * </li>
896
	 * <li>If no enabling tag is found by the formatter after a disabling tag, then the
897
	 * end of the snippet won't be formatted.<br>
898
	 * For example, to disable the formatting of the entire content of a compilation
899
	 * unit, just put a disabling tag at the beginning of the code:
900
	 * <pre>
901
	 * // disable-formatter
902
	 * class X {
903
	 * void foo1() {}
904
	 * void foo2() {}
905
	 * void bar1() {}
906
	 * void bar2() {}
907
	 * }
908
	 * </pre>
909
	 * In this case, the formatter will not change the content of the class X.
910
	 * </li>
911
	 * <li>The tag cannot include new line characters but it can have white spaces.<br>
912
	 * E.g. "<b>format: off</b>" is a valid disabling tag</li>
913
	 * <li>If a mix of disabling and enabling tags is done in the same comment, then
914
	 * the formatter will take into account the last encountered tag in the comment.
915
	 * <p>For example, in the following snippet, the formatter will be disabled after
916
	 * the comment:</p>
917
	 * <pre>
918
	 * class X {
919
	 * &#47;&#42;
920
	 * &#42; This is a comment with a mix of disabling and enabling tags:
921
	 * &#42; disable-formatter-1
922
	 * &#42; enable-formatter-1
923
	 * &#42; disable-formatter-2
924
	 * &#42; The formatter will stop to format after this comment...
925
	 * &#42;&#47;
926
	 * void foo() {}
927
	 * void bar() {}
928
	 * }
929
	 * </pre>
930
	 * </li>
931
	 * </ul>
932
	 * </p>
933
	 * @see #FORMATTER_ENABLING_TAGS
934
	 * @since 3.6
935
	 */
936
	public static final String FORMATTER_DISABLING_TAGS = JavaCore.PLUGIN_ID + ".formatter.disabling_tags";	//$NON-NLS-1$
937
	/**
938
	 * <pre>
939
	 * FORMATTER / Option to define tags used to enable formatting after the end of the comment including one of these tags.
940
	 *     - option id:         "org.eclipse.jdt.core.formatter.enabling_tags"
941
	 *     - default:           <code>null</code>
942
	 * </pre>
943
	 * <p>
944
	 * Note that:
945
	 * <ol>
946
	 * <li>As soon as the formatter encounter an enabling tag, it starts to format
947
	 * the code just after the comment including the tag. If it was already active,
948
	 * the tag is simply ignored.
949
	 * <p>Hence, nesting disabling and enabling tags has no specific effect.</p>
950
	 * For example, in the following snippet:
951
	 * <pre>
952
	 * class X {
953
	 * // disable-formatter-1
954
	 * void foo1() {}
955
	 * // disable-formatter-2
956
	 * void foo2() {}
957
	 * // enable-formatter-2
958
	 * void bar1() {}
959
	 * // enable-formatter-1
960
	 * void bar2() {}
961
	 * }
962
	 * </pre>
963
	 * the second disabling tag and the second enabling tag will be ignored by the
964
	 * formatter, which will format it the same than:
965
	 * <pre>
966
	 * class X {
967
	 * // disable-formatter-1
968
	 * void foo1() {}
969
	 * void foo2() {}
970
	 * // enable-formatter-2
971
	 * void bar1() {}
972
	 * void bar2() {}
973
	 * }
974
	 * </pre>
975
	 * </li>
976
	 * <li>The tag cannot include new line characters but it can have white spaces.<br>
977
	 * E.g. "<b>format: on</b>" is a valid enabling tag</li>
978
	 * <li>If a mix of disabling and enabling tags is done in the same comment, then
979
	 * the formatter will take into account the last encountered tag in the comment.
980
	 * <p>For example, in the following snippet, the formatter will be disabled after
981
	 * the comment:</p>
982
	 * <pre>
983
	 * class X {
984
	 * &#47;&#42;
985
	 * &#42; This is a comment with a mix of disabling and enabling tags:
986
	 * &#42; disable-formatter-1
987
	 * &#42; enable-formatter-1
988
	 * &#42; disable-formatter-2
989
	 * &#42; The formatter will stop to format after this comment...
990
	 * &#42;&#47;
991
	 * void foo() {}
992
	 * void bar() {}
993
	 * }
994
	 * </pre>
995
	 * </li>
996
	 * </ol>
997
	 * </p>
998
	 * @see #FORMATTER_DISABLING_TAGS
999
	 * @since 3.6
1000
	 */
1001
	public static final String FORMATTER_ENABLING_TAGS = JavaCore.PLUGIN_ID + ".formatter.enabling_tags";	//$NON-NLS-1$
1002
	/**
1003
	 * <pre>
859
	 * FORMATTER / Option to indent body declarations compare to its enclosing annotation declaration header
1004
	 * FORMATTER / Option to indent body declarations compare to its enclosing annotation declaration header
860
	 *     - option id:         "org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_annotation_declaration_header"
1005
	 *     - option id:         "org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_annotation_declaration_header"
861
	 *     - possible values:   { TRUE, FALSE }
1006
	 *     - possible values:   { TRUE, FALSE }
(-)formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java (-4 / +4 lines)
Lines 756-762 Link Here
756
		final char[] compilationUnitSource = string.toCharArray();
756
		final char[] compilationUnitSource = string.toCharArray();
757
757
758
		this.localScanner.setSource(compilationUnitSource);
758
		this.localScanner.setSource(compilationUnitSource);
759
		this.scribe.initializeScanner(compilationUnitSource);
759
		this.scribe.resetScanner(compilationUnitSource);
760
760
761
		if (nodes == null) {
761
		if (nodes == null) {
762
			return null;
762
			return null;
Lines 793-799 Link Here
793
		final char[] compilationUnitSource = string.toCharArray();
793
		final char[] compilationUnitSource = string.toCharArray();
794
794
795
		this.localScanner.setSource(compilationUnitSource);
795
		this.localScanner.setSource(compilationUnitSource);
796
		this.scribe.initializeScanner(compilationUnitSource);
796
		this.scribe.resetScanner(compilationUnitSource);
797
797
798
		this.lastLocalDeclarationSourceStart = -1;
798
		this.lastLocalDeclarationSourceStart = -1;
799
		try {
799
		try {
Lines 822-828 Link Here
822
		final char[] compilationUnitSource = string.toCharArray();
822
		final char[] compilationUnitSource = string.toCharArray();
823
823
824
		this.localScanner.setSource(compilationUnitSource);
824
		this.localScanner.setSource(compilationUnitSource);
825
		this.scribe.initializeScanner(compilationUnitSource);
825
		this.scribe.resetScanner(compilationUnitSource);
826
826
827
		if (constructorDeclaration == null) {
827
		if (constructorDeclaration == null) {
828
			return null;
828
			return null;
Lines 866-872 Link Here
866
		final char[] compilationUnitSource = string.toCharArray();
866
		final char[] compilationUnitSource = string.toCharArray();
867
867
868
		this.localScanner.setSource(compilationUnitSource);
868
		this.localScanner.setSource(compilationUnitSource);
869
		this.scribe.initializeScanner(compilationUnitSource);
869
		this.scribe.resetScanner(compilationUnitSource);
870
870
871
		if (expression == null) {
871
		if (expression == null) {
872
			return null;
872
			return null;
(-)formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java (+29 lines)
Lines 14-21 Link Here
14
import java.util.HashMap;
14
import java.util.HashMap;
15
import java.util.Map;
15
import java.util.Map;
16
16
17
import org.eclipse.jdt.core.compiler.CharOperation;
17
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
18
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
18
import org.eclipse.jdt.core.JavaCore;
19
import org.eclipse.jdt.core.JavaCore;
20
import org.eclipse.jdt.internal.compiler.util.Util;
19
import org.eclipse.jdt.internal.formatter.align.Alignment;
21
import org.eclipse.jdt.internal.formatter.align.Alignment;
20
22
21
/**
23
/**
Lines 116-121 Link Here
116
	public boolean comment_insert_new_line_for_parameter;
118
	public boolean comment_insert_new_line_for_parameter;
117
	public int comment_line_length;
119
	public int comment_line_length;
118
120
121
	public char[][] disabling_tags;
122
	public char[][] enabling_tags;
123
119
	public boolean indent_statements_compare_to_block;
124
	public boolean indent_statements_compare_to_block;
120
	public boolean indent_statements_compare_to_body;
125
	public boolean indent_statements_compare_to_body;
121
	public boolean indent_body_declarations_compare_to_annotation_declaration_header;
126
	public boolean indent_body_declarations_compare_to_annotation_declaration_header;
Lines 612-617 Link Here
612
		options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, Integer.toString(this.tab_size));
617
		options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, Integer.toString(this.tab_size));
613
		options.put(DefaultCodeFormatterConstants.FORMATTER_USE_TABS_ONLY_FOR_LEADING_INDENTATIONS, this.use_tabs_only_for_leading_indentations ?  DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
618
		options.put(DefaultCodeFormatterConstants.FORMATTER_USE_TABS_ONLY_FOR_LEADING_INDENTATIONS, this.use_tabs_only_for_leading_indentations ?  DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
614
		options.put(DefaultCodeFormatterConstants.FORMATTER_WRAP_BEFORE_BINARY_OPERATOR, this.wrap_before_binary_operator ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
619
		options.put(DefaultCodeFormatterConstants.FORMATTER_WRAP_BEFORE_BINARY_OPERATOR, this.wrap_before_binary_operator ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
620
		options.put(DefaultCodeFormatterConstants.FORMATTER_DISABLING_TAGS, this.disabling_tags == null ? Util.EMPTY_STRING : new String(CharOperation.concatWith(this.disabling_tags,',')));
621
		options.put(DefaultCodeFormatterConstants.FORMATTER_ENABLING_TAGS, this.enabling_tags == null ? Util.EMPTY_STRING : new String(CharOperation.concatWith(this.enabling_tags,',')));
615
		return options;
622
		return options;
616
	}
623
	}
617
624
Lines 1949-1954 Link Here
1949
		if (wrapBeforeBinaryOperatorOption != null) {
1956
		if (wrapBeforeBinaryOperatorOption != null) {
1950
			this.wrap_before_binary_operator = DefaultCodeFormatterConstants.TRUE.equals(wrapBeforeBinaryOperatorOption);
1957
			this.wrap_before_binary_operator = DefaultCodeFormatterConstants.TRUE.equals(wrapBeforeBinaryOperatorOption);
1951
		}
1958
		}
1959
		final Object disableTagsOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_DISABLING_TAGS);
1960
		if (disableTagsOption != null) {
1961
			if (disableTagsOption instanceof String) {
1962
				String stringValue = (String) disableTagsOption;
1963
				if (stringValue.trim().length() == 0) {
1964
					this.disabling_tags = null;
1965
				} else {
1966
					this.disabling_tags = CharOperation.splitAndTrimOn(',', stringValue.toCharArray());
1967
				}
1968
			}
1969
		}
1970
		final Object enableTagsOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_ENABLING_TAGS);
1971
		if (enableTagsOption != null) {
1972
			if (enableTagsOption instanceof String) {
1973
				String stringValue = (String) enableTagsOption;
1974
				if (stringValue.trim().length() == 0) {
1975
					this.enabling_tags = null;
1976
				} else {
1977
					this.enabling_tags = CharOperation.splitAndTrimOn(',', stringValue.toCharArray());
1978
				}
1979
			}
1980
		}
1952
	}
1981
	}
1953
1982
1954
	/**
1983
	/**
(-)formatter/org/eclipse/jdt/internal/formatter/FormatJavadocBlock.java (-1 / +5 lines)
Lines 358-364 Link Here
358
	boolean inlined = (this.flags & INLINED) != 0;
358
	boolean inlined = (this.flags & INLINED) != 0;
359
	if (inlined) buffer.append("	{"); //$NON-NLS-1$
359
	if (inlined) buffer.append("	{"); //$NON-NLS-1$
360
	buffer.append('@');
360
	buffer.append('@');
361
	buffer.append(TAG_NAMES[this.tagValue]);
361
	if (this.tagValue == TAG_OTHERS_VALUE) {
362
		buffer.append("others_tag"); //$NON-NLS-1$
363
	} else {
364
		buffer.append(TAG_NAMES[this.tagValue]);
365
	}
362
	super.toString(buffer);
366
	super.toString(buffer);
363
	if (this.reference == null) {
367
	if (this.reference == null) {
364
		buffer.append('\n');
368
		buffer.append('\n');
(-)formatter/org/eclipse/jdt/internal/formatter/Scribe.java (-19 / +251 lines)
Lines 78-87 Link Here
78
78
79
	private int[] lineEnds;
79
	private int[] lineEnds;
80
	private int maxLines;
80
	private int maxLines;
81
	private String lineSeparator;
82
	public Alignment memberAlignment;
81
	public Alignment memberAlignment;
83
	public boolean needSpace = false;
82
	public boolean needSpace = false;
84
83
84
	// Line separator infos
85
	final private String lineSeparator;
86
	final private char firstLS;
87
	final private int lsLength;
88
85
	public int nlsTagCounter;
89
	public int nlsTagCounter;
86
	public int pageWidth;
90
	public int pageWidth;
87
	public boolean pendingSpace = false;
91
	public boolean pendingSpace = false;
Lines 100-105 Link Here
100
	private final boolean indentEmptyLines;
104
	private final boolean indentEmptyLines;
101
	int blank_lines_between_import_groups = -1;
105
	int blank_lines_between_import_groups = -1;
102
106
107
	/** disabling */
108
	boolean editsEnabled = true;
109
103
	/* Comments formatting */
110
	/* Comments formatting */
104
	private static final int INCLUDE_BLOCK_COMMENTS = CodeFormatter.F_INCLUDE_COMMENTS | CodeFormatter.K_MULTI_LINE_COMMENT;
111
	private static final int INCLUDE_BLOCK_COMMENTS = CodeFormatter.F_INCLUDE_COMMENTS | CodeFormatter.K_MULTI_LINE_COMMENT;
105
	private static final int INCLUDE_JAVA_DOC = CodeFormatter.F_INCLUDE_COMMENTS | CodeFormatter.K_JAVA_DOC;
112
	private static final int INCLUDE_JAVA_DOC = CodeFormatter.F_INCLUDE_COMMENTS | CodeFormatter.K_JAVA_DOC;
Lines 114-119 Link Here
114
	private int formatComments = 0;
121
	private int formatComments = 0;
115
	private int headerEndPosition = -1;
122
	private int headerEndPosition = -1;
116
	String commentIndentation; // indentation requested in comments (usually in javadoc root tags description)
123
	String commentIndentation; // indentation requested in comments (usually in javadoc root tags description)
124
117
	// Class to store previous line comment information
125
	// Class to store previous line comment information
118
	static class LineComment {
126
	static class LineComment {
119
		boolean contiguous = false;
127
		boolean contiguous = false;
Lines 123-134 Link Here
123
	}
131
	}
124
	final LineComment lastLineComment = new LineComment();
132
	final LineComment lastLineComment = new LineComment();
125
133
126
127
	// New way to format javadoc
134
	// New way to format javadoc
128
	private FormatterCommentParser formatterCommentParser; // specialized parser to format comments
135
	private FormatterCommentParser formatterCommentParser; // specialized parser to format comments
129
136
137
	// Disabling and enabling tags
138
	OptimizedReplaceEdit previousDisabledEdit;
139
	private char[][] validationTags;
140
	private int disablingTagsLength;
141
	private int validationTagsLength;
142
130
	Scribe(CodeFormatterVisitor formatter, long sourceLevel, IRegion[] regions, CodeSnippetParsingUtil codeSnippetParsingUtil, boolean includeComments) {
143
	Scribe(CodeFormatterVisitor formatter, long sourceLevel, IRegion[] regions, CodeSnippetParsingUtil codeSnippetParsingUtil, boolean includeComments) {
131
		this.scanner = new Scanner(true, true, false/*nls*/, sourceLevel/*sourceLevel*/, null/*taskTags*/, null/*taskPriorities*/, true/*taskCaseSensitive*/);
144
		initializeScanner(sourceLevel, formatter.preferences);
132
		this.formatter = formatter;
145
		this.formatter = formatter;
133
		this.pageWidth = formatter.preferences.page_width;
146
		this.pageWidth = formatter.preferences.page_width;
134
		this.tabLength = formatter.preferences.tab_size;
147
		this.tabLength = formatter.preferences.tab_size;
Lines 143-148 Link Here
143
			this.indentationSize = this.tabLength;
156
			this.indentationSize = this.tabLength;
144
		}
157
		}
145
		this.lineSeparator = formatter.preferences.line_separator;
158
		this.lineSeparator = formatter.preferences.line_separator;
159
		this.firstLS = this.lineSeparator.charAt(0);
160
		this.lsLength = this.lineSeparator.length();
146
		this.indentationLevel = formatter.preferences.initial_indentation_level * this.indentationSize;
161
		this.indentationLevel = formatter.preferences.initial_indentation_level * this.indentationSize;
147
		this.regions= regions;
162
		this.regions= regions;
148
		if (codeSnippetParsingUtil != null) {
163
		if (codeSnippetParsingUtil != null) {
Lines 447-452 Link Here
447
	}
462
	}
448
463
449
	private final void addOptimizedReplaceEdit(int offset, int length, String replacement) {
464
	private final void addOptimizedReplaceEdit(int offset, int length, String replacement) {
465
		if (!this.editsEnabled) {
466
			if (this.previousDisabledEdit != null && this.previousDisabledEdit.offset == offset) {
467
				replacement = this.previousDisabledEdit.replacement;
468
			}
469
			this.previousDisabledEdit = null;
470
			if (replacement.indexOf(this.lineSeparator) >= 0) {
471
				if (length == 0 || printNewLinesCharacters(offset, length)) {
472
					this.previousDisabledEdit = new OptimizedReplaceEdit(offset, length, replacement);
473
				}
474
			}
475
			return;
476
		}
450
		if (this.editsIndex > 0) {
477
		if (this.editsIndex > 0) {
451
			// try to merge last two edits
478
			// try to merge last two edits
452
			final OptimizedReplaceEdit previous = this.edits[this.editsIndex-1];
479
			final OptimizedReplaceEdit previous = this.edits[this.editsIndex-1];
Lines 1269-1286 Link Here
1269
		this.numberOfIndentations++;
1296
		this.numberOfIndentations++;
1270
	}
1297
	}
1271
1298
1272
	/**
1299
	private void initializeScanner(long sourceLevel, DefaultCodeFormatterOptions preferences) {
1273
	 * @param compilationUnitSource
1300
		char[][] disablingTags = preferences.disabling_tags;
1274
	 */
1301
		char[][] enablingTags = preferences.enabling_tags;
1275
	public void initializeScanner(char[] compilationUnitSource) {
1302
		this.disablingTagsLength = disablingTags == null ? 0 : disablingTags.length;
1276
		this.scanner.setSource(compilationUnitSource);
1303
		int eLength = enablingTags == null ? 0 : enablingTags.length;
1277
		this.scannerEndPosition = compilationUnitSource.length;
1304
		this.validationTagsLength = this.disablingTagsLength + eLength;
1278
		this.scanner.resetTo(0, this.scannerEndPosition - 1);
1305
		char[][] taskTags = null;
1279
		this.edits = new OptimizedReplaceEdit[INITIAL_SIZE];
1306
		if (this.validationTagsLength > 0) {
1280
		this.maxLines = this.lineEnds == null ? -1 : this.lineEnds.length - 1;
1307
			this.validationTags = new char[this.validationTagsLength][];
1281
		this.scanner.lineEnds = this.lineEnds;
1308
			if (this.disablingTagsLength > 0) {
1282
		this.scanner.linePtr = this.maxLines;
1309
				System.arraycopy(disablingTags, 0, this.validationTags, 0, this.disablingTagsLength);
1283
		initFormatterCommentParser();
1310
			}
1311
			if (eLength > 0) {
1312
				System.arraycopy(enablingTags, 0, this.validationTags, this.disablingTagsLength, eLength);
1313
			}
1314
			// copy the task tags array as it will be reordered by the scanner
1315
			taskTags = new char[this.validationTagsLength][];
1316
			System.arraycopy(this.validationTags, 0, taskTags, 0, this.validationTagsLength);
1317
		}
1318
		this.scanner = new Scanner(true, true, false/*nls*/, sourceLevel/*sourceLevel*/, taskTags, null/*taskPriorities*/, true/*taskCaseSensitive*/);
1284
	}
1319
	}
1285
1320
1286
	private void initFormatterCommentParser() {
1321
	private void initFormatterCommentParser() {
Lines 2153-2159 Link Here
2153
			boolean hasLineComment = false;
2188
			boolean hasLineComment = false;
2154
			boolean hasWhitespaces = false;
2189
			boolean hasWhitespaces = false;
2155
			int lines = 0;
2190
			int lines = 0;
2191
			int previousFoundTaskCount = this.scanner.foundTaskCount;
2156
			while ((this.currentToken = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
2192
			while ((this.currentToken = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
2193
				int foundTaskCount = this.scanner.foundTaskCount;
2157
				switch(this.currentToken) {
2194
				switch(this.currentToken) {
2158
					case TerminalTokens.TokenNameWHITESPACE :
2195
					case TerminalTokens.TokenNameWHITESPACE :
2159
						char[] whiteSpaces = this.scanner.getCurrentTokenSource();
2196
						char[] whiteSpaces = this.scanner.getCurrentTokenSource();
Lines 2279-2284 Link Here
2279
						currentTokenStartPosition = this.scanner.currentPosition;
2316
						currentTokenStartPosition = this.scanner.currentPosition;
2280
						break;
2317
						break;
2281
					case TerminalTokens.TokenNameCOMMENT_LINE :
2318
					case TerminalTokens.TokenNameCOMMENT_LINE :
2319
						if (this.editsEnabled && foundTaskCount > previousFoundTaskCount) {
2320
							setEditsEnabled(foundTaskCount, previousFoundTaskCount);
2321
							if (!this.editsEnabled && this.editsIndex > 1) {
2322
								OptimizedReplaceEdit currentEdit = this.edits[this.editsIndex-1];
2323
								if (this.scanner.startPosition == currentEdit.offset+currentEdit.length) {
2324
									printNewLinesBeforeDisablingComment();
2325
								}
2326
							}
2327
							previousFoundTaskCount = foundTaskCount;
2328
						}
2282
						if (rejectLineComment) break;
2329
						if (rejectLineComment) break;
2283
						if (lines >= 1) {
2330
						if (lines >= 1) {
2284
							if (lines > 1) {
2331
							if (lines > 1) {
Lines 2294-2301 Link Here
2294
						currentTokenStartPosition = this.scanner.currentPosition;
2341
						currentTokenStartPosition = this.scanner.currentPosition;
2295
						hasLineComment = true;
2342
						hasLineComment = true;
2296
						lines = 0;
2343
						lines = 0;
2344
						if (!this.editsEnabled && foundTaskCount > previousFoundTaskCount) {
2345
							setEditsEnabled(foundTaskCount, previousFoundTaskCount);
2346
						}
2297
						break;
2347
						break;
2298
					case TerminalTokens.TokenNameCOMMENT_BLOCK :
2348
					case TerminalTokens.TokenNameCOMMENT_BLOCK :
2349
						if (this.editsEnabled && foundTaskCount > previousFoundTaskCount) {
2350
							setEditsEnabled(foundTaskCount, previousFoundTaskCount);
2351
							if (!this.editsEnabled && this.editsIndex > 1) {
2352
								OptimizedReplaceEdit currentEdit = this.edits[this.editsIndex-1];
2353
								if (this.scanner.startPosition == currentEdit.offset+currentEdit.length) {
2354
									printNewLinesBeforeDisablingComment();
2355
								}
2356
							}
2357
							previousFoundTaskCount = foundTaskCount;
2358
						}
2299
						if (trailing > NO_TRAILING_COMMENT && lines >= 1) {
2359
						if (trailing > NO_TRAILING_COMMENT && lines >= 1) {
2300
							// a block comment on next line means that there's no trailing comment
2360
							// a block comment on next line means that there's no trailing comment
2301
							this.scanner.resetTo(this.scanner.getCurrentTokenStartPosition(), this.scannerEndPosition - 1);
2361
							this.scanner.resetTo(this.scanner.getCurrentTokenStartPosition(), this.scannerEndPosition - 1);
Lines 2318-2325 Link Here
2318
						hasLineComment = false;
2378
						hasLineComment = false;
2319
						hasComment = true;
2379
						hasComment = true;
2320
						lines = 0;
2380
						lines = 0;
2381
						if (!this.editsEnabled && foundTaskCount > previousFoundTaskCount) {
2382
							setEditsEnabled(foundTaskCount, previousFoundTaskCount);
2383
						}
2321
						break;
2384
						break;
2322
					case TerminalTokens.TokenNameCOMMENT_JAVADOC :
2385
					case TerminalTokens.TokenNameCOMMENT_JAVADOC :
2386
						if (this.editsEnabled && foundTaskCount > previousFoundTaskCount) {
2387
							setEditsEnabled(foundTaskCount, previousFoundTaskCount);
2388
							previousFoundTaskCount = foundTaskCount;
2389
						}
2323
						if (trailing > NO_TRAILING_COMMENT) {
2390
						if (trailing > NO_TRAILING_COMMENT) {
2324
							// a javadoc comment should not be considered as a trailing comment
2391
							// a javadoc comment should not be considered as a trailing comment
2325
							this.scanner.resetTo(this.scanner.getCurrentTokenStartPosition(), this.scannerEndPosition - 1);
2392
							this.scanner.resetTo(this.scanner.getCurrentTokenStartPosition(), this.scannerEndPosition - 1);
Lines 2342-2347 Link Here
2342
						} else {
2409
						} else {
2343
							printBlockComment(true);
2410
							printBlockComment(true);
2344
						}
2411
						}
2412
						if (!this.editsEnabled && foundTaskCount > previousFoundTaskCount) {
2413
							setEditsEnabled(foundTaskCount, previousFoundTaskCount);
2414
						}
2345
						printNewLine();
2415
						printNewLine();
2346
						currentTokenStartPosition = this.scanner.currentPosition;
2416
						currentTokenStartPosition = this.scanner.currentPosition;
2347
						hasLineComment = false;
2417
						hasLineComment = false;
Lines 2354-2359 Link Here
2354
						this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
2424
						this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
2355
						return;
2425
						return;
2356
				}
2426
				}
2427
				previousFoundTaskCount = foundTaskCount;
2357
			}
2428
			}
2358
		} catch (InvalidInputException e) {
2429
		} catch (InvalidInputException e) {
2359
			throw new AbortFormatting(e);
2430
			throw new AbortFormatting(e);
Lines 2363-2369 Link Here
2363
	void printComment(int kind, String source, int start, int end, int level) {
2434
	void printComment(int kind, String source, int start, int end, int level) {
2364
2435
2365
		// Set scanner
2436
		// Set scanner
2366
		initializeScanner(source.toCharArray());
2437
		resetScanner(source.toCharArray());
2367
		this.scanner.resetTo(start, end);
2438
		this.scanner.resetTo(start, end);
2368
		// Put back 3.4RC2 code => comment following line  as it has an impact on Linux tests
2439
		// Put back 3.4RC2 code => comment following line  as it has an impact on Linux tests
2369
		// see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=234336
2440
		// see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=234336
Lines 2677-2685 Link Here
2677
			}
2748
			}
2678
		}
2749
		}
2679
2750
2680
		// Delete leading whitespaces if any
2751
		// Replace the line separator at the end of the comment if any...
2681
		if (previousToken != -1 && lastTokenEndPosition != commentStart && spaceEndPosition > lastTokenEndPosition) {
2752
		int startReplace = previousToken == SKIP_FIRST_WHITESPACE_TOKEN ? spaceStartPosition : lastTokenEndPosition;
2682
			addDeleteEdit(lastTokenEndPosition, spaceEndPosition-1);
2753
		if (this.column == 1 && commentEnd >= startReplace) {
2754
			addReplaceEdit(startReplace, commentEnd, this.formatter.preferences.line_separator);
2683
		}
2755
		}
2684
	}
2756
	}
2685
2757
Lines 4319-4324 Link Here
4319
		this.lastLineComment.contiguous = false;
4391
		this.lastLineComment.contiguous = false;
4320
	}
4392
	}
4321
4393
4394
	/*
4395
	 * Print the indentation of a disabling comment
4396
	 */
4397
	private void printNewLinesBeforeDisablingComment() {
4398
4399
		// Get the beginning of comment line
4400
		int linePtr = Arrays.binarySearch(this.lineEnds, this.scanner.startPosition);
4401
		if (linePtr < 0) {
4402
			linePtr = -linePtr - 1;
4403
		}
4404
		int indentation = 0;
4405
		int beginningOfLine = getLineEnd(linePtr)+1;
4406
		if (beginningOfLine == -1) {
4407
			beginningOfLine = 0;
4408
		}
4409
		
4410
		// If the comment is in the middle of the line, then there's nothing to do
4411
		OptimizedReplaceEdit currentEdit = this.edits[this.editsIndex-1];
4412
		int offset = currentEdit.offset;
4413
		if (offset >= beginningOfLine) return;
4414
4415
		// Compute the comment indentation
4416
		int scannerStartPosition = this.scanner.startPosition;
4417
		int scannerEofPosition = this.scanner.eofPosition;
4418
		int scannerCurrentPosition = this.scanner.currentPosition;
4419
		char scannerCurrentChar = this.scanner.currentCharacter;
4420
		int length = currentEdit.length;
4421
		this.scanner.resetTo(beginningOfLine, offset+length-1);
4422
		try {
4423
			while (!this.scanner.atEnd()) {
4424
				char ch = (char) this.scanner.getNextChar();
4425
				switch (ch) {
4426
					case '\t' :
4427
						if (this.tabLength != 0) {
4428
							int reminder = indentation % this.tabLength;
4429
							if (reminder == 0) {
4430
								indentation += this.tabLength;
4431
							} else {
4432
								indentation = ((indentation / this.tabLength) + 1) * this.tabLength;
4433
							}
4434
						}
4435
						break;
4436
					case ' ':
4437
						indentation++;
4438
						break;
4439
					default:
4440
						// Should not happen as the offset of the edit is before the beginning of line
4441
						return;
4442
				}
4443
			}
4444
		
4445
			// Split the existing edit to keep the change before the beginning of the last line
4446
			// but change the indentation after. Note that at this stage, the add*Edit methods
4447
			// cannot be longer used as the edits are disabled
4448
			StringBuffer indentationBuffer = new StringBuffer();
4449
			int currentIndentation = getCurrentIndentation(this.scanner.currentPosition);
4450
			if (currentIndentation > 0 && this.indentationLevel > 0) {
4451
				int col = this.column;
4452
				printIndentationIfNecessary(indentationBuffer);
4453
				this.column = col;
4454
			}
4455
			String replacement = currentEdit.replacement;
4456
			if (replacement.length() == 0) {
4457
				// previous edit was a delete, as we're sure to have a new line before
4458
				// the comment, then the edit needs to be either replaced entirely with
4459
				// the expected indentation
4460
				this.edits[this.editsIndex-1] = new OptimizedReplaceEdit(beginningOfLine, offset+length-beginningOfLine, indentationBuffer.toString());
4461
			} else {
4462
				int idx = replacement.lastIndexOf(this.lineSeparator);
4463
				if (idx >= 0) {
4464
					// replace current edit if it contains a line separator
4465
					int start = idx + this.lsLength;
4466
					StringBuffer buffer = new StringBuffer(replacement.substring(0, start));
4467
					buffer.append(indentationBuffer);
4468
					this.edits[this.editsIndex-1] = new OptimizedReplaceEdit(offset, length, buffer.toString());
4469
				}
4470
			}
4471
		}
4472
		finally {
4473
			this.scanner.startPosition = scannerStartPosition;
4474
			this.scanner.eofPosition = scannerEofPosition;
4475
			this.scanner.currentPosition = scannerCurrentPosition;
4476
			this.scanner.currentCharacter = scannerCurrentChar;
4477
		}
4478
	}
4479
4480
	/*
4481
	 * Print new lines characters when the edits are disabled. In this case, only
4482
	 * the line separator is replaced if necessary, the other white spaces are untouched.
4483
	 */
4484
	private boolean printNewLinesCharacters(int offset, int length) {
4485
		boolean foundNewLine = false;
4486
		int scannerStartPosition = this.scanner.startPosition;
4487
		int scannerEofPosition = this.scanner.eofPosition;
4488
		int scannerCurrentPosition = this.scanner.currentPosition;
4489
		char scannerCurrentChar = this.scanner.currentCharacter;
4490
		this.scanner.resetTo(offset, offset+length-1);
4491
		try {
4492
			while (!this.scanner.atEnd()) {
4493
				int start = this.scanner.currentPosition;
4494
				char ch = (char) this.scanner.getNextChar();
4495
				boolean needReplace = ch != this.firstLS;
4496
				switch (ch) {
4497
					case '\r':
4498
						if (this.scanner.atEnd()) break;
4499
						ch = (char) this.scanner.getNextChar();
4500
						if (ch != '\n') break;
4501
						needReplace = needReplace || this.lsLength != 2;
4502
						//$FALL-THROUGH$
4503
					case '\n':
4504
						if (needReplace) {
4505
							if (this.editsIndex == 0 || this.edits[this.editsIndex-1].offset != start) {
4506
								this.edits[this.editsIndex++] = new OptimizedReplaceEdit(start, this.scanner.currentPosition-start, this.lineSeparator);
4507
							}
4508
						}
4509
						foundNewLine = true;
4510
						break;
4511
				}
4512
			}
4513
		}
4514
		finally {
4515
			this.scanner.startPosition = scannerStartPosition;
4516
			this.scanner.eofPosition = scannerEofPosition;
4517
			this.scanner.currentPosition = scannerCurrentPosition;
4518
			this.scanner.currentCharacter = scannerCurrentChar;
4519
		}
4520
		return foundNewLine;		
4521
	}
4522
4322
	public void printNextToken(int expectedTokenType){
4523
	public void printNextToken(int expectedTokenType){
4323
		printNextToken(expectedTokenType, false);
4524
		printNextToken(expectedTokenType, false);
4324
	}
4525
	}
Lines 4526-4535 Link Here
4526
		this.formatter.lastLocalDeclarationSourceStart = location.lastLocalDeclarationSourceStart;
4727
		this.formatter.lastLocalDeclarationSourceStart = location.lastLocalDeclarationSourceStart;
4527
	}
4728
	}
4528
4729
4730
	/**
4731
	 * @param compilationUnitSource
4732
	 */
4733
	public void resetScanner(char[] compilationUnitSource) {
4734
		this.scanner.setSource(compilationUnitSource);
4735
		this.scannerEndPosition = compilationUnitSource.length;
4736
		this.scanner.resetTo(0, this.scannerEndPosition - 1);
4737
		this.edits = new OptimizedReplaceEdit[INITIAL_SIZE];
4738
		this.maxLines = this.lineEnds == null ? -1 : this.lineEnds.length - 1;
4739
		this.scanner.lineEnds = this.lineEnds;
4740
		this.scanner.linePtr = this.maxLines;
4741
		initFormatterCommentParser();
4742
	}
4743
4529
	private void resize() {
4744
	private void resize() {
4530
		System.arraycopy(this.edits, 0, (this.edits = new OptimizedReplaceEdit[this.editsIndex * 2]), 0, this.editsIndex);
4745
		System.arraycopy(this.edits, 0, (this.edits = new OptimizedReplaceEdit[this.editsIndex * 2]), 0, this.editsIndex);
4531
	}
4746
	}
4532
4747
4748
	/*
4749
	 * Look for the tags identified by the scanner to see whether some of them
4750
	 * may change the status of the edition for the formatter.
4751
	 */
4752
	private void setEditsEnabled(int count, int previous) {
4753
		for (int i=previous; i<count; i++) {
4754
			for (int j=0; j<this.validationTagsLength; j++) {
4755
				if (CharOperation.equals(this.scanner.foundTaskTags[i], this.validationTags[j])) {
4756
					this.editsEnabled = j >= this.disablingTagsLength;
4757
					// do not return yet, as there may have several disabling/enabling
4758
					// tags in a comment, hence the last one will be the one really
4759
					// changing the formatter behavior...
4760
				}
4761
			}
4762
		}
4763
	}
4764
4533
	void setIncludeComments(boolean on) {
4765
	void setIncludeComments(boolean on) {
4534
		if (on) {
4766
		if (on) {
4535
			this.formatComments |= CodeFormatter.F_INCLUDE_COMMENTS;
4767
			this.formatComments |= CodeFormatter.F_INCLUDE_COMMENTS;
(-)src/org/eclipse/jdt/core/tests/formatter/FormatterBugsTests.java (+568 lines)
Lines 15-20 Link Here
15
import junit.framework.Test;
15
import junit.framework.Test;
16
16
17
import org.eclipse.jdt.core.JavaModelException;
17
import org.eclipse.jdt.core.JavaModelException;
18
import org.eclipse.jdt.core.formatter.CodeFormatter;
18
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
19
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
19
import org.eclipse.jdt.core.formatter.IndentManipulation;
20
import org.eclipse.jdt.core.formatter.IndentManipulation;
20
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter;
21
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter;
Lines 66-71 Link Here
66
}
67
}
67
68
68
/**
69
/**
70
 * @bug 27079: [formatter] Tags for disabling/enabling code formatter (feature)
71
 * @test Ensure that the formatter does not format code between specific javadoc comments
72
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=27079"
73
 */
74
public void testBug027079a() throws JavaModelException {
75
	String source =
76
		"public class X01 {\n" + 
77
		"\n" + 
78
		"/* disable-formatter */\n" + 
79
		"void     foo(    )      {	\n" + 
80
		"				//      unformatted       comment\n" + 
81
		"}\n" + 
82
		"/* enable-formatter */\n" + 
83
		"void     bar(    )      {	\n" + 
84
		"				//      formatted       comment\n" + 
85
		"}\n" + 
86
		"}\n";
87
	formatSource(source,
88
		"public class X01 {\n" + 
89
		"\n" + 
90
		"	/* disable-formatter */\n" + 
91
		"	void foo() {\n" + 
92
		"		// unformatted comment\n" + 
93
		"	}\n" + 
94
		"\n" + 
95
		"	/* enable-formatter */\n" + 
96
		"	void bar() {\n" + 
97
		"		// formatted comment\n" + 
98
		"	}\n" + 
99
		"}\n"
100
	);
101
}
102
public void testBug027079a1() throws JavaModelException {
103
	this.formatterPrefs.disabling_tags = new char[][] { "disable-formatter".toCharArray() };
104
	this.formatterPrefs.enabling_tags = new char[][] { "enable-formatter".toCharArray() };
105
	String source =
106
		"public class X01 {\n" + 
107
		"\n" + 
108
		"/* disable-formatter */\n" + 
109
		"void     foo(    )      {	\n" + 
110
		"				//      unformatted       comment\n" + 
111
		"}\n" + 
112
		"/* enable-formatter */\n" + 
113
		"void     bar(    )      {	\n" + 
114
		"				//      formatted       comment\n" + 
115
		"}\n" + 
116
		"}\n";
117
	formatSource(source,
118
		"public class X {\n" + 
119
		"\n" + 
120
		"/* disable-formatter */\n" + 
121
		"void     foo(    )      {	\n" + 
122
		"				//      unformatted       comment\n" + 
123
		"}\n" + 
124
		"/* enable-formatter */\n" + 
125
		"	void bar() {\n" + 
126
		"		// formatted comment\n" + 
127
		"	}\n" + 
128
		"}\n"
129
	);
130
}
131
public void testBug027079a2() throws JavaModelException {
132
	this.formatterPrefs.disabling_tags = new char[][] { "disable-formatter".toCharArray() };
133
	this.formatterPrefs.enabling_tags = new char[][] { "enable-formatter".toCharArray() };
134
	String source =
135
		"public class X01 {\n" + 
136
		"\n" + 
137
		"/** disable-formatter */\n" + 
138
		"void     foo(    )      {	\n" + 
139
		"				//      unformatted       comment\n" + 
140
		"}\n" + 
141
		"/** enable-formatter */\n" + 
142
		"void     bar(    )      {	\n" + 
143
		"				//      formatted       comment\n" + 
144
		"}\n" + 
145
		"}\n";
146
	formatSource(source,
147
		"public class X01 {\n" + 
148
		"\n" + 
149
		"/** disable-formatter */\n" + 
150
		"void     foo(    )      {	\n" + 
151
		"				//      unformatted       comment\n" + 
152
		"}\n" + 
153
		"/** enable-formatter */\n" + 
154
		"	void bar() {\n" + 
155
		"		// formatted comment\n" + 
156
		"	}\n" + 
157
		"}\n"
158
	);
159
}
160
public void testBug027079a3() throws JavaModelException {
161
	this.formatterPrefs.disabling_tags = new char[][] { "disable-formatter".toCharArray() };
162
	this.formatterPrefs.enabling_tags = new char[][] { "enable-formatter".toCharArray() };
163
	String source =
164
		"public class X01 {\n" + 
165
		"\n" + 
166
		"// disable-formatter\n" + 
167
		"void     foo(    )      {	\n" + 
168
		"				//      unformatted       comment\n" + 
169
		"}\n" + 
170
		"// enable-formatter\n" + 
171
		"void     bar(    )      {	\n" + 
172
		"				//      formatted       comment\n" + 
173
		"}\n" + 
174
		"}\n";
175
	formatSource(source,
176
		"public class X01 {\n" + 
177
		"\n" + 
178
		"// disable-formatter\n" + 
179
		"void     foo(    )      {	\n" + 
180
		"				//      unformatted       comment\n" + 
181
		"}\n" + 
182
		"// enable-formatter\n" + 
183
		"	void bar() {\n" + 
184
		"		// formatted comment\n" + 
185
		"	}\n" + 
186
		"}\n"
187
	);
188
}
189
public void testBug027079a4() throws JavaModelException {
190
	this.formatterPrefs.disabling_tags = new char[][] { "disable-formatter".toCharArray() };
191
	this.formatterPrefs.enabling_tags = new char[][] { "enable-formatter".toCharArray() };
192
	String source =
193
		"public class X01 {\n" + 
194
		"\n" + 
195
		"// disable-formatter\n" + 
196
		"void     foo(    )      {	\n" + 
197
		"				//      unformatted       comment  	  \n" + 
198
		"}\n" + 
199
		"// enable-formatter \n" + 
200
		"void     bar(    )      {	\n" + 
201
		"				//      formatted       comment  	  \n" + 
202
		"				/* disable-formatter *//*      unformatted		comment  	  *//* enable-formatter */\n" + 
203
		"}\n" + 		"}\n";
204
	formatSource(source,
205
		"public class X01 {\n" + 
206
		"\n" + 
207
		"// disable-formatter\n" + 
208
		"void     foo(    )      {	\n" + 
209
		"				//      unformatted       comment  	  \n" + 
210
		"}\n" + 
211
		"// enable-formatter \n" + 
212
		"	void bar() {\n" + 
213
		"		// formatted comment\n" + 
214
		"		/* disable-formatter *//*      unformatted		comment  	  *//* enable-formatter */\n" + 
215
		"	}\n" + 
216
		"}\n"
217
	);
218
}
219
public void testBug027079b() throws JavaModelException {
220
	this.formatterPrefs.disabling_tags = new char[][] { "disable-formatter".toCharArray() };
221
	this.formatterPrefs.enabling_tags = new char[][] { "enable-formatter".toCharArray() };
222
	String source =
223
		"public class X02 {\n" + 
224
		"void foo() {\n" + 
225
		"/* disable-formatter */\n" + 
226
		"				/*       unformatted		comment  	  */\n" + 
227
		"	String test1= \"this\"+\n" + 
228
		"					\"is\"+\n" + 
229
		"			\"a specific\"+\n" + 
230
		"		\"line wrapping \";\n" + 
231
		"\n" + 
232
		"/* enable-formatter */\n" + 
233
		"				/*       formatted		comment  	  */\n" + 
234
		"	String test2= \"this\"+\n" + 
235
		"					\"is\"+\n" + 
236
		"			\"a specific\"+\n" + 
237
		"		\"line wrapping \";\n" + 
238
		"}\n" + 
239
		"}\n";
240
	formatSource(source,
241
		"public class X02 {\n" + 
242
		"	void foo() {\n" + 
243
		"/* disable-formatter */\n" + 
244
		"				/*       unformatted		comment  	  */\n" + 
245
		"	String test1= \"this\"+\n" + 
246
		"					\"is\"+\n" + 
247
		"			\"a specific\"+\n" + 
248
		"		\"line wrapping \";\n" + 
249
		"\n" + 
250
		"/* enable-formatter */\n" + 
251
		"		/* formatted comment */\n" + 
252
		"		String test2 = \"this\" + \"is\" + \"a specific\" + \"line wrapping \";\n" + 
253
		"	}\n" + 
254
		"}\n"
255
	);
256
}
257
public void testBug027079c() throws JavaModelException {
258
	this.formatterPrefs.disabling_tags = new char[][] { "disable-formatter".toCharArray() };
259
	this.formatterPrefs.enabling_tags = new char[][] { "enable-formatter".toCharArray() };
260
	String source =
261
		"public class X03 {\n" + 
262
		"void foo() {\n" + 
263
		"/* disable-formatter */\n" + 
264
		"	bar(\n" + 
265
		"				/**       unformatted		comment  	  */\n" + 
266
		"				\"this\"  ,\n" + 
267
		"					\"is\",\n" + 
268
		"			\"a specific\",\n" + 
269
		"		\"line wrapping \"\n" + 
270
		"	);\n" + 
271
		"\n" + 
272
		"/* enable-formatter */\n" + 
273
		"	bar(\n" + 
274
		"				/**       formatted		comment  	  */\n" + 
275
		"				\"this\"  ,\n" + 
276
		"					\"is\",\n" + 
277
		"			\"a specific\",\n" + 
278
		"		\"line wrapping \"\n" + 
279
		"	);\n" + 
280
		"}\n" + 
281
		"void bar(String... str) {}\n" + 
282
		"}\n";
283
	formatSource(source,
284
		"public class X03 {\n" + 
285
		"	void foo() {\n" + 
286
		"/* disable-formatter */\n" + 
287
		"	bar(\n" + 
288
		"				/**       unformatted		comment  	  */\n" + 
289
		"				\"this\"  ,\n" + 
290
		"					\"is\",\n" + 
291
		"			\"a specific\",\n" + 
292
		"		\"line wrapping \"\n" + 
293
		"	);\n" + 
294
		"\n" + 
295
		"/* enable-formatter */\n" + 
296
		"		bar(\n" + 
297
		"		/** formatted comment */\n" + 
298
		"		\"this\", \"is\", \"a specific\", \"line wrapping \");\n" + 
299
		"	}\n" + 
300
		"\n" + 
301
		"	void bar(String... str) {\n" + 
302
		"	}\n" + 
303
		"}\n"
304
	);
305
}
306
public void testBug027079d() throws JavaModelException {
307
	this.formatterPrefs.disabling_tags = new char[][] { "disable-formatter".toCharArray() };
308
	this.formatterPrefs.enabling_tags = new char[][] { "enable-formatter".toCharArray() };
309
	String source =
310
		"public class X03b {\n" + 
311
		"void foo() {\n" + 
312
		"	bar(\n" + 
313
		"// disable-formatter\n" + 
314
		"				/**       unformatted		comment  	  */\n" + 
315
		"				\"this\"  ,\n" + 
316
		"					\"is\",\n" + 
317
		"			\"a specific\",\n" + 
318
		"		\"line wrapping \"\n" + 
319
		"// enable-formatter\n" + 
320
		"	);\n" + 
321
		"	bar(\n" + 
322
		"				/**       formatted		comment  	  */\n" + 
323
		"				\"this\"  ,\n" + 
324
		"					\"is\",\n" + 
325
		"			\"a specific\",\n" + 
326
		"		\"line wrapping \"\n" + 
327
		"	);\n" + 
328
		"}\n" + 
329
		"void bar(String... str) {}\n" + 
330
		"}\n";
331
	formatSource(source,
332
		"public class X03b {\n" + 
333
		"	void foo() {\n" + 
334
		"		bar(\n" + 
335
		"// disable-formatter\n" + 
336
		"				/**       unformatted		comment  	  */\n" + 
337
		"				\"this\"  ,\n" + 
338
		"					\"is\",\n" + 
339
		"			\"a specific\",\n" + 
340
		"		\"line wrapping \"\n" + 
341
		"// enable-formatter\n" + 
342
		"		);\n" + 
343
		"		bar(\n" + 
344
		"		/** formatted comment */\n" + 
345
		"		\"this\", \"is\", \"a specific\", \"line wrapping \");\n" + 
346
		"	}\n" + 
347
		"\n" + 
348
		"	void bar(String... str) {\n" + 
349
		"	}\n" + 
350
		"}\n"
351
	);
352
}
353
public void testBug027079e() throws JavaModelException {
354
	this.formatterPrefs.disabling_tags = new char[][] { "disable-formatter".toCharArray() };
355
	this.formatterPrefs.enabling_tags = new char[][] { "enable-formatter".toCharArray() };
356
	String source =
357
		"public class X01 {\r\n" + 
358
		"\r\n" + 
359
		"/* disable-formatter */\r\n" + 
360
		"void     foo(    )      {	\r\n" + 
361
		"				//      unformatted       comment  	  \r\n" + 
362
		"}\r\n" + 
363
		"/* enable-formatter */\r\n" + 
364
		"void     bar(    )      {	\r\n" + 
365
		"				//      formatted       comment  	  \r\n" + 
366
		"}\r\n" + 
367
		"}\r\n";
368
	formatSource(source,
369
		"public class X01 {\n" + 
370
		"\n" + 
371
		"/* disable-formatter */\n" + 
372
		"void     foo(    )      {	\n" + 
373
		"				//      unformatted       comment  	  \n" + 
374
		"}\n" + 
375
		"/* enable-formatter */\n" + 
376
		"	void bar() {\n" + 
377
		"		// formatted comment\n" + 
378
		"	}\n" + 
379
		"}\n",
380
		CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS,
381
		0 /* indentation level */,
382
		0 /* offset */,
383
		-1 /* length (all) */,
384
		"\n",
385
		true/*repeat*/);
386
}
387
public void testBug027079f() throws JavaModelException {
388
	this.formatterPrefs.disabling_tags = new char[][] { "disable-formatter".toCharArray() };
389
	this.formatterPrefs.enabling_tags = new char[][] { "enable-formatter".toCharArray() };
390
	String source =
391
		"public class X01 {\n" + 
392
		"\n" + 
393
		"/* disable-formatter */\n" + 
394
		"void     foo(    )      {	\n" + 
395
		"				//      unformatted       comment  	  \n" + 
396
		"}\n" + 
397
		"/* enable-formatter */\n" + 
398
		"void     bar(    )      {	\n" + 
399
		"				//      formatted       comment  	  \n" + 
400
		"}\n" + 
401
		"}\n";
402
	formatSource(source,
403
		"public class X01 {\r\n" + 
404
		"\r\n" + 
405
		"/* disable-formatter */\r\n" + 
406
		"void     foo(    )      {	\r\n" + 
407
		"				//      unformatted       comment  	  \r\n" + 
408
		"}\r\n" + 
409
		"/* enable-formatter */\r\n" + 
410
		"	void bar() {\r\n" + 
411
		"		// formatted comment\r\n" + 
412
		"	}\r\n" + 
413
		"}\r\n",
414
		CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS,
415
		0 /* indentation level */,
416
		0 /* offset */,
417
		-1 /* length (all) */,
418
		"\r\n",
419
		true/*repeat*/);
420
}
421
public void testBug027079g() throws JavaModelException {
422
	this.formatterPrefs.disabling_tags = new char[][] { "disable-formatter".toCharArray() };
423
	this.formatterPrefs.enabling_tags = new char[][] { "enable-formatter".toCharArray() };
424
	String source =
425
		"public class X01 {\r\n" + 
426
		"\r\n" + 
427
		"/* disable-formatter */\r\n" + 
428
		"void     foo(    )      {	\r\n" + 
429
		"				//      unformatted       comment  	  \r\n" + 
430
		"}\r\n" + 
431
		"/* enable-formatter */\r\n" + 
432
		"void     bar(    )      {	\r\n" + 
433
		"				//      formatted       comment  	  \r\n" + 
434
		"}\r\n" + 
435
		"}\r\n";
436
	formatSource(source,
437
		"public class X01 {\r\n" + 
438
		"\r\n" + 
439
		"/* disable-formatter */\r\n" + 
440
		"void     foo(    )      {	\r\n" + 
441
		"				//      unformatted       comment  	  \r\n" + 
442
		"}\r\n" + 
443
		"/* enable-formatter */\r\n" + 
444
		"	void bar() {\r\n" + 
445
		"		// formatted comment\r\n" + 
446
		"	}\r\n" + 
447
		"}\r\n",
448
		CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS,
449
		0 /* indentation level */,
450
		0 /* offset */,
451
		-1 /* length (all) */,
452
		"\r\n",
453
		true/*repeat*/);
454
}
455
public void testBug027079h() throws JavaModelException {
456
	this.formatterPrefs.disabling_tags = new char[][] { "disable-formatter".toCharArray() };
457
	this.formatterPrefs.enabling_tags = new char[][] { "enable-formatter".toCharArray() };
458
	String source =
459
		"public class X01 {\n" + 
460
		"\n" + 
461
		"/* disable-formatter */\n" + 
462
		"void     foo(    )      {	\n" + 
463
		"				//      unformatted       comment  	  \n" + 
464
		"}\n" + 
465
		"/* enable-formatter */\n" + 
466
		"void     bar(    )      {	\n" + 
467
		"				//      formatted       comment  	  \n" + 
468
		"}\n" + 
469
		"}\n";
470
	formatSource(source,
471
		"public class X01 {\n" + 
472
		"\n" + 
473
		"/* disable-formatter */\n" + 
474
		"void     foo(    )      {	\n" + 
475
		"				//      unformatted       comment  	  \n" + 
476
		"}\n" + 
477
		"/* enable-formatter */\n" + 
478
		"	void bar() {\n" + 
479
		"		// formatted comment\n" + 
480
		"	}\n" + 
481
		"}\n",
482
		CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS,
483
		0 /* indentation level */,
484
		0 /* offset */,
485
		-1 /* length (all) */,
486
		"\n",
487
		true/*repeat*/);
488
}
489
public void testBug027079i() throws JavaModelException {
490
	this.formatterPrefs.disabling_tags = new char[][] { "format: off".toCharArray() };
491
	this.formatterPrefs.enabling_tags = new char[][] { "format: on".toCharArray() };
492
	String source =
493
		"public class X01 {\n" + 
494
		"\n" + 
495
		"/* format: off */\n" + 
496
		"void     foo(    )      {	\n" + 
497
		"				//      unformatted       comment\n" + 
498
		"}\n" + 
499
		"/* format: on */\n" + 
500
		"void     bar(    )      {	\n" + 
501
		"				//      formatted       comment\n" + 
502
		"}\n" + 
503
		"}\n";
504
	formatSource(source,
505
		"public class X01 {\n" + 
506
		"\n" + 
507
		"/* format: off */\n" + 
508
		"void     foo(    )      {	\n" + 
509
		"				//      unformatted       comment\n" + 
510
		"}\n" + 
511
		"/* format: on */\n" + 
512
		"	void bar() {\n" + 
513
		"		// formatted comment\n" + 
514
		"	}\n" + 
515
		"}\n"
516
	);
517
}
518
public void testBug027079j() throws JavaModelException {
519
	this.formatterPrefs.disabling_tags = new char[][] { "format: off".toCharArray() };
520
	this.formatterPrefs.enabling_tags = new char[][] { "format: on".toCharArray() };
521
	String source =
522
		"public class X01 {\n" + 
523
		"\n" + 
524
		"// format: off\n" + 
525
		"void     foo(    )      {	\n" + 
526
		"				//      unformatted       comment\n" + 
527
		"}\n" + 
528
		"// format: on\n" + 
529
		"void     bar(    )      {	\n" + 
530
		"				//      formatted       comment\n" + 
531
		"}\n" + 
532
		"}\n";
533
	formatSource(source,
534
		"public class X01 {\n" + 
535
		"\n" + 
536
		"// format: off\n" + 
537
		"void     foo(    )      {	\n" + 
538
		"				//      unformatted       comment\n" + 
539
		"}\n" + 
540
		"// format: on\n" + 
541
		"	void bar() {\n" + 
542
		"		// formatted comment\n" + 
543
		"	}\n" + 
544
		"}\n"
545
	);
546
}
547
public void testBug027079k() throws JavaModelException {
548
	this.formatterPrefs.disabling_tags = new char[][] { "format: off".toCharArray() };
549
	this.formatterPrefs.enabling_tags = new char[][] { "format: on".toCharArray() };
550
	String source =
551
		"public class X01 {\n" + 
552
		"\n" + 
553
		"/** format: off */\n" + 
554
		"void     foo(    )      {	\n" + 
555
		"				//      unformatted       comment\n" + 
556
		"}\n" + 
557
		"/** format: on */\n" + 
558
		"void     bar(    )      {	\n" + 
559
		"				//      formatted       comment\n" + 
560
		"}\n" + 
561
		"}\n";
562
	formatSource(source,
563
		"public class X01 {\n" + 
564
		"\n" + 
565
		"/** format: off */\n" + 
566
		"void     foo(    )      {	\n" + 
567
		"				//      unformatted       comment\n" + 
568
		"}\n" + 
569
		"/** format: on */\n" + 
570
		"	void bar() {\n" + 
571
		"		// formatted comment\n" + 
572
		"	}\n" + 
573
		"}\n"
574
	);
575
}
576
public void testBug027079l() throws JavaModelException {
577
	this.formatterPrefs.disabling_tags = new char[][] { "    format:  	  off    ".toCharArray() };
578
	this.formatterPrefs.enabling_tags = new char[][] { "	format:	  	on	".toCharArray() };
579
	String source =
580
		"public class X01 {\n" + 
581
		"\n" + 
582
		"/*	format:  	  off	*/\n" + 
583
		"void     foo(    )      {	\n" + 
584
		"				//      unformatted       comment\n" + 
585
		"}\n" + 
586
		"//    	format:	  	on	    \n" + 
587
		"void     bar(    )      {	\n" + 
588
		"				//      formatted       comment\n" + 
589
		"}\n" + 
590
		"}\n";
591
	formatSource(source,
592
		"public class X01 {\n" + 
593
		"\n" + 
594
		"/*	format:  	  off	*/\n" + 
595
		"void     foo(    )      {	\n" + 
596
		"				//      unformatted       comment\n" + 
597
		"}\n" + 
598
		"//    	format:	  	on	    \n" + 
599
		"	void bar() {\n" + 
600
		"		// formatted comment\n" + 
601
		"	}\n" + 
602
		"}\n"
603
	);
604
}
605
public void testBug027079m() throws JavaModelException {
606
	this.formatterPrefs.disabling_tags = new char[][] { "    format:  	  off    ".toCharArray() };
607
	this.formatterPrefs.enabling_tags = new char[][] { "	format:	  	on	".toCharArray() };
608
	String source =
609
		"public class X01 {\n" + 
610
		"\n" + 
611
		"/* format: off */\n" + 
612
		"void     foo(    )      {	\n" + 
613
		"				//      formatted       comment\n" + 
614
		"}\n" + 
615
		"/* format: on */\n" + 
616
		"void     bar(    )      {	\n" + 
617
		"				//      formatted       comment\n" + 
618
		"}\n" + 
619
		"}\n";
620
	formatSource(source,
621
		"public class X01 {\n" + 
622
		"\n" + 
623
		"	/* format: off */\n" + 
624
		"	void foo() {\n" + 
625
		"		// formatted comment\n" + 
626
		"	}\n" + 
627
		"\n" + 
628
		"	/* format: on */\n" + 
629
		"	void bar() {\n" + 
630
		"		// formatted comment\n" + 
631
		"	}\n" + 
632
		"}\n"
633
	);
634
}
635
636
/**
69
 * @bug 198074: [formatter] the code formatter doesn't respect my new lines
637
 * @bug 198074: [formatter] the code formatter doesn't respect my new lines
70
 * @test Ensure that the formatter keep line breaks wrapping set by users in the code
638
 * @test Ensure that the formatter keep line breaks wrapping set by users in the code
71
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=198074"
639
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=198074"
(-)src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsTests.java (-1 / +1 lines)
Lines 258-264 Link Here
258
	this.workingCopies = new ICompilationUnit[1];
258
	this.workingCopies = new ICompilationUnit[1];
259
	this.workingCopies[0] = getCompilationUnit(JAVA_PROJECT.getElementName() , "", "test."+packageName, unitName); //$NON-NLS-1$ //$NON-NLS-2$
259
	this.workingCopies[0] = getCompilationUnit(JAVA_PROJECT.getElementName() , "", "test."+packageName, unitName); //$NON-NLS-1$ //$NON-NLS-2$
260
	String outputSource = getOutputSource(this.workingCopies[0]);
260
	String outputSource = getOutputSource(this.workingCopies[0]);
261
	formatSource(this.workingCopies[0].getSource(), outputSource, kind, indentationLevel, checkNull, offset, length, lineSeparator, true);
261
	formatSource(this.workingCopies[0].getSource(), outputSource, kind, indentationLevel, offset, length, lineSeparator, true);
262
}
262
}
263
263
264
/**
264
/**
(-)src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java (-8 / +15 lines)
Lines 220-225 Link Here
220
		assertSourceEquals("Different number of length", Util.convertToIndependantLineDelimiter(expectedContents), actualContents);
220
		assertSourceEquals("Different number of length", Util.convertToIndependantLineDelimiter(expectedContents), actualContents);
221
	}
221
	}
222
222
223
	void assertLineEquals(String actualContents, String originalSource, String expectedContents) {
224
		String outputSource = expectedContents == null ? originalSource : expectedContents;
225
		assertLineEquals(actualContents, originalSource, outputSource, false /* do not check null */);
226
	}
227
223
	DefaultCodeFormatter codeFormatter() {
228
	DefaultCodeFormatter codeFormatter() {
224
		if (this.formatterOptions == null) {
229
		if (this.formatterOptions == null) {
225
			this.formatterOptions = JAVA_PROJECT.getOptions(true);
230
			this.formatterOptions = JAVA_PROJECT.getOptions(true);
Lines 227-237 Link Here
227
		DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(this.formatterPrefs, this.formatterOptions);
232
		DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(this.formatterPrefs, this.formatterOptions);
228
		return codeFormatter;
233
		return codeFormatter;
229
	}
234
	}
230
	
231
	void assertLineEquals(String actualContents, String originalSource, String expectedContents) {
232
		String outputSource = expectedContents == null ? originalSource : expectedContents;
233
		assertLineEquals(actualContents, originalSource, outputSource, false /* do not check null */);
234
	}
235
235
236
	void formatSource(String source) {
236
	void formatSource(String source) {
237
		// expect unchanged source after formatting
237
		// expect unchanged source after formatting
Lines 279-289 Link Here
279
			}
279
			}
280
			assertLineEquals(result, newSource, formattedOutput);
280
			assertLineEquals(result, newSource, formattedOutput);
281
		} else {
281
		} else {
282
			formatSource(source, formattedOutput, kind, indentationLevel, false, 0, -1, null, repeat);
282
			formatSource(source, formattedOutput, kind, indentationLevel, 0, -1, null, repeat);
283
		}
283
		}
284
	}
284
	}
285
	
285
	
286
	void formatSource(String source, String formattedOutput, int kind, int indentationLevel, boolean checkNull, int offset, int length, String lineSeparator, boolean repeat) {
286
	void formatSource(String source, String formattedOutput, int kind, int indentationLevel, int offset, int length, String lineSeparator, boolean repeat) {
287
		DefaultCodeFormatter codeFormatter = codeFormatter();
287
		DefaultCodeFormatter codeFormatter = codeFormatter();
288
		String result;
288
		String result;
289
		if (length == -1) {
289
		if (length == -1) {
Lines 291-297 Link Here
291
		} else {
291
		} else {
292
			result = runFormatter(codeFormatter, source, kind, indentationLevel, offset, length, lineSeparator, repeat);
292
			result = runFormatter(codeFormatter, source, kind, indentationLevel, offset, length, lineSeparator, repeat);
293
		}
293
		}
294
		assertLineEquals(result, source, formattedOutput);
294
		if (lineSeparator == null) {
295
			assertLineEquals(result, source, formattedOutput);
296
		} else {
297
			// Do not convert line delimiter while comparing result when a specific one is specified
298
			assertNotNull("Error(s) occured while formatting", result);
299
			String outputSource = formattedOutput == null ? source : formattedOutput;
300
			assertSourceEquals("Different number of length", outputSource, result, false/*do not convert line delimiter*/);
301
		}
295
	}
302
	}
296
303
297
304
(-)src/org/eclipse/jdt/core/tests/formatter/comment/SingleLineTestCase.java (-5 / +5 lines)
Lines 48-54 Link Here
48
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "5"); //$NON-NLS-1$
48
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "5"); //$NON-NLS-1$
49
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_BLOCK_COMMENT, DefaultCodeFormatterConstants.FALSE);
49
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_BLOCK_COMMENT, DefaultCodeFormatterConstants.FALSE);
50
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_JAVADOC_COMMENT, DefaultCodeFormatterConstants.FALSE);
50
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_JAVADOC_COMMENT, DefaultCodeFormatterConstants.FALSE);
51
		String expected = PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + DELIMITER + PREFIX + "test";
51
		String expected = PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + "//" + DELIMITER + PREFIX + "test";
52
		assertEquals(expected, testFormat("//test\t\ttest" + DELIMITER + PREFIX + DELIMITER + "//\t\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
52
		assertEquals(expected, testFormat("//test\t\ttest" + DELIMITER + PREFIX + DELIMITER + "//\t\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
53
	}
53
	}
54
54
Lines 192-201 Link Here
192
	public void testMultipleComments2() {
192
	public void testMultipleComments2() {
193
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "12"); //$NON-NLS-1$
193
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "12"); //$NON-NLS-1$
194
		String expected = "// test test" + DELIMITER +
194
		String expected = "// test test" + DELIMITER +
195
			"// test" + DELIMITER +
195
				"// test" + DELIMITER +
196
			"// " + DELIMITER +
196
				"//" + DELIMITER +
197
			"// test test" + DELIMITER +
197
				"// test test" + DELIMITER +
198
			"// test test";
198
				"// test test";
199
		assertEquals(expected, testFormat("//test test\ttest" + DELIMITER + PREFIX + DELIMITER + PREFIX + "test test test test")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
199
		assertEquals(expected, testFormat("//test test\ttest" + DELIMITER + PREFIX + DELIMITER + PREFIX + "test test test test")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
200
	}
200
	}
201
201
(-)src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java (-1 / +11 lines)
Lines 725-735 Link Here
725
	 * The line separators in 'actual' are converted to '\n' before the comparison.
725
	 * The line separators in 'actual' are converted to '\n' before the comparison.
726
	 */
726
	 */
727
	protected void assertSourceEquals(String message, String expected, String actual) {
727
	protected void assertSourceEquals(String message, String expected, String actual) {
728
		assertSourceEquals(message, expected, actual, true/*convert line delimiter*/);
729
	}
730
	/*
731
	 * Asserts that the given actual source is equal to the expected one.
732
	 * Note that if the line separators in 'actual' are converted to '\n' before the comparison,
733
	 * 'expected' is assumed to have the same '\n' line separator.
734
	 */
735
	protected void assertSourceEquals(String message, String expected, String actual, boolean convert) {
728
		if (actual == null) {
736
		if (actual == null) {
729
			assertEquals(message, expected, null);
737
			assertEquals(message, expected, null);
730
			return;
738
			return;
731
		}
739
		}
732
		actual = org.eclipse.jdt.core.tests.util.Util.convertToIndependantLineDelimiter(actual);
740
		if (convert) {
741
			actual = org.eclipse.jdt.core.tests.util.Util.convertToIndependantLineDelimiter(actual);
742
		}
733
		if (!actual.equals(expected)) {
743
		if (!actual.equals(expected)) {
734
			System.out.println("Expected source in "+getName()+" should be:");
744
			System.out.println("Expected source in "+getName()+" should be:");
735
			System.out.print(org.eclipse.jdt.core.tests.util.Util.displayString(actual.toString(), 2));
745
			System.out.print(org.eclipse.jdt.core.tests.util.Util.displayString(actual.toString(), 2));

Return to bug 27079