Skip to content

Commit dabda72

Browse files
Fix crash when classifying embedded test classifications (#76576)
2 parents bbbe484 + 8cdd391 commit dabda72

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

src/EditorFeatures/CSharpTest/Classification/SemanticClassifierTests_TestMarkup.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Classification;
2222

23-
public partial class SemanticClassifierTests : AbstractCSharpClassifierTests
23+
public sealed partial class SemanticClassifierTests : AbstractCSharpClassifierTests
2424
{
2525
private const string s_testMarkup = """
2626
@@ -53,6 +53,27 @@ void M()
5353
await TestEmbeddedCSharpWithMultipleSpansAsync(allCode, testHost, spans, expected);
5454
}
5555

56+
private async Task TestSingleLineEmbeddedCSharpAsync(
57+
string code,
58+
TestHost testHost,
59+
params FormattedClassification[] expected)
60+
{
61+
var allCode = $$"""""
62+
class C
63+
{
64+
void M()
65+
{
66+
Test.M(""""{{code}}"""");
67+
}
68+
}
69+
""""" + s_testMarkup;
70+
71+
var start = allCode.IndexOf(code, StringComparison.Ordinal);
72+
var length = code.Length;
73+
var spans = ImmutableArray.Create(new TextSpan(start, length));
74+
await TestEmbeddedCSharpWithMultipleSpansAsync(allCode, testHost, spans, expected);
75+
}
76+
5677
private async Task TestEmbeddedCSharpWithMultipleSpansAsync(
5778
string allCode,
5879
TestHost testHost,
@@ -457,4 +478,26 @@ class D
457478
Punctuation.Semicolon,
458479
Punctuation.CloseCurly);
459480
}
481+
482+
[Theory, CombinatorialData]
483+
[WorkItem("https://github.com/dotnet/roslyn/issues/76575")]
484+
public async Task TestOnlyMarkup1(TestHost testHost)
485+
{
486+
await TestEmbeddedCSharpAsync(
487+
"[||]",
488+
testHost,
489+
TestCodeMarkdown("[|"),
490+
TestCodeMarkdown("|]"));
491+
}
492+
493+
[Theory, CombinatorialData]
494+
[WorkItem("https://github.com/dotnet/roslyn/issues/76575")]
495+
public async Task TestOnlyMarkup2(TestHost testHost)
496+
{
497+
await TestSingleLineEmbeddedCSharpAsync(
498+
"[||]",
499+
testHost,
500+
TestCodeMarkdown("[|"),
501+
TestCodeMarkdown("|]"));
502+
}
460503
}

src/Features/CSharp/Portable/EmbeddedLanguages/CSharpTestEmbeddedLanguageClassifier.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@ namespace Microsoft.CodeAnalysis.CSharp.Features.EmbeddedLanguages;
2020
[ExportEmbeddedLanguageClassifier(
2121
PredefinedEmbeddedLanguageNames.CSharpTest, [LanguageNames.CSharp], supportsUnannotatedAPIs: false,
2222
PredefinedEmbeddedLanguageNames.CSharpTest), Shared]
23-
internal sealed class CSharpTestEmbeddedLanguageClassifier : IEmbeddedLanguageClassifier
23+
[method: ImportingConstructor]
24+
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
25+
internal sealed class CSharpTestEmbeddedLanguageClassifier() : IEmbeddedLanguageClassifier
2426
{
25-
[ImportingConstructor]
26-
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
27-
public CSharpTestEmbeddedLanguageClassifier()
28-
{
29-
}
30-
3127
private static TextSpan FromBounds(VirtualChar vc1, VirtualChar vc2)
3228
=> TextSpan.FromBounds(vc1.Span.Start, vc2.Span.End);
3329

@@ -90,7 +86,7 @@ public void RegisterClassifications(EmbeddedLanguageClassificationContext contex
9086
}
9187
}
9288
}
93-
else
89+
else if (!virtualCharsWithoutMarkup.IsEmpty)
9490
{
9591
context.AddClassification(
9692
ClassificationTypeNames.TestCode,

0 commit comments

Comments
 (0)