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

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

            if (label == null)
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Remove unnecessary case label",
                cancellationToken =>
            {
                return(RemoveUnnecessaryCaseLabelRefactoring.RefactorAsync(
                           context.Document,
                           label,
                           cancellationToken));
            },
                DiagnosticIdentifiers.RemoveUnnecessaryCaseLabel + EquivalenceKeySuffix);

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
        private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
        {
            var switchSection = (SwitchSectionSyntax)context.Node;

            FormatEachStatementOnSeparateLineRefactoring.Analyze(context, switchSection);

            if (AddBreakStatementToSwitchSectionRefactoring.CanRefactor(context, switchSection))
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.AddBreakStatementToSwitchSection,
                    switchSection);
            }

            RemoveRedundantDefaultSwitchSectionRefactoring.Analyze(context, switchSection);

            RemoveUnnecessaryCaseLabelRefactoring.Analyze(context, switchSection);

            FormatSwitchSectionStatementOnSeparateLineRefactoring.Analyze(context, switchSection);

            DefaultLabelShouldBeLastLabelInSwitchSectionRefactoring.Analyze(context, switchSection);

            SyntaxList <StatementSyntax> statements = switchSection.Statements;

            if (statements.Count > 1)
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.AddBracesToSwitchSectionWithMultipleStatements,
                    Location.Create(switchSection.SyntaxTree, statements.Span));
            }
        }
Example #3
0
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out CaseSwitchLabelSyntax label))
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Remove unnecessary case label",
                cancellationToken =>
            {
                return(RemoveUnnecessaryCaseLabelRefactoring.RefactorAsync(
                           context.Document,
                           label,
                           cancellationToken));
            },
                GetEquivalenceKey(DiagnosticIdentifiers.RemoveUnnecessaryCaseLabel));

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