private int getStartingPosition(Token token) { int position = this.lineStartPosition + token.getAlign() + token.getIndent(); if (token.tokenType != TokenNameNotAToken) position += COMMENT_LINE_SEPARATOR_LENGTH; return position; }
private int getStartingPosition(Token token) { int position = this.lineStartPosition + token.getAlign() + token.getIndent(); if (token.tokenType != TokenNameNotAToken) position += COMMENT_LINE_SEPARATOR_LENGTH; return position; }
@Override protected boolean token(Token token, int index) { if (index == this.stoppingIndex) return false; if (getLineBreaksBefore() > 0) this.counter = Aligner.this.tm.getPositionInLine(index); if (token.getAlign() > 0) this.counter = token.getAlign(); this.counter += Aligner.this.tm.getLength(token, this.counter); if (isSpaceAfter() && getLineBreaksAfter() == 0) this.counter++; this.maxPosition = Math.max(this.maxPosition, this.counter); return true; }
@Override protected boolean token(Token token, int index) { if (index == this.stoppingIndex) return false; if (getLineBreaksBefore() > 0) this.counter = FieldAligner.this.tm.getPositionInLine(index); if (token.getAlign() > 0) this.counter = token.getAlign(); this.counter += FieldAligner.this.tm.getLength(token, this.counter); if (isSpaceAfter() && getLineBreaksAfter() == 0) this.counter++; this.maxPosition = Math.max(this.maxPosition, this.counter); return true; }
@Override protected boolean token(Token token, int index) { if (index == this.stoppingIndex) return false; if (getLineBreaksBefore() > 0) this.counter = FieldAligner.this.tm.getPositionInLine(index); if (token.getAlign() > 0) this.counter = token.getAlign(); this.counter += FieldAligner.this.tm.getLength(token, this.counter); if (isSpaceAfter() && getLineBreaksAfter() == 0) this.counter++; this.maxPosition = Math.max(this.maxPosition, this.counter); return true; }
@Override protected boolean token(Token traversed, int index) { if (index == this.value) { this.isNLSTagInLine = false; return false; } if (traversed.hasNLSTag()) { assert traversed.tokenType == TokenNameStringLiteral; this.isNLSTagInLine = true; } if (traversed.getAlign() > 0) this.counter = traversed.getAlign(); List<Token> internalStructure = traversed.getInternalStructure(); if (internalStructure != null && !internalStructure.isEmpty()) { assert traversed.tokenType == TokenNameCOMMENT_BLOCK || traversed.tokenType == TokenNameCOMMENT_JAVADOC; this.counter = TokenManager.this.commentWrapper.wrapMultiLineComment(traversed, this.counter, true, this.isNLSTagInLine); } else { this.counter += getLength(traversed, this.counter); } if (isSpaceAfter()) this.counter++; return true; } };
@Override protected boolean token(Token traversed, int index) { if (index == this.value) { this.isNLSTagInLine = false; return false; } if (traversed.hasNLSTag()) { assert traversed.tokenType == TokenNameStringLiteral; this.isNLSTagInLine = true; } if (traversed.getAlign() > 0) this.counter = traversed.getAlign(); List<Token> internalStructure = traversed.getInternalStructure(); if (internalStructure != null && !internalStructure.isEmpty()) { assert traversed.tokenType == TokenNameCOMMENT_BLOCK || traversed.tokenType == TokenNameCOMMENT_JAVADOC; this.counter = TokenManager.this.commentWrapper.wrapMultiLineComment(traversed, this.counter, true, this.isNLSTagInLine); } else { this.counter += getLength(traversed, this.counter); } if (isSpaceAfter()) this.counter++; return true; } };
@Override protected boolean token(Token traversed, int index) { if (index == this.value) { this.isNLSTagInLine = false; return false; } if (traversed.hasNLSTag()) { assert traversed.tokenType == TokenNameStringLiteral; this.isNLSTagInLine = true; } if (traversed.getAlign() > 0) this.counter = traversed.getAlign(); List<Token> internalStructure = traversed.getInternalStructure(); if (internalStructure != null && !internalStructure.isEmpty()) { assert traversed.tokenType == TokenNameCOMMENT_BLOCK || traversed.tokenType == TokenNameCOMMENT_JAVADOC; this.counter = TokenManager.this.commentWrapper.wrapMultiLineComment(traversed, this.counter, true, this.isNLSTagInLine); } else { this.counter += getLength(traversed, this.counter); } if (isSpaceAfter()) this.counter++; return true; } };
private void alignAssignmentStatements(List<Statement> statements) { List<List<ExpressionStatement>> assignmentGroups = toAlignGroups(statements, n -> optionalCast(n, ExpressionStatement.class) .filter(es -> es.getExpression() instanceof Assignment)); this.alignGroups.addAll(assignmentGroups); AlignIndexFinder<ExpressionStatement> assignFinder = es -> { Assignment a = (Assignment) es.getExpression(); int operatorIndex = this.tm.firstIndexBefore(a.getRightHandSide(), -1); while (this.tm.get(operatorIndex).isComment()) operatorIndex--; return Optional.of(operatorIndex); }; assignmentGroups.forEach(ag -> alignNodes(ag, assignFinder)); if (this.options.align_with_spaces || this.options.tab_char != DefaultCodeFormatterOptions.TAB) { // align assign operators on their right side (e.g. +=, >>=) for (List<ExpressionStatement> group : assignmentGroups) { List<Token> assignTokens = group.stream() .map(assignFinder::findIndex) .filter(Optional::isPresent) .map(o -> this.tm.get(o.get())) .collect(toList()); int maxWidth = assignTokens.stream().mapToInt(Token::countChars).max().orElse(0); for (Token token : assignTokens) token.setAlign(token.getAlign() + maxWidth - token.countChars()); } } }
private void bufferAlign(Token token, int index) { int align = token.getAlign(); if (align == 0) return;
public int getPositionInLine(int tokenIndex) { Token token = get(tokenIndex); if (token.getAlign() > 0) return get(tokenIndex).getAlign(); // 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); if (firstTokenIndex == tokenIndex) return startingPosition; this.positionInLineCounter.value = tokenIndex; this.positionInLineCounter.counter = startingPosition; traverse(firstTokenIndex, this.positionInLineCounter); return this.positionInLineCounter.counter; }
public int getPositionInLine(int tokenIndex) { Token token = get(tokenIndex); if (token.getAlign() > 0) return get(tokenIndex).getAlign(); // 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); if (firstTokenIndex == tokenIndex) return startingPosition; this.positionInLineCounter.value = tokenIndex; this.positionInLineCounter.counter = startingPosition; traverse(firstTokenIndex, this.positionInLineCounter); return this.positionInLineCounter.counter; }
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 boolean bufferAlign(Token token, int index) { int align = token.getAlign(); int alignmentChar = this.alignChar; if (align == 0 && getLineBreaksBefore() == 0 && this.parent != null) {
} else { bufferAlign(token, index); if (isSpaceBefore() && token.getAlign() == 0) this.buffer.append(' ');
} else { bufferAlign(token, index); if (isSpaceBefore() && token.getAlign() == 0) this.buffer.append(' ');
/** * Makes sure all new lines within given node will have wrap policy so that * wrap executor will fix their indentation if necessary. */ private void forceContinuousWrapping(ASTNode node, int parentIndex) { int parentIndent = this.tm.get(parentIndex).getIndent(); int indentChange = -parentIndent; int lineStart = this.tm.findFirstTokenInLine(parentIndex); for (int i = parentIndex; i >= lineStart; i--) { int align = this.tm.get(i).getAlign(); if (align > 0) { indentChange = -2 * parentIndent + align; break; } } Token previous = null; int from = this.tm.firstIndexIn(node, -1); int to = this.tm.lastIndexIn(node, -1); for (int i = from; i <= to; i++) { Token token = this.tm.get(i); if ((token.getLineBreaksBefore() > 0 || (previous != null && previous.getLineBreaksAfter() > 0)) && (token.getWrapPolicy() == null || token.getWrapPolicy().wrapMode == WrapMode.FORCED)) { int extraIndent = token.getIndent() + indentChange; token.setWrapPolicy(new WrapPolicy(WrapMode.FORCED, parentIndex, extraIndent)); token.setIndent(parentIndent + extraIndent); } previous = token; } }
/** * Makes sure all new lines within given node will have wrap policy so that * wrap executor will fix their indentation if necessary. */ void forceContinuousWrapping(ASTNode node, int parentIndex) { int parentIndent = this.tm.get(parentIndex).getIndent(); int indentChange = -parentIndent; int lineStart = this.tm.findFirstTokenInLine(parentIndex); for (int i = parentIndex; i >= lineStart; i--) { int align = this.tm.get(i).getAlign(); if (align > 0) { indentChange = -2 * parentIndent + align; break; } } Token previous = null; int from = this.tm.firstIndexIn(node, -1); int to = this.tm.lastIndexIn(node, -1); for (int i = from; i <= to; i++) { Token token = this.tm.get(i); if ((token.getLineBreaksBefore() > 0 || (previous != null && previous.getLineBreaksAfter() > 0)) && (token.getWrapPolicy() == null || token.getWrapPolicy().wrapMode == WrapMode.BLOCK_INDENT)) { int extraIndent = token.getIndent() + indentChange; token.setWrapPolicy(new WrapPolicy(WrapMode.BLOCK_INDENT, parentIndex, extraIndent)); token.setIndent(parentIndent + extraIndent); } previous = token; } }
/** * Makes sure all new lines within given node will have wrap policy so that * wrap executor will fix their indentation if necessary. */ private void forceContinuousWrapping(ASTNode node, int parentIndex) { int parentIndent = this.tm.get(parentIndex).getIndent(); int indentChange = -parentIndent; int lineStart = this.tm.findFirstTokenInLine(parentIndex); for (int i = parentIndex; i >= lineStart; i--) { int align = this.tm.get(i).getAlign(); if (align > 0) { indentChange = -2 * parentIndent + align; break; } } Token previous = null; int from = this.tm.firstIndexIn(node, -1); int to = this.tm.lastIndexIn(node, -1); for (int i = from; i <= to; i++) { Token token = this.tm.get(i); if ((token.getLineBreaksBefore() > 0 || (previous != null && previous.getLineBreaksAfter() > 0)) && (token.getWrapPolicy() == null || token.getWrapPolicy().wrapMode == WrapMode.BLOCK_INDENT)) { int extraIndent = token.getIndent() + indentChange; token.setWrapPolicy(new WrapPolicy(WrapMode.BLOCK_INDENT, parentIndex, extraIndent)); token.setIndent(parentIndent + extraIndent); } previous = token; } }