public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context) { SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken); InterpolatedStringExpressionSyntax interpolatedString = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <InterpolatedStringExpressionSyntax>(); if (interpolatedString == null) { return; } if (InterpolatedStringRefactoring.CanConvertToStringLiteral(interpolatedString)) { context.RegisterRefactoring("Convert to string literal", cancellationToken => { return(InterpolatedStringRefactoring.ConvertToStringLiteralAsync( context.Document, interpolatedString, cancellationToken)); }); } }
private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context) { if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true) { return; } var interpolatedString = (InterpolatedStringExpressionSyntax)context.Node; if (InterpolatedStringRefactoring.CanConvertToStringLiteral(interpolatedString)) { context.ReportDiagnostic( DiagnosticDescriptors.UseStringLiteralInsteadOfInterpolatedString, context.Node.GetLocation()); SyntaxToken token = interpolatedString.StringStartToken; if (!token.IsMissing && token.Text.StartsWith("$")) { context.ReportDiagnostic( DiagnosticDescriptors.ReplaceInterpolatedStringWithStringLiteralFadeOut, Location.Create( interpolatedString.SyntaxTree, new TextSpan(token.SpanStart, 1))); } } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); InterpolatedStringExpressionSyntax interpolatedString = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <InterpolatedStringExpressionSyntax>(); if (interpolatedString == null) { return; } CodeAction codeAction = CodeAction.Create( "Convert to string literal", cancellationToken => InterpolatedStringRefactoring.ConvertToStringLiteralAsync(context.Document, interpolatedString, cancellationToken), DiagnosticIdentifiers.UseStringLiteralInsteadOfInterpolatedString + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, context.Diagnostics); }