Ejemplo n.º 1
0
        private static async Task <Document> RefactorAsync(
            Document document,
            ElseClauseSyntax elseClause,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var ifStatement = (IfStatementSyntax)elseClause.Statement;

            ElseClauseSyntax newElseClause = elseClause
                                             .WithElseKeyword(
                elseClause.ElseKeyword
                .WithTrailingTrivia(ifStatement.CloseParenToken.TrailingTrivia))
                                             .WithStatement(ifStatement.Statement);

            return(await document.ReplaceNodeAsync(elseClause, newElseClause, cancellationToken).ConfigureAwait(false));
        }
Ejemplo n.º 2
0
        private static async Task<Document> RemoveConditionFromLastElseAsync(
            Document document,
            ElseClauseSyntax elseClause,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var ifStatement = (IfStatementSyntax)elseClause.Statement;

            ElseClauseSyntax newElseClause = elseClause
                .WithElseKeyword(
                    elseClause.ElseKeyword
                        .WithTrailingTrivia(ifStatement.CloseParenToken.TrailingTrivia))
                .WithStatement(ifStatement.Statement);

            SyntaxNode newRoot = oldRoot.ReplaceNode(elseClause, newElseClause);

            return document.WithSyntaxRoot(newRoot);
        }
            ElseClauseSyntax GetNewIfWithElse(ElseClauseSyntax elseNode, StatementSyntax singleStatementInsideElse)
            {
                var newElse =
                    elseNode
                    .WithElseKeyword(
                        elseNode.ElseKeyword
                        .WithTrailingTrivia()
                        )
                    .WithStatement(
                        singleStatementInsideElse
                        .WithLeadingTrivia(SyntaxFactory.Space)
                        //.WithTrailingTrivia(trailingTriviaList)
                        );

                if (singleStatementInsideElse is ReturnStatementSyntax returnStatement)
                {
                    if (returnStatement.Expression == null || returnStatement.Expression is LiteralExpressionSyntax || returnStatement.Expression.Span.Length <= MAX_RETURN_STATEMENT_LENGTH)
                    {
                        return(newElse);
                    }
                }

                if (singleStatementInsideElse is IfStatementSyntax == false)
                {
                    if (
                        singleStatementInsideElse.WithoutTrivia().DescendantTrivia().Any(t => t.IsKind(SyntaxKind.EndOfLineTrivia))
                        ||
                        singleStatementInsideElse.Span.Length + 5 > MAX_IF_LINE_LENGTH
                        )
                    {
                        return(elseNode.WithStatement(singleStatementInsideElse));
                    }
                }

                return(newElse);
            }