private void checkTopPriorityWraps(int wrapIndex) throws WrapRestartThrowable { WrapPolicy wrapPolicy = this.tm.get(wrapIndex).getWrapPolicy(); if (wrapPolicy != null && wrapPolicy.wrapMode == WrapMode.TOP_PRIORITY && !this.usedTopPriorityWraps.contains(wrapPolicy)) throw new WrapRestartThrowable(wrapIndex); }
public int findFirstTokenInLine(int startIndex, boolean includeWraps, boolean includeIndents) { Token previous = get(startIndex); // going backwards, previous has higher index than current for (int i = startIndex - 1; i >= 0; i--) { Token token = get(i); if (token.getLineBreaksAfter() > 0 || previous.getLineBreaksBefore() > 0) { boolean include = previous.getWrapPolicy() != null && (previous.getWrapPolicy().wrapMode == WrapMode.BLOCK_INDENT ? includeIndents : includeWraps); if (!include) return i + 1; } previous = token; } return 0; }
public int findFirstTokenInLine(int startIndex, boolean includeWraps, boolean includeForced) { Token previous = get(startIndex); // going backwards, previous has higher index than current for (int i = startIndex - 1; i >= 0; i--) { Token token = get(i); if (token.getLineBreaksAfter() > 0 || previous.getLineBreaksBefore() > 0) { boolean include = previous.getWrapPolicy() != null && (previous.getWrapPolicy().wrapMode == WrapMode.FORCED ? includeForced : includeWraps); if (!include) return i + 1; } previous = token; } return 0; }
public int findFirstTokenInLine(int startIndex, boolean includeWraps, boolean includeIndents) { Token previous = get(startIndex); // going backwards, previous has higher index than current for (int i = startIndex - 1; i >= 0; i--) { Token token = get(i); if (token.getLineBreaksAfter() > 0 || previous.getLineBreaksBefore() > 0) { boolean include = previous.getWrapPolicy() != null && (previous.getWrapPolicy().wrapMode == WrapMode.BLOCK_INDENT ? includeIndents : includeWraps); if (!include) return i + 1; } previous = token; } return 0; }
private void newLine(Token token, int index) { while (!this.stack.isEmpty() && index > this.stack.peek().getWrapPolicy().groupEndIndex) this.stack.pop(); if (token.getWrapPolicy() != null) { token.setIndent(getWrapIndent(token)); handleOnColumnIndent(index, token.getWrapPolicy()); this.stack.push(token); } else if (this.stack.isEmpty()) { this.initialIndent = token.getIndent(); WrapExecutor.this.wrapSearchResults.clear(); } this.currentIndent = this.stack.isEmpty() ? this.initialIndent : this.stack.peek().getIndent(); token.setIndent(this.currentIndent); this.nextWrap = findWrapsCached(index, this.currentIndent).nextWrap; } }
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 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 boolean shouldWrap() { if (this.wrapDisabled || this.counter <= this.lineLimit) return false; if (getLineBreaksAfter() == 0 && getNext() != null && getNext().getWrapPolicy() == WrapPolicy.DISABLE_WRAP) { // The next token cannot be wrapped, so there's no need to wrap now. // Let's wait and decide when there's more information available. return false; } if (this.potentialWrapToken != null && this.potentialWrapTokenSubstitute != null && this.counterIfWrapped > this.lineLimit && this.counterIfWrappedSubstitute < this.counterIfWrapped) { // there is a normal token to wrap, but the line would overflow anyway - better use substitute this.potentialWrapToken = null; } if (this.potentialWrapToken == null && this.potentialWrapTokenSubstitute == null) { return false; } return true; }
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; }
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 boolean isActiveTopPriorityWrap(int index, WrapPolicy wrapPolicy) { if (this.activeTopPriorityWrap >= 0) return false; for (int i = index - 1; i > wrapPolicy.wrapParentIndex; i--) { Token token = WrapExecutor.this.tm.get(i); if (token.isWrappable() && token.getWrapPolicy().wrapParentIndex == wrapPolicy.wrapParentIndex && (token.getLineBreaksBefore() > 0 || WrapExecutor.this.tm.get(i - 1).getLineBreaksAfter() > 0)) { return true; } } return false; }
private boolean isActiveTopPriorityWrap(int index, WrapPolicy wrapPolicy) { if (this.activeTopPriorityWrap >= 0) return false; for (int i = index - 1; i > wrapPolicy.wrapParentIndex; i--) { Token token = WrapExecutor.this.tm.get(i); if (token.isWrappable() && token.getWrapPolicy().wrapParentIndex == wrapPolicy.wrapParentIndex && (token.getLineBreaksBefore() > 0 || WrapExecutor.this.tm.get(i - 1).getLineBreaksAfter() > 0)) { return true; } } return false; }
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 checkForceWrap(Token token, int index, int currentIndent) throws WrapRestartThrowable { // 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) { WrapPolicy lineStartPolicy = this.tm.get(this.tm.findFirstTokenInLine(index, false, true)).getWrapPolicy(); if (lineStartPolicy != null && lineStartPolicy.wrapMode != WrapMode.FORCED) { token.breakBefore(); throw new WrapRestartThrowable(-1); } } }
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); }
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; }