private void print(int length, boolean considerSpaceIfAny) { if (this.checkLineWrapping && length + this.column > this.pageWidth) { handleLineTooLong(); } this.lastNumberOfNewLines = 0; if (this.indentationLevel != 0) { printIndentationIfNecessary(); } if (considerSpaceIfAny) { space(); } if (this.pendingSpace) { addInsertEdit(this.scanner.getCurrentTokenStartPosition(), " "); //$NON-NLS-1$ } this.pendingSpace = false; this.column += length; this.needSpace = true; }
public final void addReplaceEdit(int start, int end, String replacement) { if (this.edits.length == this.editsIndex) { // resize resize(); } addOptimizedReplaceEdit(start, end - start + 1, replacement); }
public static class MultiFieldDeclaration extends FieldDeclaration { FieldDeclaration[] declarations; MultiFieldDeclaration(FieldDeclaration[] declarations){ this.declarations = declarations; this.modifiers = declarations[0].modifiers; } }
private void printEmptyLines(int linesNumber, int insertPosition) { final String buffer = getEmptyLines(linesNumber); if (Util.EMPTY_STRING == buffer) return; addInsertEdit(insertPosition, buffer); }
int currentCommentIndentation = getCurrentIndentation(whiteSpaces, 0); int relativeIndentation = currentCommentIndentation - this.lastLineComment.currentIndentation; if (this.tabLength == 0) { if (lines >= 1) { currentTokenStartPosition = tokenStartPosition; preserveEmptyLines(lines, currentTokenStartPosition); addDeleteEdit(currentTokenStartPosition, whitespacesEndPosition); this.scanner.resetTo(this.scanner.currentPosition, this.scannerEndPosition - 1); return; this.printNewLine(tokenStartPosition); addDeleteEdit(tokenStartPosition, whitespacesEndPosition); } else { if (lines == 0) { hasWhitespaces = true; if (hasLineComment && emptyLinesRules != PRESERVE_EMPTY_LINES_KEEP_LAST_NEW_LINES_INDENTATION) { addReplaceEdit(tokenStartPosition, whitespacesEndPosition, getPreserveEmptyLines(0, emptyLinesRules)); } else { addDeleteEdit(tokenStartPosition, whitespacesEndPosition); useAlignmentBreakIndentation(emptyLinesRules); currentTokenStartPosition = tokenStartPosition; preserveEmptyLines(lines, currentTokenStartPosition); addDeleteEdit(currentTokenStartPosition, whitespacesEndPosition); } else if (hasComment) { useAlignmentBreakIndentation(emptyLinesRules); if (lines == 1) {
setCommentIndentation(commentIndentationLevel); printJavadocBlockReference(block, reference); previousEnd = reference.sourceEnd; newLines = printJavadocBlockNodesNewLines(block, node, previousEnd); printJavadocGapLinesForImmutableBlock(block); } else { printJavadocGapLines(previousEnd+1, nodeStart-1, newLines, clearBlankLines, false, null); if (newLines > 0) { for (int j=0; j<newLines; j++) { printJavadocNewLine(this.tempBuffer); addInsertEdit(nodeStart, this.tempBuffer.toString()); if (newLines < node.linesBefore) newLines = node.linesBefore; if (newLines == 0) { newLines = printJavadocBlockNodesNewLines(block, node, previousEnd); printJavadocGapLines(previousEnd+1, nodeStart-1, newLines, clearBlankLines, false, null); addInsertEdit(node.sourceStart, this.commentIndentation); this.column += this.commentIndentation.length(); printJavadocImmutableText(text, block, newLines > 0); this.column += getTextLength(block, text); } else if (text.isHtmlTag()) {
addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition()); } else if (hasLineComment) { preserveEmptyLines(count, this.scanner.getCurrentTokenStartPosition()); addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition()); } else if (hasComment) { if (count == 1) { this.printNewLine(this.scanner.getCurrentTokenStartPosition()); } else { preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition()); addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition()); } else { addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition()); if (count >= 1) { if (count > 1) { preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition()); } else if (count == 1) { printNewLine(this.scanner.getCurrentTokenStartPosition()); space(); printLineComment(); currentTokenStartPosition = this.scanner.currentPosition; hasLineComment = true; if (count >= 1) { if (count > 1) { preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
private void printLineComment() { int currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition(); int currentTokenEndPosition = this.scanner.getCurrentTokenEndPosition() + 1; boolean includesLineComments = includesLineComments(); boolean isNlsTag = false; if (CharOperation.indexOf(Scanner.TAG_PREFIX, this.scanner.source, true, currentTokenStartPosition, currentTokenEndPosition) != -1) { boolean onFirstColumn = isOnFirstColumn(start); if (this.indentationLevel == 0) { commentIndentationLevel = this.column - 1; int currentCommentIndentation = getCurrentIndentation(this.lastLineComment.leadingSpaces, 0); int currentIndentationLevel = this.indentationLevel; this.indentationLevel = this.lastLineComment.indentation ; printIndentationIfNecessary(); this.indentationLevel = currentIndentationLevel; commentIndentationLevel = this.lastLineComment.indentation ; } else { printIndentationIfNecessary(); commentIndentationLevel = this.column - 1; printIndentationIfNecessary(); this.indentationLevel = currentIndentationLevel; commentIndentationLevel = this.currentAlignment.breakIndentationLevel; } else { printIndentationIfNecessary(); commentIndentationLevel = this.column - 1; this.lastLineComment.currentIndentation = getCurrentCommentIndentation(currentTokenStartPosition);
case TerminalTokens.TokenNamestrictfp : hasModifiers = true; print(this.scanner.currentPosition - this.scanner.startPosition, !isFirstModifier); isFirstModifier = false; currentTokenStartPosition = this.scanner.currentPosition; hasModifiers = true; if (!isFirstModifier) { space(); this.printNewLine(); boolean turnOff = false; if (foundTaskCount > 0) { setEditsEnabled(foundTaskCount); turnOff = true; } else if (this.tagsKind == this.currentToken OptimizedReplaceEdit currentEdit = this.edits[this.editsIndex-1]; if (this.scanner.startPosition == currentEdit.offset+currentEdit.length) { printNewLinesBeforeDisablingComment(); printBlockComment(this.currentToken == TerminalTokens.TokenNameCOMMENT_JAVADOC); if (this.useTags && !this.editsEnabled) { if (foundTaskCount > 0) { setEditsEnabled(foundTaskCount); } else if (this.tagsKind == this.currentToken) { this.editsEnabled = CharOperation.equals(this.enablingTag, this.scanner.source, tokenStartPosition, tokenEndPosition+1);
private void printBlockComment(boolean isJavadoc) { int currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition(); int currentTokenEndPosition = this.scanner.getCurrentTokenEndPosition() + 1; boolean includesBlockComments = !isJavadoc && includesBlockComments(); int nextCharacterStart = currentTokenStartPosition; int previousStart = currentTokenStartPosition; boolean onFirstColumn = isOnFirstColumn(start); || !onFirstColumn) { indentComment = true; printIndentationIfNecessary(); addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$ if (printBlockComment(currentTokenStartPosition, currentTokenEndPosition)) { return; int currentCommentIndentation = onFirstColumn ? 0 : getCurrentCommentIndentation(start); boolean formatComment = (isJavadoc && (this.formatComments & CodeFormatter.K_JAVA_DOC) != 0) || (!isJavadoc && (this.formatComments & CodeFormatter.K_MULTI_LINE_COMMENT) != 0); this.tempBuffer.append(this.lineSeparator); if (this.indentationLevel > 0) { printIndentationIfNecessary(this.tempBuffer); addReplaceEdit(start, previousStart - 1, replacement); } else { this.column += (nextCharacterStart - previousStart);
try { do { printComment(CodeFormatter.K_UNKNOWN, NO_TRAILING_COMMENT); switch(this.currentToken = this.scanner.getNextToken()) { case TerminalTokens.TokenNameEOF : return; case TerminalTokens.TokenNameWHITESPACE : addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition()); currentTokenStartPosition = this.scanner.currentPosition; break; case TerminalTokens.TokenNameCOMMENT_BLOCK : case TerminalTokens.TokenNameCOMMENT_JAVADOC : printBlockComment(false); currentTokenStartPosition = this.scanner.currentPosition; break; case TerminalTokens.TokenNameCOMMENT_LINE : printLineComment(); currentTokenStartPosition = this.scanner.currentPosition; break; case TerminalTokens.TokenNameIdentifier : case TerminalTokens.TokenNameDOT : print(this.scanner.currentPosition - this.scanner.startPosition, false); currentTokenStartPosition = this.scanner.currentPosition; break;
addReplaceEdit(textStartPosition, textEndPosition, " "); //$NON-NLS-1$ } else { output.append(' '); this.javadocGapLinesBuffer.append(this.lineSeparator); this.column = 1; printIndentationIfNecessary(this.javadocGapLinesBuffer); if (footer) { this.javadocGapLinesBuffer.append(' '); addInsertEdit(textStartPosition, this.javadocGapLinesBuffer.toString()); } else { output.append(this.javadocGapLinesBuffer); addReplaceEdit(start, textEndPosition, this.javadocGapLinesBuffer.toString()); } else { output.append(this.javadocGapLinesBuffer); printIndentationIfNecessary(this.javadocGapLinesBuffer); if (i == (linesGap-1)) { this.javadocGapLinesBuffer.append(' '); int tokenLength = this.scanner.currentPosition - currentTokenStartPosition; if (output == null) { addReplaceEdit(start, currentTokenStartPosition-1, this.javadocGapLinesBuffer.toString()); } else { output.append(this.javadocGapLinesBuffer); if (textEndPosition >= currentTokenStartPosition) {
int indentLevel = this.indentationLevel; int indentations = this.numberOfIndentations; this.indentationLevel = getNextIndentationLevel(firstColumn); if (this.indentationSize != 0) { this.numberOfIndentations = this.indentationLevel / this.indentationSize; token = this.scanner.getNextToken(); } catch (InvalidInputException iie) { token = consumeInvalidToken(commentEnd); addReplaceEdit(spaceStartPosition, startPosition-1, " "); //$NON-NLS-1$ addInsertEdit(this.scanner.startPosition, " "); //$NON-NLS-1$ this.column++; addReplaceEdit(spaceStartPosition, this.scanner.startPosition-1, " "); //$NON-NLS-1$ this.column++; spaceStartPosition = -1; // do not use this position to split the comment this.column = 1; if (!this.formatter.preferences.never_indent_line_comments_on_first_column) { printIndentationIfNecessary(this.tempBuffer); addReplaceEdit(spaceStartPosition, spaceEndPosition, newLineString); spaceStartPosition = -1; if (insertSpace) { addReplaceEdit(spaceStartPosition, this.scanner.startPosition-1, " "); //$NON-NLS-1$
printIndentationIfNecessary(); addInsertEdit(start, " "); //$NON-NLS-1$ int currentLine = this.line; int firstBlockStart = previousBlock.sourceStart; printIndentationIfNecessary(null); this.column += JAVADOC_HEADER_LENGTH; // consider that the header is already scanned printJavadocBlock(previousBlock); FormatJavadocBlock block = javadoc.blocks[index++]; int newLines = this.formatter.preferences.comment_insert_empty_line_before_root_tags ? 2 : 1; printJavadocGapLines(previousBlock.sourceEnd+1, block.sourceStart-1, newLines, this.formatter.preferences.comment_clear_blank_lines_in_javadoc_comment, false, null); previousBlock = block; printJavadocBlock(previousBlock); FormatJavadocBlock block = javadoc.blocks[index++]; printJavadocGapLines(previousBlock.sourceEnd+1, block.sourceStart-1, 1, this.formatter.preferences.comment_clear_blank_lines_in_javadoc_comment, false, null); previousBlock = block; printJavadocBlock(previousBlock); printJavadocGapLines(javadoc.textStart, firstBlockStart-1, newLines, this.formatter.preferences.comment_clear_blank_lines_in_javadoc_comment, false, null); printJavadocGapLines(previousBlock.sourceEnd+1, javadoc.textEnd, newLines, this.formatter.preferences.comment_clear_blank_lines_in_javadoc_comment, true, null);
this.column = 1; this.tempBuffer.setLength(0); printIndentationIfNecessary(this.tempBuffer); this.tempBuffer.append(BLOCK_LINE_PREFIX); this.column += BLOCK_LINE_PREFIX_LENGTH; addReplaceEdit(newLineStart, newLineStart+indentationColumn-2, newLineString); this.tempBuffer.setLength(0); this.column = 1; printIndentationIfNecessary(this.tempBuffer); this.tempBuffer.append(BLOCK_LINE_PREFIX); this.column += BLOCK_LINE_PREFIX_LENGTH; addReplaceEdit(newLineStart, secondLineStart-1, newLineString); addReplaceEdit(firstLineEnd, end, " "); //$NON-NLS-1$ this.tempBuffer.setLength(0); this.column = 1; printIndentationIfNecessary(this.tempBuffer); this.tempBuffer.append(BLOCK_LINE_PREFIX); this.column += BLOCK_LINE_PREFIX_LENGTH; addReplaceEdit(newLineStart, secondLineStart-1, newLineString); this.tempBuffer.setLength(0); this.column = 1; printIndentationIfNecessary(this.tempBuffer);
/** * @see org.eclipse.jdt.internal.compiler.ASTVisitor#visit(org.eclipse.jdt.internal.compiler.ast.StringLiteral, org.eclipse.jdt.internal.compiler.lookup.BlockScope) */ public boolean visit(StringLiteral stringLiteral, BlockScope scope) { final int numberOfParens = (stringLiteral.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT; if (numberOfParens > 0) { manageOpeningParenthesizedExpression(stringLiteral, numberOfParens); } this.scribe.checkNLSTag(stringLiteral.sourceStart); this.scribe.printNextToken(TerminalTokens.TokenNameStringLiteral); this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT); if (numberOfParens > 0) { manageClosingParenthesizedExpression(stringLiteral, numberOfParens); } return false; }
public TextEdit getRootEdit() { adaptRegions(); adaptEdits(); OptimizedReplaceEdit currentEdit = this.edits[i]; if (currentEdit.offset >= 0 && currentEdit.offset <= this.scannerEndPosition) { if (currentEdit.length == 0 || (currentEdit.offset != this.scannerEndPosition && isMeaningfulEdit(currentEdit))) { try { edit.addChild(new ReplaceEdit(currentEdit.offset, currentEdit.length, currentEdit.replacement));
printJavadocTextLine(this.javadocTextBuffer, nextStart, end, block, idx==0 || (!joinLines && textOnNewLine)/*first text?*/, needIndentation, false /*not an html tag*/); textOnNewLine = false; if (joinLines) gapLine++; // if not preserving line break then gap must be at least of one line if (startLine > gapLine) { addReplaceEdit(textStart, end, this.javadocTextBuffer.toString()); textStart = nextStart; this.javadocTextBuffer.setLength(0); int newLines = startLine - endLine; if (clearBlankLines) newLines = 1; printJavadocGapLines(end+1, nextStart-1, newLines, this.formatter.preferences.comment_clear_blank_lines_in_javadoc_comment, false, null); textOnNewLine = true; printJavadocTextLine(this.javadocTextBuffer, nextStart, text.sourceEnd, block, text.separatorsPtr==-1 /* first text?*/, needIndentation, false /*not an html tag*/); addReplaceEdit(textStart, text.sourceEnd, this.javadocTextBuffer.toString());
void printIndentationIfNecessary() { this.tempBuffer.setLength(0); printIndentationIfNecessary(this.tempBuffer); if (this.tempBuffer.length() > 0) { addInsertEdit(this.scanner.getCurrentTokenStartPosition(), this.tempBuffer.toString()); this.pendingSpace = false; } }
public CodeFormatterVisitor(DefaultCodeFormatterOptions preferences, Map settings, IRegion[] regions, CodeSnippetParsingUtil codeSnippetParsingUtil, boolean includeComments) { long sourceLevel = settings == null ? ClassFileConstants.JDK1_3 : CompilerOptions.versionToJdkLevel(settings.get(JavaCore.COMPILER_SOURCE)); this.localScanner = new Scanner(true, false, false/*nls*/, sourceLevel/*sourceLevel*/, null/*taskTags*/, null/*taskPriorities*/, true/*taskCaseSensitive*/); this.preferences = preferences; this.scribe = new Scribe(this, sourceLevel, regions, codeSnippetParsingUtil, includeComments); }