Search in sources :

Example 1 with Assoc

use of suite.node.io.Operator.Assoc in project suite by stupidsing.

the class PrettyPrinter method prettyPrint_.

// op0 for avoiding unnecessary indenting; prec0 for parenthesizing
private void prettyPrint_(Node node, Operator op0, int prec0) {
    int x = getX(), y = getY();
    int length = lengthEstimator.getEstimatedLength(node);
    // line too long?
    if (node instanceof Tree) {
        Tree tree = (Tree) node;
        Operator op = tree.getOperator();
        int prec = op.getPrecedence();
        boolean isNeedPars = prec <= prec0;
        int parsIndent = 0, parsIndent0 = 0;
        if (isNeedPars) {
            parsIndent = currentLineIndent;
            parsIndent0 = incrementIndent();
            append("(");
        }
        if (lineLength < x + length)
            if (isLookingLikeList(op, node))
                prettyPrintList(op, node);
            else {
                Node left = tree.getLeft();
                Node right = tree.getRight();
                Assoc assoc = op.getAssoc();
                int leftPrec = prec - (assoc == Assoc.LEFT ? 1 : 0);
                int rightPrec = prec - (assoc == Assoc.RIGHT ? 1 : 0);
                if (op == TermOp.BRACES)
                    leftPrec = rightPrec = 0;
                Tree tree1 = Tree.decompose(right, op);
                Node r0 = tree1 != null ? tree1.getLeft() : null;
                int es0 = lengthEstimator.getEstimatedLength(left);
                int es1 = r0 != null ? lengthEstimator.getEstimatedLength(r0) : lineLength;
                int opLength = op.getName().length();
                // breaks "a + b + xxx" in the second operator
                if (// 
                assoc == Assoc.RIGHT && // 
                x + es0 + es1 + opLength < lineLength && r0 != preferLineBreakBeforeKeyword) {
                    prettyPrint_(left, op, leftPrec);
                    OperatorPosition opPos = appendOperator(op);
                    prettyPrint_(right, op, rightPrec);
                    closeBraces(op, opPos);
                } else {
                    // breaks after the operator
                    boolean isIncRightIndent = op != op0;
                    int indent0 = 0;
                    prettyPrint_(left, op, leftPrec);
                    if (isIncRightIndent)
                        indent0 = incrementIndent();
                    OperatorPosition opPos;
                    if (getLineSize() + lengthEstimator.getEstimatedLength(right) < squeezeLineLength)
                        opPos = appendOperator(op);
                    else
                        opPos = appendOperatorLineFeed(op);
                    prettyPrint_(right, op, rightPrec);
                    closeBraces(op, opPos);
                    if (isIncRightIndent)
                        revertIndent(indent0);
                }
            }
        else
            append(Formatter.dump(node));
        if (isNeedPars) {
            if (y != getY())
                nl(parsIndent);
            append(")");
            revertIndent(parsIndent0);
        }
    } else {
        if (node == lineBreakBeforeKeyword && !isLineBegin())
            nl();
        // space sufficient
        append(Formatter.dump(node));
    }
}
Also used : Operator(suite.node.io.Operator) Node(suite.node.Node) Tree(suite.node.Tree) Assoc(suite.node.io.Operator.Assoc)

Aggregations

Node (suite.node.Node)1 Tree (suite.node.Tree)1 Operator (suite.node.io.Operator)1 Assoc (suite.node.io.Operator.Assoc)1