예제 #1
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ForStatementSyntax forStatement)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceForWithForEach) &&
                context.SupportsSemanticModel &&
                context.Span.IsEmpty &&
                forStatement.Span.Contains(context.Span) &&
                (await ReplaceForWithForEachRefactoring.CanRefactorAsync(context, forStatement).ConfigureAwait(false)))
            {
                context.RegisterRefactoring(
                    "Replace for with foreach",
                    cancellationToken => ReplaceForWithForEachRefactoring.RefactorAsync(context.Document, forStatement, cancellationToken));
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReverseForLoop) &&
                context.Span.IsEmpty &&
                forStatement.Span.Contains(context.Span))
            {
                if (ReverseForLoopRefactoring.CanRefactor(forStatement))
                {
                    context.RegisterRefactoring(
                        "Reverse for loop",
                        cancellationToken => ReverseForLoopRefactoring.RefactorAsync(context.Document, forStatement, cancellationToken));
                }
                else if (ReverseReversedForLoopRefactoring.CanRefactor(forStatement))
                {
                    context.RegisterRefactoring(
                        "Reverse for loop",
                        cancellationToken => ReverseReversedForLoopRefactoring.RefactorAsync(context.Document, forStatement, cancellationToken));
                }
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ForStatementSyntax forStatement)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ConvertForToForEach) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(forStatement) &&
                (await ConvertForToForEachRefactoring.CanRefactorAsync(context, forStatement).ConfigureAwait(false)))
            {
                context.RegisterRefactoring(
                    "Convert to 'foreach'",
                    cancellationToken => ConvertForToForEachRefactoring.RefactorAsync(context.Document, forStatement, cancellationToken),
                    RefactoringIdentifiers.ConvertForToForEach);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ConvertForToWhile) &&
                (context.Span.IsEmptyAndContainedInSpan(forStatement.ForKeyword) || context.Span.IsBetweenSpans(forStatement)))
            {
                context.RegisterRefactoring(
                    "Convert to 'while'",
                    cancellationToken => ConvertForToWhileRefactoring.RefactorAsync(context.Document, forStatement, cancellationToken),
                    RefactoringIdentifiers.ConvertForToWhile);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReverseForLoop) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(forStatement))
            {
                if (ReverseForLoopRefactoring.CanRefactor(forStatement))
                {
                    context.RegisterRefactoring(
                        "Reverse 'for' loop",
                        cancellationToken => ReverseForLoopRefactoring.RefactorAsync(context.Document, forStatement, cancellationToken),
                        RefactoringIdentifiers.ReverseForLoop);
                }
                else if (ReverseReversedForLoopRefactoring.CanRefactor(forStatement))
                {
                    context.RegisterRefactoring(
                        "Reverse 'for' loop",
                        cancellationToken => ReverseReversedForLoopRefactoring.RefactorAsync(context.Document, forStatement, cancellationToken),
                        RefactoringIdentifiers.ReverseForLoop);
                }
            }
        }