public static Task <Document> RefactorAsync(
            Document document,
            ObjectCreationExpressionSyntax objectCreation,
            StatementListSelection selectedStatements,
            CancellationToken cancellationToken = default)
        {
            ExpressionStatementSyntax[] expressionStatements = selectedStatements
                                                               .Skip(1)
                                                               .Cast <ExpressionStatementSyntax>()
                                                               .ToArray();

            StatementSyntax firstStatement = selectedStatements.First();

            SyntaxList <StatementSyntax> newStatements = selectedStatements.UnderlyingList.Replace(
                firstStatement,
                firstStatement.ReplaceNode(
                    objectCreation,
                    objectCreation.WithInitializer(CreateInitializer(objectCreation, expressionStatements))));

            int count = expressionStatements.Length;
            int index = selectedStatements.FirstIndex + 1;

            while (count > 0)
            {
                newStatements = newStatements.RemoveAt(index);
                count--;
            }

            return(document.ReplaceStatementsAsync(SyntaxInfo.StatementListInfo(selectedStatements), newStatements, cancellationToken));
        }
Ejemplo n.º 2
0
        private static Task <Document> RefactorAsync(
            Document document,
            StatementListSelection selectedStatements,
            int ifStatementCount,
            CancellationToken cancellationToken)
        {
            IfStatementSyntax newIfStatement = null;

            for (int i = ifStatementCount - 1; i >= 0; i--)
            {
                var ifStatement = (IfStatementSyntax)selectedStatements[i];

                IfStatementSyntax lastIf = ifStatement.GetCascadeInfo().Last.AsIf();

                StatementSyntax elseStatement = newIfStatement;

                if (elseStatement == null)
                {
                    if (selectedStatements.Count - ifStatementCount > 1)
                    {
                        elseStatement = Block(selectedStatements.Skip(ifStatementCount));
                    }
                    else
                    {
                        elseStatement = selectedStatements.Last();

                        if (!elseStatement.IsKind(SyntaxKind.IfStatement) &&
                            lastIf.Statement.IsKind(SyntaxKind.Block))
                        {
                            elseStatement = Block(elseStatement);
                        }
                    }
                }

                StatementSyntax newStatement = lastIf.Statement;

                if (!newStatement.IsKind(SyntaxKind.Block))
                {
                    if (elseStatement.IsKind(SyntaxKind.Block))
                    {
                        newStatement = Block(newStatement);
                    }
                    else if (elseStatement.IsKind(SyntaxKind.IfStatement) &&
                             ((IfStatementSyntax)elseStatement).AsCascade().All(f => f.Statement.IsKind(SyntaxKind.Block)))
                    {
                        newStatement = Block(newStatement);
                    }
                }

                IfStatementSyntax newLastIf = lastIf.Update(
                    lastIf.IfKeyword,
                    lastIf.OpenParenToken,
                    lastIf.Condition,
                    lastIf.CloseParenToken,
                    newStatement,
                    ElseClause(elseStatement));

                newIfStatement = ifStatement.ReplaceNode(lastIf, newLastIf);
            }

            SyntaxList <StatementSyntax> newStatements = selectedStatements.UnderlyingList
                                                         .Replace(selectedStatements.First(), newIfStatement.WithFormatterAnnotation())
                                                         .RemoveRange(selectedStatements.FirstIndex + 1, selectedStatements.Count - 1);

            return(document.ReplaceStatementsAsync(SyntaxInfo.StatementListInfo(selectedStatements), newStatements, cancellationToken));
        }