public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindToken(root, context.Span.Start, out SyntaxToken token)) { return; } Debug.Assert(token.IsKind(SyntaxKind.AsyncKeyword), token.Kind().ToString()); Diagnostic diagnostic = context.Diagnostics[0]; CodeAction codeAction = CodeActionFactory.RemoveAsyncAwait(context.Document, token, equivalenceKey: GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, SyntaxToken token) { switch (token.Parent) { case MethodDeclarationSyntax methodDeclaration: { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); using (RemoveAsyncAwaitAnalysis analysis = RemoveAsyncAwaitAnalysis.Create(methodDeclaration, semanticModel, context.CancellationToken)) { if (analysis.Success) { RegisterRefactoring(); } } return; } case LocalFunctionStatementSyntax localFunction: { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); using (RemoveAsyncAwaitAnalysis analysis = RemoveAsyncAwaitAnalysis.Create(localFunction, semanticModel, context.CancellationToken)) { if (analysis.Success) { RegisterRefactoring(); } } return; } case ParenthesizedLambdaExpressionSyntax parenthesizedLambda: { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); using (RemoveAsyncAwaitAnalysis analysis = RemoveAsyncAwaitAnalysis.Create(parenthesizedLambda, semanticModel, context.CancellationToken)) { if (analysis.Success) { RegisterRefactoring(); } } return; } case SimpleLambdaExpressionSyntax simpleLambda: { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); using (RemoveAsyncAwaitAnalysis analysis = RemoveAsyncAwaitAnalysis.Create(simpleLambda, semanticModel, context.CancellationToken)) { if (analysis.Success) { RegisterRefactoring(); } } return; } case AnonymousMethodExpressionSyntax anonymousMethod: { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); using (RemoveAsyncAwaitAnalysis analysis = RemoveAsyncAwaitAnalysis.Create(anonymousMethod, semanticModel, context.CancellationToken)) { if (analysis.Success) { RegisterRefactoring(); } } return; } } void RegisterRefactoring() { CodeAction codeAction = CodeActionFactory.RemoveAsyncAwait(context.Document, token, equivalenceKey: EquivalenceKey.Create(RefactoringDescriptors.RemoveAsyncAwait)); context.RegisterRefactoring(codeAction); } }