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 |