Example #1
0
        public static Task <Document> RefactorAsync(
            Document document,
            InterpolatedStringExpressionSyntax interpolatedString,
            TextSpan span,
            bool addNameOf = false,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            string s = interpolatedString.ToString();

            int startIndex = span.Start - interpolatedString.SpanStart;

            var sb = new StringBuilder();

            sb.Append(s, 0, startIndex);
            sb.Append('{');

            if (addNameOf)
            {
                string identifier = StringLiteralParser.Parse(
                    s,
                    startIndex,
                    span.Length,
                    isVerbatim: interpolatedString.IsVerbatim(),
                    isInterpolatedText: true);

                sb.Append(CSharpFactory.NameOfExpression(identifier));
            }

            int closeBracePosition = sb.Length;

            sb.Append('}');

            startIndex += span.Length;
            sb.Append(s, startIndex, s.Length - startIndex);

            ExpressionSyntax newNode = ParseExpression(sb.ToString()).WithTriviaFrom(interpolatedString);

            SyntaxToken closeBrace = newNode.FindToken(closeBracePosition);

            newNode = newNode.ReplaceToken(closeBrace, closeBrace.WithNavigationAnnotation());

            return(document.ReplaceNodeAsync(interpolatedString, newNode, cancellationToken));
        }