Пример #1
0
        private async Task <Document> InsertConfigureAwaitAsync(Document document, PrefixUnaryExpressionSyntax oldAwaitExpr, bool arg, CancellationToken cancellationToken)
        {
            var configureAwait = SyntaxFactory.IdentifierName("ConfigureAwait");

            var oldOperand = oldAwaitExpr.Operand;
            var newOperand =
                SyntaxFactory.InvocationExpression(
                    SyntaxFactory.MemberAccessExpression(
                        SyntaxKind.SimpleMemberAccessExpression,
                        oldOperand,
                        configureAwait
                        ),
                    SyntaxFactory.ArgumentList(
                        SyntaxFactory.Token(SyntaxKind.OpenParenToken),
                        SyntaxFactory.SeparatedList(new[]
            {
                SyntaxFactory.Argument(
                    SyntaxFactory.LiteralExpression(arg ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression)
                    )
            }),
                        SyntaxFactory.Token(SyntaxKind.CloseParenToken)
                        )
                    );

            var newAwaitExpr = oldAwaitExpr.WithOperand(newOperand);

            var oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var newRoot = oldRoot.ReplaceNode(oldAwaitExpr, newAwaitExpr);

            return(document.WithSyntaxRoot(newRoot));
        }
Пример #2
0
        private ExpressionSyntax DifferentiateExpression(
            PrefixUnaryExpressionSyntax expression, SyntaxToken x)
        {
            var innerDerivative = DifferentiateExpression(expression.Operand, x);

            switch (expression.Kind())
            {
            case SyntaxKind.UnaryPlusExpression:
            case SyntaxKind.UnaryMinusExpression:
                return(expression.WithOperand(innerDerivative));

            default:
                throw new ArgumentException($"Unexpected syntax kind: {expression.Kind()}");
            }
        }
Пример #3
0
        public override SyntaxNode VisitPrefixUnaryExpression(PrefixUnaryExpressionSyntax node)
        {
            if (node.Kind() != SyntaxKind.LogicalNotExpression)
            {
                throw new InvalidPreprocessorExpressionException("Expected logical not expression");
            }

            var newExpression = (ExpressionSyntax)node.Operand.Accept(this);

            if (newExpression.Kind() == SyntaxKind.LogicalNotExpression)
            {
                return(((PrefixUnaryExpressionSyntax)newExpression).Operand
                       .WithLeadingTrivia(node.GetLeadingTrivia())
                       .WithTrailingTrivia(node.GetTrailingTrivia()));
            }
            else
            {
                return(node.Operand != newExpression?
                       node.WithOperand(newExpression) : node);
            }
        }
        public override SyntaxNode VisitPrefixUnaryExpression(PrefixUnaryExpressionSyntax node)
        {
            if (node.Kind() != SyntaxKind.LogicalNotExpression)
            {
                throw new InvalidPreprocessorExpressionException("Expected logical not expression");
            }

            var newExpression = (ExpressionSyntax)node.Operand.Accept(this);
            if (newExpression.Kind() == SyntaxKind.LogicalNotExpression)
            {
                return ((PrefixUnaryExpressionSyntax)newExpression).Operand
                    .WithLeadingTrivia(node.GetLeadingTrivia())
                    .WithTrailingTrivia(node.GetTrailingTrivia());
            }
            else
            {
                return node.Operand != newExpression ?
                    node.WithOperand(newExpression) : node;
            }
        }