private void initTraverse(List<Token> tokens, int startIndex) { if (tokens.isEmpty()) return; this.structureChanged = false; this.previous = this.next = null; if (startIndex > 0) this.previous = tokens.get(startIndex - 1); this.current = tokens.get(startIndex); this.lineBreaksBefore = Math.max(this.previous != null ? this.previous.getLineBreaksAfter() : 0, this.current.getLineBreaksBefore()); this.spaceBefore = this.current.isSpaceBefore(); if (this.lineBreaksBefore == 0) { this.spaceBefore = this.spaceBefore || (this.previous != null && this.previous.isSpaceAfter()); } }
private void initTraverse(List<Token> tokens, int startIndex) { if (tokens.isEmpty()) return; this.structureChanged = false; this.previous = this.next = null; if (startIndex > 0) this.previous = tokens.get(startIndex - 1); this.current = tokens.get(startIndex); this.lineBreaksBefore = Math.max(this.previous != null ? this.previous.getLineBreaksAfter() : 0, this.current.getLineBreaksBefore()); this.spaceBefore = this.current.isSpaceBefore(); if (this.lineBreaksBefore == 0) { this.spaceBefore = this.spaceBefore || (this.previous != null && this.previous.isSpaceAfter()); } }
private void initTraverse(List<Token> tokens, int startIndex) { if (tokens.isEmpty()) return; this.structureChanged = false; this.previous = this.next = null; if (startIndex > 0) this.previous = tokens.get(startIndex - 1); this.current = tokens.get(startIndex); this.lineBreaksBefore = Math.max(this.previous != null ? this.previous.getLineBreaksAfter() : 0, this.current.getLineBreaksBefore()); this.spaceBefore = this.current.isSpaceBefore(); if (this.lineBreaksBefore == 0) { this.spaceBefore = this.spaceBefore || (this.previous != null && this.previous.isSpaceAfter()); } }
private int tryToFitInOneLine(List<Token> structure, int startPosition, boolean noWrap) { int position = startPosition; boolean hasWrapPotential = false; boolean wasSpaceAfter = false; for (int i = 0; i < structure.size(); i++) { Token token = structure.get(i); if (token.getLineBreaksBefore() > 0 || token.getLineBreaksAfter() > 0) { assert !noWrap; // comment already wrapped return -1; } if (!wasSpaceAfter && token.isSpaceBefore()) position++; position += this.tm.getLength(token, position); wasSpaceAfter = token.isSpaceAfter(); if (wasSpaceAfter) position++; WrapPolicy policy = token.getWrapPolicy(); if (i > 1 && (policy == null || policy == WrapPolicy.SUBSTITUTE_ONLY)) hasWrapPotential = true; } if (position <= this.lineLimit || noWrap || !hasWrapPotential) return position; return -1; }
this.spaceAfter = this.current.isSpaceAfter(); if (this.lineBreaksAfter == 0) { this.spaceAfter = this.spaceAfter || (this.next != null && this.next.isSpaceBefore());
private int tryToFitInOneLine(List<Token> structure, int startPosition, boolean noWrap) { int position = startPosition; boolean hasWrapPotential = false; boolean wasSpaceAfter = false; for (int i = 0; i < structure.size(); i++) { Token token = structure.get(i); if (token.getLineBreaksBefore() > 0 || token.getLineBreaksAfter() > 0) { assert !noWrap; // comment already wrapped return -1; } if (!wasSpaceAfter && token.isSpaceBefore()) position++; position += this.tm.getLength(token, position); wasSpaceAfter = token.isSpaceAfter(); if (wasSpaceAfter) position++; WrapPolicy policy = token.getWrapPolicy(); if (i > 1 && (policy == null || policy == WrapPolicy.SUBSTITUTE_ONLY)) hasWrapPotential = true; } if (position <= this.lineLimit || noWrap || !hasWrapPotential) return position; return -1; }
this.spaceAfter = this.current.isSpaceAfter(); if (this.lineBreaksAfter == 0) { this.spaceAfter = this.spaceAfter || (this.next != null && this.next.isSpaceBefore());
this.spaceAfter = this.current.isSpaceAfter(); if (this.lineBreaksAfter == 0) { this.spaceAfter = this.spaceAfter || (this.next != null && this.next.isSpaceBefore());
private void disableFormatting(int startIndex, int endIndex) { Token startToken = this.ctm.get(startIndex), endToken = this.ctm.get(endIndex); Token noFormatToken = new Token(startToken.originalStart, endToken.originalEnd, TokenNameCOMMENT_JAVADOC); List<Token> tokensToReplace = this.commentStructure.subList(startIndex, endIndex + 1); if (this.ctm.countLineBreaksBetween(startToken, endToken) == 0) { tokensToReplace.clear(); tokensToReplace.add(noFormatToken); } else { int commentStart = findCommentLineIndent(startIndex); tokensToReplace.clear(); tokensToReplace.addAll(commentToLines(noFormatToken, commentStart)); } Token first = tokensToReplace.get(0), last = tokensToReplace.get(tokensToReplace.size() - 1); if (startToken.isSpaceBefore()) first.spaceBefore(); first.putLineBreaksBefore(startToken.getLineBreaksBefore()); first.setWrapPolicy(startToken.getWrapPolicy()); if (endToken.isSpaceAfter()) last.spaceAfter(); last.putLineBreaksAfter(endToken.getLineBreaksAfter()); for (Token token : tokensToReplace) if (token.tokenType == TokenNameCOMMENT_JAVADOC) token.setIndent(startToken.getIndent()); }
private void disableFormatting(int startIndex, int endIndex) { Token startToken = this.ctm.get(startIndex), endToken = this.ctm.get(endIndex); Token noFormatToken = new Token(startToken.originalStart, endToken.originalEnd, TokenNameCOMMENT_JAVADOC); List<Token> tokensToReplace = this.commentStructure.subList(startIndex, endIndex + 1); if (this.ctm.countLineBreaksBetween(startToken, endToken) == 0) { tokensToReplace.clear(); tokensToReplace.add(noFormatToken); } else { int commentStart = findCommentLineIndent(startIndex); tokensToReplace.clear(); tokensToReplace.addAll(commentToLines(noFormatToken, commentStart)); } Token first = tokensToReplace.get(0), last = tokensToReplace.get(tokensToReplace.size() - 1); if (startToken.isSpaceBefore()) first.spaceBefore(); first.putLineBreaksBefore(startToken.getLineBreaksBefore()); first.setWrapPolicy(startToken.getWrapPolicy()); if (endToken.isSpaceAfter()) last.spaceAfter(); last.putLineBreaksAfter(endToken.getLineBreaksAfter()); for (Token token : tokensToReplace) if (token.tokenType == TokenNameCOMMENT_JAVADOC) token.setIndent(startToken.getIndent()); }
private void disableFormatting(int startIndex, int endIndex) { Token startToken = this.ctm.get(startIndex), endToken = this.ctm.get(endIndex); Token noFormatToken = new Token(startToken.originalStart, endToken.originalEnd, TokenNameCOMMENT_JAVADOC); List<Token> tokensToReplace = this.commentStructure.subList(startIndex, endIndex + 1); if (this.ctm.countLineBreaksBetween(startToken, endToken) == 0) { tokensToReplace.clear(); tokensToReplace.add(noFormatToken); } else { int commentStart = findCommentLineIndent(startIndex); tokensToReplace.clear(); tokensToReplace.addAll(commentToLines(noFormatToken, commentStart)); } Token first = tokensToReplace.get(0), last = tokensToReplace.get(tokensToReplace.size() - 1); if (startToken.isSpaceBefore()) first.spaceBefore(); first.putLineBreaksBefore(startToken.getLineBreaksBefore()); first.setWrapPolicy(startToken.getWrapPolicy()); if (endToken.isSpaceAfter()) last.spaceAfter(); last.putLineBreaksAfter(endToken.getLineBreaksAfter()); for (Token token : tokensToReplace) if (token.tokenType == TokenNameCOMMENT_JAVADOC) token.setIndent(startToken.getIndent()); }
if (this.options.number_of_empty_lines_to_preserve > 0 && preexistingBreaks > 1) return; // blank line will be preserved boolean isSpace = prev.isSpaceAfter() || token.isSpaceBefore(); if (prev.isComment() || token.isComment()) { if (preexistingBreaks > 0)
public void applyFormatOff() { if (this.formatOffTagPairs == null) return; for (Token[] pair : this.formatOffTagPairs) { int index1 = findIndex(pair[0].originalStart, -1, false); int index2 = findIndex(pair[1].originalEnd, -1, false); pair[0] = get(index1); pair[1] = get(index2); Token unformatted = new Token(pair[0].originalStart, pair[1].originalEnd, TokenNameWHITESPACE); unformatted.setIndent(Math.min(pair[0].getIndent(), findSourcePositionInLine(pair[0].originalStart))); unformatted.putLineBreaksBefore(pair[0].getLineBreaksBefore()); if (pair[0].isSpaceBefore()) unformatted.spaceBefore(); unformatted.putLineBreaksAfter(pair[1].getLineBreaksAfter()); if (pair[1].isSpaceAfter()) unformatted.spaceAfter(); this.tokens.set(index1, unformatted); this.tokens.subList(index1 + 1, index2 + 1).clear(); } } }
private void splitToken(Token token, int tokenIndex, int splitPosition) { assert splitPosition > token.originalStart && splitPosition <= token.originalEnd; Token part1 = new Token(token.originalStart, splitPosition - 1, token.tokenType); Token part2 = new Token(splitPosition, token.originalEnd, token.tokenType); if (token.isSpaceBefore()) part1.spaceBefore(); part1.putLineBreaksBefore(token.getLineBreaksBefore()); if (token.isSpaceAfter()) part2.spaceAfter(); part2.putLineBreaksAfter(token.getLineBreaksAfter()); part1.setIndent(token.getIndent()); part2.setIndent(token.getIndent()); part1.setWrapPolicy(token.getWrapPolicy()); this.commentStructure.set(tokenIndex, part1); this.commentStructure.add(tokenIndex + 1, part2); }
private void splitToken(Token token, int tokenIndex, int splitPosition) { assert splitPosition > token.originalStart && splitPosition <= token.originalEnd; Token part1 = new Token(token.originalStart, splitPosition - 1, token.tokenType); Token part2 = new Token(splitPosition, token.originalEnd, token.tokenType); if (token.isSpaceBefore()) part1.spaceBefore(); part1.putLineBreaksBefore(token.getLineBreaksBefore()); if (token.isSpaceAfter()) part2.spaceAfter(); part2.putLineBreaksAfter(token.getLineBreaksAfter()); part1.setIndent(token.getIndent()); part2.setIndent(token.getIndent()); part1.setWrapPolicy(token.getWrapPolicy()); this.commentStructure.set(tokenIndex, part1); this.commentStructure.add(tokenIndex + 1, part2); }
private void splitToken(Token token, int tokenIndex, int splitPosition) { assert splitPosition > token.originalStart && splitPosition <= token.originalEnd; Token part1 = new Token(token.originalStart, splitPosition - 1, token.tokenType); Token part2 = new Token(splitPosition, token.originalEnd, token.tokenType); if (token.isSpaceBefore()) part1.spaceBefore(); part1.putLineBreaksBefore(token.getLineBreaksBefore()); if (token.isSpaceAfter()) part2.spaceAfter(); part2.putLineBreaksAfter(token.getLineBreaksAfter()); part1.setIndent(token.getIndent()); part2.setIndent(token.getIndent()); part1.setWrapPolicy(token.getWrapPolicy()); this.commentStructure.set(tokenIndex, part1); this.commentStructure.add(tokenIndex + 1, part2); }
int getWrapIndent(Token token) { WrapPolicy policy = token.getWrapPolicy(); if (policy == null) return token.getIndent(); if (policy == WrapPolicy.FORCE_FIRST_COLUMN) return 0; Token wrapParent = this.tm.get(policy.wrapParentIndex); int wrapIndent = wrapParent.getIndent(); if (policy.indentOnColumn) { wrapIndent = this.tm.getPositionInLine(policy.wrapParentIndex); wrapIndent += this.tm.getLength(wrapParent, wrapIndent); if (wrapParent.isSpaceAfter() || this.tm.get(policy.wrapParentIndex + 1).isSpaceBefore()) wrapIndent++; } wrapIndent += policy.extraIndent; return this.tm.toIndent(wrapIndent, true); } }
int getWrapIndent(Token token) { WrapPolicy policy = token.getWrapPolicy(); if (policy == null) return token.getIndent(); if (policy == WrapPolicy.FORCE_FIRST_COLUMN) return 0; Token wrapParent = this.tm.get(policy.wrapParentIndex); int wrapIndent = wrapParent.getIndent(); if (policy.indentOnColumn) { wrapIndent = this.tm.getPositionInLine(policy.wrapParentIndex); wrapIndent += this.tm.getLength(wrapParent, wrapIndent); Token next = this.tm.get(policy.wrapParentIndex + 1); if (wrapParent.isSpaceAfter() || (next.isSpaceBefore() && !next.isComment())) wrapIndent++; } wrapIndent += policy.extraIndent; return this.tm.toIndent(wrapIndent, true); } }
int getWrapIndent(Token token) { WrapPolicy policy = token.getWrapPolicy(); if (policy == null) return token.getIndent(); if (this.options.never_indent_line_comments_on_first_column && token.tokenType == TokenNameCOMMENT_LINE && token.getIndent() == 0) return 0; if (this.options.never_indent_block_comments_on_first_column && token.tokenType == TokenNameCOMMENT_BLOCK && token.getIndent() == 0) return 0; Token wrapParent = this.tm.get(policy.wrapParentIndex); int wrapIndent = wrapParent.getIndent(); if (policy.indentOnColumn) { wrapIndent = this.tm.getPositionInLine(policy.wrapParentIndex); wrapIndent += this.tm.getLength(wrapParent, wrapIndent); if (wrapParent.isSpaceAfter() || this.tm.get(policy.wrapParentIndex + 1).isSpaceBefore()) wrapIndent++; } wrapIndent += policy.extraIndent; return this.tm.toIndent(wrapIndent, true); } }