Exemplo n.º 1
0
        public static ScriptNestedExpression Wrap(ScriptExpression expression, bool transferTrivia = false)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }
            var nested = new ScriptNestedExpression()
            {
                Span       = expression.Span,
                Expression = expression
            };

            if (!transferTrivia)
            {
                return(nested);
            }

            var firstTerminal = expression.FindFirstTerminal();

            firstTerminal?.MoveLeadingTriviasTo(nested.OpenParen);

            var lastTerminal = expression.FindLastTerminal();

            lastTerminal?.MoveTrailingTriviasTo(nested.CloseParen, true);

            return(nested);
        }
Exemplo n.º 2
0
        public override ScriptNode Visit(ScriptBinaryExpression node)
        {
            if (_isScientific)
            {
                var newNode = ScientificFunctionCallRewriter.Rewrite(_context, node);
                if (newNode != node)
                {
                    return(Visit((ScriptNode)newNode));
                }
            }

            var binaryExpression = (ScriptBinaryExpression)base.Visit((ScriptBinaryExpression)node);

            // We don't surround range with spaces
            if (_flags.HasFlags(ScriptFormatterFlags.AddSpaceBetweenOperators))
            {
                if (binaryExpression.Operator < ScriptBinaryOperator.RangeInclude)
                {
                    binaryExpression.OperatorToken.AddLeadingSpace();
                    binaryExpression.OperatorToken.AddSpaceAfter();
                }
            }

            if (_flags.HasFlags(ScriptFormatterFlags.ExplicitParenthesis))
            {
                if (binaryExpression.Operator == ScriptBinaryOperator.Divide || binaryExpression.Operator == ScriptBinaryOperator.DivideRound || (_isScientific && binaryExpression.Operator == ScriptBinaryOperator.Power))
                {
                    if (binaryExpression.Left is ScriptBinaryExpression leftBin && HasSimilarPrecedenceThanMultiply(leftBin.Operator))
                    {
                        binaryExpression.Left = null;
                        var nested = new ScriptNestedExpression(leftBin);
                        binaryExpression.Left = nested;
                        leftBin.MoveTrailingTriviasTo(nested.CloseParen, true);
                    }

                    if (binaryExpression.Right is ScriptBinaryExpression rightBin && HasSimilarPrecedenceThanMultiply(rightBin.Operator))
                    {
                        binaryExpression.Right = null;
                        var nested = new ScriptNestedExpression(rightBin);
                        binaryExpression.Right = nested;
                        rightBin.MoveTrailingTriviasTo(nested.CloseParen, true);
                    }
                }

                if (binaryExpression.Operator == ScriptBinaryOperator.Divide || binaryExpression.Operator == ScriptBinaryOperator.DivideRound || (_isScientific && binaryExpression.Operator == ScriptBinaryOperator.Power))
                {
                    var nested = new ScriptNestedExpression()
                    {
                        Expression = binaryExpression
                    };
                    binaryExpression.MoveTrailingTriviasTo(nested.CloseParen, true);
                    return(nested);
                }
            }

            return(binaryExpression);
        }