public static void ComputeRefactorings(RefactoringContext context, InterpolationSyntax interpolation)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MergeInterpolationIntoInterpolatedString) &&
                MergeInterpolationIntoInterpolatedStringRefactoring.CanRefactor(interpolation))
            {
                string innerText = SyntaxUtility.GetStringLiteralInnerText((LiteralExpressionSyntax)interpolation.Expression);

                context.RegisterRefactoring(
                    $"Merge '{innerText}' into interpolated string",
                    cancellationToken => MergeInterpolationIntoInterpolatedStringRefactoring.RefactorAsync(context.Document, interpolation, cancellationToken));
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveInterpolation) &&
                (interpolation.OpenBraceToken.Span.Contains(context.Span) ||
                 interpolation.CloseBraceToken.Span.Contains(context.Span)))
            {
                context.RegisterRefactoring("Remove interpolation",
                                            cancellationToken =>
                {
                    return(RemoveInterpolationRefactoring.RefactorAsync(
                               context.Document,
                               interpolation,
                               cancellationToken));
                });
            }
        }
예제 #2
0
        public static async Task <Document> RefactorAsync(
            Document document,
            InterpolationSyntax interpolation,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var interpolatedString = (InterpolatedStringExpressionSyntax)interpolation.Parent;

            string s = interpolatedString.ToString();

            s = s.Substring(0, interpolation.Span.Start - interpolatedString.Span.Start)
                + SyntaxUtility.GetStringLiteralInnerText((LiteralExpressionSyntax)interpolation.Expression)
                + s.Substring(interpolation.Span.End - interpolatedString.Span.Start);

            var newInterpolatedString = (InterpolatedStringExpressionSyntax)SyntaxFactory.ParseExpression(s)
                                        .WithTriviaFrom(interpolatedString);

            SyntaxNode newRoot = root.ReplaceNode(interpolatedString, newInterpolatedString);

            return(document.WithSyntaxRoot(newRoot));
        }
        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);
        }
예제 #4
0
        public static void ComputeRefactorings(RefactoringContext context, LiteralExpressionSyntax literalExpression)
        {
            if (!literalExpression.Span.Contains(context.Span))
            {
                return;
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.InsertStringInterpolation) &&
                context.SupportsCSharp6 &&
                context.Span.End < literalExpression.Span.End)
            {
                int startIndex = GetStartIndex(context, literalExpression);

                if (startIndex != -1)
                {
                    context.RegisterRefactoring(
                        "Insert interpolation",
                        cancellationToken =>
                    {
                        return(ReplaceStringLiteralRefactoring.ReplaceWithInterpolatedStringAsync(
                                   context.Document,
                                   literalExpression,
                                   startIndex,
                                   context.Span.Length,
                                   cancellationToken));
                    });
                }
            }

            if (context.Span.IsBetweenSpans(literalExpression))
            {
                string text = SyntaxUtility.GetStringLiteralInnerText(literalExpression);

                if (literalExpression.IsVerbatimStringLiteral())
                {
                    if (text.Contains("\"\""))
                    {
                        if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceVerbatimStringLiteralWithRegularStringLiteral))
                        {
                            context.RegisterRefactoring(
                                "Replace verbatim string literal with regular string literal",
                                cancellationToken =>
                            {
                                return(ReplaceStringLiteralRefactoring.ReplaceWithRegularStringLiteralAsync(
                                           context.Document,
                                           literalExpression,
                                           cancellationToken));
                            });
                        }

                        if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceVerbatimStringLiteralWithRegularStringLiterals) &&
                            text.Contains("\n"))
                        {
                            context.RegisterRefactoring(
                                "Replace verbatim string literal with regular string literals",
                                cancellationToken =>
                            {
                                return(ReplaceStringLiteralRefactoring.ReplaceWithRegularStringLiteralsAsync(
                                           context.Document,
                                           literalExpression,
                                           cancellationToken));
                            });
                        }
                    }
                }
                else if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceRegularStringLiteralWithVerbatimStringLiteral) &&
                         text.Contains(@"\"))
                {
                    context.RegisterRefactoring(
                        "Replace regular string literal with verbatim string literal",
                        cancellationToken =>
                    {
                        return(ReplaceStringLiteralRefactoring.ReplaceWithVerbatimStringLiteralAsync(
                                   context.Document,
                                   literalExpression,
                                   cancellationToken));
                    });
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceEmptyStringLiteralWithStringEmpty) &&
                ReplaceStringLiteralRefactoring.CanReplaceWithStringEmpty(literalExpression))
            {
                context.RegisterRefactoring(
                    "Replace \"\" with 'string.Empty'",
                    cancellationToken =>
                {
                    return(ReplaceStringLiteralRefactoring.ReplaceWithStringEmptyAsync(
                               context.Document,
                               literalExpression,
                               cancellationToken));
                });
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceStringLiteralWithCharacterLiteral) &&
                ReplaceStringLiteralRefactoring.CanReplaceWithCharacterLiteral(literalExpression))
            {
                context.RegisterRefactoring(
                    "Replace string literal with character literal",
                    cancellationToken =>
                {
                    return(ReplaceStringLiteralRefactoring.ReplaceWithCharacterLiteralAsync(
                               context.Document,
                               literalExpression,
                               cancellationToken));
                });
            }
        }