public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out DoStatementSyntax doStatement)) { return; } CodeAction codeAction = CodeAction.Create( "Convert to 'while'", cancellationToken => { return(ConvertDoToWhileRefactoring.RefactorAsync( context.Document, doStatement, cancellationToken)); }, GetEquivalenceKey(DiagnosticIdentifiers.AvoidUsageOfDoStatementToCreateInfiniteLoop)); context.RegisterCodeFix(codeAction, context.Diagnostics); }
public override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out StatementSyntax statement)) { return; } Document document = context.Document; Diagnostic diagnostic = context.Diagnostics[0]; if (statement.IsKind(SyntaxKind.ForStatement)) { var forStatement = (ForStatementSyntax)statement; CodeAction codeAction = CodeAction.Create( "Convert to 'while'", ct => ConvertForToWhileAsync(document, forStatement, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } else if (statement.IsKind(SyntaxKind.WhileStatement)) { var whileStatement = (WhileStatementSyntax)statement; CodeAction codeAction = CodeAction.Create( "Convert to 'for'", ct => { ForStatementSyntax forStatement = SyntaxRefactorings.ConvertWhileStatementToForStatement(whileStatement) .WithFormatterAnnotation(); return(document.ReplaceNodeAsync(whileStatement, forStatement, ct)); }, GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } else if (statement.IsKind(SyntaxKind.DoStatement)) { var doStatement = (DoStatementSyntax)statement; if (document.GetConfigOptions(statement.SyntaxTree).GetInfiniteLoopStyle() == InfiniteLoopStyle.ForStatement) { CodeAction codeAction = CodeAction.Create( "Convert to 'for'", ct => ConvertDoToForAsync(document, doStatement, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } else { CodeAction codeAction = CodeAction.Create( "Convert to 'while'", ct => ConvertDoToWhileRefactoring.RefactorAsync(document, doStatement, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } } }