未验证 提交 8926f8c8 编写于 作者: G Gen Lu 提交者: GitHub

Merge pull request #32890 from genlu/fix_32864

Handle string concat with no string literals in ConvertToInterpolatedString
......@@ -707,6 +707,27 @@ void M()
{
var v = $""{3}string{1}string"";
}
}");
}
[WorkItem(32864, "https://github.com/dotnet/roslyn/issues/32864")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertToInterpolatedString)]
public async Task TestConcatenationWithNoStringLiterals()
{
await TestInRegularAndScriptAsync(
@"public class C
{
void M()
{
var v = 1 [||]+ (""string"");
}
}",
@"public class C
{
void M()
{
var v = $""{1}{(""string"")}"";
}
}");
}
}
......
......@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
......@@ -60,25 +61,32 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
var pieces = new List<SyntaxNode>();
CollectPiecesDown(syntaxFacts, pieces, top, semanticModel, cancellationToken);
var stringLiterals = pieces.Where(syntaxFacts.IsStringLiteralExpression).ToImmutableArray();
// If the entire expression is just concatenated strings, then don't offer to
// make an interpolated string. The user likely manually split this for
// readability.
if (pieces.All(syntaxFacts.IsStringLiteralExpression))
if (stringLiterals.Length == pieces.Count)
{
return;
}
// Make sure that all the string tokens we're concatenating are the same type
// of string literal. i.e. if we have an expression like: @" "" " + " \r\n "
// then we don't merge this. We don't want to be munging different types of
// escape sequences in these strings, so we only support combining the string
// tokens if they're all the same type.
var firstStringToken = pieces.First(syntaxFacts.IsStringLiteralExpression).GetFirstToken();
var isVerbatimStringLiteral = syntaxFacts.IsVerbatimStringLiteral(firstStringToken);
if (pieces.Where(syntaxFacts.IsStringLiteralExpression).Any(
lit => isVerbatimStringLiteral != syntaxFacts.IsVerbatimStringLiteral(lit.GetFirstToken())))
var isVerbatimStringLiteral = false;
if (stringLiterals.Length > 0)
{
return;
// Make sure that all the string tokens we're concatenating are the same type
// of string literal. i.e. if we have an expression like: @" "" " + " \r\n "
// then we don't merge this. We don't want to be munging different types of
// 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);
if (stringLiterals.Any(
lit => isVerbatimStringLiteral != syntaxFacts.IsVerbatimStringLiteral(lit.GetFirstToken())))
{
return;
}
}
var interpolatedString = CreateInterpolatedString(document, isVerbatimStringLiteral, pieces);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册