public int findFirstTokenInLine(int startIndex) { return findFirstTokenInLine(startIndex, false, false); }
public int findFirstTokenInLine(int startIndex) { return findFirstTokenInLine(startIndex, false, false); }
public int findFirstTokenInLine(int startIndex) { return findFirstTokenInLine(startIndex, false, false); }
private int findFirstTokenInLine(ASTNode node) { if (node instanceof FieldDeclaration) { int typeIndex = this.tm.firstIndexIn(((FieldDeclaration) node).getType(), -1); return this.tm.findFirstTokenInLine(typeIndex); } if (node instanceof VariableDeclarationStatement) { int typeIndex = this.tm.firstIndexIn(((VariableDeclarationStatement) node).getType(), -1); return this.tm.findFirstTokenInLine(typeIndex); } if (node instanceof ExpressionStatement) { return this.tm.firstIndexIn(node, -1); } throw new IllegalArgumentException(node.getClass().getName()); }
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; }
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); } } }
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, int firstIndexInLine = this.tm.findFirstTokenInLine(typeIndex); int lastIndex = this.tm.lastIndexIn(declaration, -1); lastIndex = Math.min(lastIndex, this.tm.size() - 2);
@Override protected boolean token(Token token, int index) { if (index == 0 || getLineBreaksBefore() > 0) { newLine(token, index); } else if ((this.nextWrap != null && index == this.nextWrap.wrapTokenIndex) || checkForceWrap(token, index, this.currentIndent) || (token.isNextLineOnWrap() && WrapExecutor.this.tm .get(WrapExecutor.this.tm.findFirstTokenInLine(index)).isWrappable())) { token.breakBefore(); newLine(token, index); } else { token.setIndent(this.currentIndent); } return true; }
@Override protected boolean token(Token token, int index) { if (index == 0 || getLineBreaksBefore() > 0) { newLine(token, index); } else if ((this.nextWrap != null && index == this.nextWrap.wrapTokenIndex) || checkForceWrap(token, index, this.currentIndent) || (token.isNextLineOnWrap() && WrapExecutor.this.tm .get(WrapExecutor.this.tm.findFirstTokenInLine(index)).isWrappable())) { token.breakBefore(); newLine(token, index); } else { token.setIndent(this.currentIndent); } return true; }
private void bufferIndent(Token token, int index) { int indent = token.getIndent(); if (getCurrent() != null && getCurrent() != token) indent += getCurrent().getEmptyLineIndentAdjustment(); int spaces = 0; if (this.options.use_tabs_only_for_leading_indentations && this.options.tab_char != DefaultCodeFormatterOptions.SPACE) { WrapPolicy wrapPolicy = token.getWrapPolicy(); boolean isWrappedBlockComment = this.childBuilder != null && this.childBuilder.parentTokenIndex == index; if (isWrappedBlockComment) { Token lineStart = this.tm.get(this.tm.findFirstTokenInLine(index)); spaces = token.getIndent() - lineStart.getIndent(); token = lineStart; wrapPolicy = token.getWrapPolicy(); } while (wrapPolicy != null) { Token parentLineStart = this.tm.get(this.tm.findFirstTokenInLine(wrapPolicy.wrapParentIndex)); if (wrapPolicy.wrapMode != WrapMode.FORCED) spaces += token.getIndent() - parentLineStart.getIndent(); token = parentLineStart; wrapPolicy = token.getWrapPolicy(); } } appendIndentationString(this.buffer, this.options.tab_char, this.options.tab_size, indent - spaces, spaces); }
private void bufferIndent(Token token, int index) { int indent = token.getIndent(); if (getCurrent() != null && getCurrent() != token) indent += getCurrent().getEmptyLineIndentAdjustment(); int spaces = 0; if (this.options.use_tabs_only_for_leading_indentations && this.options.tab_char != DefaultCodeFormatterOptions.SPACE) { WrapPolicy wrapPolicy = token.getWrapPolicy(); boolean isWrappedBlockComment = this.childBuilder != null && this.childBuilder.parentTokenIndex == index; if (isWrappedBlockComment) { Token lineStart = this.tm.get(this.tm.findFirstTokenInLine(index)); spaces = token.getIndent() - lineStart.getIndent(); token = lineStart; wrapPolicy = token.getWrapPolicy(); } while (wrapPolicy != null) { Token parentLineStart = this.tm.get(this.tm.findFirstTokenInLine(wrapPolicy.wrapParentIndex)); if (wrapPolicy.wrapMode != WrapMode.BLOCK_INDENT) spaces += token.getIndent() - parentLineStart.getIndent(); token = parentLineStart; wrapPolicy = token.getWrapPolicy(); } } appendIndentationString(this.buffer, this.options.tab_char, this.options.tab_size, indent - spaces, spaces); }
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; }
private void bufferIndent(Token token, int index) { int indent = token.getIndent(); if (getCurrent() != null && getCurrent() != token) indent += getCurrent().getEmptyLineIndentAdjustment(); int spaces = 0; if (this.options.use_tabs_only_for_leading_indentations && this.options.tab_char != DefaultCodeFormatterOptions.SPACE) { WrapPolicy wrapPolicy = token.getWrapPolicy(); boolean isWrappedBlockComment = this.childBuilder != null && this.childBuilder.parentTokenIndex == index; if (isWrappedBlockComment) { Token lineStart = this.tm.get(this.tm.findFirstTokenInLine(index)); spaces = token.getIndent() - lineStart.getIndent(); token = lineStart; wrapPolicy = token.getWrapPolicy(); } while (wrapPolicy != null) { Token parentLineStart = this.tm.get(this.tm.findFirstTokenInLine(wrapPolicy.wrapParentIndex)); if (wrapPolicy.wrapMode != WrapMode.BLOCK_INDENT) spaces += token.getIndent() - parentLineStart.getIndent(); token = parentLineStart; if (wrapPolicy == token.getWrapPolicy()) { assert wrapPolicy == WrapPolicy.FORCE_FIRST_COLUMN || wrapPolicy == WrapPolicy.DISABLE_WRAP; break; } wrapPolicy = token.getWrapPolicy(); } } appendIndentationString(this.buffer, this.options.tab_char, this.options.tab_size, indent - spaces, spaces); }
boolean isBlockIndent = token.getWrapPolicy() != null && token.getWrapPolicy().wrapMode == WrapMode.BLOCK_INDENT; Token previous = this.tm.get(this.tm.findFirstTokenInLine(index - 1, true, !isBlockIndent)); indentToken = (token.getIndent() > previous.getIndent()) ? token : previous;
/** * 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; } }