Skip to content

Commit

Permalink
Fix crash when classifying embedded test classifications (#76576)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored Jan 2, 2025
2 parents bbbe484 + 8cdd391 commit dabda72
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

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

public partial class SemanticClassifierTests : AbstractCSharpClassifierTests
public sealed partial class SemanticClassifierTests : AbstractCSharpClassifierTests
{
private const string s_testMarkup = """
Expand Down Expand Up @@ -53,6 +53,27 @@ void M()
await TestEmbeddedCSharpWithMultipleSpansAsync(allCode, testHost, spans, expected);
}

private async Task TestSingleLineEmbeddedCSharpAsync(
string code,
TestHost testHost,
params FormattedClassification[] expected)
{
var allCode = $$"""""
class C
{
void M()
{
Test.M(""""{{code}}"""");
}
}
""""" + s_testMarkup;

var start = allCode.IndexOf(code, StringComparison.Ordinal);
var length = code.Length;
var spans = ImmutableArray.Create(new TextSpan(start, length));
await TestEmbeddedCSharpWithMultipleSpansAsync(allCode, testHost, spans, expected);
}

private async Task TestEmbeddedCSharpWithMultipleSpansAsync(
string allCode,
TestHost testHost,
Expand Down Expand Up @@ -457,4 +478,26 @@ class D
Punctuation.Semicolon,
Punctuation.CloseCurly);
}

[Theory, CombinatorialData]
[WorkItem("https://github.com/dotnet/roslyn/issues/76575")]
public async Task TestOnlyMarkup1(TestHost testHost)
{
await TestEmbeddedCSharpAsync(
"[||]",
testHost,
TestCodeMarkdown("[|"),
TestCodeMarkdown("|]"));
}

[Theory, CombinatorialData]
[WorkItem("https://github.com/dotnet/roslyn/issues/76575")]
public async Task TestOnlyMarkup2(TestHost testHost)
{
await TestSingleLineEmbeddedCSharpAsync(
"[||]",
testHost,
TestCodeMarkdown("[|"),
TestCodeMarkdown("|]"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ namespace Microsoft.CodeAnalysis.CSharp.Features.EmbeddedLanguages;
[ExportEmbeddedLanguageClassifier(
PredefinedEmbeddedLanguageNames.CSharpTest, [LanguageNames.CSharp], supportsUnannotatedAPIs: false,
PredefinedEmbeddedLanguageNames.CSharpTest), Shared]
internal sealed class CSharpTestEmbeddedLanguageClassifier : IEmbeddedLanguageClassifier
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class CSharpTestEmbeddedLanguageClassifier() : IEmbeddedLanguageClassifier
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpTestEmbeddedLanguageClassifier()
{
}

private static TextSpan FromBounds(VirtualChar vc1, VirtualChar vc2)
=> TextSpan.FromBounds(vc1.Span.Start, vc2.Span.End);

Expand Down Expand Up @@ -90,7 +86,7 @@ public void RegisterClassifications(EmbeddedLanguageClassificationContext contex
}
}
}
else
else if (!virtualCharsWithoutMarkup.IsEmpty)
{
context.AddClassification(
ClassificationTypeNames.TestCode,
Expand Down

0 comments on commit dabda72

Please sign in to comment.