private <N extends ASTNode> void alignNodes(List<N> alignGroup, AlignIndexFinder<N> tokenFinder) { int[] tokenIndexes = alignGroup.stream() .map(tokenFinder::findIndex) .filter(Optional::isPresent) .mapToInt(Optional::get).toArray(); OptionalInt maxPosition = IntStream.of(tokenIndexes).map(this.tm::getPositionInLine).max(); if (maxPosition.isPresent()) { int align = normalizedAlign(maxPosition.getAsInt()); for (int tokenIndex : tokenIndexes) this.tm.get(tokenIndex).setAlign(align); } }
private void handleOnColumnIndent(int tokenIndex, WrapPolicy wrapPolicy) { if (wrapPolicy != null && wrapPolicy.indentOnColumn && !wrapPolicy.isFirstInGroup && this.options.tab_char == DefaultCodeFormatterOptions.TAB && !this.options.use_tabs_only_for_leading_indentations) { // special case: first wrap in a group should be aligned on column even if it's not wrapped for (int i = tokenIndex - 1; i >= 0; i--) { Token token = this.tm.get(i); WrapPolicy wrapPolicy2 = token.getWrapPolicy(); if (wrapPolicy2 != null && wrapPolicy2.isFirstInGroup && wrapPolicy2.wrapParentIndex == wrapPolicy.wrapParentIndex) { token.setAlign(getWrapIndent(token)); break; } } } }
void handleOnColumnIndent(int tokenIndex, WrapPolicy wrapPolicy) { if (wrapPolicy != null && wrapPolicy.indentOnColumn && !wrapPolicy.isFirstInGroup && this.options.tab_char == DefaultCodeFormatterOptions.TAB && !this.options.use_tabs_only_for_leading_indentations) { // special case: first wrap in a group should be aligned on column even if it's not wrapped for (int i = tokenIndex - 1; i >= 0; i--) { Token token = this.tm.get(i); WrapPolicy wrapPolicy2 = token.getWrapPolicy(); if (wrapPolicy2 != null && wrapPolicy2.isFirstInGroup && wrapPolicy2.wrapParentIndex == wrapPolicy.wrapParentIndex) { token.setAlign(getWrapIndent(token)); break; } } } }
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()); } } }
void handleOnColumnIndent(int tokenIndex, WrapPolicy wrapPolicy) { if (wrapPolicy != null && wrapPolicy.indentOnColumn && !wrapPolicy.isFirstInGroup && this.options.tab_char == DefaultCodeFormatterOptions.TAB && !this.options.use_tabs_only_for_leading_indentations) { // special case: first wrap in a group should be aligned on column even if it's not wrapped for (int i = tokenIndex - 1; i >= 0; i--) { Token token = this.tm.get(i); WrapPolicy wrapPolicy2 = token.getWrapPolicy(); if (wrapPolicy2 != null && wrapPolicy2.isFirstInGroup && wrapPolicy2.wrapParentIndex == wrapPolicy.wrapParentIndex) { token.setAlign(getWrapIndent(token)); break; } } } }
structure.get(0).breakAfter(); endingToken.breakBefore(); endingToken.setAlign(1);
lineComment.breakAfter(); lineComment.spaceBefore(); lineComment.setAlign(WrapExecutor.this.tm.getNLSAlign(index)); lineComment.setInternalStructure(new ArrayList<Token>()); WrapExecutor.this.tm.insert(index + 1, lineComment);
if (align == 0 && getLineBreaksBefore() == 0 && this.parent != null) { align = token.getIndent(); token.setAlign(align); alignmentChar = DefaultCodeFormatterOptions.SPACE;
lineComment.breakAfter(); lineComment.spaceBefore(); lineComment.setAlign(WrapExecutor.this.tm.getNLSAlign(index)); lineComment.setInternalStructure(new ArrayList<Token>()); WrapExecutor.this.tm.insert(index + 1, lineComment);
private void cleanupHTMLElement(int openingTagIndex, int closingTagIndex, boolean formattedCode) { Token previous = this.ctm.get(openingTagIndex); int indent = previous.getIndent(); for (int i = openingTagIndex + 1; i < closingTagIndex; i++) { Token token = this.ctm.get(i); token.setToEscape(true); if (formattedCode && (token.getLineBreaksBefore() > 0 || previous.getLineBreaksAfter() > 0)) token.setAlign(indent); previous = token; } for (int i = closingTagIndex; i < this.ctm.size(); i++) { Token token = this.ctm.get(i); if (token.getIndent() == 0) break; token.setIndent(indent); previous = token; } }
private void cleanupHTMLElement(int openingTagIndex, int closingTagIndex, boolean formattedCode) { Token previous = this.ctm.get(openingTagIndex); int indent = previous.getIndent(); for (int i = openingTagIndex + 1; i < closingTagIndex; i++) { Token token = this.ctm.get(i); token.setToEscape(true); if (formattedCode && (token.getLineBreaksBefore() > 0 || previous.getLineBreaksAfter() > 0)) token.setAlign(indent); previous = token; } for (int i = closingTagIndex; i < this.ctm.size(); i++) { Token token = this.ctm.get(i); if (token.getIndent() == 0) break; token.setIndent(indent); previous = token; } }
private void cleanupHTMLElement(int openingTagIndex, int closingTagIndex, boolean formattedCode) { Token previous = this.ctm.get(openingTagIndex); int indent = previous.getIndent(); for (int i = openingTagIndex + 1; i < closingTagIndex; i++) { Token token = this.ctm.get(i); token.setToEscape(true); if (formattedCode && (token.getLineBreaksBefore() > 0 || previous.getLineBreaksAfter() > 0)) token.setAlign(indent); previous = token; } for (int i = closingTagIndex; i < this.ctm.size(); i++) { Token token = this.ctm.get(i); if (token.getIndent() == 0) break; token.setIndent(indent); previous = token; } }