public static Task <Document> RefactorAsync(
            Document document,
            ForStatementSyntax forStatement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            VariableDeclarationSyntax declaration = forStatement.Declaration;

            var incrementor = (PostfixUnaryExpressionSyntax)forStatement.Incrementors[0];

            VariableDeclarationSyntax newDeclaration = declaration.ReplaceNode(
                declaration.Variables[0].Initializer.Value,
                SubtractExpression(
                    ((BinaryExpressionSyntax)forStatement.Condition).Right,
                    NumericLiteralExpression(1)));

            BinaryExpressionSyntax newCondition = GreaterThanOrEqualExpression(
                ((BinaryExpressionSyntax)forStatement.Condition).Left,
                NumericLiteralExpression(0));

            SeparatedSyntaxList <ExpressionSyntax> newIncrementors = forStatement.Incrementors.Replace(
                incrementor,
                incrementor.WithOperatorToken(Token(SyntaxKind.MinusMinusToken)));

            ForStatementSyntax newForStatement = forStatement
                                                 .WithDeclaration(newDeclaration)
                                                 .WithCondition(newCondition)
                                                 .WithIncrementors(newIncrementors);

            return(document.ReplaceNodeAsync(forStatement, newForStatement, cancellationToken));
        }
        public static Task <Document> RefactorAsync(
            Document document,
            ForStatementSyntax forStatement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            VariableDeclarationSyntax declaration = forStatement.Declaration;

            var incrementor = (PostfixUnaryExpressionSyntax)forStatement.Incrementors[0];

            var initializerValue = (BinaryExpressionSyntax)declaration.Variables[0].Initializer.Value;

            VariableDeclarationSyntax newDeclaration = declaration.ReplaceNode(
                initializerValue,
                NumericLiteralExpression(0));

            BinaryExpressionSyntax newCondition = ((BinaryExpressionSyntax)forStatement.Condition)
                                                  .WithOperatorToken(LessThanToken())
                                                  .WithRight(initializerValue.Left);

            SeparatedSyntaxList <ExpressionSyntax> newIncrementors = forStatement.Incrementors.Replace(
                incrementor,
                incrementor.WithOperatorToken(PlusPlusToken()));

            ForStatementSyntax newForStatement = forStatement
                                                 .WithDeclaration(newDeclaration)
                                                 .WithCondition(newCondition)
                                                 .WithIncrementors(newIncrementors);

            return(document.ReplaceNodeAsync(forStatement, newForStatement, cancellationToken));
        }
        private static void ChangeTypeAndAddAwait(
            CodeFixContext context,
            Diagnostic diagnostic,
            ExpressionSyntax expression,
            VariableDeclarationSyntax variableDeclaration,
            TypeSyntax type,
            ITypeSymbol newTypeSymbol,
            SemanticModel semanticModel)
        {
            AwaitExpressionSyntax newExpression = SyntaxFactory.AwaitExpression(expression).WithTriviaFrom(expression);

            VariableDeclarationSyntax newNode = variableDeclaration.ReplaceNode(expression, newExpression);

            TypeSyntax newType = newTypeSymbol.ToMinimalTypeSyntax(semanticModel, type.SpanStart).WithTriviaFrom(type);

            newNode = newNode.WithType(newType);

            string typeName = SymbolDisplay.ToMinimalDisplayString(newTypeSymbol, semanticModel, type.SpanStart, SymbolDisplayFormats.Default);

            CodeAction codeAction = CodeAction.Create(
                $"Change type to '{typeName}' and add await",
                cancellationToken => context.Document.ReplaceNodeAsync(variableDeclaration, newNode, cancellationToken),
                EquivalenceKey.Create(diagnostic, CodeFixIdentifiers.ChangeTypeAccordingToInitializer, "AddAwait"));

            context.RegisterCodeFix(codeAction, diagnostic);
        }
Пример #4
0
        public static Task <Document> ChangeTypeAndAddAwaitAsync(
            Document document,
            VariableDeclarationSyntax variableDeclaration,
            VariableDeclaratorSyntax variableDeclarator,
            SyntaxNode containingDeclaration,
            ITypeSymbol newTypeSymbol,
            CancellationToken cancellationToken)
        {
            TypeSyntax type = variableDeclaration.Type;

            ExpressionSyntax value = variableDeclarator.Initializer.Value;

            AwaitExpressionSyntax newValue = AwaitExpression(value.WithoutTrivia()).WithTriviaFrom(value);

            TypeSyntax newType = ChangeType(type, newTypeSymbol);

            VariableDeclarationSyntax newVariableDeclaration = variableDeclaration
                                                               .ReplaceNode(value, newValue)
                                                               .WithType(newType);

            if (!SyntaxInfo.ModifierListInfo(containingDeclaration).IsAsync)
            {
                SyntaxNode newDeclaration = containingDeclaration
                                            .ReplaceNode(variableDeclaration, newVariableDeclaration)
                                            .InsertModifier(SyntaxKind.AsyncKeyword);

                return(document.ReplaceNodeAsync(containingDeclaration, newDeclaration, cancellationToken));
            }

            return(document.ReplaceNodeAsync(variableDeclaration, newVariableDeclaration, cancellationToken));
        }
        private static async Task <Document> ChangeTypeAndAddAwaitAsync(
            Document document,
            VariableDeclarationSyntax variableDeclaration,
            ITypeSymbol typeSymbol,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            ExpressionSyntax initializerValue = variableDeclaration.Variables[0].Initializer.Value;

            AwaitExpressionSyntax newInitializerValue = SyntaxFactory.AwaitExpression(initializerValue)
                                                        .WithTriviaFrom(initializerValue);

            VariableDeclarationSyntax newNode = variableDeclaration.ReplaceNode(initializerValue, newInitializerValue);

            newNode = newNode
                      .WithType(
                CSharpFactory.Type(typeSymbol)
                .WithTriviaFrom(variableDeclaration.Type)
                .WithSimplifierAnnotation());

            SyntaxNode newRoot = oldRoot.ReplaceNode(variableDeclaration, newNode);

            return(document.WithSyntaxRoot(newRoot));
        }
        public static async Task <Document> RefactorAsync(
            Document document,
            ForStatementSyntax forStatement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            VariableDeclarationSyntax declaration = forStatement.Declaration;

            var incrementor = (PostfixUnaryExpressionSyntax)forStatement.Incrementors[0];

            var initializerValue = (BinaryExpressionSyntax)declaration.Variables[0].Initializer.Value;

            VariableDeclarationSyntax newDeclaration = declaration.ReplaceNode(
                initializerValue,
                ZeroLiteralExpression());

            BinaryExpressionSyntax newCondition = ((BinaryExpressionSyntax)forStatement.Condition)
                                                  .WithOperatorToken(Token(SyntaxKind.LessThanToken))
                                                  .WithRight(initializerValue.Left);

            SeparatedSyntaxList <ExpressionSyntax> newIncrementors = forStatement.Incrementors.Replace(
                incrementor,
                incrementor.WithOperatorToken(Token(SyntaxKind.PlusPlusToken)));

            ForStatementSyntax newForStatement = forStatement
                                                 .WithDeclaration(newDeclaration)
                                                 .WithCondition(newCondition)
                                                 .WithIncrementors(newIncrementors);

            root = root.ReplaceNode(forStatement, newForStatement);

            return(document.WithSyntaxRoot(root));
        }
Пример #7
0
        private static Task <Document> ChangeTypeAndAddAwait(
            Document document,
            SyntaxNode declaration,
            VariableDeclarationSyntax variableDeclaration,
            TypeSyntax type,
            ExpressionSyntax expression,
            ITypeSymbol newTypeSymbol,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            AwaitExpressionSyntax newExpression = SyntaxFactory.AwaitExpression(expression).WithTriviaFrom(expression);

            VariableDeclarationSyntax newVariableDeclaration = variableDeclaration.ReplaceNode(expression, newExpression);

            TypeSyntax newType = newTypeSymbol.ToMinimalTypeSyntax(semanticModel, type.SpanStart).WithTriviaFrom(type);

            newVariableDeclaration = newVariableDeclaration.WithType(newType);

            if (!SyntaxInfo.ModifierListInfo(declaration).IsAsync)
            {
                SyntaxNode newDeclaration = declaration
                                            .ReplaceNode(variableDeclaration, newVariableDeclaration)
                                            .InsertModifier(SyntaxKind.AsyncKeyword);

                return(document.ReplaceNodeAsync(declaration, newDeclaration, cancellationToken));
            }

            return(document.ReplaceNodeAsync(variableDeclaration, newVariableDeclaration, cancellationToken));
        }
Пример #8
0
        public static VariableDeclarationSyntax ChangeTypeAndAddAwait(
            VariableDeclarationSyntax variableDeclaration,
            VariableDeclaratorSyntax variableDeclarator,
            ITypeSymbol typeSymbol)
        {
            TypeSyntax type = variableDeclaration.Type;

            ExpressionSyntax value = variableDeclarator.Initializer.Value;

            AwaitExpressionSyntax newValue = AwaitExpression(value.WithoutTrivia()).WithTriviaFrom(value);

            TypeSyntax newType = ChangeType(type, typeSymbol);

            return(variableDeclaration
                   .ReplaceNode(value, newValue)
                   .WithType(newType));
        }
        private static async Task <Document> ChangeTypeAndAddAwaitAsync(
            Document document,
            VariableDeclarationSyntax variableDeclaration,
            ITypeSymbol typeSymbol,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            TypeSyntax type = variableDeclaration.Type;

            ExpressionSyntax value = variableDeclaration.Variables[0].Initializer.Value;

            AwaitExpressionSyntax newInitializerValue = SyntaxFactory.AwaitExpression(value)
                                                        .WithTriviaFrom(value);

            VariableDeclarationSyntax newNode = variableDeclaration.ReplaceNode(value, newInitializerValue);

            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            newNode = newNode.WithType(
                typeSymbol.ToMinimalTypeSyntax(semanticModel, type.SpanStart).WithTriviaFrom(type));

            return(await document.ReplaceNodeAsync(variableDeclaration, newNode, cancellationToken).ConfigureAwait(false));
        }
        public static async Task <Document> RefactorAsync(
            Document document,
            ForStatementSyntax forStatement,
            CancellationToken cancellationToken)
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken);

            VariableDeclarationSyntax declaration = forStatement.Declaration;

            var incrementor = (PostfixUnaryExpressionSyntax)forStatement.Incrementors[0];

            VariableDeclarationSyntax newDeclaration = declaration.ReplaceNode(
                declaration.Variables[0].Initializer.Value,
                BinaryExpression(
                    SyntaxKind.SubtractExpression,
                    ((BinaryExpressionSyntax)forStatement.Condition).Right,
                    LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(1))));

            BinaryExpressionSyntax newCondition = BinaryExpression(
                SyntaxKind.GreaterThanOrEqualExpression,
                ((BinaryExpressionSyntax)forStatement.Condition).Left,
                LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)));

            SeparatedSyntaxList <ExpressionSyntax> newIncrementors = forStatement.Incrementors.Replace(
                incrementor,
                incrementor.WithOperatorToken(Token(SyntaxKind.MinusMinusToken)));

            ForStatementSyntax newForStatement = forStatement
                                                 .WithDeclaration(newDeclaration)
                                                 .WithCondition(newCondition)
                                                 .WithIncrementors(newIncrementors);

            SyntaxNode newRoot = oldRoot.ReplaceNode(forStatement, newForStatement);

            return(document.WithSyntaxRoot(newRoot));
        }