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);
                }
            }
        }
Esempio n. 2
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));
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SwitchStatementSyntax switchStatement)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddMissingCases))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                AddMissingCasesRefactoring.ComputeRefactoring(context, switchStatement, semanticModel);
            }

            SelectedSwitchSectionsRefactoring.ComputeRefactorings(context, switchStatement);

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceSwitchWithIf))
            {
                if (context.Span.IsEmptyAndContainedInSpan(switchStatement.SwitchKeyword) ||
                    context.Span.IsBetweenSpans(switchStatement))
                {
                    ReplaceSwitchWithIfElseRefactoring.ComputeRefactoring(context, switchStatement);
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.DuplicateSwitchSection))
            {
                DuplicateSwitchSectionRefactoring.ComputeRefactoring(context, switchStatement);
            }
        }
        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);
            }
        }