public static Task <Document> RefactorAsync(
            Document document,
            ForStatementSyntax forStatement,
            CancellationToken cancellationToken)
        {
            LiteralExpressionSyntax trueLiteral = TrueLiteralExpression();

            TextSpan span = TextSpan.FromBounds(
                forStatement.OpenParenToken.FullSpan.End,
                forStatement.CloseParenToken.FullSpan.Start);

            IEnumerable <SyntaxTrivia> trivia = forStatement.DescendantTrivia(span);

            if (!trivia.All(f => f.IsWhitespaceOrEndOfLineTrivia()))
            {
                trueLiteral = trueLiteral.WithTrailingTrivia(trivia);
            }

            WhileStatementSyntax whileStatement = WhileStatement(
                WhileKeyword().WithTriviaFrom(forStatement.ForKeyword),
                forStatement.OpenParenToken,
                trueLiteral,
                forStatement.CloseParenToken,
                forStatement.Statement);

            whileStatement = whileStatement
                             .WithTriviaFrom(forStatement)
                             .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(forStatement, whileStatement, cancellationToken));
        }
        private static async Task <Document> ConvertForStatementToWhileStatementAsync(
            Document document,
            ForStatementSyntax forStatement,
            CancellationToken cancellationToken)
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            WhileStatementSyntax newNode = WhileStatement(
                Token(SyntaxKind.WhileKeyword)
                .WithTriviaFrom(forStatement.ForKeyword),
                Token(
                    forStatement.OpenParenToken.LeadingTrivia,
                    SyntaxKind.OpenParenToken,
                    default(SyntaxTriviaList)),
                LiteralExpression(SyntaxKind.TrueLiteralExpression),
                Token(
                    default(SyntaxTriviaList),
                    SyntaxKind.CloseParenToken,
                    forStatement.CloseParenToken.TrailingTrivia),
                forStatement.Statement);

            newNode = newNode
                      .WithTriviaFrom(forStatement)
                      .WithFormatterAnnotation();

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

            return(document.WithSyntaxRoot(newRoot));
        }
        public static async Task <Document> ConvertToWhileStatementAsync(
            Document document,
            DoStatementSyntax doStatement,
            CancellationToken cancellationToken)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (doStatement == null)
            {
                throw new ArgumentNullException(nameof(doStatement));
            }

            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken);

            WhileStatementSyntax newNode = WhileStatement(
                Token(SyntaxKind.WhileKeyword),
                Token(SyntaxKind.OpenParenToken),
                LiteralExpression(SyntaxKind.TrueLiteralExpression),
                Token(
                    default(SyntaxTriviaList),
                    SyntaxKind.CloseParenToken,
                    doStatement.DoKeyword.TrailingTrivia),
                doStatement.Statement);

            newNode = newNode
                      .WithTriviaFrom(doStatement)
                      .WithAdditionalAnnotations(Formatter.Annotation);

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

            return(document.WithSyntaxRoot(newRoot));
        }
        public static Task <Document> RefactorAsync(
            Document document,
            DoStatementSyntax doStatement,
            CancellationToken cancellationToken = default)
        {
            WhileStatementSyntax whileStatement = WhileStatement(
                doStatement.WhileKeyword,
                doStatement.OpenParenToken,
                doStatement.Condition,
                doStatement.CloseParenToken.WithTrailingTrivia(doStatement.DoKeyword.TrailingTrivia),
                doStatement.Statement);

            whileStatement = whileStatement
                             .WithTriviaFrom(doStatement)
                             .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(doStatement, whileStatement, cancellationToken));
        }
Пример #5
0
        public static async Task <Document> RefactorAsync(
            Document document,
            DoStatementSyntax doStatement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            WhileStatementSyntax whileStatement = WhileStatement(
                doStatement.WhileKeyword,
                doStatement.OpenParenToken,
                doStatement.Condition,
                doStatement.CloseParenToken.WithTrailingTrivia(doStatement.DoKeyword.TrailingTrivia),
                doStatement.Statement);

            whileStatement = whileStatement
                             .WithTriviaFrom(doStatement)
                             .WithFormatterAnnotation();

            SyntaxNode newRoot = oldRoot.ReplaceNode(doStatement, whileStatement);

            return(document.WithSyntaxRoot(newRoot));
        }