Ejemplo n.º 1
0
        public static async Task ComputeRefactoringAsync(RefactoringContext context, StatementListSelection selectedStatements)
        {
            if (!(selectedStatements.Last() is WhileStatementSyntax whileStatement))
            {
                return;
            }

            if (selectedStatements.Count == 1)
            {
                context.RegisterRefactoring(
                    Title,
                    cancellationToken => RefactorAsync(context.Document, whileStatement, cancellationToken),
                    RefactoringIdentifiers.ConvertWhileToFor);
            }
            else
            {
                SyntaxKind kind = selectedStatements.First().Kind();

                if (kind == SyntaxKind.LocalDeclarationStatement)
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    if (FindLocalDeclarationStatementIndex(
                            whileStatement,
                            selectedStatements.UnderlyingList,
                            selectedStatements.FirstIndex,
                            selectedStatements.Count,
                            mustBeReferencedInsideWhileStatement: false,
                            semanticModel: semanticModel,
                            cancellationToken: context.CancellationToken) == selectedStatements.FirstIndex)
                    {
                        List <LocalDeclarationStatementSyntax> localDeclarations = selectedStatements
                                                                                   .Take(selectedStatements.Count - 1)
                                                                                   .Cast <LocalDeclarationStatementSyntax>()
                                                                                   .ToList();

                        context.RegisterRefactoring(
                            Title,
                            cancellationToken => RefactorAsync(context.Document, whileStatement, localDeclarations, cancellationToken),
                            RefactoringIdentifiers.ConvertWhileToFor);
                    }
                }
                else if (kind == SyntaxKind.ExpressionStatement)
                {
                    if (VerifyExpressionStatements(selectedStatements))
                    {
                        List <ExpressionStatementSyntax> expressionStatements = selectedStatements
                                                                                .Take(selectedStatements.Count - 1)
                                                                                .Cast <ExpressionStatementSyntax>()
                                                                                .ToList();

                        context.RegisterRefactoring(
                            Title,
                            cancellationToken => RefactorAsync(context.Document, whileStatement, expressionStatements, cancellationToken),
                            RefactoringIdentifiers.ConvertWhileToFor);
                    }
                }
            }
        }
        public static async Task ComputeRefactoringAsync(RefactoringContext context, StatementListSelection selectedStatements)
        {
            if (!(selectedStatements.Last() is WhileStatementSyntax whileStatement))
            {
                return;
            }

            if (selectedStatements.Count == 1)
            {
                context.RegisterRefactoring(
                    Title,
                    cancellationToken => RefactorAsync(context.Document, whileStatement, cancellationToken));
            }
            else
            {
                SyntaxKind kind = selectedStatements.First().Kind();

                if (kind == SyntaxKind.LocalDeclarationStatement)
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    if (VerifyLocalDeclarationStatements(selectedStatements, semanticModel, context.CancellationToken))
                    {
                        List <LocalDeclarationStatementSyntax> localDeclarations = selectedStatements
                                                                                   .Take(selectedStatements.Count - 1)
                                                                                   .Cast <LocalDeclarationStatementSyntax>()
                                                                                   .ToList();

                        context.RegisterRefactoring(
                            Title,
                            cancellationToken => RefactorAsync(context.Document, whileStatement, localDeclarations, cancellationToken));
                    }
                }
                else if (kind == SyntaxKind.ExpressionStatement)
                {
                    if (VerifyExpressionStatements(selectedStatements))
                    {
                        List <ExpressionStatementSyntax> expressionStatements = selectedStatements
                                                                                .Take(selectedStatements.Count - 1)
                                                                                .Cast <ExpressionStatementSyntax>()
                                                                                .ToList();

                        context.RegisterRefactoring(
                            Title,
                            cancellationToken => RefactorAsync(context.Document, whileStatement, expressionStatements, cancellationToken));
                    }
                }
            }
        }