Esempio n. 1
0
        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 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));
                });
            }
        }