Exemple #1
0
        public static void ComputeRefactorings(RefactoringContext context, InterpolatedStringExpressionSyntax interpolatedString)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.InsertStringInterpolation) &&
                context.Span.IsEmpty &&
                InsertInterpolationRefactoring.CanRefactor(context, interpolatedString))
            {
                context.RegisterRefactoring("Insert interpolation",
                                            cancellationToken =>
                {
                    return(InsertInterpolationRefactoring.RefactorAsync(
                               context.Document,
                               interpolatedString,
                               context.Span,
                               addNameOf: false,
                               cancellationToken: cancellationToken));
                });
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceInterpolatedStringWithStringLiteral) &&
                ReplaceInterpolatedStringWithStringLiteralAnalysis.IsFixable(interpolatedString))
            {
                context.RegisterRefactoring("Remove $",
                                            cancellationToken =>
                {
                    return(ReplaceInterpolatedStringWithStringLiteralRefactoring.RefactorAsync(
                               context.Document,
                               interpolatedString,
                               cancellationToken));
                });
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceInterpolatedStringWithInterpolationExpression) &&
                interpolatedString.Span.Contains(context.Span) &&
                ReplaceInterpolatedStringWithInterpolationExpressionRefactoring.CanRefactor(interpolatedString))
            {
                ExpressionSyntax expression = ((InterpolationSyntax)(interpolatedString.Contents[0])).Expression;

                context.RegisterRefactoring(
                    $"Replace interpolated string with '{expression}'",
                    cancellationToken =>
                {
                    return(ReplaceInterpolatedStringWithInterpolationExpressionRefactoring.RefactorAsync(
                               context.Document,
                               interpolatedString,
                               cancellationToken));
                });
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceInterpolatedStringWithConcatenation) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(interpolatedString))
            {
                ReplaceInterpolatedStringWithConcatenationRefactoring.ComputeRefactoring(context, interpolatedString);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceInterpolatedStringWithStringFormat) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(interpolatedString))
            {
                ReplaceInterpolatedStringWithStringFormatRefactoring.ComputeRefactoring(context, interpolatedString);
            }
        }
Exemple #2
0
 public static void ComputeRefactorings(RefactoringContext context, InterpolatedStringTextSyntax interpolatedStringText)
 {
     if (context.IsRefactoringEnabled(RefactoringIdentifiers.InsertStringInterpolation) &&
         interpolatedStringText.IsParentKind(SyntaxKind.InterpolatedStringExpression))
     {
         context.RegisterRefactoring("Insert interpolation",
                                     cancellationToken =>
         {
             return(InsertInterpolationRefactoring.RefactorAsync(
                        context.Document,
                        (InterpolatedStringExpressionSyntax)interpolatedStringText.Parent,
                        context.Span,
                        cancellationToken));
         });
     }
 }
        public static void ComputeRefactorings(RefactoringContext context, InterpolatedStringExpressionSyntax interpolatedString)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.InsertStringInterpolation) &&
                context.Span.IsEmpty &&
                InsertInterpolationRefactoring.CanRefactor(context, interpolatedString))
            {
                context.RegisterRefactoring("Insert interpolation",
                                            cancellationToken =>
                {
                    return(InsertInterpolationRefactoring.RefactorAsync(
                               context.Document,
                               interpolatedString,
                               context.Span,
                               cancellationToken));
                });
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceInterpolatedStringWithStringLiteral) &&
                ReplaceInterpolatedStringWithStringLiteralRefactoring.CanRefactor(interpolatedString))
            {
                context.RegisterRefactoring("Remove '$'",
                                            cancellationToken =>
                {
                    return(ReplaceInterpolatedStringWithStringLiteralRefactoring.RefactorAsync(
                               context.Document,
                               interpolatedString,
                               cancellationToken));
                });
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceInterpolatedStringWithInterpolationExpression) &&
                interpolatedString.Span.Contains(context.Span) &&
                ReplaceInterpolatedStringWithInterpolationExpressionRefactoring.CanRefactor(interpolatedString))
            {
                ExpressionSyntax expression = ((InterpolationSyntax)(interpolatedString.Contents[0])).Expression;

                context.RegisterRefactoring(
                    $"Replace interpolated string with '{expression}'",
                    cancellationToken =>
                {
                    return(ReplaceInterpolatedStringWithInterpolationExpressionRefactoring.RefactorAsync(
                               context.Document,
                               interpolatedString,
                               cancellationToken));
                });
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, InterpolatedStringTextSyntax interpolatedStringText)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.InsertStringInterpolation) &&
                interpolatedStringText.IsParentKind(SyntaxKind.InterpolatedStringExpression))
            {
                TextSpan span = context.Span;
                var      interpolatedString = (InterpolatedStringExpressionSyntax)interpolatedStringText.Parent;
                string   text       = interpolatedStringText.TextToken.Text;
                bool     isVerbatim = interpolatedString.IsVerbatim();

                if (StringLiteralParser.CanExtractSpan(
                        text,
                        span.Offset(-interpolatedStringText.SpanStart),
                        isVerbatim,
                        isInterpolatedText: true))
                {
                    context.RegisterRefactoring(
                        "Insert interpolation",
                        cancellationToken =>
                    {
                        return(InsertInterpolationRefactoring.RefactorAsync(
                                   context.Document,
                                   interpolatedString,
                                   span,
                                   addNameOf: false,
                                   cancellationToken: cancellationToken));
                    });

                    if (!span.IsEmpty)
                    {
                        SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                        string name = StringLiteralParser.Parse(
                            text,
                            span.Start - interpolatedStringText.SpanStart,
                            span.Length,
                            isVerbatim: isVerbatim,
                            isInterpolatedText: true);

                        foreach (ISymbol symbol in semanticModel.LookupSymbols(interpolatedStringText.SpanStart))
                        {
                            if (string.Equals(name, symbol.MetadataName, StringComparison.Ordinal))
                            {
                                context.RegisterRefactoring(
                                    "Insert interpolation with nameof",
                                    cancellationToken =>
                                {
                                    return(InsertInterpolationRefactoring.RefactorAsync(
                                               context.Document,
                                               interpolatedString,
                                               span,
                                               addNameOf: true,
                                               cancellationToken: cancellationToken));
                                });

                                break;
                            }
                        }
                    }
                }
            }
        }