public override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out SyntaxNode node, predicate: f => f is AssignmentExpressionSyntax || f.IsKind(SyntaxKind.CoalesceExpression))) { return; } Document document = context.Document; Diagnostic diagnostic = context.Diagnostics[0]; if (node is AssignmentExpressionSyntax assignment) { var binaryExpression = (BinaryExpressionSyntax)assignment.Right.WalkDownParentheses(); string operatorText = UseCompoundAssignmentAnalyzer.GetCompoundAssignmentOperatorText(binaryExpression); CodeAction codeAction = CodeAction.Create( Title, ct => UseCompoundAssignmentAsync(document, assignment, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } else { var coalesceExpression = (BinaryExpressionSyntax)node; CodeAction codeAction = CodeAction.Create( Title, ct => ConvertLazyInitializationToCompoundAssignmentAsync(document, coalesceExpression, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out AssignmentExpressionSyntax assignment)) { return; } Document document = context.Document; foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.UseCompoundAssignment: { var binaryExpression = (BinaryExpressionSyntax)assignment.Right.WalkDownParentheses(); string operatorText = UseCompoundAssignmentAnalyzer.GetCompoundAssignmentOperatorText(binaryExpression); CodeAction codeAction = CodeAction.Create( $"Use {operatorText} operator", cancellationToken => UseCompoundAssignmentRefactoring.RefactorAsync(document, assignment, cancellationToken), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.UseUnaryOperatorInsteadOfAssignment: { string operatorText = UseUnaryOperatorInsteadOfAssignmentAnalyzer.GetOperatorText(assignment); CodeAction codeAction = CodeAction.Create( $"Use {operatorText} operator", ct => UseUnaryOperatorInsteadOfAssignmentAsync(document, assignment, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.RemoveRedundantDelegateCreation: { CodeAction codeAction = CodeAction.Create( "Remove redundant delegate creation", cancellationToken => { return(RemoveRedundantDelegateCreationRefactoring.RefactorAsync( document, (ObjectCreationExpressionSyntax)assignment.Right, cancellationToken)); }, GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.RemoveRedundantAssignment: { CodeAction codeAction = CodeAction.Create( "Remove redundant assignment", cancellationToken => { return(RemoveRedundantAssignmentRefactoring.RefactorAsync( document, assignment, cancellationToken)); }, GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } } } }