public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out ForStatementSyntax forStatement)) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.AvoidUsageOfForStatementToCreateInfiniteLoop: { CodeAction codeAction = CodeAction.Create( "Use while to create an infinite loop", cancellationToken => { return(AvoidUsageOfForStatementToCreateInfiniteLoopRefactoring.RefactorAsync( context.Document, forStatement, 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); ForStatementSyntax forStatement = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <ForStatementSyntax>(); Debug.Assert(forStatement != null, $"{nameof(forStatement)} is null"); if (forStatement == null) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.AvoidUsageOfForStatementToCreateInfiniteLoop: { CodeAction codeAction = CodeAction.Create( "Use while to create an infinite loop", cancellationToken => { return(AvoidUsageOfForStatementToCreateInfiniteLoopRefactoring.RefactorAsync( context.Document, forStatement, cancellationToken)); }, diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } } } }