public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out AssignmentExpressionSyntax assignment)) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.UseCompoundAssignment: { var binaryExpression = (BinaryExpressionSyntax)assignment.Right; string operatorText = UseCompoundAssignmentRefactoring.GetCompoundOperatorText(binaryExpression); CodeAction codeAction = CodeAction.Create( $"Use {operatorText} operator", cancellationToken => UseCompoundAssignmentRefactoring.RefactorAsync(context.Document, assignment, cancellationToken), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.UsePostfixUnaryOperatorInsteadOfAssignment: { string operatorText = UsePostfixUnaryOperatorInsteadOfAssignmentRefactoring.GetOperatorText(assignment); CodeAction codeAction = CodeAction.Create( $"Use {operatorText} operator", c => UsePostfixUnaryOperatorInsteadOfAssignmentRefactoring.RefactorAsync(context.Document, assignment, c), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.RemoveRedundantDelegateCreation: { CodeAction codeAction = CodeAction.Create( "Remove redundant delegate creation", cancellationToken => { return(RemoveRedundantDelegateCreationRefactoring.RefactorAsync( context.Document, (ObjectCreationExpressionSyntax)assignment.Right, 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); AssignmentExpressionSyntax assignment = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <AssignmentExpressionSyntax>(); if (assignment == null) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.SimplifyAssignmentExpression: { CodeAction codeAction = CodeAction.Create( "Simplify assignment expression", cancellationToken => { return(SimplifyAssignmentExpressionRefactoring.RefactorAsync( context.Document, assignment, cancellationToken)); }, diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.UsePostfixUnaryOperatorInsteadOfAssignment: { SyntaxKind kind = UsePostfixUnaryOperatorInsteadOfAssignmentRefactoring.GetPostfixUnaryOperatorKind(assignment); string operatorText = UsePostfixUnaryOperatorInsteadOfAssignmentRefactoring.GetOperatorText(kind); CodeAction codeAction = CodeAction.Create( $"Use {operatorText} operator", cancellationToken => { return(UsePostfixUnaryOperatorInsteadOfAssignmentRefactoring.RefactorAsync( context.Document, assignment, kind, cancellationToken)); }, diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } } } }