public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken);

            ForEachStatementSyntax forEachStatement = root
                                                      .FindNode(context.Span, getInnermostNodeForTie: true)?
                                                      .FirstAncestorOrSelf <ForEachStatementSyntax>();

            if (forEachStatement == null)
            {
                return;
            }

            SemanticModel semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken);

            if (semanticModel == null)
            {
                return;
            }

            ChangeType(context, semanticModel, forEachStatement);

            RenameIdentifierNameAccordingToTypeName(context, semanticModel, forEachStatement);

            ChangeTypeAccordingToExpression(context, semanticModel, forEachStatement);

            if (ForEachToForRefactoring.CanRefactor(forEachStatement, semanticModel, context.CancellationToken))
            {
                context.RegisterRefactoring(
                    "Convert foreach to for",
                    cancellationToken => ForEachToForRefactoring.RefactorAsync(context.Document, forEachStatement, cancellationToken));
            }
        }
Example #2
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            ForEachStatementSyntax forEachStatement = root
                                                      .FindNode(context.Span, getInnermostNodeForTie: true)?
                                                      .FirstAncestorOrSelf <ForEachStatementSyntax>();

            if (forEachStatement == null)
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Convert foreach to for",
                cancellationToken => ForEachToForRefactoring.RefactorAsync(context.Document, forEachStatement, cancellationToken),
                DiagnosticIdentifiers.ConvertForEachToFor + EquivalenceKeySuffix);

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }