Lines 842-851
Link Here
|
842 |
return indentation; |
842 |
return indentation; |
843 |
} |
843 |
} |
844 |
|
844 |
|
845 |
int getCurrentIndentation(char[] whitespaces) { |
845 |
int getCurrentIndentation(char[] whitespaces, int offset) { |
846 |
int length = whitespaces.length; |
846 |
int length = whitespaces.length; |
847 |
if (this.tabLength == 0) return length; |
847 |
if (this.tabLength == 0) return length; |
848 |
int indentation = 0; |
848 |
int indentation = offset; |
849 |
for (int i=0; i<length; i++) { |
849 |
for (int i=0; i<length; i++) { |
850 |
char ch = whitespaces[i]; |
850 |
char ch = whitespaces[i]; |
851 |
switch (ch) { |
851 |
switch (ch) { |
Lines 2178-2184
Link Here
|
2178 |
// when following comment column (after having been rounded) is below the preceding one, |
2178 |
// when following comment column (after having been rounded) is below the preceding one, |
2179 |
// then it becomes not a good idea to change the trailing flag |
2179 |
// then it becomes not a good idea to change the trailing flag |
2180 |
if (trailing == BASIC_TRAILING_COMMENT && hasLineComment) { |
2180 |
if (trailing == BASIC_TRAILING_COMMENT && hasLineComment) { |
2181 |
int currentCommentIndentation = getCurrentIndentation(whiteSpaces); |
2181 |
int currentCommentIndentation = getCurrentIndentation(whiteSpaces, 0); |
2182 |
int lastCommentIndentation = this.lastLineComment.currentIndentation; |
2182 |
int lastCommentIndentation = this.lastLineComment.currentIndentation; |
2183 |
if (this.tabLength > 0) { |
2183 |
if (this.tabLength > 0) { |
2184 |
if ((currentCommentIndentation % this.tabLength) == 0) { |
2184 |
if ((currentCommentIndentation % this.tabLength) == 0) { |
Lines 2410-2416
Link Here
|
2410 |
// see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=293300 |
2410 |
// see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=293300 |
2411 |
if (this.lastLineComment.contiguous) { |
2411 |
if (this.lastLineComment.contiguous) { |
2412 |
// The leading spaces have been set while looping in the printComment(int) method |
2412 |
// The leading spaces have been set while looping in the printComment(int) method |
2413 |
int currentCommentIndentation = getCurrentIndentation(this.lastLineComment.leadingSpaces); |
2413 |
int currentCommentIndentation = getCurrentIndentation(this.lastLineComment.leadingSpaces, 0); |
2414 |
// Keep the current comment indentation when over the previous contiguous line comment |
2414 |
// Keep the current comment indentation when over the previous contiguous line comment |
2415 |
// and the previous comment has not been reindented |
2415 |
// and the previous comment has not been reindented |
2416 |
int lastCommentIndentation = this.lastLineComment.currentIndentation; |
2416 |
int lastCommentIndentation = this.lastLineComment.currentIndentation; |
Lines 2869-2875
Link Here
|
2869 |
if (newLines == 0 && (!node.isImmutable() || block.reference != null)) { |
2869 |
if (newLines == 0 && (!node.isImmutable() || block.reference != null)) { |
2870 |
newLines = printJavadocBlockNodesNewLines(block, node, previousEnd); |
2870 |
newLines = printJavadocBlockNodesNewLines(block, node, previousEnd); |
2871 |
} |
2871 |
} |
2872 |
printJavadocGapLines(previousEnd+1, nodeStart-1, newLines, clearBlankLines, false, null); |
2872 |
if (block.isImmutable()) { |
|
|
2873 |
printJavadocGapLinesForImmutableBlock(block); |
2874 |
} else { |
2875 |
printJavadocGapLines(previousEnd+1, nodeStart-1, newLines, clearBlankLines, false, null); |
2876 |
} |
2873 |
} else { |
2877 |
} else { |
2874 |
StringBuffer buffer = new StringBuffer(); |
2878 |
StringBuffer buffer = new StringBuffer(); |
2875 |
if (newLines > 0) { |
2879 |
if (newLines > 0) { |
Lines 2900-2906
Link Here
|
2900 |
FormatJavadocText text = (FormatJavadocText) node; |
2904 |
FormatJavadocText text = (FormatJavadocText) node; |
2901 |
if (text.isImmutable()) { |
2905 |
if (text.isImmutable()) { |
2902 |
// Indent if new line was added |
2906 |
// Indent if new line was added |
2903 |
if (newLines > 0 && this.commentIndentation != null) { |
2907 |
if (text.isImmutableHtmlTag() && newLines > 0 && this.commentIndentation != null) { |
2904 |
addInsertEdit(node.sourceStart, this.commentIndentation); |
2908 |
addInsertEdit(node.sourceStart, this.commentIndentation); |
2905 |
this.column += this.commentIndentation.length(); |
2909 |
this.column += this.commentIndentation.length(); |
2906 |
} |
2910 |
} |
Lines 2964-2993
Link Here
|
2964 |
int token = this.scanner.getNextToken(); |
2968 |
int token = this.scanner.getNextToken(); |
2965 |
switch (token) { |
2969 |
switch (token) { |
2966 |
case TerminalTokens.TokenNameWHITESPACE: |
2970 |
case TerminalTokens.TokenNameWHITESPACE: |
2967 |
if (nodeIsText) { |
2971 |
if (CharOperation.indexOf('\n', this.scanner.source, this.scanner.startPosition, this.scanner.currentPosition) >= 0) { |
2968 |
if (CharOperation.indexOf('\n', this.scanner.source, this.scanner.startPosition, this.scanner.currentPosition) >= 0) { |
2972 |
return 0; |
2969 |
return 0; |
|
|
2970 |
} |
2971 |
} |
2973 |
} |
2972 |
length = 1; |
2974 |
lastColumn = getCurrentIndentation(this.scanner.getCurrentTokenSource(), lastColumn); |
2973 |
break; |
2975 |
break; |
2974 |
case TerminalTokens.TokenNameMULTIPLY: |
2976 |
case TerminalTokens.TokenNameMULTIPLY: |
2975 |
if (newLine) { |
2977 |
if (newLine) { |
2976 |
newLine = false; |
2978 |
newLine = false; |
2977 |
continue; |
2979 |
continue; |
2978 |
} |
2980 |
} |
2979 |
length = 1; |
2981 |
lastColumn++; |
2980 |
break; |
2982 |
break; |
2981 |
default: |
2983 |
default: |
2982 |
length = (this.scanner.atEnd() ? this.scanner.eofPosition : this.scanner.currentPosition) - this.scanner.startPosition; |
2984 |
lastColumn += (this.scanner.atEnd() ? this.scanner.eofPosition : this.scanner.currentPosition) - this.scanner.startPosition; |
2983 |
break; |
2985 |
break; |
2984 |
} |
2986 |
} |
2985 |
} |
2987 |
} |
2986 |
catch (InvalidInputException iie) { |
2988 |
catch (InvalidInputException iie) { |
2987 |
// maybe an unterminated string or comment |
2989 |
// maybe an unterminated string or comment |
2988 |
length = (this.scanner.atEnd() ? this.scanner.eofPosition : this.scanner.currentPosition) - this.scanner.startPosition; |
2990 |
lastColumn += (this.scanner.atEnd() ? this.scanner.eofPosition : this.scanner.currentPosition) - this.scanner.startPosition; |
2989 |
} |
2991 |
} |
2990 |
lastColumn += length; |
|
|
2991 |
if (lastColumn > maxColumn) { |
2992 |
if (lastColumn > maxColumn) { |
2992 |
return 1; |
2993 |
return 1; |
2993 |
} |
2994 |
} |
Lines 3347-3353
Link Here
|
3347 |
if (linesGap > 0) { |
3348 |
if (linesGap > 0) { |
3348 |
StringBuffer buffer = new StringBuffer(); |
3349 |
StringBuffer buffer = new StringBuffer(); |
3349 |
if (lineCount > 0) { |
3350 |
if (lineCount > 0) { |
3350 |
// TODO (eric) https://bugs.eclipse.org/bugs/show_bug.cgi?id=49619 |
3351 |
// TODO https://bugs.eclipse.org/bugs/show_bug.cgi?id=49619 |
3351 |
buffer.append( ' '); |
3352 |
buffer.append( ' '); |
3352 |
} |
3353 |
} |
3353 |
for (int i = 0; i < linesGap ; i++) { |
3354 |
for (int i = 0; i < linesGap ; i++) { |
Lines 3409-3415
Link Here
|
3409 |
// Insert new lines as not enough was encountered while scanning the whitespaces |
3410 |
// Insert new lines as not enough was encountered while scanning the whitespaces |
3410 |
StringBuffer buffer = new StringBuffer(); |
3411 |
StringBuffer buffer = new StringBuffer(); |
3411 |
if (lineCount > 0) { |
3412 |
if (lineCount > 0) { |
3412 |
// TODO (eric) https://bugs.eclipse.org/bugs/show_bug.cgi?id=49619 |
3413 |
// TODO https://bugs.eclipse.org/bugs/show_bug.cgi?id=49619 |
3413 |
buffer.append( ' '); |
3414 |
buffer.append( ' '); |
3414 |
} |
3415 |
} |
3415 |
for (int i = lineCount; i < newLines-1; i++) { |
3416 |
for (int i = lineCount; i < newLines-1; i++) { |
Lines 3440-3446
Link Here
|
3440 |
StringBuffer buffer = new StringBuffer(); |
3441 |
StringBuffer buffer = new StringBuffer(); |
3441 |
if (this.scanner.linePtr > linePtr) { |
3442 |
if (this.scanner.linePtr > linePtr) { |
3442 |
if (lineCount > 0) { |
3443 |
if (lineCount > 0) { |
3443 |
// TODO (eric) https://bugs.eclipse.org/bugs/show_bug.cgi?id=49619 |
3444 |
// TODO https://bugs.eclipse.org/bugs/show_bug.cgi?id=49619 |
3444 |
buffer.append( ' '); |
3445 |
buffer.append( ' '); |
3445 |
} |
3446 |
} |
3446 |
buffer.append(this.lineSeparator); |
3447 |
buffer.append(this.lineSeparator); |
Lines 3481-3494
Link Here
|
3481 |
|
3482 |
|
3482 |
try { |
3483 |
try { |
3483 |
// Iterate on text line separators |
3484 |
// Iterate on text line separators |
3484 |
int lineNumber = text.lineStart; |
3485 |
int textLineStart = text.lineStart; |
3485 |
this.scanner.tokenizeWhiteSpace = false; |
3486 |
this.scanner.tokenizeWhiteSpace = false; |
3486 |
StringBuffer buffer = null; |
3487 |
StringBuffer buffer = null; |
3487 |
for (int idx=0, max=text.separatorsPtr; idx<=max ; idx++) { |
3488 |
for (int idx=0, max=text.separatorsPtr; idx<=max ; idx++) { |
3488 |
int start = (int) text.separators[idx]; |
3489 |
int start = (int) text.separators[idx]; |
3489 |
int lineStart = Util.getLineNumber(start, this.lineEnds, lineNumber-1, this.maxLines); |
3490 |
int lineStart = Util.getLineNumber(start, this.lineEnds, textLineStart-1, this.maxLines); |
3490 |
while (lineNumber < lineStart) { |
3491 |
while (textLineStart < lineStart) { |
3491 |
int end = this.lineEnds[lineNumber-1]; |
3492 |
int end = this.lineEnds[textLineStart-1]; |
3492 |
this.scanner.resetTo(end, start); |
3493 |
this.scanner.resetTo(end, start); |
3493 |
int token = this.scanner.getNextToken(); |
3494 |
int token = this.scanner.getNextToken(); |
3494 |
switch (token) { |
3495 |
switch (token) { |
Lines 3509-3515
Link Here
|
3509 |
this.column += BLOCK_LINE_PREFIX_LENGTH; |
3510 |
this.column += BLOCK_LINE_PREFIX_LENGTH; |
3510 |
} |
3511 |
} |
3511 |
addReplaceEdit(end+1, this.scanner.getCurrentTokenEndPosition(), buffer.toString()); |
3512 |
addReplaceEdit(end+1, this.scanner.getCurrentTokenEndPosition(), buffer.toString()); |
3512 |
lineNumber++; |
3513 |
textLineStart++; |
3513 |
} |
3514 |
} |
3514 |
} |
3515 |
} |
3515 |
} |
3516 |
} |
Lines 3524-3529
Link Here
|
3524 |
} |
3525 |
} |
3525 |
} |
3526 |
} |
3526 |
|
3527 |
|
|
|
3528 |
/* |
3529 |
* Print the gap lines for an immutable block. |
3530 |
* That's needed to be specific as the formatter needs to keep white spaces |
3531 |
* if possible except those which are indentation ones. |
3532 |
* Note that in the peculiar case of a two lines immutable tag (multi lines block), |
3533 |
* the formatter will join the two lines. |
3534 |
*/ |
3535 |
private void printJavadocGapLinesForImmutableBlock(FormatJavadocBlock block) { |
3536 |
|
3537 |
// Init |
3538 |
int firstLineEnd = -1; // not initialized |
3539 |
int newLineStart = -1; // not initialized |
3540 |
int secondLineStart = -1; // not initialized |
3541 |
int starPosition = -1; // not initialized |
3542 |
int offset = 0; |
3543 |
int start = block.tagEnd + 1; |
3544 |
int end = block.nodes[0].sourceStart-1; |
3545 |
this.scanner.resetTo(start, end); |
3546 |
int lineStart = block.lineStart; |
3547 |
int lineEnd = Util.getLineNumber(block.nodes[0].sourceEnd, this.lineEnds, lineStart-1, this.maxLines); |
3548 |
boolean multiLinesBlock = lineEnd > (lineStart+1); |
3549 |
int previousPosition = this.scanner.currentPosition; |
3550 |
StringBuffer buffer = null; |
3551 |
int indentationColumn = 0; |
3552 |
int leadingSpaces = -1; |
3553 |
|
3554 |
// Scan the existing gap |
3555 |
while (!this.scanner.atEnd()) { |
3556 |
char ch = (char) this.scanner.getNextChar(); |
3557 |
switch (ch) { |
3558 |
case '\t' : |
3559 |
// increase the corresponding counter from the appropriate tab value |
3560 |
if (secondLineStart > 0 || firstLineEnd < 0) { |
3561 |
int reminder = this.tabLength == 0 ? 0 : offset % this.tabLength; |
3562 |
if (reminder == 0) { |
3563 |
offset += this.tabLength; |
3564 |
} else { |
3565 |
offset = ((offset / this.tabLength) + 1) * this.tabLength; |
3566 |
} |
3567 |
} else if (leadingSpaces >= 0) { |
3568 |
int reminder = this.tabLength == 0 ? 0 : offset % this.tabLength; |
3569 |
if (reminder == 0) { |
3570 |
leadingSpaces += this.tabLength; |
3571 |
} else { |
3572 |
leadingSpaces = ((offset / this.tabLength) + 1) * this.tabLength; |
3573 |
} |
3574 |
} |
3575 |
break; |
3576 |
case '\r' : |
3577 |
case '\n' : |
3578 |
// new line, store the end of the first one |
3579 |
if (firstLineEnd < 0) { |
3580 |
firstLineEnd = previousPosition; |
3581 |
} |
3582 |
// print indentation if there were spaces without any star on the line |
3583 |
if (leadingSpaces > 0 && multiLinesBlock) { |
3584 |
if (buffer == null) { |
3585 |
buffer = new StringBuffer(); |
3586 |
this.column = 1; |
3587 |
printIndentationIfNecessary(buffer); |
3588 |
buffer.append(BLOCK_LINE_PREFIX); |
3589 |
this.column += BLOCK_LINE_PREFIX_LENGTH; |
3590 |
indentationColumn = this.column; |
3591 |
} else { |
3592 |
this.column = indentationColumn; |
3593 |
} |
3594 |
addReplaceEdit(newLineStart, newLineStart+indentationColumn-2, buffer.toString()); |
3595 |
} |
3596 |
// store line start and reset positions |
3597 |
newLineStart = this.scanner.currentPosition; |
3598 |
leadingSpaces = 0; |
3599 |
starPosition = -1; |
3600 |
if (multiLinesBlock) { |
3601 |
offset = 0; |
3602 |
secondLineStart = -1; |
3603 |
} |
3604 |
break; |
3605 |
case '*' : |
3606 |
// store line start position if this is the first star of the line |
3607 |
if (starPosition < 0 && firstLineEnd > 0) { |
3608 |
secondLineStart = this.scanner.currentPosition; |
3609 |
starPosition = this.scanner.currentPosition; |
3610 |
leadingSpaces = -1; |
3611 |
} |
3612 |
break; |
3613 |
default : |
3614 |
// increment offset if line has started |
3615 |
if (secondLineStart > 0) { |
3616 |
// skip first white space after the first '*' |
3617 |
if (secondLineStart == starPosition) { |
3618 |
secondLineStart = this.scanner.currentPosition; |
3619 |
} else { |
3620 |
// print indentation before the following characters |
3621 |
if (offset == 0 && multiLinesBlock) { |
3622 |
if (buffer == null) { |
3623 |
buffer = new StringBuffer(); |
3624 |
this.column = 1; |
3625 |
printIndentationIfNecessary(buffer); |
3626 |
buffer.append(BLOCK_LINE_PREFIX); |
3627 |
this.column += BLOCK_LINE_PREFIX_LENGTH; |
3628 |
indentationColumn = this.column; |
3629 |
} else { |
3630 |
this.column = indentationColumn; |
3631 |
} |
3632 |
addReplaceEdit(newLineStart, secondLineStart-1, buffer.toString()); |
3633 |
} |
3634 |
offset++; |
3635 |
} |
3636 |
} else if (firstLineEnd < 0) { |
3637 |
// no new line yet, increment the offset |
3638 |
offset++; |
3639 |
} else if (leadingSpaces >= 0) { |
3640 |
// no star yet, increment the leading spaces |
3641 |
leadingSpaces++; |
3642 |
} |
3643 |
break; |
3644 |
} |
3645 |
previousPosition = this.scanner.currentPosition; |
3646 |
} |
3647 |
|
3648 |
// Increment the columns from the numbers of characters counted on the line |
3649 |
if (multiLinesBlock) { |
3650 |
this.column += offset; |
3651 |
} else { |
3652 |
this.column++; |
3653 |
} |
3654 |
|
3655 |
// Replace the new line with a single space when there's only one separator |
3656 |
// or, if necessary, print the indentation on the last line |
3657 |
if (!multiLinesBlock) { |
3658 |
addReplaceEdit(firstLineEnd, end, " "); //$NON-NLS-1$ |
3659 |
} |
3660 |
else if (secondLineStart > 0) { |
3661 |
if (buffer == null) { |
3662 |
buffer = new StringBuffer(); |
3663 |
this.column = 1; |
3664 |
printIndentationIfNecessary(buffer); |
3665 |
buffer.append(BLOCK_LINE_PREFIX); |
3666 |
this.column += BLOCK_LINE_PREFIX_LENGTH; |
3667 |
indentationColumn = this.column; |
3668 |
} else { |
3669 |
this.column = indentationColumn; |
3670 |
} |
3671 |
addReplaceEdit(newLineStart, secondLineStart-1, buffer.toString()); |
3672 |
} |
3673 |
else if (leadingSpaces > 0) { |
3674 |
if (buffer == null) { |
3675 |
buffer = new StringBuffer(); |
3676 |
this.column = 1; |
3677 |
printIndentationIfNecessary(buffer); |
3678 |
buffer.append(BLOCK_LINE_PREFIX); |
3679 |
this.column += BLOCK_LINE_PREFIX_LENGTH; |
3680 |
indentationColumn = this.column; |
3681 |
} else { |
3682 |
this.column = indentationColumn; |
3683 |
} |
3684 |
addReplaceEdit(newLineStart, newLineStart+indentationColumn-2, buffer.toString()); |
3685 |
} |
3686 |
|
3687 |
// Reset |
3688 |
this.needSpace = false; |
3689 |
this.scanner.resetTo(end+1, this.scannerEndPosition - 1); |
3690 |
} |
3691 |
|
3527 |
private int printJavadocHtmlTag(FormatJavadocText text, FormatJavadocBlock block, boolean textOnNewLine) { |
3692 |
private int printJavadocHtmlTag(FormatJavadocText text, FormatJavadocBlock block, boolean textOnNewLine) { |
3528 |
|
3693 |
|
3529 |
// Compute indentation if necessary |
3694 |
// Compute indentation if necessary |