Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 9768d913 authored by Patrick Gaskin's avatar Patrick Gaskin
Browse files

ExactCalculator: Add more special cases for smart parentheses

See I8fe696e23d75545a1ee23641448ea31ce31da0c0.

Change-Id: I69e45a44f9c024f67f9af0430a2444487ec70341
parent d4242253
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -744,12 +744,14 @@ public class Calculator extends AppCompatActivity
        } else if (id == R.id.paren) {
            // If we just added a function or left paren, add another.
            // If we don't have any open parentheses, add a left one.
            // If we end with a digit or a right parenthesis, add a right one.
            // If we end with a digit, symbolic constant, right parenthesis, or suffix operator, add
            // a right one.
            // If we end with an operator, add a left one.
            if (!mEvaluator.getExpr(Evaluator.MAIN_INDEX).hasTrailingLeftParen() &&
                    mEvaluator.getExpr(Evaluator.MAIN_INDEX).hasOpenParentheses() &&
                    (mEvaluator.getExpr(Evaluator.MAIN_INDEX).hasTrailingRightParen() ||
                            mEvaluator.getExpr(Evaluator.MAIN_INDEX).hasTrailingConstant()))
                            mEvaluator.getExpr(Evaluator.MAIN_INDEX).hasTrailingConstant() ||
                            mEvaluator.getExpr(Evaluator.MAIN_INDEX).hasTrailingSuffix()))
                addExplicitKeyToExpr(R.id.rparen);
            else
                addExplicitKeyToExpr(R.id.lparen);
+16 −1
Original line number Diff line number Diff line
@@ -429,7 +429,10 @@ class CalculatorExpr {
            return false;
        }
        Token t = mExpr.get(s-1);
        return t instanceof Constant;
        if ((t instanceof Constant)) return true;
        if (!(t instanceof Operator)) return false;
        Operator o = (Operator)t;
        return o.id == R.id.const_pi || o.id == R.id.const_e;
    }

    /**
@@ -444,6 +447,18 @@ class CalculatorExpr {
        return (KeyMaps.isBinary(o.id));
    }

    /**
     * Does this expression end with a suffix operator?
     */
    boolean hasTrailingSuffix() {
        int s = mExpr.size();
        if (s == 0) return false;
        Token t = mExpr.get(s-1);
        if (!(t instanceof Operator)) return false;
        Operator o = (Operator)t;
        return (KeyMaps.isSuffix(o.id));
    }

    /**
     * Does this expression contain an unmatched lparen?
     */