Example #1
0
        private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
        {
            var interpolation = (InterpolationSyntax)context.Node;

            if (MergeInterpolationIntoInterpolatedStringRefactoring.CanRefactor(interpolation))
            {
                context.ReportDiagnostic(DiagnosticDescriptors.MergeInterpolationIntoInterpolatedString, interpolation);
            }
        }
        private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
        {
            if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true)
            {
                return;
            }

            var interpolation = (InterpolationSyntax)context.Node;

            if (MergeInterpolationIntoInterpolatedStringRefactoring.CanRefactor(interpolation))
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.MergeInterpolationIntoInterpolatedString,
                    interpolation.GetLocation());
            }
        }
Example #3
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out InterpolationSyntax interpolation))
            {
                return;
            }

            string innerText = ((LiteralExpressionSyntax)interpolation.Expression).GetStringLiteralInnerText();

            CodeAction codeAction = CodeAction.Create(
                $"Merge '{innerText}' into interpolated string",
                cancellationToken => MergeInterpolationIntoInterpolatedStringRefactoring.RefactorAsync(context.Document, interpolation, cancellationToken),
                GetEquivalenceKey(DiagnosticIdentifiers.MergeInterpolationIntoInterpolatedString));

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            InterpolationSyntax interpolation = root
                                                .FindNode(context.Span, getInnermostNodeForTie: true)?
                                                .FirstAncestorOrSelf <InterpolationSyntax>();

            Debug.Assert(interpolation != null, $"{nameof(interpolation)} is null");

            if (interpolation == null)
            {
                return;
            }

            string innerText = SyntaxUtility.GetStringLiteralInnerText((LiteralExpressionSyntax)interpolation.Expression);

            CodeAction codeAction = CodeAction.Create(
                $"Merge '{innerText}' into interpolated string",
                cancellationToken => MergeInterpolationIntoInterpolatedStringRefactoring.RefactorAsync(context.Document, interpolation, cancellationToken),
                DiagnosticIdentifiers.MergeInterpolationIntoInterpolatedString + EquivalenceKeySuffix);

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }