Lines 26-33
Link Here
|
26 |
import org.eclipse.jdt.internal.core.util.RecordedParsingInformation; |
26 |
import org.eclipse.jdt.internal.core.util.RecordedParsingInformation; |
27 |
import org.eclipse.jdt.internal.formatter.align.Alignment; |
27 |
import org.eclipse.jdt.internal.formatter.align.Alignment; |
28 |
import org.eclipse.jdt.internal.formatter.align.AlignmentException; |
28 |
import org.eclipse.jdt.internal.formatter.align.AlignmentException; |
|
|
29 |
import org.eclipse.jdt.internal.formatter.comment.CommentFormatterUtil; |
30 |
import org.eclipse.jdt.internal.formatter.comment.IJavaDocTagConstants; |
29 |
import org.eclipse.jface.text.IRegion; |
31 |
import org.eclipse.jface.text.IRegion; |
30 |
import org.eclipse.jface.text.Region; |
32 |
import org.eclipse.jface.text.Region; |
|
|
33 |
import org.eclipse.text.edits.MalformedTreeException; |
31 |
import org.eclipse.text.edits.MultiTextEdit; |
34 |
import org.eclipse.text.edits.MultiTextEdit; |
32 |
import org.eclipse.text.edits.ReplaceEdit; |
35 |
import org.eclipse.text.edits.ReplaceEdit; |
33 |
import org.eclipse.text.edits.TextEdit; |
36 |
import org.eclipse.text.edits.TextEdit; |
Lines 81-86
Link Here
|
81 |
|
84 |
|
82 |
private final boolean formatJavadocComment; |
85 |
private final boolean formatJavadocComment; |
83 |
private final boolean formatBlockComment; |
86 |
private final boolean formatBlockComment; |
|
|
87 |
private FormatterCommentParser formatterCommentParser; // specialized parser to format comments |
84 |
|
88 |
|
85 |
Scribe(CodeFormatterVisitor formatter, long sourceLevel, IRegion[] regions, CodeSnippetParsingUtil codeSnippetParsingUtil) { |
89 |
Scribe(CodeFormatterVisitor formatter, long sourceLevel, IRegion[] regions, CodeSnippetParsingUtil codeSnippetParsingUtil) { |
86 |
this.scanner = new Scanner(true, true, false/*nls*/, sourceLevel/*sourceLevel*/, null/*taskTags*/, null/*taskPriorities*/, true/*taskCaseSensitive*/); |
90 |
this.scanner = new Scanner(true, true, false/*nls*/, sourceLevel/*sourceLevel*/, null/*taskTags*/, null/*taskPriorities*/, true/*taskCaseSensitive*/); |
Lines 438-443
Link Here
|
438 |
this.memberAlignment = current.enclosing; |
442 |
this.memberAlignment = current.enclosing; |
439 |
} |
443 |
} |
440 |
|
444 |
|
|
|
445 |
private void formatJavadocTag(FormatJavadocTag aTag) { |
446 |
if( aTag == null) return; |
447 |
|
448 |
int lastEnd = aTag.startPosition; |
449 |
boolean isTagParam = false, isTextSection = false; // handle formatting preferences |
450 |
|
451 |
// tag name |
452 |
String tagName = aTag.tagName; |
453 |
if (tagName != null) { |
454 |
isTagParam = IJavaDocTagConstants.PARAM_TAG.equalsIgnoreCase(tagName); |
455 |
lastEnd += tagName.length(); |
456 |
this.scanner.resetTo(lastEnd, this.scannerEndPosition - 1); |
457 |
} else { |
458 |
isTextSection = true; |
459 |
} |
460 |
|
461 |
// reference |
462 |
FormatJavadocReference reference = aTag.reference; |
463 |
if (reference != null) { |
464 |
lastEnd = reference.endPosition; |
465 |
String refName = reference.name; |
466 |
if (refName != null) { |
467 |
int start = reference.startPosition; |
468 |
if (start - lastEnd > 2) { // trim blanks if any |
469 |
addReplaceEdit(lastEnd, start - 1, IJavaDocTagConstants.SPACE); |
470 |
} |
471 |
} |
472 |
|
473 |
// handle formatting prefs |
474 |
if (! aTag.inlineTag) { |
475 |
if (! isTextSection && aTag.nodesPtr > - 1) { |
476 |
int startLine = Util.getLineNumber(aTag.startPosition, this.lineEnds, 0, this.line); |
477 |
boolean multiLines = Util.getLineNumber(aTag.endPosition, this.lineEnds, 0, this.line) - startLine > 0; |
478 |
if (multiLines) { |
479 |
if (isTagParam) { |
480 |
// pref: new line after param tag |
481 |
if (this.formatter.preferences.comment_insert_new_line_for_parameter) { |
482 |
printNewLine(this.scanner.currentPosition + 1); |
483 |
} |
484 |
// pref: indent description after @param tags |
485 |
if (this.formatter.preferences.comment_indent_parameter_description) { |
486 |
this.indent(); |
487 |
} |
488 |
} |
489 |
// pref: indent javadoc tags |
490 |
if (this.formatter.preferences.comment_indent_root_tags) { |
491 |
this.indent(); |
492 |
} |
493 |
} |
494 |
} |
495 |
} |
496 |
this.scanner.resetTo(lastEnd, this.scannerEndPosition - 1); |
497 |
} |
498 |
|
499 |
// tag section iterate through the tags composing this Javadoc |
500 |
for (int i = 0, max = aTag.nodesPtr; i <= max ; i++) { |
501 |
FormatJavadocNode aNode = aTag.nodes[i]; |
502 |
this.scanner.resetTo(aNode.startPosition, this.scannerEndPosition - 1); |
503 |
// trim blanks if any, we we're one the same line |
504 |
if (aNode.startPosition - lastEnd > 2 && |
505 |
this.line == Util.getLineNumber(aNode.startPosition, this.lineEnds, 0, this.line)) { |
506 |
addReplaceEdit(lastEnd + 1, aNode.startPosition - 1, IJavaDocTagConstants.SPACE); |
507 |
} |
508 |
if (aNode.isTag()) { // format inline tag |
509 |
this.formatJavadocTag((FormatJavadocTag) aNode); |
510 |
} else { // format text |
511 |
this.formatText((FormatJavadocText) aNode, true); |
512 |
} |
513 |
lastEnd = aNode.endPosition; |
514 |
} |
515 |
this.scanner.resetTo(aTag.endPosition, this.scannerEndPosition - 1); |
516 |
} |
517 |
|
518 |
/* |
519 |
* formats the first section of a javadoc comment |
520 |
*/ |
521 |
private void formatJavadocHeader() { |
522 |
FormatJavadoc javadoc = (FormatJavadoc) formatterCommentParser.docComment; |
523 |
FormatJavadocTag firstTag = javadoc.tags[0]; |
524 |
FormatJavadocNode firstNode = firstTag.nodes[0]; |
525 |
if (firstNode.isText()) { |
526 |
formatText((FormatJavadocText)firstNode, false); |
527 |
} |
528 |
this.scanner.resetTo(firstTag.endPosition + 1, this.scannerEndPosition - 1); |
529 |
} |
530 |
|
531 |
/* |
532 |
* |
533 |
*/ |
534 |
private void formatText(FormatJavadocText text, boolean insideTag) { |
535 |
int sectionStart, sectionLength; |
536 |
int nextLine = 0, previousLine = -1, lineCount = 0, lastEnd = 0; // counters to handle the text formatting |
537 |
|
538 |
// iterate through text sections |
539 |
for (int j = 0, jMax = text.textSeparatorsPtr; j <= jMax ; j++) { |
540 |
sectionStart = (int) (text.textSeparators[j] >>> 32); |
541 |
sectionLength = (int) (text.textSeparators[j] & 0x00000000FFFFFFFFL); |
542 |
nextLine = Util.getLineNumber(sectionStart, this.lineEnds, 0, this.line); |
543 |
lineCount = nextLine - previousLine; |
544 |
|
545 |
// empty lines may have been skipped at parsing time, fill the gap if needed |
546 |
if (previousLine != -1 && lineCount > 1) { |
547 |
this.formatText(lastEnd + 1, sectionStart - lastEnd, insideTag); |
548 |
} |
549 |
// format the text section |
550 |
formatText(sectionStart, sectionLength, insideTag); |
551 |
lastEnd = sectionStart + sectionLength; |
552 |
previousLine = nextLine; |
553 |
} |
554 |
} |
555 |
|
556 |
private void formatText(int startPosition, int length, boolean insideTag) { |
557 |
// init scanner and variables |
558 |
int endPosition = startPosition + length; |
559 |
this.scanner.resetTo(startPosition, endPosition - 1); |
560 |
int currentCharacter; |
561 |
boolean isNewLine = false; |
562 |
int start = startPosition; |
563 |
int nextCharacterStart = startPosition; |
564 |
int previousStart = startPosition; |
565 |
boolean onFirstColumn = isOnFirstColumn(start); |
566 |
|
567 |
// handle indentation |
568 |
if (! insideTag && this.indentationLevel != 0) { |
569 |
printIndentationIfNecessary(); |
570 |
} |
571 |
|
572 |
if (this.pendingSpace) { |
573 |
this.addInsertEdit(startPosition, " "); //$NON-NLS-1$ |
574 |
} |
575 |
this.needSpace = false; |
576 |
this.pendingSpace = false; |
577 |
|
578 |
int currentCommentOffset = onFirstColumn ? 0 : getCurrentCommentOffset(start); |
579 |
|
580 |
while (nextCharacterStart <= endPosition && (currentCharacter = this.scanner.getNextChar()) != -1) { |
581 |
nextCharacterStart = this.scanner.currentPosition; |
582 |
|
583 |
switch(currentCharacter) { |
584 |
case '\r' : |
585 |
start = previousStart; |
586 |
isNewLine = true; |
587 |
if (this.scanner.getNextChar('\n')) { |
588 |
currentCharacter = '\n'; |
589 |
nextCharacterStart = this.scanner.currentPosition; |
590 |
} |
591 |
break; |
592 |
case '\n' : |
593 |
start = previousStart; |
594 |
isNewLine = true; |
595 |
nextCharacterStart = this.scanner.currentPosition; |
596 |
break; |
597 |
default: |
598 |
if (isNewLine) { |
599 |
this.column = 1; |
600 |
this.line++; |
601 |
isNewLine = false; |
602 |
|
603 |
StringBuffer buffer = new StringBuffer(); |
604 |
if (onFirstColumn) { |
605 |
// simply insert indentation if necessary |
606 |
buffer.append(this.lineSeparator); |
607 |
printIndentationIfNecessary(buffer); |
608 |
if (ScannerHelper.isWhitespace((char) currentCharacter)) { |
609 |
int previousStartPosition = this.scanner.currentPosition; |
610 |
while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { |
611 |
previousStart = nextCharacterStart; |
612 |
previousStartPosition = this.scanner.currentPosition; |
613 |
currentCharacter = this.scanner.getNextChar(); |
614 |
nextCharacterStart = this.scanner.currentPosition; |
615 |
} |
616 |
if (currentCharacter == '\r' || currentCharacter == '\n') { |
617 |
nextCharacterStart = previousStartPosition; |
618 |
} |
619 |
} |
620 |
if (currentCharacter != '\r' && currentCharacter != '\n') { |
621 |
buffer.append(' '); |
622 |
} |
623 |
} else { |
624 |
if (ScannerHelper.isWhitespace((char) currentCharacter)) { |
625 |
int previousStartPosition = this.scanner.currentPosition; |
626 |
int count = 0; |
627 |
loop: while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { |
628 |
if (count >= currentCommentOffset) { |
629 |
break loop; |
630 |
} |
631 |
previousStart = nextCharacterStart; |
632 |
previousStartPosition = this.scanner.currentPosition; |
633 |
switch(currentCharacter) { |
634 |
case '\t' : |
635 |
count += this.tabLength; |
636 |
break; |
637 |
default : |
638 |
count ++; |
639 |
} |
640 |
currentCharacter = this.scanner.getNextChar(); |
641 |
nextCharacterStart = this.scanner.currentPosition; |
642 |
} |
643 |
if (currentCharacter == '\r' || currentCharacter == '\n') { |
644 |
nextCharacterStart = previousStartPosition; |
645 |
} |
646 |
} |
647 |
buffer.append(this.lineSeparator); |
648 |
printIndentationIfNecessary(buffer); |
649 |
int previousStartTemp = previousStart; |
650 |
int nextCharacterStartTemp = nextCharacterStart; |
651 |
while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { |
652 |
previousStart = nextCharacterStart; |
653 |
currentCharacter = this.scanner.getNextChar(); |
654 |
nextCharacterStart = this.scanner.currentPosition; |
655 |
} |
656 |
if (currentCharacter == '*') { |
657 |
buffer.append(' '); |
658 |
} else { |
659 |
previousStart = previousStartTemp; |
660 |
nextCharacterStart = nextCharacterStartTemp; |
661 |
} |
662 |
this.scanner.currentPosition = nextCharacterStart; |
663 |
} |
664 |
addReplaceEdit(start, previousStart - 1, String.valueOf(buffer)); |
665 |
} else { |
666 |
this.column += (nextCharacterStart - previousStart); |
667 |
} |
668 |
} |
669 |
previousStart = nextCharacterStart; |
670 |
this.scanner.currentPosition = nextCharacterStart; |
671 |
} |
672 |
this.lastNumberOfNewLines = 0; |
673 |
needSpace = false; |
674 |
this.scanner.resetTo(endPosition, this.scannerEndPosition - 1); |
675 |
} |
676 |
|
441 |
public Alignment getAlignment(String name){ |
677 |
public Alignment getAlignment(String name){ |
442 |
if (this.currentAlignment != null) { |
678 |
if (this.currentAlignment != null) { |
443 |
return this.currentAlignment.getAlignment(name); |
679 |
return this.currentAlignment.getAlignment(name); |
Lines 657-666
Link Here
|
657 |
} else { |
893 |
} else { |
658 |
edit = new MultiTextEdit(textRegionStart, length - 1); |
894 |
edit = new MultiTextEdit(textRegionStart, length - 1); |
659 |
} |
895 |
} |
|
|
896 |
|
660 |
for (int i= 0, max = this.editsIndex; i < max; i++) { |
897 |
for (int i= 0, max = this.editsIndex; i < max; i++) { |
661 |
OptimizedReplaceEdit currentEdit = edits[i]; |
898 |
OptimizedReplaceEdit currentEdit = edits[i]; |
662 |
if (isValidEdit(currentEdit)) { |
899 |
if (isValidEdit(currentEdit)) { |
663 |
edit.addChild(new ReplaceEdit(currentEdit.offset, currentEdit.length, currentEdit.replacement)); |
900 |
try { |
|
|
901 |
edit.addChild(new ReplaceEdit(currentEdit.offset, currentEdit.length, currentEdit.replacement)); |
902 |
} |
903 |
catch (MalformedTreeException ex) { |
904 |
// log exception in case of error |
905 |
CommentFormatterUtil.log(ex); |
906 |
} |
664 |
} |
907 |
} |
665 |
} |
908 |
} |
666 |
this.edits = null; |
909 |
this.edits = null; |
Lines 736-741
Link Here
|
736 |
this.scannerEndPosition = compilationUnitSource.length; |
979 |
this.scannerEndPosition = compilationUnitSource.length; |
737 |
this.scanner.resetTo(0, this.scannerEndPosition - 1); |
980 |
this.scanner.resetTo(0, this.scannerEndPosition - 1); |
738 |
this.edits = new OptimizedReplaceEdit[INITIAL_SIZE]; |
981 |
this.edits = new OptimizedReplaceEdit[INITIAL_SIZE]; |
|
|
982 |
initFormatterCommentParser(); |
983 |
} |
984 |
|
985 |
private void initFormatterCommentParser() { |
986 |
this.formatterCommentParser = new FormatterCommentParser(null); |
987 |
this.formatterCommentParser.scanner.setSource(this.scanner.source); |
988 |
this.formatterCommentParser.source = this.scanner.source; |
989 |
this.formatterCommentParser.scanner.lineEnds = this.scanner.lineEnds; |
739 |
} |
990 |
} |
740 |
|
991 |
|
741 |
/** |
992 |
/** |
Lines 936-943
Link Here
|
936 |
column += s.length; |
1187 |
column += s.length; |
937 |
needSpace = true; |
1188 |
needSpace = true; |
938 |
} |
1189 |
} |
|
|
1190 |
|
1191 |
/* |
1192 |
* print and format a block comment |
1193 |
*/ |
1194 |
private void printBlockComment() { |
1195 |
int currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition(); |
1196 |
int currentTokenEndPosition = this.scanner.getCurrentTokenEndPosition() + 1; |
1197 |
|
1198 |
this.scanner.resetTo(currentTokenStartPosition, currentTokenEndPosition - 1); |
1199 |
int currentCharacter; |
1200 |
boolean isNewLine = false; |
1201 |
int start = currentTokenStartPosition; |
1202 |
int nextCharacterStart = currentTokenStartPosition; |
1203 |
int previousStart = currentTokenStartPosition; |
1204 |
boolean onFirstColumn = isOnFirstColumn(start); |
939 |
|
1205 |
|
940 |
private void printBlockComment(char[] s, boolean isJavadoc) { |
1206 |
boolean indentComment = false; |
|
|
1207 |
if (this.indentationLevel != 0) { |
1208 |
if (!this.formatter.preferences.never_indent_block_comments_on_first_column |
1209 |
|| !onFirstColumn) { |
1210 |
indentComment = true; |
1211 |
printIndentationIfNecessary(); |
1212 |
} |
1213 |
} |
1214 |
if (this.pendingSpace) { |
1215 |
this.addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$ |
1216 |
} |
1217 |
this.needSpace = false; |
1218 |
this.pendingSpace = false; |
1219 |
|
1220 |
int currentCommentOffset = onFirstColumn ? 0 : getCurrentCommentOffset(start); |
1221 |
|
1222 |
while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter = this.scanner.getNextChar()) != -1) { |
1223 |
nextCharacterStart = this.scanner.currentPosition; |
1224 |
|
1225 |
switch(currentCharacter) { |
1226 |
case '\r' : |
1227 |
start = previousStart; |
1228 |
isNewLine = true; |
1229 |
if (this.scanner.getNextChar('\n')) { |
1230 |
currentCharacter = '\n'; |
1231 |
nextCharacterStart = this.scanner.currentPosition; |
1232 |
} |
1233 |
break; |
1234 |
case '\n' : |
1235 |
start = previousStart; |
1236 |
isNewLine = true; |
1237 |
nextCharacterStart = this.scanner.currentPosition; |
1238 |
break; |
1239 |
default: |
1240 |
if (isNewLine) { |
1241 |
this.column = 1; |
1242 |
this.line++; |
1243 |
isNewLine = false; |
1244 |
|
1245 |
StringBuffer buffer = new StringBuffer(); |
1246 |
if (onFirstColumn) { |
1247 |
// simply insert indentation if necessary |
1248 |
buffer.append(this.lineSeparator); |
1249 |
if (indentComment) { |
1250 |
printIndentationIfNecessary(buffer); |
1251 |
} |
1252 |
if (ScannerHelper.isWhitespace((char) currentCharacter)) { |
1253 |
int previousStartPosition = this.scanner.currentPosition; |
1254 |
while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { |
1255 |
previousStart = nextCharacterStart; |
1256 |
previousStartPosition = this.scanner.currentPosition; |
1257 |
currentCharacter = this.scanner.getNextChar(); |
1258 |
nextCharacterStart = this.scanner.currentPosition; |
1259 |
} |
1260 |
if (currentCharacter == '\r' || currentCharacter == '\n') { |
1261 |
nextCharacterStart = previousStartPosition; |
1262 |
} |
1263 |
} |
1264 |
if (currentCharacter != '\r' && currentCharacter != '\n') { |
1265 |
buffer.append(' '); |
1266 |
} |
1267 |
} else { |
1268 |
if (ScannerHelper.isWhitespace((char) currentCharacter)) { |
1269 |
int previousStartPosition = this.scanner.currentPosition; |
1270 |
int count = 0; |
1271 |
loop: while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { |
1272 |
if (count >= currentCommentOffset) { |
1273 |
break loop; |
1274 |
} |
1275 |
previousStart = nextCharacterStart; |
1276 |
previousStartPosition = this.scanner.currentPosition; |
1277 |
switch(currentCharacter) { |
1278 |
case '\t' : |
1279 |
count += this.tabLength; |
1280 |
break; |
1281 |
default : |
1282 |
count ++; |
1283 |
} |
1284 |
currentCharacter = this.scanner.getNextChar(); |
1285 |
nextCharacterStart = this.scanner.currentPosition; |
1286 |
} |
1287 |
if (currentCharacter == '\r' || currentCharacter == '\n') { |
1288 |
nextCharacterStart = previousStartPosition; |
1289 |
} |
1290 |
} |
1291 |
buffer.append(this.lineSeparator); |
1292 |
if (indentComment) { |
1293 |
printIndentationIfNecessary(buffer); |
1294 |
} |
1295 |
int previousStartTemp = previousStart; |
1296 |
int nextCharacterStartTemp = nextCharacterStart; |
1297 |
while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { |
1298 |
previousStart = nextCharacterStart; |
1299 |
currentCharacter = this.scanner.getNextChar(); |
1300 |
nextCharacterStart = this.scanner.currentPosition; |
1301 |
} |
1302 |
if (currentCharacter == '*') { |
1303 |
buffer.append(' '); |
1304 |
} else { |
1305 |
previousStart = previousStartTemp; |
1306 |
nextCharacterStart = nextCharacterStartTemp; |
1307 |
} |
1308 |
this.scanner.currentPosition = nextCharacterStart; |
1309 |
} |
1310 |
addReplaceEdit(start, previousStart - 1, String.valueOf(buffer)); |
1311 |
} else { |
1312 |
this.column += (nextCharacterStart - previousStart); |
1313 |
} |
1314 |
} |
1315 |
previousStart = nextCharacterStart; |
1316 |
this.scanner.currentPosition = nextCharacterStart; |
1317 |
} |
1318 |
this.lastNumberOfNewLines = 0; |
1319 |
needSpace = false; |
1320 |
this.scanner.resetTo(currentTokenEndPosition, this.scannerEndPosition - 1); |
1321 |
} |
1322 |
|
1323 |
/* |
1324 |
* Common method to format javadoc or block comments without formatting |
1325 |
*/ |
1326 |
private void printCommentWithoutFormatting() { |
941 |
int currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition(); |
1327 |
int currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition(); |
942 |
int currentTokenEndPosition = this.scanner.getCurrentTokenEndPosition() + 1; |
1328 |
int currentTokenEndPosition = this.scanner.getCurrentTokenEndPosition() + 1; |
943 |
|
1329 |
|
Lines 951-958
Link Here
|
951 |
|
1337 |
|
952 |
boolean indentComment = false; |
1338 |
boolean indentComment = false; |
953 |
if (this.indentationLevel != 0) { |
1339 |
if (this.indentationLevel != 0) { |
954 |
if (isJavadoc |
1340 |
if (!this.formatter.preferences.never_indent_block_comments_on_first_column |
955 |
|| !this.formatter.preferences.never_indent_block_comments_on_first_column |
|
|
956 |
|| !onFirstColumn) { |
1341 |
|| !onFirstColumn) { |
957 |
indentComment = true; |
1342 |
indentComment = true; |
958 |
printIndentationIfNecessary(); |
1343 |
printIndentationIfNecessary(); |
Lines 965-971
Link Here
|
965 |
this.pendingSpace = false; |
1350 |
this.pendingSpace = false; |
966 |
|
1351 |
|
967 |
int currentCommentOffset = onFirstColumn ? 0 : getCurrentCommentOffset(start); |
1352 |
int currentCommentOffset = onFirstColumn ? 0 : getCurrentCommentOffset(start); |
968 |
boolean formatComment = (isJavadoc && formatJavadocComment) || (!isJavadoc && formatBlockComment); |
|
|
969 |
|
1353 |
|
970 |
while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter = this.scanner.getNextChar()) != -1) { |
1354 |
while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter = this.scanner.getNextChar()) != -1) { |
971 |
nextCharacterStart = this.scanner.currentPosition; |
1355 |
nextCharacterStart = this.scanner.currentPosition; |
Lines 997-1003
Link Here
|
997 |
if (indentComment) { |
1381 |
if (indentComment) { |
998 |
printIndentationIfNecessary(buffer); |
1382 |
printIndentationIfNecessary(buffer); |
999 |
} |
1383 |
} |
1000 |
if (formatComment) { |
1384 |
if (this.formatBlockComment) { |
1001 |
if (ScannerHelper.isWhitespace((char) currentCharacter)) { |
1385 |
if (ScannerHelper.isWhitespace((char) currentCharacter)) { |
1002 |
int previousStartPosition = this.scanner.currentPosition; |
1386 |
int previousStartPosition = this.scanner.currentPosition; |
1003 |
while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { |
1387 |
while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { |
Lines 1042-1048
Link Here
|
1042 |
if (indentComment) { |
1426 |
if (indentComment) { |
1043 |
printIndentationIfNecessary(buffer); |
1427 |
printIndentationIfNecessary(buffer); |
1044 |
} |
1428 |
} |
1045 |
if (formatComment) { |
1429 |
if (this.formatBlockComment) { |
1046 |
int previousStartTemp = previousStart; |
1430 |
int previousStartTemp = previousStart; |
1047 |
int nextCharacterStartTemp = nextCharacterStart; |
1431 |
int nextCharacterStartTemp = nextCharacterStart; |
1048 |
while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { |
1432 |
while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { |
Lines 1070-1078
Link Here
|
1070 |
this.lastNumberOfNewLines = 0; |
1454 |
this.lastNumberOfNewLines = 0; |
1071 |
needSpace = false; |
1455 |
needSpace = false; |
1072 |
this.scanner.resetTo(currentTokenEndPosition, this.scannerEndPosition - 1); |
1456 |
this.scanner.resetTo(currentTokenEndPosition, this.scannerEndPosition - 1); |
1073 |
if (isJavadoc) { |
|
|
1074 |
printNewLine(); |
1075 |
} |
1076 |
} |
1457 |
} |
1077 |
|
1458 |
|
1078 |
public void printEndOfCompilationUnit() { |
1459 |
public void printEndOfCompilationUnit() { |
Lines 1132-1138
Link Here
|
1132 |
space(); |
1513 |
space(); |
1133 |
} |
1514 |
} |
1134 |
hasWhitespace = false; |
1515 |
hasWhitespace = false; |
1135 |
this.printLineComment(this.scanner.getRawTokenSource()); |
1516 |
this.printLineComment(); |
1136 |
currentTokenStartPosition = this.scanner.currentPosition; |
1517 |
currentTokenStartPosition = this.scanner.currentPosition; |
1137 |
hasLineComment = true; |
1518 |
hasLineComment = true; |
1138 |
count = 0; |
1519 |
count = 0; |
Lines 1148-1154
Link Here
|
1148 |
space(); |
1529 |
space(); |
1149 |
} |
1530 |
} |
1150 |
hasWhitespace = false; |
1531 |
hasWhitespace = false; |
1151 |
this.printBlockComment(this.scanner.getRawTokenSource(), false); |
1532 |
this.printBlockComment(); |
1152 |
currentTokenStartPosition = this.scanner.currentPosition; |
1533 |
currentTokenStartPosition = this.scanner.currentPosition; |
1153 |
hasLineComment = false; |
1534 |
hasLineComment = false; |
1154 |
hasComment = true; |
1535 |
hasComment = true; |
Lines 1165-1171
Link Here
|
1165 |
space(); |
1546 |
space(); |
1166 |
} |
1547 |
} |
1167 |
hasWhitespace = false; |
1548 |
hasWhitespace = false; |
1168 |
this.printBlockComment(this.scanner.getRawTokenSource(), true); |
1549 |
this.printJavadocComment(); |
1169 |
currentTokenStartPosition = this.scanner.currentPosition; |
1550 |
currentTokenStartPosition = this.scanner.currentPosition; |
1170 |
hasLineComment = false; |
1551 |
hasLineComment = false; |
1171 |
hasComment = true; |
1552 |
hasComment = true; |
Lines 1190-1196
Link Here
|
1190 |
throw new AbortFormatting(e); |
1571 |
throw new AbortFormatting(e); |
1191 |
} |
1572 |
} |
1192 |
} |
1573 |
} |
1193 |
|
1574 |
|
|
|
1575 |
/* |
1576 |
* Main method to print and format comments (javadoc, block and single line comments) |
1577 |
*/ |
1194 |
public void printComment() { |
1578 |
public void printComment() { |
1195 |
try { |
1579 |
try { |
1196 |
// if we have a space between two tokens we ensure it will be dumped in the formatted string |
1580 |
// if we have a space between two tokens we ensure it will be dumped in the formatted string |
Lines 1232-1237
Link Here
|
1232 |
this.preserveEmptyLines(count, this.scanner.getCurrentTokenStartPosition()); |
1616 |
this.preserveEmptyLines(count, this.scanner.getCurrentTokenStartPosition()); |
1233 |
addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition()); |
1617 |
addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition()); |
1234 |
} else if (count != 0 && this.formatter.preferences.number_of_empty_lines_to_preserve != 0) { |
1618 |
} else if (count != 0 && this.formatter.preferences.number_of_empty_lines_to_preserve != 0) { |
|
|
1619 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=184398 |
1620 |
if (currentTokenStartPosition == 0 && count > 1 && (count * this.lineSeparator.length() < this.scanner.source.length)) { |
1621 |
count = count - this.lineSeparator.length() + 1; |
1622 |
} |
1235 |
addReplaceEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition(), this.getPreserveEmptyLines(count - 1)); |
1623 |
addReplaceEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition(), this.getPreserveEmptyLines(count - 1)); |
1236 |
} else { |
1624 |
} else { |
1237 |
addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition()); |
1625 |
addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition()); |
Lines 1249-1255
Link Here
|
1249 |
space(); |
1637 |
space(); |
1250 |
} |
1638 |
} |
1251 |
hasWhitespace = false; |
1639 |
hasWhitespace = false; |
1252 |
this.printLineComment(this.scanner.getRawTokenSource()); |
1640 |
this.printLineComment(); |
1253 |
currentTokenStartPosition = this.scanner.currentPosition; |
1641 |
currentTokenStartPosition = this.scanner.currentPosition; |
1254 |
hasLineComment = true; |
1642 |
hasLineComment = true; |
1255 |
count = 0; |
1643 |
count = 0; |
Lines 1265-1271
Link Here
|
1265 |
space(); |
1653 |
space(); |
1266 |
} |
1654 |
} |
1267 |
hasWhitespace = false; |
1655 |
hasWhitespace = false; |
1268 |
this.printBlockComment(this.scanner.getRawTokenSource(), false); |
1656 |
if (this.formatBlockComment) { |
|
|
1657 |
this.printBlockComment(); |
1658 |
} else { |
1659 |
this.printCommentWithoutFormatting(); |
1660 |
} |
1269 |
currentTokenStartPosition = this.scanner.currentPosition; |
1661 |
currentTokenStartPosition = this.scanner.currentPosition; |
1270 |
hasLineComment = false; |
1662 |
hasLineComment = false; |
1271 |
hasComment = true; |
1663 |
hasComment = true; |
Lines 1282-1288
Link Here
|
1282 |
space(); |
1674 |
space(); |
1283 |
} |
1675 |
} |
1284 |
hasWhitespace = false; |
1676 |
hasWhitespace = false; |
1285 |
this.printBlockComment(this.scanner.getRawTokenSource(), true); |
1677 |
if (this.formatJavadocComment) { |
|
|
1678 |
this.printJavadocComment(); |
1679 |
} else { |
1680 |
this.printCommentWithoutFormatting(); |
1681 |
} |
1286 |
currentTokenStartPosition = this.scanner.currentPosition; |
1682 |
currentTokenStartPosition = this.scanner.currentPosition; |
1287 |
hasLineComment = false; |
1683 |
hasLineComment = false; |
1288 |
hasComment = true; |
1684 |
hasComment = true; |
Lines 1299-1305
Link Here
|
1299 |
} |
1695 |
} |
1300 |
} |
1696 |
} |
1301 |
|
1697 |
|
1302 |
private void printLineComment(char[] s) { |
1698 |
/* |
|
|
1699 |
* Print and formats a single line comment |
1700 |
*/ |
1701 |
private void printLineComment() { |
1303 |
int currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition(); |
1702 |
int currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition(); |
1304 |
int currentTokenEndPosition = this.scanner.getCurrentTokenEndPosition() + 1; |
1703 |
int currentTokenEndPosition = this.scanner.getCurrentTokenEndPosition() + 1; |
1305 |
if (CharOperation.indexOf(Scanner.TAG_PREFIX, this.scanner.source, true, currentTokenStartPosition, currentTokenEndPosition) != -1) { |
1704 |
if (CharOperation.indexOf(Scanner.TAG_PREFIX, this.scanner.source, true, currentTokenStartPosition, currentTokenEndPosition) != -1) { |
Lines 1468-1474
Link Here
|
1468 |
break; |
1867 |
break; |
1469 |
} |
1868 |
} |
1470 |
} |
1869 |
} |
|
|
1870 |
|
1871 |
/* |
1872 |
* Print and formats a javadoc comments |
1873 |
*/ |
1874 |
private void printJavadocComment() { |
1875 |
int currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition(); |
1876 |
int currentTokenEndPosition = this.scanner.getCurrentTokenEndPosition() + 1; |
1877 |
|
1878 |
// handle indentation |
1879 |
int lastIndentationLevel = this.indentationLevel; |
1880 |
if (this.indentationLevel != 0) { |
1881 |
printIndentationIfNecessary(); |
1882 |
} |
1883 |
|
1884 |
// handle pending space if any |
1885 |
if (this.pendingSpace) { |
1886 |
this.addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$ |
1887 |
} |
1888 |
this.needSpace = false; |
1889 |
this.pendingSpace = false; |
1890 |
|
1891 |
// parse the comment on the fly |
1892 |
if (! this.formatterCommentParser.parse(currentTokenStartPosition, currentTokenEndPosition, this.lineSeparator.length())) { |
1893 |
// problem occured while parsing the javadoc, early abort formatting |
1894 |
return; |
1895 |
} |
1896 |
|
1897 |
FormatJavadoc javadoc = (FormatJavadoc) formatterCommentParser.docComment; |
1898 |
if (javadoc.tags.length > 0) { |
1899 |
int max = javadoc.tags.length; |
1900 |
// check if we're dealing with a single text section |
1901 |
if (max == 2 && javadoc.tags[1].tagName == null) { |
1902 |
// we only have the header + a text section --> format as text |
1903 |
int textStart = javadoc.tags[0].startPosition; |
1904 |
this.formatText(javadoc.sourceStart, javadoc.sourceEnd - textStart, false); |
1905 |
} else {// formatting a standard javadoc, composed of a text section + a tag section |
1906 |
// format the comment header |
1907 |
formatJavadocHeader(); |
1908 |
|
1909 |
// format all other FJTags composing this comment |
1910 |
int lineOfDescriptionEnd = -1; |
1911 |
for (int i = 1; i < max ; i++) { |
1912 |
FormatJavadocTag nextTag = javadoc.tags[i]; |
1913 |
// pref: blank line before javadoc tag section |
1914 |
if (lineOfDescriptionEnd > -1 && this.formatter.preferences.comment_insert_empty_line_before_root_tags) { |
1915 |
if (Util.getLineNumber(nextTag.startPosition, this.lineEnds, 0, this.line) - lineOfDescriptionEnd == 1) { // add new line only if needed |
1916 |
printNewCommentLine(javadoc.tags[i - 1].endPosition); |
1917 |
} |
1918 |
} |
1919 |
|
1920 |
// format tag |
1921 |
this.formatJavadocTag(nextTag); |
1471 |
|
1922 |
|
|
|
1923 |
if (nextTag.tagName == null) { // description section formatted, remember line number |
1924 |
lineOfDescriptionEnd = this.line; |
1925 |
} |
1926 |
} |
1927 |
} |
1928 |
} |
1929 |
this.lastNumberOfNewLines = 0; |
1930 |
needSpace = false; |
1931 |
this.scanner.resetTo(javadoc.sourceEnd, this.scannerEndPosition - 1); |
1932 |
printNewLine(); |
1933 |
this.indentationLevel = lastIndentationLevel; |
1934 |
} |
1935 |
|
1472 |
public void printModifiers(Annotation[] annotations, ASTVisitor visitor) { |
1936 |
public void printModifiers(Annotation[] annotations, ASTVisitor visitor) { |
1473 |
printModifiers(annotations, visitor, ICodeFormatterConstants.ANNOTATION_UNSPECIFIED); |
1937 |
printModifiers(annotations, visitor, ICodeFormatterConstants.ANNOTATION_UNSPECIFIED); |
1474 |
} |
1938 |
} |
Lines 1538-1554
Link Here
|
1538 |
currentTokenStartPosition = this.scanner.currentPosition; |
2002 |
currentTokenStartPosition = this.scanner.currentPosition; |
1539 |
break; |
2003 |
break; |
1540 |
case TerminalTokens.TokenNameCOMMENT_BLOCK : |
2004 |
case TerminalTokens.TokenNameCOMMENT_BLOCK : |
1541 |
this.printBlockComment(this.scanner.getRawTokenSource(), false); |
2005 |
this.printBlockComment(); |
1542 |
currentTokenStartPosition = this.scanner.currentPosition; |
2006 |
currentTokenStartPosition = this.scanner.currentPosition; |
1543 |
hasComment = true; |
2007 |
hasComment = true; |
1544 |
break; |
2008 |
break; |
1545 |
case TerminalTokens.TokenNameCOMMENT_JAVADOC : |
2009 |
case TerminalTokens.TokenNameCOMMENT_JAVADOC : |
1546 |
this.printBlockComment(this.scanner.getRawTokenSource(), true); |
2010 |
this.printJavadocComment(); |
1547 |
currentTokenStartPosition = this.scanner.currentPosition; |
2011 |
currentTokenStartPosition = this.scanner.currentPosition; |
1548 |
hasComment = true; |
2012 |
hasComment = true; |
1549 |
break; |
2013 |
break; |
1550 |
case TerminalTokens.TokenNameCOMMENT_LINE : |
2014 |
case TerminalTokens.TokenNameCOMMENT_LINE : |
1551 |
this.printLineComment(this.scanner.getRawTokenSource()); |
2015 |
this.printLineComment(); |
1552 |
currentTokenStartPosition = this.scanner.currentPosition; |
2016 |
currentTokenStartPosition = this.scanner.currentPosition; |
1553 |
break; |
2017 |
break; |
1554 |
case TerminalTokens.TokenNameWHITESPACE : |
2018 |
case TerminalTokens.TokenNameWHITESPACE : |
Lines 1587-1608
Link Here
|
1587 |
} catch (InvalidInputException e) { |
2051 |
} catch (InvalidInputException e) { |
1588 |
throw new AbortFormatting(e); |
2052 |
throw new AbortFormatting(e); |
1589 |
} |
2053 |
} |
|
|
2054 |
} |
2055 |
|
2056 |
/* |
2057 |
* Inserts a new comment (javadoc / block) line at the given position |
2058 |
*/ |
2059 |
private void printNewCommentLine(int insertPosition) { |
2060 |
this.column = 0; |
2061 |
StringBuffer buffer = new StringBuffer(); |
2062 |
buffer.append(this.lineSeparator); |
2063 |
printIndentationIfNecessary(buffer); |
2064 |
buffer.append(" * "); //$NON-NLS-1$ |
2065 |
addInsertEdit(insertPosition, buffer.toString()); |
1590 |
} |
2066 |
} |
1591 |
|
2067 |
|
1592 |
public void printNewLine() { |
2068 |
public void printNewLine() { |
1593 |
if (this.nlsTagCounter > 0) { |
2069 |
this.printNewLine(this.scanner.getCurrentTokenEndPosition() + 1); |
1594 |
return; |
|
|
1595 |
} |
1596 |
if (lastNumberOfNewLines >= 1) { |
1597 |
column = 1; // ensure that the scribe is at the beginning of a new line |
1598 |
return; |
1599 |
} |
1600 |
addInsertEdit(this.scanner.getCurrentTokenEndPosition() + 1, this.lineSeparator); |
1601 |
line++; |
1602 |
lastNumberOfNewLines = 1; |
1603 |
column = 1; |
1604 |
needSpace = false; |
1605 |
this.pendingSpace = false; |
1606 |
} |
2070 |
} |
1607 |
|
2071 |
|
1608 |
public void printNewLine(int insertPosition) { |
2072 |
public void printNewLine(int insertPosition) { |
Lines 1678-1689
Link Here
|
1678 |
currentTokenStartPosition = this.scanner.currentPosition; |
2142 |
currentTokenStartPosition = this.scanner.currentPosition; |
1679 |
break; |
2143 |
break; |
1680 |
case TerminalTokens.TokenNameCOMMENT_BLOCK : |
2144 |
case TerminalTokens.TokenNameCOMMENT_BLOCK : |
|
|
2145 |
this.printBlockComment(); |
2146 |
currentTokenStartPosition = this.scanner.currentPosition; |
2147 |
break; |
1681 |
case TerminalTokens.TokenNameCOMMENT_JAVADOC : |
2148 |
case TerminalTokens.TokenNameCOMMENT_JAVADOC : |
1682 |
this.printBlockComment(this.scanner.getRawTokenSource(), false); |
2149 |
this.printJavadocComment(); |
1683 |
currentTokenStartPosition = this.scanner.currentPosition; |
2150 |
currentTokenStartPosition = this.scanner.currentPosition; |
1684 |
break; |
2151 |
break; |
1685 |
case TerminalTokens.TokenNameCOMMENT_LINE : |
2152 |
case TerminalTokens.TokenNameCOMMENT_LINE : |
1686 |
this.printLineComment(this.scanner.getRawTokenSource()); |
2153 |
this.printLineComment(); |
1687 |
currentTokenStartPosition = this.scanner.currentPosition; |
2154 |
currentTokenStartPosition = this.scanner.currentPosition; |
1688 |
break; |
2155 |
break; |
1689 |
case TerminalTokens.TokenNameIdentifier : |
2156 |
case TerminalTokens.TokenNameIdentifier : |
Lines 1722-1732
Link Here
|
1722 |
break; |
2189 |
break; |
1723 |
case TerminalTokens.TokenNameCOMMENT_BLOCK : |
2190 |
case TerminalTokens.TokenNameCOMMENT_BLOCK : |
1724 |
case TerminalTokens.TokenNameCOMMENT_JAVADOC : |
2191 |
case TerminalTokens.TokenNameCOMMENT_JAVADOC : |
1725 |
this.printBlockComment(this.scanner.getRawTokenSource(), false); |
2192 |
this.printBlockComment(); |
1726 |
currentTokenStartPosition = this.scanner.currentPosition; |
2193 |
currentTokenStartPosition = this.scanner.currentPosition; |
1727 |
break; |
2194 |
break; |
1728 |
case TerminalTokens.TokenNameCOMMENT_LINE : |
2195 |
case TerminalTokens.TokenNameCOMMENT_LINE : |
1729 |
this.printLineComment(this.scanner.getRawTokenSource()); |
2196 |
this.printLineComment(); |
1730 |
currentTokenStartPosition = this.scanner.currentPosition; |
2197 |
currentTokenStartPosition = this.scanner.currentPosition; |
1731 |
break; |
2198 |
break; |
1732 |
case TerminalTokens.TokenNameIdentifier : |
2199 |
case TerminalTokens.TokenNameIdentifier : |
Lines 1809-1815
Link Here
|
1809 |
if (hasWhitespaces) { |
2276 |
if (hasWhitespaces) { |
1810 |
space(); |
2277 |
space(); |
1811 |
} |
2278 |
} |
1812 |
this.printLineComment(this.scanner.getRawTokenSource()); |
2279 |
this.printLineComment(); |
1813 |
currentTokenStartPosition = this.scanner.currentPosition; |
2280 |
currentTokenStartPosition = this.scanner.currentPosition; |
1814 |
hasLineComment = true; |
2281 |
hasLineComment = true; |
1815 |
break; |
2282 |
break; |
Lines 1817-1823
Link Here
|
1817 |
if (hasWhitespaces) { |
2284 |
if (hasWhitespaces) { |
1818 |
space(); |
2285 |
space(); |
1819 |
} |
2286 |
} |
1820 |
this.printBlockComment(this.scanner.getRawTokenSource(), false); |
2287 |
this.printBlockComment(); |
1821 |
currentTokenStartPosition = this.scanner.currentPosition; |
2288 |
currentTokenStartPosition = this.scanner.currentPosition; |
1822 |
break; |
2289 |
break; |
1823 |
default : |
2290 |
default : |
Lines 1882-1888
Link Here
|
1882 |
if (hasWhitespaces) { |
2349 |
if (hasWhitespaces) { |
1883 |
space(); |
2350 |
space(); |
1884 |
} |
2351 |
} |
1885 |
this.printLineComment(this.scanner.getRawTokenSource()); |
2352 |
this.printLineComment(); |
1886 |
currentTokenStartPosition = this.scanner.currentPosition; |
2353 |
currentTokenStartPosition = this.scanner.currentPosition; |
1887 |
hasLineComment = true; |
2354 |
hasLineComment = true; |
1888 |
break; |
2355 |
break; |
Lines 1890-1896
Link Here
|
1890 |
if (hasWhitespaces) { |
2357 |
if (hasWhitespaces) { |
1891 |
space(); |
2358 |
space(); |
1892 |
} |
2359 |
} |
1893 |
this.printBlockComment(this.scanner.getRawTokenSource(), false); |
2360 |
this.printBlockComment(); |
1894 |
currentTokenStartPosition = this.scanner.currentPosition; |
2361 |
currentTokenStartPosition = this.scanner.currentPosition; |
1895 |
hasComment = true; |
2362 |
hasComment = true; |
1896 |
break; |
2363 |
break; |