1 11 package org.eclipse.jdt.internal.formatter; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.jdt.core.dom.ASTNode; 17 import org.eclipse.jdt.core.dom.ASTVisitor; 18 import org.eclipse.jdt.core.dom.Expression; 19 import org.eclipse.jdt.core.dom.InfixExpression; 20 21 public class InfixExpressionWrappingBuilder extends ASTVisitor { 22 private final static int DEFAULT_SIZE = 10; 23 ArrayList fragments = new ArrayList (); 24 int[] operators = new int[DEFAULT_SIZE]; 25 int operatorCounter = 0; 26 27 public int getFragmentsCounter() { 28 return this.fragments.size(); 29 } 30 31 public int[] getOperators() { 32 if (operators.length != operatorCounter) { 33 System.arraycopy(this.operators, 0, (this.operators = new int[this.operatorCounter]), 0, this.operatorCounter); 35 } 36 return this.operators; 37 } 38 private boolean isSplittable(Expression expression) { 39 return true; 46 } 47 public boolean visit(InfixExpression node) { 48 final Expression leftOperand = node.getLeftOperand(); 49 final Expression rightOperand = node.getRightOperand(); 50 if (leftOperand.getNodeType() == ASTNode.INFIX_EXPRESSION && isSplittable(leftOperand)) { 51 leftOperand.accept(this); 52 } else { 53 this.fragments.add(leftOperand); 54 } 55 if (operatorCounter == this.operators.length) { 56 System.arraycopy(this.operators, 0, (this.operators = new int[this.operatorCounter * 2]), 0, this.operatorCounter); 58 } 59 final int extractInfixExpressionOperator = CodeFormatterVisitor2.extractInfixExpressionOperator(node); 60 this.operators[this.operatorCounter++] = extractInfixExpressionOperator; 61 if (rightOperand.getNodeType() == ASTNode.INFIX_EXPRESSION && isSplittable(leftOperand)) { 62 rightOperand.accept(this); 63 } else { 64 this.fragments.add(rightOperand); 65 } 66 final List extendedOperands = node.extendedOperands(); 67 final int extendedOperandsLength = extendedOperands.size(); 68 if (extendedOperandsLength != 0) { 69 for (int i = 0; i < extendedOperandsLength; i++) { 70 if (operatorCounter == this.operators.length) { 71 System.arraycopy(this.operators, 0, (this.operators = new int[this.operatorCounter * 2]), 0, this.operatorCounter); 73 } 74 this.operators[this.operatorCounter++] = extractInfixExpressionOperator; 75 final Expression extendedOperand = ((Expression) extendedOperands.get(i)); 76 if (extendedOperand instanceof InfixExpression) { 77 extendedOperand.accept(this); 78 } else { 79 this.fragments.add(extendedOperand); 80 } 81 } 82 } 83 return false; 84 } 85 86 } 87 | Popular Tags |