public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); var diagnostic = context.Diagnostics.First(); var diagnosticSpan = diagnostic.Location.SourceSpan; // find the 'new XError()' expression var creationExpr = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <ObjectCreationExpressionSyntax>().First(); var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken); if (ErrorExceptionUsageAnalyzer.TryMatchLiteralCreation(creationExpr, semanticModel, out var literalCreation)) { // Register a code action that will invoke the fix. foreach (var sev in Severities) { context.RegisterCodeFix( CodeAction.Create( title: string.Format(Title, sev), createChangedSolution: c => ConvertMessagesToConstantsAsync(context.Document, new[] { literalCreation }, sev, c), equivalenceKey: string.Format(Title, sev)), diagnostic); } } }
static IEnumerable <LiteralCreation> MatchLiteralCreations( [NotNull] IEnumerable <ObjectCreationExpressionSyntax> creationExprs, SemanticModel semanticModel) { foreach (var expr in creationExprs) { if (ErrorExceptionUsageAnalyzer.TryMatchLiteralCreation(expr, semanticModel, out var literalCreation)) { yield return(literalCreation); } } }