@Override public boolean preVisit2(ASTNode node) { this.currentDepth++; assert this.wrapIndexes.isEmpty() && this.secondaryWrapIndexes.isEmpty() && this.wrapPenalties.isEmpty(); assert this.wrapParentIndex == -1 && this.wrapGroupEnd == -1; boolean isMalformed = (node.getFlags() & ASTNode.MALFORMED) != 0; if (isMalformed) { this.tm.addDisableFormatTokenPair(this.tm.firstTokenIn(node, -1), this.tm.lastTokenIn(node, -1)); } return !isMalformed; }
private int findCommentLineIndent(int commentFragmentIndex) { int position = this.ctm.get(commentFragmentIndex).originalStart; int lastNonWhitespace = position; while (--position > 0) { char c = this.ctm.charAt(position); if (c == '\r' || c == '\n') break; if (!ScannerHelper.isWhitespace(c)) lastNonWhitespace = position; } if (lastNonWhitespace > 0 && this.ctm.charAt(lastNonWhitespace - 1) == ' ') lastNonWhitespace--; return this.ctm.getLength(position, lastNonWhitespace - 1, 0); }
private boolean isNewGroup(ASTNode node, ASTNode previousNode) { if (previousNode == null) return true; int lineBreaks = 0; int from = this.tm.lastIndexIn(previousNode, -1); int to = this.tm.firstIndexIn(node, -1); Token previousToken = this.tm.get(from); for (int i = from + 1; i <= to; i++) { Token token = this.tm.get(i); lineBreaks += Math.min(this.tm.countLineBreaksBetween(previousToken, token), this.options.number_of_empty_lines_to_preserve + 1); previousToken = token; } return lineBreaks > this.options.align_fields_grouping_blank_lines; }
public void finishUp() { if (this.lastFormatOffComment != null) this.tm.addDisableFormatTokenPair(this.lastFormatOffComment, this.tm.get(this.tm.size() - 1)); } }
public void handleLineComment(int commentIndex) { Token commentToken = this.tm.get(commentIndex); int positionInLine = this.tm.findSourcePositionInLine(commentToken.originalStart); boolean isContinuation = commentIndex > 0 && this.tm.get(commentIndex - 1) == this.lastLineComment && (positionInLine >= this.lastLineCommentPosition - this.options.indentation_size + 1) && this.tm.countLineBreaksBetween(this.lastLineComment, commentToken) == 1; boolean isHeader = this.tm.isInHeader(commentIndex); boolean formattingEnabled = (this.options.comment_format_line_comment && !isHeader) || (this.options.comment_format_header && isHeader); WrapPolicy policy = this.lastLineComment.getWrapPolicy(); if (policy == null) { int lineStart = this.tm.getPositionInLine(this.tm.findFirstTokenInLine(commentIndex - 1)); int commentStart = this.tm.getPositionInLine(commentIndex - 1); policy = new WrapPolicy(WrapMode.WHERE_NECESSARY, commentIndex - 1, commentStart - lineStart); this.tm.remove(commentIndex - 1); this.tm.insert(commentIndex - 1, merged); this.tm.remove(commentIndex); List<Token> lastStructure = this.lastLineComment.getInternalStructure(); lastStructure.addAll(structure);
@Override public boolean visit(Javadoc node) { this.noFormatTagOpenStart = -1; this.formatCodeTagOpenEnd = -1; this.lastFormatCodeClosingTagIndex = -1; this.firstTagToken = null; this.ctm = null; int commentIndex = this.tm.firstIndexIn(node, TokenNameCOMMENT_JAVADOC); Token commentToken = this.tm.get(commentIndex); if (node.getParent() == null) { // not a proper javadoc, treat as block comment handleWhitespaceAround(commentIndex); } if (commentIndex < this.tm.size() - 1) commentToken.breakAfter(); if (handleFormatOnOffTags(commentToken)) return false; boolean isHeader = this.tm.isInHeader(commentIndex); boolean formattingEnabled = (this.options.comment_format_javadoc_comment && !isHeader) || (this.options.comment_format_header && isHeader); if (!formattingEnabled || !tokenizeMultilineComment(commentToken)) { commentToken.setInternalStructure(commentToLines(commentToken, -1)); return false; } this.commentStructure = commentToken.getInternalStructure(); this.commentIndent = this.tm.toIndent(commentToken.getIndent(), true); this.ctm = new TokenManager(commentToken.getInternalStructure(), this.tm); return true; }
int maxCommentAlign = 0; for (FieldDeclaration declaration : alignGroup) { int typeIndex = this.tm.firstIndexIn(declaration.getType(), -1); int firstIndexInLine = this.tm.findFirstTokenInLine(typeIndex); int lastIndex = this.tm.lastIndexIn(declaration, -1) + 1; maxCommentAlign = Math.max(maxCommentAlign, positionCounter.findMaxPosition(firstIndexInLine, lastIndex)); maxCommentAlign = this.tm.toIndent(maxCommentAlign, false); int typeIndex = this.tm.firstIndexIn(declaration.getType(), -1); int firstIndexInLine = this.tm.findFirstTokenInLine(typeIndex); int lastIndex = this.tm.lastIndexIn(declaration, -1); lastIndex = Math.min(lastIndex, this.tm.size() - 2); for (int i = firstIndexInLine; i <= lastIndex; i++) { Token token = this.tm.get(i); Token next = this.tm.get(i + 1); boolean lineBreak = token.getLineBreaksAfter() > 0 || next.getLineBreaksBefore() > 0; if (lineBreak) { token.setAlign(maxCommentAlign); } else { this.tm.addNLSAlignIndex(i, maxCommentAlign);
public void handleBlockComment(int commentIndex) { Token commentToken = this.tm.get(commentIndex); boolean isFirstColumn = handleWhitespaceAround(commentIndex); if (handleFormatOnOffTags(commentToken)) return; boolean isHeader = this.tm.isInHeader(commentIndex); boolean formattingEnabled = (this.options.comment_format_block_comment && !isHeader) || (this.options.comment_format_header && isHeader); formattingEnabled = formattingEnabled && this.tm.charAt(commentToken.originalStart + 2) != '-'; if (formattingEnabled && tokenizeMultilineComment(commentToken)) { this.commentStructure = commentToken.getInternalStructure(); this.ctm = new TokenManager(this.commentStructure, this.tm); handleStringLiterals(this.tm.toString(commentToken), commentToken.originalStart); addSubstituteWraps(); } else { commentToken.setInternalStructure(commentToLines(commentToken, -1)); } if (this.options.never_indent_block_comments_on_first_column && isFirstColumn) { commentToken.setIndent(0); commentToken.setWrapPolicy(null); } }
public int getPositionInLine(int tokenIndex) { Token token = get(tokenIndex); // find the first token in line and calculate position of given token int firstTokenIndex = token.getLineBreaksBefore() > 0 ? tokenIndex : findFirstTokenInLine(tokenIndex); Token firstToken = get(firstTokenIndex); int startingPosition = toIndent(firstToken.getIndent(), firstToken.getWrapPolicy() != null); this.positionInLineCounter.value = tokenIndex; this.positionInLineCounter.counter = startingPosition; traverse(firstTokenIndex, this.positionInLineCounter); return Math.max(this.positionInLineCounter.counter, token.getAlign()); }
private void adjustEmptyLineAfter(int tokenIndex, int indentationAdjustment) { if (tokenIndex + 1 >= this.tm.size()) return; Token token = this.tm.get(tokenIndex); Token next = this.tm.get(tokenIndex + 1); if (this.tm.countLineBreaksBetween(token, next) < 2 || !this.options.indent_empty_lines) return; next.setEmptyLineIndentAdjustment(indentationAdjustment * this.options.indentation_size); }
@Override public boolean visit(ClassInstanceCreation node) { int lParen = this.tm.firstIndexAfter(node.getType(), TokenNameLPAREN); int rParen = node.getAnonymousClassDeclaration() == null ? this.tm.lastIndexIn(node, TokenNameRPAREN) : this.tm.firstIndexBefore(node.getAnonymousClassDeclaration(), TokenNameRPAREN); handleParenthesesPositions(lParen, rParen, this.options.parenthesis_positions_in_method_invocation); return true; }
@Override public boolean visit(EnumConstantDeclaration node) { this.declarationModifierVisited = false; int lParen = this.tm.firstIndexAfter(node.getName(), -1); while (this.tm.get(lParen).isComment()) lParen++; if (this.tm.get(lParen).tokenType == TokenNameLPAREN) { int rParen = node.getAnonymousClassDeclaration() == null ? this.tm.lastIndexIn(node, TokenNameRPAREN) : this.tm.firstIndexBefore(node.getAnonymousClassDeclaration(), TokenNameRPAREN); handleParenthesesPositions(lParen, rParen, this.options.parenthesis_positions_in_enum_constant_declaration); } return true; }
for (ASTNode node : alignGroup) { int firstIndexInLine = findFirstTokenInLine(node); int lastIndex = this.tm.lastIndexIn(node, -1) + 1; maxCommentAlign = Math.max(maxCommentAlign, positionCounter.findMaxPosition(firstIndexInLine, lastIndex)); int lastIndex = this.tm.lastIndexIn(node, -1); lastIndex = Math.min(lastIndex, this.tm.size() - 2); for (int i = firstIndexInLine; i <= lastIndex; i++) { Token token = this.tm.get(i); Token next = this.tm.get(i + 1); boolean lineBreak = token.getLineBreaksAfter() > 0 || next.getLineBreaksBefore() > 0; if (lineBreak) { token.setAlign(maxCommentAlign); } else if (alignLineComments) { this.tm.addNLSAlignIndex(i, maxCommentAlign);
boolean checkForceWrap(Token token, int index, int currentIndent) { // A token that will have smaller indent when wrapped than the current line indent, // should be wrapped because it's a low depth token following some complex wraps of higher depth. // This rule could not be implemented in getWrapPenalty() because a token's wrap indent may depend // on wraps in previous lines, which are not determined yet when the token's penalty is calculated. if (!token.isWrappable() || !this.options.wrap_outer_expressions_when_nested || getWrapIndent(token) >= currentIndent) return false; WrapPolicy lineStartPolicy = this.tm.get(this.tm.findFirstTokenInLine(index, false, true)).getWrapPolicy(); return lineStartPolicy != null && lineStartPolicy.wrapMode != WrapMode.BLOCK_INDENT; }
private void tryKeepOnOneLine(ASTNode node, ASTNode nodeBeforeOpenBrace, List<? extends ASTNode> items, String oneLineOption) { int openBraceIndex = nodeBeforeOpenBrace == null ? this.tm.firstIndexIn(node, TokenNameLBRACE) : this.tm.firstIndexAfter(nodeBeforeOpenBrace, TokenNameLBRACE); int closeBraceIndex = this.tm.lastIndexIn(node, TokenNameRBRACE); tryKeepOnOneLine(openBraceIndex, closeBraceIndex, closeBraceIndex, items, oneLineOption); }
private void handleOperator(String operator, ASTNode nodeAfter, boolean spaceBefore, boolean spaceAfter) { if (spaceBefore || spaceAfter) { int i = this.tm.firstIndexBefore(nodeAfter, -1); while (!operator.equals(this.tm.toString(i))) { i--; } handleToken(this.tm.get(i), spaceBefore, spaceAfter); } }
public int firstIndexIn(ASTNode node, int tokenType) { int index = findIndex(node.getStartPosition(), tokenType, true); assert tokenInside(node, index); return index; }
@Override public boolean visit(CatchClause node) { if (this.options.insert_new_line_before_catch_in_try_statement) breakLineBefore(node); int lParen = this.tm.firstIndexIn(node, TokenNameLPAREN); int rParen = this.tm.firstIndexBefore(node.getBody(), TokenNameRPAREN); handleParenthesesPositions(lParen, rParen, this.options.parenthesis_positions_in_catch_clause); return true; }