public override async Task RegisterCodeFixesAsync(CodeFixContext context) { var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); // Get the node at the location of the diagnostic var diagnostic = context.Diagnostics.First(); var diagnosticSpan = diagnostic.Location.SourceSpan; var node = root.FindNode(diagnosticSpan); // Get the fixable node for this diagnostic var fixableNode = await context.GetFixableNodeAsync(node).ConfigureAwait(false); // Register a code fix that will fix the issue with the fixable node context.RegisterCodeFix(...) }
public override async Task RegisterCodeFixesAsync(CodeFixContext context) { var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); // Get the node at the location of the diagnostic var diagnostic = context.Diagnostics.First(); var diagnosticSpan = diagnostic.Location.SourceSpan; var node = root.FindNode(diagnosticSpan); // Check if the node is a binary expression if (node is BinaryExpressionSyntax binaryExpression) { // Get the left and right operands of the binary expression var left = binaryExpression.Left; var right = binaryExpression.Right; // Get the fixable nodes for the left and right operands var fixableLeft = await context.GetFixableNodeAsync(left).ConfigureAwait(false); var fixableRight = await context.GetFixableNodeAsync(right).ConfigureAwait(false); // Register a code fix that will fix the issue with the fixable nodes context.RegisterCodeFix(...) } }In this example, we use the GetFixableNodeAsync method to get fixable nodes for the left and right operands of a binary expression. We then register a code fix that will fix the issues with the fixable nodes. These examples use the Microsoft.CodeAnalysis.CodeFixes package library.