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

            DoStatementSyntax doStatement = DoStatement(
                Token(
                    whileStatement.WhileKeyword.LeadingTrivia,
                    SyntaxKind.DoKeyword,
                    whileStatement.CloseParenToken.TrailingTrivia),
                whileStatement.Statement.WithoutTrailingTrivia(),
                Token(SyntaxKind.WhileKeyword),
                whileStatement.OpenParenToken,
                whileStatement.Condition,
                whileStatement.CloseParenToken.WithoutTrailingTrivia(),
                SemicolonToken());

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

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

            return(document.WithSyntaxRoot(newRoot));
        }
Пример #2
0
        private static Task <Document> ConvertWhileToDoAsync(
            Document document,
            WhileStatementSyntax whileStatement,
            bool omitIfStatement = false,
            CancellationToken cancellationToken = default)
        {
            if (omitIfStatement)
            {
                DoStatementSyntax doStatement = DoStatement(
                    Token(
                        whileStatement.WhileKeyword.LeadingTrivia,
                        SyntaxKind.DoKeyword,
                        whileStatement.CloseParenToken.TrailingTrivia),
                    whileStatement.Statement.WithoutTrailingTrivia(),
                    Token(SyntaxKind.WhileKeyword),
                    whileStatement.OpenParenToken,
                    whileStatement.Condition,
                    whileStatement.CloseParenToken.WithoutTrailingTrivia(),
                    SemicolonToken());

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

                return(document.ReplaceNodeAsync(whileStatement, doStatement, cancellationToken));
            }
            else
            {
                DoStatementSyntax doStatement = DoStatement(
                    Token(SyntaxKind.DoKeyword),
                    whileStatement.Statement.WithoutTrailingTrivia(),
                    Token(SyntaxKind.WhileKeyword),
                    OpenParenToken(),
                    whileStatement.Condition,
                    CloseParenToken(),
                    SemicolonToken());

                IfStatementSyntax ifStatement = IfStatement(
                    Token(whileStatement.WhileKeyword.LeadingTrivia, SyntaxKind.IfKeyword, TriviaList()),
                    OpenParenToken(),
                    whileStatement.Condition,
                    CloseParenToken(),
                    Block(OpenBraceToken(), doStatement, Token(TriviaList(), SyntaxKind.CloseBraceToken, whileStatement.Statement.GetTrailingTrivia())),
                    default(ElseClauseSyntax));

                ifStatement = ifStatement.WithFormatterAnnotation();

                return(document.ReplaceNodeAsync(whileStatement, ifStatement, cancellationToken));
            }
        }
Пример #3
0
        public static Task <Document> RefactorAsync(
            Document document,
            WhileStatementSyntax whileStatement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            DoStatementSyntax doStatement = DoStatement(
                Token(
                    whileStatement.WhileKeyword.LeadingTrivia,
                    SyntaxKind.DoKeyword,
                    whileStatement.CloseParenToken.TrailingTrivia),
                whileStatement.Statement.WithoutTrailingTrivia(),
                Token(SyntaxKind.WhileKeyword),
                whileStatement.OpenParenToken,
                whileStatement.Condition,
                whileStatement.CloseParenToken.WithoutTrailingTrivia(),
                SemicolonToken());

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

            return(document.ReplaceNodeAsync(whileStatement, doStatement, cancellationToken));
        }