private boolean areKeptOnOneLine(List<? extends ASTNode> nodes) { return nodes.stream().allMatch(n -> this.tm.firstTokenIn(n, -1).getLineBreaksBefore() == 0); }
private String getEscapedTokenString(Token token) { if (token.getLineBreaksBefore() > 0 && charAt(token.originalStart) == '@') { return "@" + this.source.substring(token.originalStart + 1, token.originalEnd + 1); //$NON-NLS-1$ } else if (token.tokenType == TokenNameNotAToken) { String text = token.toString(this.source); Matcher matcher = COMMENT_LINE_ANNOTATION_PATTERN.matcher(text); if (matcher.find()) { return matcher.group(1) + "@" + text.substring(matcher.end(2)); //$NON-NLS-1$ } } return token.toString(this.source); }
private String getEscapedTokenString(Token token) { if (token.getLineBreaksBefore() > 0 && charAt(token.originalStart) == '@') { return "@" + this.source.substring(token.originalStart + 1, token.originalEnd + 1); //$NON-NLS-1$ } else if (token.tokenType == TokenNameNotAToken) { String text = token.toString(this.source); Matcher matcher = COMMENT_LINE_ANNOTATION_PATTERN.matcher(text); if (matcher.find()) { return matcher.group(1) + "@" + text.substring(matcher.end(2)); //$NON-NLS-1$ } } return token.toString(this.source); }
private String getEscapedTokenString(Token token) { if (token.getLineBreaksBefore() > 0 && charAt(token.originalStart) == '@') { return "@" + this.source.substring(token.originalStart + 1, token.originalEnd + 1); //$NON-NLS-1$ } else if (token.tokenType == TokenNameNotAToken) { String text = token.toString(this.source); Matcher matcher = COMMENT_LINE_ANNOTATION_PATTERN.matcher(text); if (matcher.find()) { return matcher.group(1) + "@" + text.substring(matcher.end(2)); //$NON-NLS-1$ } } return token.toString(this.source); }
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 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 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; }
@Override public boolean visit(VariableDeclarationFragment node) { if (node.getInitializer() == null) return true; int rightSideIndex = this.tm.firstIndexIn(node.getInitializer(), -1); if (this.tm.get(rightSideIndex).getLineBreaksBefore() > 0) return true; // must be an array initializer in new line because of brace_position_for_array_initializer int equalIndex = this.tm.firstIndexBefore(node.getInitializer(), TokenNameEQUAL); this.wrapIndexes.add(this.options.wrap_before_assignment_operator ? equalIndex : rightSideIndex); this.secondaryWrapIndexes.add(this.options.wrap_before_assignment_operator ? rightSideIndex : equalIndex); this.wrapParentIndex = equalIndex - 1; this.wrapGroupEnd = this.tm.lastIndexIn(node.getInitializer(), -1); handleWrap(this.options.alignment_for_assignment); return true; }
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 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()); }
@Override public boolean visit(VariableDeclarationFragment node) { if (node.getInitializer() == null) return true; int rightSideIndex = this.tm.firstIndexIn(node.getInitializer(), -1); if (this.tm.get(rightSideIndex).getLineBreaksBefore() > 0) return true; // must be an array initializer in new line because of brace_position_for_array_initializer int equalIndex = this.tm.firstIndexBefore(node.getInitializer(), TokenNameEQUAL); this.wrapIndexes.add(this.options.wrap_before_assignment_operator ? equalIndex : rightSideIndex); this.secondaryWrapIndexes.add(this.options.wrap_before_assignment_operator ? rightSideIndex : equalIndex); this.wrapParentIndex = equalIndex - 1; this.wrapGroupEnd = this.tm.lastIndexIn(node.getInitializer(), -1); handleWrap(this.options.alignment_for_assignment); return true; }
@Override public boolean visit(VariableDeclarationFragment node) { if (node.getInitializer() == null) return true; int rightSideIndex = this.tm.firstIndexIn(node.getInitializer(), -1); if (this.tm.get(rightSideIndex).getLineBreaksBefore() > 0) return true; // must be an array initializer in new line because of brace_position_for_array_initializer int equalIndex = this.tm.firstIndexBefore(node.getInitializer(), TokenNameEQUAL); this.wrapIndexes.add(this.options.wrap_before_assignment_operator ? equalIndex : rightSideIndex); this.secondaryWrapIndexes.add(this.options.wrap_before_assignment_operator ? rightSideIndex : equalIndex); this.wrapParentIndex = equalIndex - 1; this.wrapGroupEnd = this.tm.lastIndexIn(node.getInitializer(), -1); handleWrap(this.options.alignment_for_assignment); return true; }
@Override public boolean visit(Assignment node) { int rightSideIndex = this.tm.firstIndexIn(node.getRightHandSide(), -1); if (this.tm.get(rightSideIndex).getLineBreaksBefore() > 0) return true; // must be an array initializer in new line because of brace_position_for_array_initializer int operatorIndex = this.tm.firstIndexBefore(node.getRightHandSide(), -1); while (this.tm.get(operatorIndex).isComment()) operatorIndex--; assert node.getOperator().toString().equals(this.tm.toString(operatorIndex)); this.wrapIndexes.add(this.options.wrap_before_assignment_operator ? operatorIndex : rightSideIndex); this.secondaryWrapIndexes.add(this.options.wrap_before_assignment_operator ? rightSideIndex : operatorIndex); this.wrapParentIndex = operatorIndex - 1; this.wrapGroupEnd = this.tm.lastIndexIn(node.getRightHandSide(), -1); handleWrap(this.options.alignment_for_assignment); return true; }
@Override public boolean visit(Assignment node) { int rightSideIndex = this.tm.firstIndexIn(node.getRightHandSide(), -1); if (this.tm.get(rightSideIndex).getLineBreaksBefore() > 0) return true; // must be an array initializer in new line because of brace_position_for_array_initializer int operatorIndex = this.tm.firstIndexBefore(node.getRightHandSide(), -1); while (this.tm.get(operatorIndex).isComment()) operatorIndex--; assert node.getOperator().toString().equals(this.tm.toString(operatorIndex)); this.wrapIndexes.add(this.options.wrap_before_assignment_operator ? operatorIndex : rightSideIndex); this.secondaryWrapIndexes.add(this.options.wrap_before_assignment_operator ? rightSideIndex : operatorIndex); this.wrapParentIndex = operatorIndex - 1; this.wrapGroupEnd = this.tm.lastIndexIn(node.getRightHandSide(), -1); handleWrap(this.options.alignment_for_assignment); return true; }
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; }