public static async Task ComputeRefactoringsAsync(RefactoringContext context, LiteralExpressionSyntax literalExpression) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.InsertStringInterpolation) && context.SupportsCSharp6 && context.Span.End < literalExpression.Span.End) { int startIndex = GetStartIndex(literalExpression, context.Span); if (startIndex != -1) { context.RegisterRefactoring( "Insert interpolation", cancellationToken => { return(ReplaceWithInterpolatedStringAsync( context.Document, literalExpression, startIndex, context.Span.Length, addNameOf: false, cancellationToken: cancellationToken)); }); if (!context.Span.IsEmpty) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); string name = StringLiteralParser.Parse(literalExpression.Token.Text, startIndex, context.Span.Length, literalExpression.IsVerbatimStringLiteral(), isInterpolatedText: false); foreach (ISymbol symbol in semanticModel.LookupSymbols(literalExpression.SpanStart)) { if (string.Equals(name, symbol.MetadataName, StringComparison.Ordinal)) { context.RegisterRefactoring( "Insert interpolation with nameof", cancellationToken => { return(ReplaceWithInterpolatedStringAsync( context.Document, literalExpression, startIndex, context.Span.Length, addNameOf: true, cancellationToken: cancellationToken)); }); break; } } } } } if (context.Span.IsBetweenSpans(literalExpression)) { string text = literalExpression.GetStringLiteralInnerText(); if (literalExpression.IsVerbatimStringLiteral()) { if (text.Contains("\"\"")) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceVerbatimStringLiteralWithRegularStringLiteral)) { context.RegisterRefactoring( "Replace verbatim string literal with regular string literal", cancellationToken => { return(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(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(ReplaceWithVerbatimStringLiteralAsync( context.Document, literalExpression, cancellationToken)); }); } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseStringEmptyInsteadOfEmptyStringLiteral) && CanReplaceWithStringEmpty(literalExpression)) { context.RegisterRefactoring( "Replace \"\" with 'string.Empty'", cancellationToken => { return(ReplaceWithStringEmptyAsync( context.Document, literalExpression, cancellationToken)); }); } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, LiteralExpressionSyntax literalExpression) { 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(ReplaceWithInterpolatedStringAsync( context.Document, literalExpression, startIndex, context.Span.Length, cancellationToken)); }); } } if (context.Span.IsBetweenSpans(literalExpression)) { string text = literalExpression.GetStringLiteralInnerText(); if (literalExpression.IsVerbatimStringLiteral()) { if (text.Contains("\"\"")) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceVerbatimStringLiteralWithRegularStringLiteral)) { context.RegisterRefactoring( "Replace verbatim string literal with regular string literal", cancellationToken => { return(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(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(ReplaceWithVerbatimStringLiteralAsync( context.Document, literalExpression, cancellationToken)); }); } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceEmptyStringLiteralWithStringEmpty) && CanReplaceWithStringEmpty(literalExpression)) { context.RegisterRefactoring( "Replace \"\" with 'string.Empty'", cancellationToken => { return(ReplaceWithStringEmptyAsync( context.Document, literalExpression, cancellationToken)); }); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceStringLiteralWithCharacterLiteral)) { await ReplaceStringLiteralWithCharacterLiteralRefactoring.ComputeRefactoringAsync(context, literalExpression).ConfigureAwait(false); } }