public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); WhileStatementSyntax whileStatement = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <WhileStatementSyntax>(); if (whileStatement == null) { return; } CodeAction codeAction = CodeAction.Create( "Use for to create an infinite loop", cancellationToken => { return(AvoidUsageOfWhileStatementToCreateInfiniteLoopRefactoring.RefactorAsync( context.Document, whileStatement, cancellationToken)); }, DiagnosticIdentifiers.AvoidUsageOfWhileStatementToCreateInfiniteLoop + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, context.Diagnostics); }
public override void Initialize(AnalysisContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } base.Initialize(context); context.RegisterSyntaxNodeAction( f => AvoidUsageOfWhileStatementToCreateInfiniteLoopRefactoring.Analyze(f, (WhileStatementSyntax)f.Node), SyntaxKind.WhileStatement); }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out WhileStatementSyntax whileStatement)) { return; } CodeAction codeAction = CodeAction.Create( "Use for to create an infinite loop", cancellationToken => { return(AvoidUsageOfWhileStatementToCreateInfiniteLoopRefactoring.RefactorAsync( context.Document, whileStatement, cancellationToken)); }, GetEquivalenceKey(DiagnosticIdentifiers.AvoidUsageOfWhileStatementToCreateInfiniteLoop)); context.RegisterCodeFix(codeAction, context.Diagnostics); }
private void AnalyzeWhileStatement(SyntaxNodeAnalysisContext context) { var whileStatement = (WhileStatementSyntax)context.Node; AvoidUsageOfWhileStatementToCreateInfiniteLoopRefactoring.Analyze(context, whileStatement); }