Lines 73-81
Link Here
|
73 |
public int numberOfIndentations; |
73 |
public int numberOfIndentations; |
74 |
private boolean useTabsOnlyForLeadingIndents; |
74 |
private boolean useTabsOnlyForLeadingIndents; |
75 |
|
75 |
|
76 |
/** indent empty lines*/ |
76 |
/** indent empty lines*/ |
77 |
private final boolean indentEmptyLines; |
77 |
private final boolean indentEmptyLines; |
78 |
|
78 |
|
|
|
79 |
private final boolean formatJavadocComment; |
80 |
private final boolean formatBlockComment; |
81 |
|
79 |
Scribe(CodeFormatterVisitor formatter, long sourceLevel, int offset, int length, CodeSnippetParsingUtil codeSnippetParsingUtil) { |
82 |
Scribe(CodeFormatterVisitor formatter, long sourceLevel, int offset, int length, CodeSnippetParsingUtil codeSnippetParsingUtil) { |
80 |
this.scanner = new Scanner(true, true, false/*nls*/, sourceLevel/*sourceLevel*/, null/*taskTags*/, null/*taskPriorities*/, true/*taskCaseSensitive*/); |
83 |
this.scanner = new Scanner(true, true, false/*nls*/, sourceLevel/*sourceLevel*/, null/*taskTags*/, null/*taskPriorities*/, true/*taskCaseSensitive*/); |
81 |
this.formatter = formatter; |
84 |
this.formatter = formatter; |
Lines 102-107
Link Here
|
102 |
this.commentPositions = information.commentPositions; |
105 |
this.commentPositions = information.commentPositions; |
103 |
} |
106 |
} |
104 |
} |
107 |
} |
|
|
108 |
this.formatBlockComment = formatter.preferences.comment_format_block_comment; |
109 |
this.formatJavadocComment = formatter.preferences.comment_format_javadoc_comment; |
105 |
reset(); |
110 |
reset(); |
106 |
} |
111 |
} |
107 |
|
112 |
|
Lines 401-406
Link Here
|
401 |
return -(g + 1); |
406 |
return -(g + 1); |
402 |
} |
407 |
} |
403 |
|
408 |
|
|
|
409 |
private int getCurrentCommentOffset(int start) { |
410 |
int linePtr = -Arrays.binarySearch(this.lineEnds, start); |
411 |
int offset = 0; |
412 |
int beginningOfLine = this.getLineEnd(linePtr - 1); |
413 |
if (beginningOfLine == -1) { |
414 |
beginningOfLine = 0; |
415 |
} |
416 |
int currentStartPosition = start; |
417 |
char[] source = scanner.source; |
418 |
|
419 |
// find the position of the beginning of the line containing the comment |
420 |
while (beginningOfLine > currentStartPosition) { |
421 |
if (linePtr > 0) { |
422 |
beginningOfLine = this.getLineEnd(--linePtr); |
423 |
} else { |
424 |
beginningOfLine = 0; |
425 |
break; |
426 |
} |
427 |
} |
428 |
for (int i = currentStartPosition - 1; i >= beginningOfLine ; i--) { |
429 |
char currentCharacter = source[i]; |
430 |
switch (currentCharacter) { |
431 |
case '\t' : |
432 |
offset += this.tabLength; |
433 |
break; |
434 |
case ' ' : |
435 |
offset++; |
436 |
break; |
437 |
case '\r' : |
438 |
case '\n' : |
439 |
break; |
440 |
default: |
441 |
return offset; |
442 |
} |
443 |
} |
444 |
return offset; |
445 |
} |
446 |
|
404 |
public String getEmptyLines(int linesNumber) { |
447 |
public String getEmptyLines(int linesNumber) { |
405 |
if (this.nlsTagCounter > 0) { |
448 |
if (this.nlsTagCounter > 0) { |
406 |
return Util.EMPTY_STRING; |
449 |
return Util.EMPTY_STRING; |
Lines 607-613
Link Here
|
607 |
this.scanner.resetTo(0, this.scannerEndPosition - 1); |
650 |
this.scanner.resetTo(0, this.scannerEndPosition - 1); |
608 |
this.edits = new OptimizedReplaceEdit[INITIAL_SIZE]; |
651 |
this.edits = new OptimizedReplaceEdit[INITIAL_SIZE]; |
609 |
} |
652 |
} |
610 |
|
653 |
|
|
|
654 |
private boolean isOnFirstColumn(int start) { |
655 |
if (this.lineEnds == null) return start == 0; |
656 |
int index = Arrays.binarySearch(this.lineEnds, start); |
657 |
// we want the line end of the previous line |
658 |
int previousLineEnd = this.getLineEnd(-index - 1); |
659 |
return previousLineEnd != -1 && previousLineEnd == start - 1; |
660 |
} |
661 |
|
611 |
private boolean isValidEdit(OptimizedReplaceEdit edit) { |
662 |
private boolean isValidEdit(OptimizedReplaceEdit edit) { |
612 |
final int editLength= edit.length; |
663 |
final int editLength= edit.length; |
613 |
final int editReplacementLength= edit.replacement.length(); |
664 |
final int editReplacementLength= edit.replacement.length(); |
Lines 663-669
Link Here
|
663 |
handleLineTooLong(); |
714 |
handleLineTooLong(); |
664 |
} |
715 |
} |
665 |
this.lastNumberOfNewLines = 0; |
716 |
this.lastNumberOfNewLines = 0; |
666 |
printIndentationIfNecessary(); |
717 |
if (this.indentationLevel != 0) { |
|
|
718 |
printIndentationIfNecessary(); |
719 |
} |
667 |
if (considerSpaceIfAny) { |
720 |
if (considerSpaceIfAny) { |
668 |
this.space(); |
721 |
this.space(); |
669 |
} |
722 |
} |
Lines 685-697
Link Here
|
685 |
boolean isNewLine = false; |
738 |
boolean isNewLine = false; |
686 |
int start = currentTokenStartPosition; |
739 |
int start = currentTokenStartPosition; |
687 |
int nextCharacterStart = currentTokenStartPosition; |
740 |
int nextCharacterStart = currentTokenStartPosition; |
688 |
printIndentationIfNecessary(); |
741 |
int previousStart = currentTokenStartPosition; |
|
|
742 |
boolean onFirstColumn = isOnFirstColumn(start); |
743 |
|
744 |
boolean indentComment = false; |
745 |
if (this.indentationLevel != 0) { |
746 |
if (isJavadoc |
747 |
|| !this.formatter.preferences.never_indent_block_comments_on_first_column |
748 |
|| !onFirstColumn) { |
749 |
indentComment = true; |
750 |
printIndentationIfNecessary(); |
751 |
} |
752 |
} |
689 |
if (this.pendingSpace) { |
753 |
if (this.pendingSpace) { |
690 |
this.addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$ |
754 |
this.addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$ |
691 |
} |
755 |
} |
692 |
this.needSpace = false; |
756 |
this.needSpace = false; |
693 |
this.pendingSpace = false; |
757 |
this.pendingSpace = false; |
694 |
int previousStart = currentTokenStartPosition; |
758 |
|
|
|
759 |
int currentCommentOffset = onFirstColumn ? 0 : getCurrentCommentOffset(start); |
760 |
boolean formatComment = (isJavadoc && formatJavadocComment) || (!isJavadoc && formatBlockComment); |
695 |
|
761 |
|
696 |
while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter = this.scanner.getNextChar()) != -1) { |
762 |
while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter = this.scanner.getNextChar()) != -1) { |
697 |
nextCharacterStart = this.scanner.currentPosition; |
763 |
nextCharacterStart = this.scanner.currentPosition; |
Lines 708-741
Link Here
|
708 |
case '\n' : |
774 |
case '\n' : |
709 |
start = previousStart; |
775 |
start = previousStart; |
710 |
isNewLine = true; |
776 |
isNewLine = true; |
|
|
777 |
nextCharacterStart = this.scanner.currentPosition; |
711 |
break; |
778 |
break; |
712 |
default: |
779 |
default: |
713 |
if (isNewLine) { |
780 |
if (isNewLine) { |
714 |
if (ScannerHelper.isWhitespace((char) currentCharacter)) { |
|
|
715 |
int previousStartPosition = this.scanner.currentPosition; |
716 |
while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { |
717 |
previousStart = nextCharacterStart; |
718 |
previousStartPosition = this.scanner.currentPosition; |
719 |
currentCharacter = this.scanner.getNextChar(); |
720 |
nextCharacterStart = this.scanner.currentPosition; |
721 |
} |
722 |
if (currentCharacter == '\r' || currentCharacter == '\n') { |
723 |
nextCharacterStart = previousStartPosition; |
724 |
} |
725 |
} |
726 |
this.column = 1; |
781 |
this.column = 1; |
727 |
this.line++; |
782 |
this.line++; |
728 |
|
783 |
isNewLine = false; |
|
|
784 |
|
729 |
StringBuffer buffer = new StringBuffer(); |
785 |
StringBuffer buffer = new StringBuffer(); |
730 |
buffer.append(this.lineSeparator); |
786 |
if (onFirstColumn) { |
731 |
printIndentationIfNecessary(buffer); |
787 |
// simply insert indentation if necessary |
732 |
buffer.append(' '); |
788 |
buffer.append(this.lineSeparator); |
733 |
|
789 |
if (indentComment) { |
|
|
790 |
printIndentationIfNecessary(buffer); |
791 |
} |
792 |
if (formatComment) { |
793 |
if (ScannerHelper.isWhitespace((char) currentCharacter)) { |
794 |
int previousStartPosition = this.scanner.currentPosition; |
795 |
while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { |
796 |
previousStart = nextCharacterStart; |
797 |
previousStartPosition = this.scanner.currentPosition; |
798 |
currentCharacter = this.scanner.getNextChar(); |
799 |
nextCharacterStart = this.scanner.currentPosition; |
800 |
} |
801 |
if (currentCharacter == '\r' || currentCharacter == '\n') { |
802 |
nextCharacterStart = previousStartPosition; |
803 |
} |
804 |
} |
805 |
if (currentCharacter != '\r' && currentCharacter != '\n') { |
806 |
buffer.append(' '); |
807 |
} |
808 |
} |
809 |
} else { |
810 |
if (ScannerHelper.isWhitespace((char) currentCharacter)) { |
811 |
int previousStartPosition = this.scanner.currentPosition; |
812 |
int count = 0; |
813 |
loop: while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { |
814 |
if (count >= currentCommentOffset) { |
815 |
break loop; |
816 |
} |
817 |
previousStart = nextCharacterStart; |
818 |
previousStartPosition = this.scanner.currentPosition; |
819 |
switch(currentCharacter) { |
820 |
case '\t' : |
821 |
count += this.tabLength; |
822 |
break; |
823 |
default : |
824 |
count ++; |
825 |
} |
826 |
currentCharacter = this.scanner.getNextChar(); |
827 |
nextCharacterStart = this.scanner.currentPosition; |
828 |
} |
829 |
if (currentCharacter == '\r' || currentCharacter == '\n') { |
830 |
nextCharacterStart = previousStartPosition; |
831 |
} |
832 |
} |
833 |
buffer.append(this.lineSeparator); |
834 |
if (indentComment) { |
835 |
printIndentationIfNecessary(buffer); |
836 |
} |
837 |
if (formatComment) { |
838 |
int previousStartTemp = previousStart; |
839 |
int nextCharacterStartTemp = nextCharacterStart; |
840 |
while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { |
841 |
previousStart = nextCharacterStart; |
842 |
currentCharacter = this.scanner.getNextChar(); |
843 |
nextCharacterStart = this.scanner.currentPosition; |
844 |
} |
845 |
if (currentCharacter == '*') { |
846 |
buffer.append(' '); |
847 |
} else { |
848 |
previousStart = previousStartTemp; |
849 |
nextCharacterStart = nextCharacterStartTemp; |
850 |
} |
851 |
this.scanner.currentPosition = nextCharacterStart; |
852 |
} |
853 |
} |
734 |
addReplaceEdit(start, previousStart - 1, String.valueOf(buffer)); |
854 |
addReplaceEdit(start, previousStart - 1, String.valueOf(buffer)); |
735 |
} else { |
855 |
} else { |
736 |
this.column += (nextCharacterStart - previousStart); |
856 |
this.column += (nextCharacterStart - previousStart); |
737 |
} |
857 |
} |
738 |
isNewLine = false; |
|
|
739 |
} |
858 |
} |
740 |
previousStart = nextCharacterStart; |
859 |
previousStart = nextCharacterStart; |
741 |
this.scanner.currentPosition = nextCharacterStart; |
860 |
this.scanner.currentPosition = nextCharacterStart; |
Lines 805-811
Link Here
|
805 |
space(); |
924 |
space(); |
806 |
} |
925 |
} |
807 |
hasWhitespace = false; |
926 |
hasWhitespace = false; |
808 |
this.printCommentLine(this.scanner.getRawTokenSource()); |
927 |
this.printLineComment(this.scanner.getRawTokenSource()); |
809 |
currentTokenStartPosition = this.scanner.currentPosition; |
928 |
currentTokenStartPosition = this.scanner.currentPosition; |
810 |
hasLineComment = true; |
929 |
hasLineComment = true; |
811 |
count = 0; |
930 |
count = 0; |
Lines 922-928
Link Here
|
922 |
space(); |
1041 |
space(); |
923 |
} |
1042 |
} |
924 |
hasWhitespace = false; |
1043 |
hasWhitespace = false; |
925 |
this.printCommentLine(this.scanner.getRawTokenSource()); |
1044 |
this.printLineComment(this.scanner.getRawTokenSource()); |
926 |
currentTokenStartPosition = this.scanner.currentPosition; |
1045 |
currentTokenStartPosition = this.scanner.currentPosition; |
927 |
hasLineComment = true; |
1046 |
hasLineComment = true; |
928 |
count = 0; |
1047 |
count = 0; |
Lines 972-978
Link Here
|
972 |
} |
1091 |
} |
973 |
} |
1092 |
} |
974 |
|
1093 |
|
975 |
private void printCommentLine(char[] s) { |
1094 |
private void printLineComment(char[] s) { |
976 |
int currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition(); |
1095 |
int currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition(); |
977 |
int currentTokenEndPosition = this.scanner.getCurrentTokenEndPosition() + 1; |
1096 |
int currentTokenEndPosition = this.scanner.getCurrentTokenEndPosition() + 1; |
978 |
if (CharOperation.indexOf(Scanner.TAG_PREFIX, this.scanner.source, true, currentTokenStartPosition) != -1) { |
1097 |
if (CharOperation.indexOf(Scanner.TAG_PREFIX, this.scanner.source, true, currentTokenStartPosition) != -1) { |
Lines 982-993
Link Here
|
982 |
int currentCharacter; |
1101 |
int currentCharacter; |
983 |
int start = currentTokenStartPosition; |
1102 |
int start = currentTokenStartPosition; |
984 |
int nextCharacterStart = currentTokenStartPosition; |
1103 |
int nextCharacterStart = currentTokenStartPosition; |
985 |
printIndentationIfNecessary(); |
1104 |
|
|
|
1105 |
if (this.indentationLevel != 0) { |
1106 |
if (!this.formatter.preferences.never_indent_line_comments_on_first_column |
1107 |
|| !isOnFirstColumn(start)) { |
1108 |
printIndentationIfNecessary(); |
1109 |
} |
1110 |
} |
986 |
if (this.pendingSpace) { |
1111 |
if (this.pendingSpace) { |
987 |
this.addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$ |
1112 |
this.addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$ |
988 |
} |
1113 |
} |
989 |
this.needSpace = false; |
1114 |
this.needSpace = false; |
990 |
this.pendingSpace = false; |
1115 |
this.pendingSpace = false; |
991 |
int previousStart = currentTokenStartPosition; |
1116 |
int previousStart = currentTokenStartPosition; |
992 |
|
1117 |
|
993 |
loop: while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter = this.scanner.getNextChar()) != -1) { |
1118 |
loop: while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter = this.scanner.getNextChar()) != -1) { |
Lines 1190-1196
Link Here
|
1190 |
hasComment = true; |
1315 |
hasComment = true; |
1191 |
break; |
1316 |
break; |
1192 |
case TerminalTokens.TokenNameCOMMENT_LINE : |
1317 |
case TerminalTokens.TokenNameCOMMENT_LINE : |
1193 |
this.printCommentLine(this.scanner.getRawTokenSource()); |
1318 |
this.printLineComment(this.scanner.getRawTokenSource()); |
1194 |
currentTokenStartPosition = this.scanner.currentPosition; |
1319 |
currentTokenStartPosition = this.scanner.currentPosition; |
1195 |
break; |
1320 |
break; |
1196 |
case TerminalTokens.TokenNameWHITESPACE : |
1321 |
case TerminalTokens.TokenNameWHITESPACE : |
Lines 1325-1331
Link Here
|
1325 |
currentTokenStartPosition = this.scanner.currentPosition; |
1450 |
currentTokenStartPosition = this.scanner.currentPosition; |
1326 |
break; |
1451 |
break; |
1327 |
case TerminalTokens.TokenNameCOMMENT_LINE : |
1452 |
case TerminalTokens.TokenNameCOMMENT_LINE : |
1328 |
this.printCommentLine(this.scanner.getRawTokenSource()); |
1453 |
this.printLineComment(this.scanner.getRawTokenSource()); |
1329 |
currentTokenStartPosition = this.scanner.currentPosition; |
1454 |
currentTokenStartPosition = this.scanner.currentPosition; |
1330 |
break; |
1455 |
break; |
1331 |
case TerminalTokens.TokenNameIdentifier : |
1456 |
case TerminalTokens.TokenNameIdentifier : |
Lines 1368-1374
Link Here
|
1368 |
currentTokenStartPosition = this.scanner.currentPosition; |
1493 |
currentTokenStartPosition = this.scanner.currentPosition; |
1369 |
break; |
1494 |
break; |
1370 |
case TerminalTokens.TokenNameCOMMENT_LINE : |
1495 |
case TerminalTokens.TokenNameCOMMENT_LINE : |
1371 |
this.printCommentLine(this.scanner.getRawTokenSource()); |
1496 |
this.printLineComment(this.scanner.getRawTokenSource()); |
1372 |
currentTokenStartPosition = this.scanner.currentPosition; |
1497 |
currentTokenStartPosition = this.scanner.currentPosition; |
1373 |
break; |
1498 |
break; |
1374 |
case TerminalTokens.TokenNameIdentifier : |
1499 |
case TerminalTokens.TokenNameIdentifier : |
Lines 1452-1458
Link Here
|
1452 |
if (hasWhitespaces) { |
1577 |
if (hasWhitespaces) { |
1453 |
space(); |
1578 |
space(); |
1454 |
} |
1579 |
} |
1455 |
this.printCommentLine(this.scanner.getRawTokenSource()); |
1580 |
this.printLineComment(this.scanner.getRawTokenSource()); |
1456 |
currentTokenStartPosition = this.scanner.currentPosition; |
1581 |
currentTokenStartPosition = this.scanner.currentPosition; |
1457 |
hasLineComment = true; |
1582 |
hasLineComment = true; |
1458 |
break; |
1583 |
break; |
Lines 1526-1532
Link Here
|
1526 |
if (hasWhitespaces) { |
1651 |
if (hasWhitespaces) { |
1527 |
space(); |
1652 |
space(); |
1528 |
} |
1653 |
} |
1529 |
this.printCommentLine(this.scanner.getRawTokenSource()); |
1654 |
this.printLineComment(this.scanner.getRawTokenSource()); |
1530 |
currentTokenStartPosition = this.scanner.currentPosition; |
1655 |
currentTokenStartPosition = this.scanner.currentPosition; |
1531 |
hasLineComment = true; |
1656 |
hasLineComment = true; |
1532 |
break; |
1657 |
break; |