Esempio n. 1
0
        private static Task <Document> AddCallToConfigureAwaitRefactorAsync(
            Document document,
            AwaitExpressionSyntax awaitExpression,
            CancellationToken cancellationToken)
        {
            ExpressionSyntax expression = awaitExpression.Expression;

            SyntaxTriviaList leading = default;

            switch (expression.Kind())
            {
            case SyntaxKind.SimpleMemberAccessExpression:
            {
                var memberAccess = (MemberAccessExpressionSyntax)expression;

                leading = memberAccess.OperatorToken.LeadingTrivia;
                break;
            }

            case SyntaxKind.InvocationExpression:
            {
                var invocation = (InvocationExpressionSyntax)expression;

                SimpleMemberInvocationExpressionInfo invocationInfo = SyntaxInfo.SimpleMemberInvocationExpressionInfo(invocation);

                leading = invocationInfo.OperatorToken.LeadingTrivia;
                break;
            }
            }

            SyntaxTrivia last = leading.LastOrDefault();

            last = (last.IsWhitespaceTrivia()) ? last : default;

            ParenthesizedExpressionSyntax expression2 = expression.WithoutTrailingTrivia().Parenthesize();

            if (last.IsWhitespaceTrivia())
            {
                expression2 = expression2.WithTrailingTrivia(SyntaxTriviaAnalysis.DetermineEndOfLine(awaitExpression));
            }

            InvocationExpressionSyntax newInvocationExpression = InvocationExpression(
                SimpleMemberAccessExpression(
                    expression2,
                    Token(SyntaxKind.DotToken).WithLeadingTrivia(last),
                    IdentifierName("ConfigureAwait")),
                ArgumentList(
                    Token(SyntaxKind.OpenParenToken),
                    SingletonSeparatedList(Argument(FalseLiteralExpression())),
                    Token(default, SyntaxKind.CloseParenToken, expression.GetTrailingTrivia())));
Esempio n. 2
0
        private static ConditionalExpressionSyntax CreateSinglelineConditionalExpression(ConditionalExpressionSyntax conditionalExpression)
        {
            ParenthesizedExpressionSyntax condition = null;

            if (conditionalExpression.Condition.IsKind(SyntaxKind.ParenthesizedExpression))
            {
                condition = (ParenthesizedExpressionSyntax)conditionalExpression.Condition;
            }
            else
            {
                condition = ParenthesizedExpression(conditionalExpression.Condition.WithoutTrailingTrivia())
                            .WithCloseParenToken(CreateTokenWithTrailingNewLine(SyntaxKind.CloseParenToken));
            }

            return(ConditionalExpression(
                       condition.WithTrailingTrivia(Space),
                       conditionalExpression.WhenTrue.WithTrailingTrivia(Space),
                       conditionalExpression.WhenFalse.WithoutTrailingTrivia())
                   .WithQuestionToken(Token(SyntaxKind.QuestionToken).WithTrailingSpace())
                   .WithColonToken(Token(SyntaxKind.ColonToken).WithTrailingSpace()));
        }