コード例 #1
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SwitchStatementSyntax switchStatement)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.GenerateSwitchSections) &&
                await GenerateSwitchSectionsRefactoring.CanRefactorAsync(context, switchStatement).ConfigureAwait(false))
            {
                context.RegisterRefactoring(
                    "Generate sections",
                    cancellationToken =>
                {
                    return(GenerateSwitchSectionsRefactoring.RefactorAsync(
                               context.Document,
                               switchStatement,
                               cancellationToken));
                });
            }

            SelectedSwitchSectionsRefactoring.ComputeRefactorings(context, switchStatement);

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceSwitchWithIfElse) &&
                (switchStatement.SwitchKeyword.Span.Contains(context.Span) || context.Span.IsBetweenSpans(switchStatement)) &&
                switchStatement
                .Sections
                .Any(section => !section.Labels.Contains(SyntaxKind.DefaultSwitchLabel)))
            {
                context.RegisterRefactoring(
                    "Replace switch with if-else",
                    cancellationToken => ReplaceSwitchWithIfElseRefactoring.RefactorAsync(context.Document, switchStatement, cancellationToken));
            }
        }
コード例 #2
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SwitchStatementSyntax switchStatement)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.GenerateSwitchSections))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                if (GenerateSwitchSectionsRefactoring.CanRefactor(switchStatement, semanticModel, context.CancellationToken))
                {
                    context.RegisterRefactoring(
                        "Generate sections",
                        cancellationToken => GenerateSwitchSectionsRefactoring.RefactorAsync(context.Document, switchStatement, cancellationToken));
                }
            }

            SelectedSwitchSectionsRefactoring.ComputeRefactorings(context, switchStatement);

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceSwitchWithIfElse))
            {
                TextSpan span = context.Span;

                if (span.IsEmptyAndContainedInSpan(switchStatement.SwitchKeyword) ||
                    span.IsBetweenSpans(switchStatement))
                {
                    ReplaceSwitchWithIfElseRefactoring.ComputeRefactoring(context, switchStatement);
                }
            }
        }
コード例 #3
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SwitchStatementSyntax switchStatement)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.GenerateSwitchSections) &&
                await GenerateSwitchSectionsRefactoring.CanRefactorAsync(context, switchStatement).ConfigureAwait(false))
            {
                context.RegisterRefactoring(
                    "Generate sections",
                    cancellationToken => GenerateSwitchSectionsRefactoring.RefactorAsync(context.Document, switchStatement, cancellationToken));
            }

            SelectedSwitchSectionsRefactoring.ComputeRefactorings(context, switchStatement);

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceSwitchWithIfElse) &&
                context.Span.IsBetweenSpans(switchStatement))
            {
                ReplaceSwitchWithIfElseRefactoring.ComputeRefactoring(context, switchStatement);
            }
        }