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