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

Collapse All | Expand All

(-)formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java (-95 / +22 lines)
Lines 31-42 Link Here
31
import org.eclipse.jdt.internal.formatter.comment.CommentRegion;
31
import org.eclipse.jdt.internal.formatter.comment.CommentRegion;
32
import org.eclipse.jdt.internal.formatter.comment.JavaDocRegion;
32
import org.eclipse.jdt.internal.formatter.comment.JavaDocRegion;
33
import org.eclipse.jdt.internal.formatter.comment.MultiCommentRegion;
33
import org.eclipse.jdt.internal.formatter.comment.MultiCommentRegion;
34
import org.eclipse.jface.text.Document;
35
import org.eclipse.jface.text.IDocument;
34
import org.eclipse.jface.text.IDocument;
36
import org.eclipse.jface.text.IRegion;
35
import org.eclipse.jface.text.IRegion;
37
import org.eclipse.jface.text.Position;
36
import org.eclipse.jface.text.Position;
38
import org.eclipse.jface.text.Region;
37
import org.eclipse.jface.text.Region;
39
import org.eclipse.text.edits.MultiTextEdit;
40
import org.eclipse.text.edits.TextEdit;
38
import org.eclipse.text.edits.TextEdit;
41
39
42
public class DefaultCodeFormatter extends CodeFormatter {
40
public class DefaultCodeFormatter extends CodeFormatter {
Lines 56-70 Link Here
56
		| K_MULTI_LINE_COMMENT
54
		| K_MULTI_LINE_COMMENT
57
		| K_JAVA_DOC;
55
		| K_JAVA_DOC;
58
56
59
	/*
60
	 * Temporary internal statics to enable new comments formatter
61
	 * see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=102780
62
	 * see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=227043
63
	 * TODO (frederic) remove in 3.5
64
	 */
65
	private final static String NEW_COMMENTS_FORMAT = System.getProperty("org.eclipse.jdt.core.formatter.comments.new"); //$NON-NLS-1$
66
	public static boolean ENABLE_NEW_COMMENTS_FORMAT = !JavaCore.DISABLED.equals(NEW_COMMENTS_FORMAT);
67
68
	// Scanner use to probe the kind of the source given to the formatter
57
	// Scanner use to probe the kind of the source given to the formatter
69
	private static Scanner PROBING_SCANNER;
58
	private static Scanner PROBING_SCANNER;
70
59
Lines 174-190 Link Here
174
			case K_JAVA_DOC :
163
			case K_JAVA_DOC :
175
				// https://bugs.eclipse.org/bugs/show_bug.cgi?id=102780
164
				// https://bugs.eclipse.org/bugs/show_bug.cgi?id=102780
176
				// use the integrated comment formatter to format comment
165
				// use the integrated comment formatter to format comment
177
				if (ENABLE_NEW_COMMENTS_FORMAT) {
166
                return formatComment(kind & K_MASK, source, indentationLevel, lineSeparator, new IRegion[] {new Region(offset, length)});
178
	                return formatComment(kind & K_MASK, source, indentationLevel, lineSeparator, new IRegion[] {new Region(offset, length)});
179
				}
180
				// $FALL-THROUGH$ - fall through next case when old comment formatter is activated
167
				// $FALL-THROUGH$ - fall through next case when old comment formatter is activated
181
			case K_MULTI_LINE_COMMENT :
168
			case K_MULTI_LINE_COMMENT :
182
			case K_SINGLE_LINE_COMMENT :
169
			case K_SINGLE_LINE_COMMENT :
183
				if (ENABLE_NEW_COMMENTS_FORMAT) {
170
                return formatComment(kind & K_MASK, source, indentationLevel, lineSeparator, new IRegion[] {new Region(offset, length)});
184
	                return formatComment(kind & K_MASK, source, indentationLevel, lineSeparator, new IRegion[] {new Region(offset, length)});
185
				}
186
				this.codeSnippetParsingUtil = new CodeSnippetParsingUtil();
187
				return formatComment(kind, source, indentationLevel, lineSeparator, new IRegion[] {new Region(offset, length)}, false);
188
		}
171
		}
189
172
190
		return format(kind, source, new IRegion[] {new Region(offset, length)}, indentationLevel, lineSeparator);
173
		return format(kind, source, new IRegion[] {new Region(offset, length)}, indentationLevel, lineSeparator);
Lines 199-216 Link Here
199
		}
182
		}
200
183
201
		this.codeSnippetParsingUtil = new CodeSnippetParsingUtil();
184
		this.codeSnippetParsingUtil = new CodeSnippetParsingUtil();
185
		boolean includeComments =  (kind & F_INCLUDE_COMMENTS) != 0;
202
		switch(kind & K_MASK) {
186
		switch(kind & K_MASK) {
203
			case K_CLASS_BODY_DECLARATIONS :
187
			case K_CLASS_BODY_DECLARATIONS :
204
				return formatClassBodyDeclarations(source, indentationLevel, lineSeparator, regions);
188
				return formatClassBodyDeclarations(source, indentationLevel, lineSeparator, regions, includeComments);
205
			case K_COMPILATION_UNIT :
189
			case K_COMPILATION_UNIT :
206
				boolean includeComments =  (kind & F_INCLUDE_COMMENTS) != 0; // || FORCE_NEW_COMMENTS_FORMAT;
207
				return formatCompilationUnit(source, indentationLevel, lineSeparator, regions, includeComments);
190
				return formatCompilationUnit(source, indentationLevel, lineSeparator, regions, includeComments);
208
			case K_EXPRESSION :
191
			case K_EXPRESSION :
209
				return formatExpression(source, indentationLevel, lineSeparator, regions);
192
				return formatExpression(source, indentationLevel, lineSeparator, regions, includeComments);
210
			case K_STATEMENTS :
193
			case K_STATEMENTS :
211
				return formatStatements(source, indentationLevel, lineSeparator, regions);
194
				return formatStatements(source, indentationLevel, lineSeparator, regions, includeComments);
212
			case K_UNKNOWN :
195
			case K_UNKNOWN :
213
				includeComments =  (kind & F_INCLUDE_COMMENTS) != 0; // || FORCE_NEW_COMMENTS_FORMAT;
214
				return probeFormatting(source, indentationLevel, lineSeparator, regions, includeComments);
196
				return probeFormatting(source, indentationLevel, lineSeparator, regions, includeComments);
215
			case K_JAVA_DOC :
197
			case K_JAVA_DOC :
216
			case K_MULTI_LINE_COMMENT :
198
			case K_MULTI_LINE_COMMENT :
Lines 221-234 Link Here
221
		return null;
203
		return null;
222
	}
204
	}
223
205
224
	private TextEdit formatClassBodyDeclarations(String source, int indentationLevel, String lineSeparator, IRegion[] regions) {
206
	private TextEdit formatClassBodyDeclarations(String source, int indentationLevel, String lineSeparator, IRegion[] regions, boolean includeComments) {
225
		ASTNode[] bodyDeclarations = this.codeSnippetParsingUtil.parseClassBodyDeclarations(source.toCharArray(), getDefaultCompilerOptions(), true);
207
		ASTNode[] bodyDeclarations = this.codeSnippetParsingUtil.parseClassBodyDeclarations(source.toCharArray(), getDefaultCompilerOptions(), true);
226
208
227
		if (bodyDeclarations == null) {
209
		if (bodyDeclarations == null) {
228
			// a problem occurred while parsing the source
210
			// a problem occurred while parsing the source
229
			return null;
211
			return null;
230
		}
212
		}
231
		return internalFormatClassBodyDeclarations(source, indentationLevel, lineSeparator, bodyDeclarations, regions);
213
		return internalFormatClassBodyDeclarations(source, indentationLevel, lineSeparator, bodyDeclarations, regions, includeComments);
232
	}
214
	}
233
215
234
	/*
216
	/*
Lines 271-325 Link Here
271
		return null;
253
		return null;
272
	}
254
	}
273
255
274
	/**
275
	 * Returns the resulting text edit after formatting the given comment.
276
	 *
277
	 * @param kind the given kind
278
	 * @param source the given source
279
	 * @param indentationLevel the given indentation level
280
	 * @param lineSeparator the given line separator
281
	 * @param regions the given regions
282
	 * @param includeComments TODO
283
	 * @return the resulting text edit
284
	 */
285
	private TextEdit formatComment(int kind, String source, int indentationLevel, String lineSeparator, IRegion[] regions, boolean includeComments) {
286
		Object oldOption = oldCommentFormatOption();
287
		boolean isFormattingComments = false;
288
		if (oldOption == null) {
289
			switch (kind & K_MASK) {
290
				case K_SINGLE_LINE_COMMENT:
291
					isFormattingComments = DefaultCodeFormatterConstants.TRUE.equals(this.options.get(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_LINE_COMMENT));
292
					break;
293
				case K_MULTI_LINE_COMMENT:
294
					isFormattingComments = DefaultCodeFormatterConstants.TRUE.equals(this.options.get(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_BLOCK_COMMENT));
295
					break;
296
				case K_JAVA_DOC:
297
					isFormattingComments = DefaultCodeFormatterConstants.TRUE.equals(this.options.get(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_JAVADOC_COMMENT));
298
			}
299
		} else {
300
			isFormattingComments = DefaultCodeFormatterConstants.TRUE.equals(oldOption);
301
		}
302
		if (isFormattingComments) {
303
			if (lineSeparator != null) {
304
				this.preferences.line_separator = lineSeparator;
305
			} else {
306
				this.preferences.line_separator = Util.LINE_SEPARATOR;
307
			}
308
			this.preferences.initial_indentation_level = indentationLevel;
309
			this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, null, includeComments);
310
311
			IRegion coveredRegion = getCoveredRegion(regions);
312
			int offset = coveredRegion.getOffset();
313
			int length = coveredRegion.getLength();
314
315
			final CommentRegion region = createRegion(kind, new Document(source), new Position(offset, length), this.newCodeFormatter);
316
			if (region != null) {
317
				return this.newCodeFormatter.format(source, region);
318
			}
319
		}
320
		return new MultiTextEdit();
321
	}
322
323
	private TextEdit formatCompilationUnit(String source, int indentationLevel, String lineSeparator, IRegion[] regions, boolean includeComments) {
256
	private TextEdit formatCompilationUnit(String source, int indentationLevel, String lineSeparator, IRegion[] regions, boolean includeComments) {
324
		CompilationUnitDeclaration compilationUnitDeclaration = this.codeSnippetParsingUtil.parseCompilationUnit(source.toCharArray(), getDefaultCompilerOptions(), true);
257
		CompilationUnitDeclaration compilationUnitDeclaration = this.codeSnippetParsingUtil.parseCompilationUnit(source.toCharArray(), getDefaultCompilerOptions(), true);
325
258
Lines 335-358 Link Here
335
		return this.newCodeFormatter.format(source, compilationUnitDeclaration);
268
		return this.newCodeFormatter.format(source, compilationUnitDeclaration);
336
	}
269
	}
337
270
338
	private TextEdit formatExpression(String source, int indentationLevel, String lineSeparator, IRegion[] regions) {
271
	private TextEdit formatExpression(String source, int indentationLevel, String lineSeparator, IRegion[] regions, boolean includeComments) {
339
		Expression expression = this.codeSnippetParsingUtil.parseExpression(source.toCharArray(), getDefaultCompilerOptions(), true);
272
		Expression expression = this.codeSnippetParsingUtil.parseExpression(source.toCharArray(), getDefaultCompilerOptions(), true);
340
273
341
		if (expression == null) {
274
		if (expression == null) {
342
			// a problem occurred while parsing the source
275
			// a problem occurred while parsing the source
343
			return null;
276
			return null;
344
		}
277
		}
345
		return internalFormatExpression(source, indentationLevel, lineSeparator, expression, regions);
278
		return internalFormatExpression(source, indentationLevel, lineSeparator, expression, regions, includeComments);
346
	}
279
	}
347
280
348
	private TextEdit formatStatements(String source, int indentationLevel, String lineSeparator, IRegion[] regions) {
281
	private TextEdit formatStatements(String source, int indentationLevel, String lineSeparator, IRegion[] regions, boolean includeComments) {
349
		ConstructorDeclaration constructorDeclaration = this.codeSnippetParsingUtil.parseStatements(source.toCharArray(), getDefaultCompilerOptions(), true, false);
282
		ConstructorDeclaration constructorDeclaration = this.codeSnippetParsingUtil.parseStatements(source.toCharArray(), getDefaultCompilerOptions(), true, false);
350
283
351
		if (constructorDeclaration.statements == null) {
284
		if (constructorDeclaration.statements == null) {
352
			// a problem occured while parsing the source
285
			// a problem occured while parsing the source
353
			return null;
286
			return null;
354
		}
287
		}
355
		return internalFormatStatements(source, indentationLevel, lineSeparator, constructorDeclaration, regions);
288
		return internalFormatStatements(source, indentationLevel, lineSeparator, constructorDeclaration, regions, includeComments);
356
	}
289
	}
357
290
358
	private IRegion getCoveredRegion(IRegion[] regions) {
291
	private IRegion getCoveredRegion(IRegion[] regions) {
Lines 443-449 Link Here
443
		return this.defaultCompilerOptions;
376
		return this.defaultCompilerOptions;
444
	}
377
	}
445
378
446
	private TextEdit internalFormatClassBodyDeclarations(String source, int indentationLevel, String lineSeparator, ASTNode[] bodyDeclarations, IRegion[] regions) {
379
	private TextEdit internalFormatClassBodyDeclarations(String source, int indentationLevel, String lineSeparator, ASTNode[] bodyDeclarations, IRegion[] regions, boolean includeComments) {
447
		if (lineSeparator != null) {
380
		if (lineSeparator != null) {
448
			this.preferences.line_separator = lineSeparator;
381
			this.preferences.line_separator = lineSeparator;
449
		} else {
382
		} else {
Lines 451-461 Link Here
451
		}
384
		}
452
		this.preferences.initial_indentation_level = indentationLevel;
385
		this.preferences.initial_indentation_level = indentationLevel;
453
386
454
		this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil, false);
387
		this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil, includeComments);
455
		return this.newCodeFormatter.format(source, bodyDeclarations);
388
		return this.newCodeFormatter.format(source, bodyDeclarations);
456
	}
389
	}
457
390
458
	private TextEdit internalFormatExpression(String source, int indentationLevel, String lineSeparator, Expression expression, IRegion[] regions) {
391
	private TextEdit internalFormatExpression(String source, int indentationLevel, String lineSeparator, Expression expression, IRegion[] regions, boolean includeComments) {
459
		if (lineSeparator != null) {
392
		if (lineSeparator != null) {
460
			this.preferences.line_separator = lineSeparator;
393
			this.preferences.line_separator = lineSeparator;
461
		} else {
394
		} else {
Lines 463-475 Link Here
463
		}
396
		}
464
		this.preferences.initial_indentation_level = indentationLevel;
397
		this.preferences.initial_indentation_level = indentationLevel;
465
398
466
		this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil, false);
399
		this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil, includeComments);
467
400
468
		TextEdit textEdit = this.newCodeFormatter.format(source, expression);
401
		TextEdit textEdit = this.newCodeFormatter.format(source, expression);
469
		return textEdit;
402
		return textEdit;
470
	}
403
	}
471
404
472
	private TextEdit internalFormatStatements(String source, int indentationLevel, String lineSeparator, ConstructorDeclaration constructorDeclaration, IRegion[] regions) {
405
	private TextEdit internalFormatStatements(String source, int indentationLevel, String lineSeparator, ConstructorDeclaration constructorDeclaration, IRegion[] regions, boolean includeComments) {
473
		if (lineSeparator != null) {
406
		if (lineSeparator != null) {
474
			this.preferences.line_separator = lineSeparator;
407
			this.preferences.line_separator = lineSeparator;
475
		} else {
408
		} else {
Lines 477-483 Link Here
477
		}
410
		}
478
		this.preferences.initial_indentation_level = indentationLevel;
411
		this.preferences.initial_indentation_level = indentationLevel;
479
412
480
		this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil, false);
413
		this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil, includeComments);
481
414
482
		return this.newCodeFormatter.format(source, constructorDeclaration);
415
		return this.newCodeFormatter.format(source, constructorDeclaration);
483
	}
416
	}
Lines 523-535 Link Here
523
					break;
456
					break;
524
			}
457
			}
525
			if (kind != -1) {
458
			if (kind != -1) {
526
				// https://bugs.eclipse.org/bugs/show_bug.cgi?id=227043
459
				return formatComment(kind, source, indentationLevel, lineSeparator, regions);
527
				// https://bugs.eclipse.org/bugs/show_bug.cgi?id=102780
528
				// use the integrated comment formatter to format comment
529
				if (ENABLE_NEW_COMMENTS_FORMAT /*|| FORCE_NEW_COMMENTS_FORMAT*/) {
530
					return formatComment(kind, source, indentationLevel, lineSeparator, regions);
531
				}
532
				return formatComment(kind, source, indentationLevel, lineSeparator, regions, includeComments);
533
			}
460
			}
534
		} catch (InvalidInputException e) {
461
		} catch (InvalidInputException e) {
535
			// ignore
462
			// ignore
Lines 539-557 Link Here
539
		// probe for expression
466
		// probe for expression
540
		Expression expression = this.codeSnippetParsingUtil.parseExpression(source.toCharArray(), getDefaultCompilerOptions(), true);
467
		Expression expression = this.codeSnippetParsingUtil.parseExpression(source.toCharArray(), getDefaultCompilerOptions(), true);
541
		if (expression != null) {
468
		if (expression != null) {
542
			return internalFormatExpression(source, indentationLevel, lineSeparator, expression, regions);
469
			return internalFormatExpression(source, indentationLevel, lineSeparator, expression, regions, includeComments);
543
		}
470
		}
544
471
545
		// probe for body declarations (fields, methods, constructors)
472
		// probe for body declarations (fields, methods, constructors)
546
		ASTNode[] bodyDeclarations = this.codeSnippetParsingUtil.parseClassBodyDeclarations(source.toCharArray(), getDefaultCompilerOptions(), true);
473
		ASTNode[] bodyDeclarations = this.codeSnippetParsingUtil.parseClassBodyDeclarations(source.toCharArray(), getDefaultCompilerOptions(), true);
547
		if (bodyDeclarations != null) {
474
		if (bodyDeclarations != null) {
548
			return internalFormatClassBodyDeclarations(source, indentationLevel, lineSeparator, bodyDeclarations, regions);
475
			return internalFormatClassBodyDeclarations(source, indentationLevel, lineSeparator, bodyDeclarations, regions, includeComments);
549
		}
476
		}
550
477
551
		// probe for statements
478
		// probe for statements
552
		ConstructorDeclaration constructorDeclaration = this.codeSnippetParsingUtil.parseStatements(source.toCharArray(), getDefaultCompilerOptions(), true, false);
479
		ConstructorDeclaration constructorDeclaration = this.codeSnippetParsingUtil.parseStatements(source.toCharArray(), getDefaultCompilerOptions(), true, false);
553
		if (constructorDeclaration.statements != null) {
480
		if (constructorDeclaration.statements != null) {
554
			return internalFormatStatements(source, indentationLevel, lineSeparator, constructorDeclaration, regions);
481
			return internalFormatStatements(source, indentationLevel, lineSeparator, constructorDeclaration, regions, includeComments);
555
		}
482
		}
556
483
557
		// this has to be a compilation unit
484
		// this has to be a compilation unit
(-)src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java (-3 / +137 lines)
Lines 2200-2207 Link Here
2200
		"	  return null;\n" +
2200
		"	  return null;\n" +
2201
		"  }\n" +
2201
		"  }\n" +
2202
		"}\n";
2202
		"}\n";
2203
	// TODO (frederic) line comment should be formatted when F_INCLUDE_COMMENTS
2204
	// flag will work for all snippet kinds
2205
	formatSource(source,
2203
	formatSource(source,
2206
		"/**\n" +
2204
		"/**\n" +
2207
		" * Need a javadoc comment before to get the exception.\n" +
2205
		" * Need a javadoc comment before to get the exception.\n" +
Lines 2213-2219 Link Here
2213
		"	 * If there is an authority, it is:\n" +
2211
		"	 * If there is an authority, it is:\n" +
2214
		"	 * \n" +
2212
		"	 * \n" +
2215
		"	 * <pre>\n" +
2213
		"	 * <pre>\n" +
2216
		"	 * //class	body		snippet\n" +
2214
		"	 * // class body snippet\n" + 
2217
		"	 * public class X {\n" +
2215
		"	 * public class X {\n" +
2218
		"	 * }\n" +
2216
		"	 * }\n" +
2219
		"	 * </pre>\n" +
2217
		"	 * </pre>\n" +
Lines 2265-2270 Link Here
2265
}
2263
}
2266
2264
2267
/**
2265
/**
2266
 * @bug 236406: [formatter] Formatting qualified invocations can be broken when the Line Wrapping policy forces element to be on a new line
2267
 * @test Verify that wrapping policies forcing the first element to be on a new line are working again...
2268
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=236406"
2269
 */
2270
public void testBug236406_CDB1() {
2271
	String source = 
2272
		"/**        Javadoc		comment    	    */void foo1() {System.out.println();}\n" +
2273
		"//        Line		comment    	    \n" +
2274
		"void foo2() {System.out.println();}\n" +
2275
		"/*        Block		comment    	    */\n" +
2276
		"void foo3() {\n" +
2277
		"/*        statement Block		comment    	    */\n" +
2278
		"System.out.println();}\n";
2279
	formatSource(source,
2280
		"/**        Javadoc		comment    	    */\n" + 
2281
		"void foo1() {\n" + 
2282
		"	System.out.println();\n" + 
2283
		"}\n" + 
2284
		"\n" + 
2285
		"//        Line		comment    	    \n" + 
2286
		"void foo2() {\n" + 
2287
		"	System.out.println();\n" + 
2288
		"}\n" + 
2289
		"\n" + 
2290
		"/*        Block		comment    	    */\n" +
2291
		"void foo3() {\n" +
2292
		"	/*        statement Block		comment    	    */\n" + 
2293
		"	System.out.println();\n" + 
2294
		"}",
2295
		CodeFormatter.K_CLASS_BODY_DECLARATIONS
2296
	);
2297
}
2298
public void testBug236406_CDB2() {
2299
	String source = 
2300
		"/**        Javadoc		comment    	    */void foo1() {System.out.println();}\n" +
2301
		"//        Line		comment    	    \n" +
2302
		"void foo2() {System.out.println();}\n" +
2303
		"/*        Block		comment    	    */\n" +
2304
		"void foo3() {\n" +
2305
		"/*        statement Block		comment    	    */\n" +
2306
		"System.out.println();}\n";
2307
	formatSource(source,
2308
		"/** Javadoc comment */\n" + 
2309
		"void foo1() {\n" + 
2310
		"	System.out.println();\n" + 
2311
		"}\n" + 
2312
		"\n" + 
2313
		"// Line comment\n" + 
2314
		"void foo2() {\n" + 
2315
		"	System.out.println();\n" + 
2316
		"}\n" + 
2317
		"\n" + 
2318
		"/* Block comment */\n" +
2319
		"void foo3() {\n" + 
2320
		"	/* statement Block comment */\n" + 
2321
		"	System.out.println();\n" + 
2322
		"}",
2323
		CodeFormatter.K_CLASS_BODY_DECLARATIONS | CodeFormatter.F_INCLUDE_COMMENTS
2324
	);
2325
}
2326
public void testBug236406_EX1() {
2327
	String source = 
2328
		"//        Line		comment    	    \n" +
2329
		"i = \n" +
2330
		"/**        Javadoc		comment    	    */\n" +
2331
		"1     +     (/*      Block		comment*/++a)\n";
2332
	formatSource(source,
2333
		"//        Line		comment    	    \n" + 
2334
		"i =\n" + 
2335
		"/**        Javadoc		comment    	    */\n" + 
2336
		"1 + (/*      Block		comment*/++a)",
2337
		CodeFormatter.K_EXPRESSION
2338
	);
2339
}
2340
public void testBug236406_EX2() {
2341
	String source = 
2342
		"//        Line		comment    	    \n" +
2343
		"i = \n" +
2344
		"/**        Javadoc		comment    	    */\n" +
2345
		"1     +     (/*      Block		comment*/++a)\n";
2346
	formatSource(source,
2347
		"// Line comment\n" + 
2348
		"i =\n" + 
2349
		"/** Javadoc comment */\n" + 
2350
		"1 + (/* Block comment */++a)",
2351
		CodeFormatter.K_EXPRESSION | CodeFormatter.F_INCLUDE_COMMENTS
2352
	);
2353
}
2354
public void testBug236406_ST1() {
2355
	String source = 
2356
		"/**        Javadoc		comment    	    */foo1();\n" +
2357
		"//        Line		comment    	    \n" +
2358
		"foo2();\n" +
2359
		"/*        Block		comment    	    */\n" +
2360
		"foo3(); {\n" +
2361
		"/*        indented Block		comment    	    */\n" +
2362
		"System.out.println();}\n";
2363
	formatSource(source,
2364
		"/**        Javadoc		comment    	    */\n" + 
2365
		"foo1();\n" + 
2366
		"//        Line		comment    	    \n" + 
2367
		"foo2();\n" + 
2368
		"/*        Block		comment    	    */\n" + 
2369
		"foo3();\n" + 
2370
		"{\n" + 
2371
		"	/*        indented Block		comment    	    */\n" + 
2372
		"	System.out.println();\n" + 
2373
		"}",
2374
		CodeFormatter.K_STATEMENTS
2375
	);
2376
}
2377
public void testBug236406_ST2() {
2378
	String source = 
2379
		"/**        Javadoc		comment    	    */foo1();\n" +
2380
		"//        Line		comment    	    \n" +
2381
		"foo2();\n" +
2382
		"/*        Block		comment    	    */\n" +
2383
		"foo3(); {\n" +
2384
		"/*        indented Block		comment    	    */\n" +
2385
		"System.out.println();}\n";
2386
	formatSource(source,
2387
		"/** Javadoc comment */\n" + 
2388
		"foo1();\n" + 
2389
		"// Line comment\n" + 
2390
		"foo2();\n" + 
2391
		"/* Block comment */\n" + 
2392
		"foo3();\n" + 
2393
		"{\n" + 
2394
		"	/* indented Block comment */\n" + 
2395
		"	System.out.println();\n" + 
2396
		"}",
2397
		CodeFormatter.K_STATEMENTS | CodeFormatter.F_INCLUDE_COMMENTS
2398
	);
2399
}
2400
2401
/**
2268
 * @bug 237051: [formatter] Formatter insert blank lines after javadoc if javadoc contains Commons Attributes @@ annotations
2402
 * @bug 237051: [formatter] Formatter insert blank lines after javadoc if javadoc contains Commons Attributes @@ annotations
2269
 * @test Ensure that Commons Attributes @@ annotations do not screw up the comment formatter
2403
 * @test Ensure that Commons Attributes @@ annotations do not screw up the comment formatter
2270
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=237051"
2404
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=237051"
(-)src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java (+4 lines)
Lines 239-244 Link Here
239
	void formatSource(String source, String formattedOutput) {
239
	void formatSource(String source, String formattedOutput) {
240
		formatSource(source, formattedOutput, CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, 0, true /*repeat formatting twice*/);
240
		formatSource(source, formattedOutput, CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, 0, true /*repeat formatting twice*/);
241
	}
241
	}
242
243
	void formatSource(String source, String formattedOutput, int kind) {
244
		formatSource(source, formattedOutput, kind, 0, true /*repeat formatting twice*/);
245
	}
242
	
246
	
243
	void formatSource(String source, String formattedOutput, boolean repeat) {
247
	void formatSource(String source, String formattedOutput, boolean repeat) {
244
		formatSource(source, formattedOutput, CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, 0, repeat);
248
		formatSource(source, formattedOutput, CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, 0, repeat);
(-)src/org/eclipse/jdt/core/tests/formatter/comment/JavaDocTestCase.java (-48 / +30 lines)
Lines 19-25 Link Here
19
import org.eclipse.jdt.core.formatter.CodeFormatter;
19
import org.eclipse.jdt.core.formatter.CodeFormatter;
20
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
20
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
21
21
22
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter;
23
import org.eclipse.jdt.internal.formatter.comment.JavaDocLine;
22
import org.eclipse.jdt.internal.formatter.comment.JavaDocLine;
24
import org.eclipse.jdt.internal.formatter.comment.MultiCommentLine;
23
import org.eclipse.jdt.internal.formatter.comment.MultiCommentLine;
25
import org.eclipse.text.edits.TextEdit;
24
import org.eclipse.text.edits.TextEdit;
Lines 698-704 Link Here
698
697
699
		String expected = "/**" + DELIMITER +
698
		String expected = "/**" + DELIMITER +
700
				" * <pre>" + DELIMITER +
699
				" * <pre>" + DELIMITER +
701
				(DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT ? " * " +  DELIMITER : "") +
700
				" * " +  DELIMITER +
702
				" * </pre>" + DELIMITER +
701
				" * </pre>" + DELIMITER +
703
				" * " +  DELIMITER +
702
				" * " +  DELIMITER +
704
				" * " + DELIMITER +
703
				" * " + DELIMITER +
Lines 876-898 Link Here
876
				" * </code>" + DELIMITER +
875
				" * </code>" + DELIMITER +
877
				" */";
876
				" */";
878
877
879
		String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
880
			? input // do not change as <code> is an immutable tag
881
			:	"/**" + DELIMITER +
882
				" * <code>" + DELIMITER +
883
				" * <pre>" + DELIMITER +
884
				" * setLeadingComment(&quot;/* traditional comment &#42;/&quot;); // correct" + DELIMITER +
885
				" * setLeadingComment(&quot;missing comment delimiters&quot;); // wrong" + DELIMITER +
886
				" * setLeadingComment(&quot;/* unterminated traditional comment &quot;); // wrong" + DELIMITER +
887
				" * setLeadingComment(&quot;/* broken\\n traditional comment &#42;/&quot;); // correct" + DELIMITER +
888
				" * setLeadingComment(&quot;// end-of-line comment\\n&quot;); // correct" + DELIMITER +
889
				" * setLeadingComment(&quot;// end-of-line comment without line terminator&quot;); // correct" + DELIMITER +
890
				" * setLeadingComment(&quot;// broken\\n end-of-line comment\\n&quot;); // wrong" + DELIMITER +
891
				" * </pre>" + DELIMITER +
892
				" * </code>" + DELIMITER +
893
				" */";
894
		String result=testFormat(input, options);
878
		String result=testFormat(input, options);
895
		assertEquals(expected, result);
879
		assertEquals(input, result);
896
	}
880
	}
897
881
898
	public void test109636_2() {
882
	public void test109636_2() {
Lines 940-976 Link Here
940
	}
924
	}
941
925
942
	public void test109636_4() {
926
	public void test109636_4() {
943
		if (DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT) {
927
		Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings();
944
			Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings();
928
929
		String input =
930
				"/**" + DELIMITER +
931
				" * <pre>" + DELIMITER +
932
				" * setLeadingComment(\"/&#42; traditional comment &#42;/\");  // correct" + DELIMITER +
933
				" * setLeadingComment(\"missing comment delimiters\");  // wrong" + DELIMITER +
934
				" * setLeadingComment(\"/&#42; unterminated traditional comment \");  // wrong" + DELIMITER +
935
				" * setLeadingComment(\"/&#42; broken\\n traditional comment &#42;/\");  // correct" + DELIMITER +
936
				" * setLeadingComment(\"// end-of-line comment\\n\");  // correct" + DELIMITER +
937
				" * setLeadingComment(\"// end-of-line comment without line terminator\");  // correct" + DELIMITER +
938
				" * setLeadingComment(\"// broken\\n end-of-line comment\\n\");  // wrong" + DELIMITER +
939
				" * </pre>" + DELIMITER +
940
				" */";
945
941
946
			String input =
942
		String expected =
947
					"/**" + DELIMITER +
943
				"/**" + DELIMITER +
948
					" * <pre>" + DELIMITER +
944
				" * <pre>" + DELIMITER +
949
					" * setLeadingComment(\"/&#42; traditional comment &#42;/\");  // correct" + DELIMITER +
945
				" * setLeadingComment(&quot;/* traditional comment &#42;/&quot;); // correct" + DELIMITER +
950
					" * setLeadingComment(\"missing comment delimiters\");  // wrong" + DELIMITER +
946
				" * setLeadingComment(&quot;missing comment delimiters&quot;); // wrong" + DELIMITER +
951
					" * setLeadingComment(\"/&#42; unterminated traditional comment \");  // wrong" + DELIMITER +
947
				" * setLeadingComment(&quot;/* unterminated traditional comment &quot;); // wrong" + DELIMITER +
952
					" * setLeadingComment(\"/&#42; broken\\n traditional comment &#42;/\");  // correct" + DELIMITER +
948
				" * setLeadingComment(&quot;/* broken\\n traditional comment &#42;/&quot;); // correct" + DELIMITER +
953
					" * setLeadingComment(\"// end-of-line comment\\n\");  // correct" + DELIMITER +
949
				" * setLeadingComment(&quot;// end-of-line comment\\n&quot;); // correct" + DELIMITER +
954
					" * setLeadingComment(\"// end-of-line comment without line terminator\");  // correct" + DELIMITER +
950
				" * setLeadingComment(&quot;// end-of-line comment without line terminator&quot;); // correct" + DELIMITER +
955
					" * setLeadingComment(\"// broken\\n end-of-line comment\\n\");  // wrong" + DELIMITER +
951
				" * setLeadingComment(&quot;// broken\\n end-of-line comment\\n&quot;); // wrong" + DELIMITER +
956
					" * </pre>" + DELIMITER +
952
				" * </pre>" + DELIMITER +
957
					" */";
953
				" */";
958
954
		String result=testFormat(input, options);
959
			String expected =
955
		assertEquals(expected, result);
960
					"/**" + DELIMITER +
961
					" * <pre>" + DELIMITER +
962
					" * setLeadingComment(&quot;/* traditional comment &#42;/&quot;); // correct" + DELIMITER +
963
					" * setLeadingComment(&quot;missing comment delimiters&quot;); // wrong" + DELIMITER +
964
					" * setLeadingComment(&quot;/* unterminated traditional comment &quot;); // wrong" + DELIMITER +
965
					" * setLeadingComment(&quot;/* broken\\n traditional comment &#42;/&quot;); // correct" + DELIMITER +
966
					" * setLeadingComment(&quot;// end-of-line comment\\n&quot;); // correct" + DELIMITER +
967
					" * setLeadingComment(&quot;// end-of-line comment without line terminator&quot;); // correct" + DELIMITER +
968
					" * setLeadingComment(&quot;// broken\\n end-of-line comment\\n&quot;); // wrong" + DELIMITER +
969
					" * </pre>" + DELIMITER +
970
					" */";
971
			String result=testFormat(input, options);
972
			assertEquals(expected, result);
973
		}
974
	}
956
	}
975
957
976
	/**
958
	/**
(-)src/org/eclipse/jdt/core/tests/formatter/comment/MultiLineTestCase.java (-8 / +3 lines)
Lines 17-23 Link Here
17
17
18
import junit.framework.Test;
18
import junit.framework.Test;
19
19
20
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter;
21
import org.eclipse.jdt.internal.formatter.comment.MultiCommentLine;
20
import org.eclipse.jdt.internal.formatter.comment.MultiCommentLine;
22
21
23
public class MultiLineTestCase extends CommentTestCase {
22
public class MultiLineTestCase extends CommentTestCase {
Lines 116-128 Link Here
116
				" * Member comment\n" +//$NON-NLS-1$
115
				" * Member comment\n" +//$NON-NLS-1$
117
				" */";//$NON-NLS-1$
116
				" */";//$NON-NLS-1$
118
		String result= testFormat(input, 0, input.length(), CodeFormatter.K_MULTI_LINE_COMMENT , 2);
117
		String result= testFormat(input, 0, input.length(), CodeFormatter.K_MULTI_LINE_COMMENT , 2);
119
		String expectedOutput = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
118
		String expectedOutput = "/**\n" +
120
			?	"/**\n" +
119
			" * Member comment\n" +
121
				" * Member comment\n" +
120
			" */";
122
				" */"
123
			:	"/***********************************************************************\n" +
124
				"	 * Member comment\n" +
125
				"	 */";
126
		assertEquals("Different output", expectedOutput, result);
121
		assertEquals("Different output", expectedOutput, result);
127
	}
122
	}
128
123
(-)src/org/eclipse/jdt/core/tests/formatter/comment/SingleLineTestCase.java (-59 / +24 lines)
Lines 16-22 Link Here
16
import org.eclipse.jdt.core.formatter.CodeFormatter;
16
import org.eclipse.jdt.core.formatter.CodeFormatter;
17
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
17
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
18
18
19
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter;
20
import org.eclipse.jdt.internal.formatter.comment.SingleCommentLine;
19
import org.eclipse.jdt.internal.formatter.comment.SingleCommentLine;
21
20
22
public class SingleLineTestCase extends CommentTestCase {
21
public class SingleLineTestCase extends CommentTestCase {
Lines 42-50 Link Here
42
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "5"); //$NON-NLS-1$
41
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "5"); //$NON-NLS-1$
43
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_BLOCK_COMMENT, DefaultCodeFormatterConstants.FALSE);
42
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_BLOCK_COMMENT, DefaultCodeFormatterConstants.FALSE);
44
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_JAVADOC_COMMENT, DefaultCodeFormatterConstants.FALSE);
43
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_JAVADOC_COMMENT, DefaultCodeFormatterConstants.FALSE);
45
		String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
44
		String expected =PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + "//"+ DELIMITER + PREFIX + "test";
46
			? PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + "//"+ DELIMITER + PREFIX + "test"
47
			: PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER;
48
		assertEquals(expected, testFormat("//test\ttest" + DELIMITER + "//" + DELIMITER + "//\t\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
45
		assertEquals(expected, testFormat("//test\ttest" + DELIMITER + "//" + DELIMITER + "//\t\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
49
	}
46
	}
50
47
Lines 52-60 Link Here
52
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "5"); //$NON-NLS-1$
49
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "5"); //$NON-NLS-1$
53
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_BLOCK_COMMENT, DefaultCodeFormatterConstants.FALSE);
50
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_BLOCK_COMMENT, DefaultCodeFormatterConstants.FALSE);
54
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_JAVADOC_COMMENT, DefaultCodeFormatterConstants.FALSE);
51
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_JAVADOC_COMMENT, DefaultCodeFormatterConstants.FALSE);
55
		String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
52
		String expected = PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + DELIMITER + PREFIX + "test";
56
			? PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + DELIMITER + PREFIX + "test"
57
			: PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER;
58
		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
		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$
59
	}
54
	}
60
55
Lines 62-84 Link Here
62
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "5"); //$NON-NLS-1$
57
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "5"); //$NON-NLS-1$
63
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_BLOCK_COMMENT, DefaultCodeFormatterConstants.FALSE);
58
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_BLOCK_COMMENT, DefaultCodeFormatterConstants.FALSE);
64
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_JAVADOC_COMMENT, DefaultCodeFormatterConstants.FALSE);
59
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_JAVADOC_COMMENT, DefaultCodeFormatterConstants.FALSE);
65
		String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
60
		String expected = PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + "//"+ DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test";
66
			? PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + "//"+ DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test"
67
			: PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER;
68
		assertEquals(expected, testFormat("//test\ttest" + DELIMITER + "//" + DELIMITER + PREFIX + "test\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
61
		assertEquals(expected, testFormat("//test\ttest" + DELIMITER + "//" + DELIMITER + PREFIX + "test\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
69
	}
62
	}
70
63
71
	public void testCommentBegin1() {
64
	public void testCommentBegin1() {
72
		String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
65
		String expected = PREFIX + "test";
73
			? PREFIX + "test"
74
			: PREFIX + "test" + DELIMITER;
75
		assertEquals(expected, testFormat("//test")); //$NON-NLS-1$ //$NON-NLS-2$
66
		assertEquals(expected, testFormat("//test")); //$NON-NLS-1$ //$NON-NLS-2$
76
	}
67
	}
77
68
78
	public void testCommentBegin2() {
69
	public void testCommentBegin2() {
79
		String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
70
		String expected = PREFIX + "test";
80
			? PREFIX + "test"
81
			: PREFIX + "test" + DELIMITER;
82
		assertEquals(expected, testFormat(PREFIX + "test")); //$NON-NLS-1$ //$NON-NLS-2$
71
		assertEquals(expected, testFormat(PREFIX + "test")); //$NON-NLS-1$ //$NON-NLS-2$
83
	}
72
	}
84
73
Lines 87-100 Link Here
87
	}
76
	}
88
77
89
	public void testCommentDelimiter1() {
78
	public void testCommentDelimiter1() {
90
		String expected = PREFIX + "test" + DELIMITER;
79
		String expected = PREFIX + "test" + DELIMITER + DELIMITER;
91
		if (DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT) expected += DELIMITER;
92
		assertEquals(expected, testFormat("//\t\ttest " + DELIMITER + DELIMITER)); //$NON-NLS-1$ //$NON-NLS-2$
80
		assertEquals(expected, testFormat("//\t\ttest " + DELIMITER + DELIMITER)); //$NON-NLS-1$ //$NON-NLS-2$
93
	}
81
	}
94
82
95
	public void testCommentDelimiter2() {
83
	public void testCommentDelimiter2() {
96
		String expected = PREFIX + "test" + DELIMITER;
84
		String expected = PREFIX + "test" + DELIMITER + DELIMITER ;
97
		if (DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT) expected += DELIMITER ;
98
		assertEquals(expected, testFormat(PREFIX + "test " + DELIMITER + DELIMITER + DELIMITER)); //$NON-NLS-1$ //$NON-NLS-2$
85
		assertEquals(expected, testFormat(PREFIX + "test " + DELIMITER + DELIMITER + DELIMITER)); //$NON-NLS-1$ //$NON-NLS-2$
99
	}
86
	}
100
87
Lines 123-169 Link Here
123
	}
110
	}
124
111
125
	public void testCommentSpace1() {
112
	public void testCommentSpace1() {
126
		String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
113
		String expected = PREFIX + "test test";
127
			? PREFIX + "test test"
128
			: PREFIX + "test test" + DELIMITER;
129
		assertEquals(expected, testFormat("//test\t \t test")); //$NON-NLS-1$ //$NON-NLS-2$
114
		assertEquals(expected, testFormat("//test\t \t test")); //$NON-NLS-1$ //$NON-NLS-2$
130
	}
115
	}
131
116
132
	public void testCommentSpace2() {
117
	public void testCommentSpace2() {
133
		String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
118
		String expected = PREFIX + "test test";
134
			? PREFIX + "test test"
135
			: PREFIX + "test test" + DELIMITER;
136
		assertEquals(expected, testFormat("//test test")); //$NON-NLS-1$ //$NON-NLS-2$
119
		assertEquals(expected, testFormat("//test test")); //$NON-NLS-1$ //$NON-NLS-2$
137
	}
120
	}
138
121
139
	public void testCommentSpace3() {
122
	public void testCommentSpace3() {
140
		String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
123
		String expected = PREFIX + "test test";
141
			? PREFIX + "test test"
142
			: PREFIX + "test test" + DELIMITER;
143
		assertEquals(expected, testFormat(PREFIX + "test \t    \t test")); //$NON-NLS-1$ //$NON-NLS-2$
124
		assertEquals(expected, testFormat(PREFIX + "test \t    \t test")); //$NON-NLS-1$ //$NON-NLS-2$
144
	}
125
	}
145
126
146
	public void testCommentWrapping1() {
127
	public void testCommentWrapping1() {
147
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "5"); //$NON-NLS-1$
128
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "5"); //$NON-NLS-1$
148
		String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
129
		String expected = PREFIX + "test" + DELIMITER + PREFIX + "test";
149
			? PREFIX + "test" + DELIMITER + PREFIX + "test"
150
			: PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER;
151
		assertEquals(expected, testFormat("//test\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
130
		assertEquals(expected, testFormat("//test\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
152
	}
131
	}
153
132
154
	public void testCommentWrapping2() {
133
	public void testCommentWrapping2() {
155
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "1"); //$NON-NLS-1$
134
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "1"); //$NON-NLS-1$
156
		String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
135
		String expected = PREFIX + "test" + DELIMITER + PREFIX + "test";
157
			? PREFIX + "test" + DELIMITER + PREFIX + "test"
158
			: PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER;
159
		assertEquals(expected, testFormat("//test\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
136
		assertEquals(expected, testFormat("//test\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
160
	}
137
	}
161
138
162
	public void testCommentWrapping3() {
139
	public void testCommentWrapping3() {
163
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "32"); //$NON-NLS-1$
140
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "32"); //$NON-NLS-1$
164
		String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
141
		String expected = PREFIX + "test test";
165
			? PREFIX + "test test"
166
			: PREFIX + "test test" + DELIMITER;
167
		assertEquals(expected, testFormat("//test\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
142
		assertEquals(expected, testFormat("//test\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
168
	}
143
	}
169
144
Lines 186-243 Link Here
186
	public void testHeaderComment1() {
161
	public void testHeaderComment1() {
187
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_HEADER, DefaultCodeFormatterConstants.FALSE);
162
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_HEADER, DefaultCodeFormatterConstants.FALSE);
188
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "12"); //$NON-NLS-1$
163
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "12"); //$NON-NLS-1$
189
		String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
164
		String expected = PREFIX + "test test" + DELIMITER + PREFIX + "test test" + DELIMITER + PREFIX + "test test";
190
			? PREFIX + "test test" + DELIMITER + PREFIX + "test test" + DELIMITER + PREFIX + "test test"
191
			: PREFIX + "test test" + DELIMITER + PREFIX + "test test" + DELIMITER + PREFIX + "test test" + DELIMITER;
192
		assertEquals(expected, testFormat("//test\t\t\t\ttest" + DELIMITER + PREFIX + "test test test test")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
165
		assertEquals(expected, testFormat("//test\t\t\t\ttest" + DELIMITER + PREFIX + "test test test test")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
193
	}
166
	}
194
167
195
	public void testHeaderComment2() {
168
	public void testHeaderComment2() {
196
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_HEADER, DefaultCodeFormatterConstants.FALSE);
169
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_HEADER, DefaultCodeFormatterConstants.FALSE);
197
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "24"); //$NON-NLS-1$
170
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "24"); //$NON-NLS-1$
198
		String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
171
		String expected = "// test" + DELIMITER + PREFIX + "test test test test" + DELIMITER;
199
			? "// test" + DELIMITER + PREFIX + "test test test test" + DELIMITER
200
			: PREFIX + "test test test test" + DELIMITER + PREFIX + "test" + DELIMITER;
201
		assertEquals(expected, testFormat("//test\t\t\t" + DELIMITER + PREFIX + "test test test test" + DELIMITER)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
172
		assertEquals(expected, testFormat("//test\t\t\t" + DELIMITER + PREFIX + "test test test test" + DELIMITER)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
202
	}
173
	}
203
174
204
	public void testIllegalLineLength1() {
175
	public void testIllegalLineLength1() {
205
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "1"); //$NON-NLS-1$
176
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "1"); //$NON-NLS-1$
206
		String expected =PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER;
177
		String expected =PREFIX + "test" + DELIMITER + PREFIX + "test";
207
		if (DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT) expected = expected.substring(0, expected.length()-DELIMITER.length());
208
		assertEquals(expected, testFormat("//test\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
178
		assertEquals(expected, testFormat("//test\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
209
	}
179
	}
210
180
211
	public void testIllegalLineLength2() {
181
	public void testIllegalLineLength2() {
212
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "-16"); //$NON-NLS-1$
182
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "-16"); //$NON-NLS-1$
213
		String expected = PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER;
183
		String expected = PREFIX + "test" + DELIMITER + PREFIX + "test";
214
		if (DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT) expected = expected.substring(0, expected.length()-DELIMITER.length());
215
		assertEquals(expected, testFormat(PREFIX + "\t\t test\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
184
		assertEquals(expected, testFormat(PREFIX + "\t\t test\ttest")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
216
	}
185
	}
217
186
218
	public void testMultipleComments1() {
187
	public void testMultipleComments1() {
219
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "5"); //$NON-NLS-1$
188
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "5"); //$NON-NLS-1$
220
		String expected = PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER;
189
		String expected = PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test";
221
		if (DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT) expected = expected.substring(0, expected.length()-DELIMITER.length());
222
		assertEquals(expected, testFormat("//test test" + DELIMITER + PREFIX + "test test test test")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
190
		assertEquals(expected, testFormat("//test test" + DELIMITER + PREFIX + "test test test test")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
223
	}
191
	}
224
192
225
	public void testMultipleComments2() {
193
	public void testMultipleComments2() {
226
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "12"); //$NON-NLS-1$
194
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "12"); //$NON-NLS-1$
227
		String expected = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT
195
		String expected = "// test test" + DELIMITER +
228
			? "// test test" + DELIMITER +
196
			"// test" + DELIMITER +
229
				"// test" + DELIMITER +
197
			"// " + DELIMITER +
230
				"// " + DELIMITER +
198
			"// test test" + DELIMITER +
231
				"// test test" + DELIMITER +
199
			"// test test";
232
				"// test test"
233
			: PREFIX + "test test" + DELIMITER + PREFIX + "test test" + DELIMITER + PREFIX + "test test" + DELIMITER + PREFIX + "test" + DELIMITER;
234
		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
		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$
235
	}
201
	}
236
202
237
	public void testMultipleComments3() {
203
	public void testMultipleComments3() {
238
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "11"); //$NON-NLS-1$
204
		setUserOption(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, "11"); //$NON-NLS-1$
239
		String expected = PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER;
205
		String expected = PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test" + DELIMITER + PREFIX + "test";
240
		if (DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT) expected = expected.substring(0, expected.length()-DELIMITER.length());
241
		assertEquals(expected, testFormat("//   test\t\t\ttest\ttest" + DELIMITER + PREFIX + "test test test test")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$
206
		assertEquals(expected, testFormat("//   test\t\t\ttest\ttest" + DELIMITER + PREFIX + "test test test test")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$
242
	}
207
	}
243
208

Return to bug 236406