Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable 'use interpolated string' with raw strings #76021

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@
namespace Microsoft.CodeAnalysis.CSharp.ConvertToInterpolatedString;

[ExportCodeRefactoringProvider(LanguageNames.CSharp, Name = PredefinedCodeRefactoringProviderNames.ConvertConcatenationToInterpolatedString), Shared]
internal sealed class CSharpConvertConcatenationToInterpolatedStringRefactoringProvider :
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class CSharpConvertConcatenationToInterpolatedStringRefactoringProvider() :
AbstractConvertConcatenationToInterpolatedStringRefactoringProvider<ExpressionSyntax>
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpConvertConcatenationToInterpolatedStringRefactoringProvider()
{
}

protected override bool SupportsInterpolatedStringHandler(Compilation compilation)
=> compilation.GetTypeByMetadataName("System.Runtime.CompilerServices.DefaultInterpolatedStringHandler") != null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1388,4 +1388,26 @@ public override string ToString()
ReferenceAssemblies = ReferenceAssemblies.Net.Net60,
}.RunAsync();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/61256")]
public async Task TestWithRawString()
{
await new VerifyCS.Test
{
TestCode = """"
struct ValueTuple
{
public void Goo()
{
var someVariable = "Some text";

var fullText = someVariable [||]+ """
Appended line
""";
}
}
"""",
LanguageVersion = LanguageVersion.CSharp11,
}.RunAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContex
return;

var isVerbatimStringLiteral = false;

if (stringLiterals.Length > 0)
{

Expand All @@ -89,10 +90,18 @@ public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContex
// escape sequences in these strings, so we only support combining the string
// tokens if they're all the same type.
var firstStringToken = stringLiterals[0].GetFirstToken();

isVerbatimStringLiteral = syntaxFacts.IsVerbatimStringLiteral(firstStringToken);
for (int i = 1, n = stringLiterals.Length; i < n; i++)

foreach (var literal in stringLiterals)
{
if (isVerbatimStringLiteral != syntaxFacts.IsVerbatimStringLiteral(stringLiterals[i].GetFirstToken()))
var firstToken = literal.GetFirstToken();
if (isVerbatimStringLiteral != syntaxFacts.IsVerbatimStringLiteral(firstToken))
return;

// Not currently supported with raw string literals due to the complexity of merging them and forming
// the final interpolated raw string.
if (syntaxFacts.IsRawStringLiteral(firstToken))
return;
}
}
Expand All @@ -117,7 +126,10 @@ private async Task<Document> UpdateDocumentAsync(
}

protected async Task<SyntaxNode> CreateInterpolatedStringAsync(
Document document, bool isVerbatimStringLiteral, ImmutableArray<SyntaxNode> pieces, CancellationToken cancellationToken)
Document document,
bool isVerbatimStringLiteral,
ImmutableArray<SyntaxNode> pieces,
CancellationToken cancellationToken)
{
var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var supportsInterpolatedStringHandler = this.SupportsInterpolatedStringHandler(semanticModel.Compilation);
Expand All @@ -133,6 +145,7 @@ protected async Task<SyntaxNode> CreateInterpolatedStringAsync(

using var _ = ArrayBuilder<SyntaxNode>.GetInstance(pieces.Length, out var content);
var previousContentWasStringLiteralExpression = false;

foreach (var piece in pieces)
{
var isCharacterLiteral = syntaxFacts.IsCharacterLiteralExpression(piece);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,13 @@ public SyntaxList<SyntaxNode> GetContentsOfInterpolatedString(SyntaxNode interpo
public bool IsVerbatimStringLiteral(SyntaxToken token)
=> token.IsVerbatimStringLiteral();

public bool IsRawStringLiteral(SyntaxToken token)
=> token.Kind() is
SyntaxKind.SingleLineRawStringLiteralToken or
SyntaxKind.MultiLineRawStringLiteralToken or
SyntaxKind.Utf8SingleLineRawStringLiteralToken or
SyntaxKind.Utf8MultiLineRawStringLiteralToken;

public bool IsNumericLiteral(SyntaxToken token)
=> token.Kind() == SyntaxKind.NumericLiteralToken;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ internal interface ISyntaxFacts

bool IsNumericLiteral(SyntaxToken token);
bool IsVerbatimStringLiteral(SyntaxToken token);
bool IsRawStringLiteral(SyntaxToken token);

bool IsUsingOrExternOrImport([NotNullWhen(true)] SyntaxNode? node);
bool IsGlobalAssemblyAttribute([NotNullWhen(true)] SyntaxNode? node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.LanguageService
Return False
End Function

Public Function IsRawStringLiteral(token As SyntaxToken) As Boolean Implements ISyntaxFacts.IsRawStringLiteral
' VB does not have raw strings
Return False
End Function

Public Function GetArgumentsOfObjectCreationExpression(node As SyntaxNode) As SeparatedSyntaxList(Of SyntaxNode) Implements ISyntaxFacts.GetArgumentsOfObjectCreationExpression
Dim argumentList = DirectCast(node, ObjectCreationExpressionSyntax).ArgumentList
Return If(argumentList Is Nothing, Nothing, GetArgumentsOfArgumentList(argumentList))
Expand Down
Loading