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 static void ComputeRefactorings(RefactoringContext context, InterpolatedStringExpressionSyntax interpolatedString) { if (context.IsRefactoringEnabled(RefactoringDescriptors.InsertStringInterpolation) && context.Span.IsEmpty && InsertInterpolationRefactoring.CanRefactor(context, interpolatedString)) { context.RegisterRefactoring( "Insert interpolation", ct => { return(InsertInterpolationRefactoring.RefactorAsync( context.Document, interpolatedString, context.Span, addNameOf: false, cancellationToken: ct)); }, RefactoringDescriptors.InsertStringInterpolation); } if (context.IsRefactoringEnabled(RefactoringDescriptors.ConvertInterpolatedStringToStringLiteral) && ConvertInterpolatedStringToStringLiteralAnalysis.IsFixable(interpolatedString)) { context.RegisterRefactoring( "Remove '$'", ct => { return(ConvertInterpolatedStringToStringLiteralRefactoring.RefactorAsync( context.Document, interpolatedString, ct)); }, RefactoringDescriptors.ConvertInterpolatedStringToStringLiteral); } if (context.IsRefactoringEnabled(RefactoringDescriptors.ReplaceInterpolatedStringWithInterpolationExpression) && interpolatedString.Span.Contains(context.Span) && ReplaceInterpolatedStringWithInterpolationExpressionRefactoring.CanRefactor(interpolatedString)) { ExpressionSyntax expression = ((InterpolationSyntax)(interpolatedString.Contents[0])).Expression; context.RegisterRefactoring( $"Replace interpolated string with '{expression}'", ct => { return(ReplaceInterpolatedStringWithInterpolationExpressionRefactoring.RefactorAsync( context.Document, interpolatedString, ct)); }, RefactoringDescriptors.ReplaceInterpolatedStringWithInterpolationExpression); } if (context.IsRefactoringEnabled(RefactoringDescriptors.ConvertInterpolatedStringToConcatenation) && context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(interpolatedString)) { ConvertInterpolatedStringToConcatenationRefactoring.ComputeRefactoring(context, interpolatedString); } if (context.IsRefactoringEnabled(RefactoringDescriptors.ConvertInterpolatedStringToStringFormat) && context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(interpolatedString)) { ConvertInterpolatedStringToStringFormatRefactoring.ComputeRefactoring(context, interpolatedString); } }