public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out SwitchSectionSyntax switchSection))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.RemoveRedundantDefaultSwitchSection:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Remove redundant switch section",
                        cancellationToken => RemoveRedundantDefaultSwitchSectionRefactoring.RefactorAsync(context.Document, switchSection, cancellationToken),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.DefaultLabelShouldBeLastLabelInSwitchSection:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Move default label to the last position",
                        cancellationToken => DefaultLabelShouldBeLastLabelInSwitchSectionRefactoring.RefactorAsync(context.Document, switchSection, cancellationToken),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.AddBracesToSwitchSectionWithMultipleStatements:
                {
                    CodeAction codeAction = CodeAction.Create(
                        AddBracesToSwitchSectionRefactoring.Title,
                        cancellationToken => AddBracesToSwitchSectionRefactoring.RefactorAsync(context.Document, switchSection, cancellationToken),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.MergeSwitchSectionsWithEquivalentContent:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Merge sections",
                        cancellationToken => MergeSwitchSectionsRefactoring.RefactorAsync(context.Document, switchSection, cancellationToken),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }
Example #2
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

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

            Debug.Assert(switchSection != null, $"{nameof(switchSection)} is null");

            if (switchSection == null)
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.RemoveRedundantDefaultSwitchSection:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Remove redundant switch section",
                        cancellationToken => RemoveRedundantDefaultSwitchSectionRefactoring.RefactorAsync(context.Document, switchSection, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.DefaultLabelShouldBeLastLabelInSwitchSection:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Move default label to the last position",
                        cancellationToken => DefaultLabelShouldBeLastLabelInSwitchSectionRefactoring.RefactorAsync(context.Document, switchSection, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.AddBracesToSwitchSectionWithMultipleStatements:
                {
                    CodeAction codeAction = CodeAction.Create(
                        AddBracesToSwitchSectionRefactoring.Title,
                        cancellationToken => AddBracesToSwitchSectionRefactoring.RefactorAsync(context.Document, switchSection, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.AddBreakStatementToSwitchSection:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Add break;",
                        cancellationToken => AddBreakStatementToSwitchSectionRefactoring.RefactorAsync(context.Document, switchSection, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.MergeSwitchSectionsWithEquivalentContent:
                {
                    int additionalSections = diagnostic.AdditionalLocations.Count;

                    CodeAction codeAction = CodeAction.Create(
                        "Merge sections",
                        cancellationToken => MergeSwitchSectionsRefactoring.RefactorAsync(context.Document, switchSection, additionalSections, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }