public override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out InterpolatedStringExpressionSyntax interpolatedString)) { return; } Document document = context.Document; foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.UnnecessaryInterpolatedString: { if (ConvertInterpolatedStringToStringLiteralAnalysis.IsFixable(interpolatedString)) { CodeAction codeAction = CodeAction.Create( "Remove '$'", ct => ConvertInterpolatedStringToStringLiteralRefactoring.RefactorAsync(document, interpolatedString, ct), GetEquivalenceKey(diagnostic.Id)); context.RegisterCodeFix(codeAction, diagnostic); } else { var interpolation = (InterpolationSyntax)interpolatedString.Contents[0]; CodeAction codeAction = CodeAction.Create( $"Replace interpolated string with '{interpolation.Expression}'", ct => UnnecessaryInterpolatedStringRefactoring.RefactorAsync(document, interpolatedString, ct), GetEquivalenceKey(diagnostic.Id)); context.RegisterCodeFix(codeAction, diagnostic); } break; } case DiagnosticIdentifiers.ConvertInterpolatedStringToConcatenation: { CodeAction codeAction = CodeAction.Create( "Convert to concatenation", ct => ConvertInterpolatedStringToConcatenationRefactoring.RefactorAsync(document, interpolatedString, ct), GetEquivalenceKey(diagnostic.Id)); context.RegisterCodeFix(codeAction, diagnostic); break; } } } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out InterpolatedStringExpressionSyntax interpolatedString)) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.AvoidInterpolatedStringWithNoInterpolation: { CodeAction codeAction = CodeAction.Create( "Remove $", cancellationToken => ReplaceInterpolatedStringWithStringLiteralRefactoring.RefactorAsync(context.Document, interpolatedString, cancellationToken), GetEquivalenceKey(diagnostic.Id)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.UnnecessaryInterpolatedString: { var interpolation = (InterpolationSyntax)interpolatedString.Contents[0]; CodeAction codeAction = CodeAction.Create( $"Replace interpolated string with '{interpolation.Expression}'", cancellationToken => UnnecessaryInterpolatedStringRefactoring.RefactorAsync(context.Document, interpolatedString, cancellationToken), GetEquivalenceKey(diagnostic.Id)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.ReplaceInterpolatedStringWithConcatenation: { CodeAction codeAction = CodeAction.Create( "Replace interpolated string with concatenation", cancellationToken => ReplaceInterpolatedStringWithConcatenationRefactoring.RefactorAsync(context.Document, interpolatedString, cancellationToken), GetEquivalenceKey(diagnostic.Id)); context.RegisterCodeFix(codeAction, diagnostic); break; } } } }