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)); } }
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; } } } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.Document .GetSyntaxRootAsync(context.CancellationToken) .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; } } } }
public override void Initialize(AnalysisContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } base.Initialize(context); context.RegisterSyntaxNodeAction( f => RemoveRedundantDefaultSwitchSectionRefactoring.AnalyzeSwitchStatement(f), SyntaxKind.SwitchStatement); }