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

Fix excerpt service to allow for multi line verbatim strings #10675

Merged
merged 1 commit into from
Jul 30, 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 @@ -135,9 +135,13 @@ internal class RazorDocumentExcerptService(
var offset = primarySpan.Start - secondarySpan.Start;
foreach (var classifiedSecondarySpan in classifiedSecondarySpans)
{
Debug.Assert(secondarySpan.Contains(classifiedSecondarySpan.TextSpan));
// It's possible for the classified span to extend past our secondary span, so we cap it
var classifiedSpan = classifiedSecondarySpan.TextSpan.End > secondarySpan.End
? TextSpan.FromBounds(classifiedSecondarySpan.TextSpan.Start, secondarySpan.End)
: classifiedSecondarySpan.TextSpan;
Debug.Assert(secondarySpan.Contains(classifiedSpan));

var updated = new TextSpan(classifiedSecondarySpan.TextSpan.Start + offset, classifiedSecondarySpan.TextSpan.Length);
var updated = new TextSpan(classifiedSpan.Start + offset, classifiedSpan.Length);
Debug.Assert(primarySpan.Contains(updated));

// Make sure that we're not introducing a gap. Remember, we need to fill in the whitespace.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,188 @@ public async Task TryGetExcerptInternalAsync_SingleLine_CanClassifyCSharp_Comple
});
}

[Fact]
public async Task TryGetExcerptInternalAsync_MultiLine_MultilineString()
{
// Arrange
var razorSource = """
<html>
@{
[|string|] bigString = @"
Razor shows 3 lines in a
tooltip maximum, so this
multi-line verbatim
string must be longer
than that.
";
}
</html>
""";

var (primary, secondary, secondarySpan) = await InitializeWithSnapshotAsync(razorSource);

var service = CreateExcerptService(primary);

// Act
var options = RazorClassificationOptionsWrapper.Default;
var result = await service.TryGetExcerptInternalAsync(secondary, secondarySpan, ExcerptModeInternal.Tooltip, options, DisposalToken);

// Assert
Assert.NotNull(result);
Assert.Equal(secondarySpan, result.Value.Span);
Assert.Same(secondary, result.Value.Document);

// Verifies that the right part of the primary document will be highlighted.
Assert.Equal(
(await secondary.GetTextAsync()).GetSubText(secondarySpan).ToString(),
result.Value.Content.GetSubText(result.Value.MappedSpan).ToString(),
ignoreLineEndingDifferences: true);

Assert.Equal("""
<html>
@{
string bigString = @"
Razor shows 3 lines in a
tooltip maximum, so this
multi-line verbatim
""",
result.Value.Content.ToString(), ignoreLineEndingDifferences: true);

Assert.Collection(
result.Value.ClassifiedSpans,
c =>
{
Assert.Equal(ClassificationTypeNames.Text, c.ClassificationType);
Assert.Equal("""
<html>
@{
""",
result.Value.Content.GetSubText(c.TextSpan).ToString(),
ignoreLineEndingDifferences: true);
},
c =>
{
Assert.Equal(ClassificationTypeNames.Text, c.ClassificationType);
Assert.Equal("\r\n ", result.Value.Content.GetSubText(c.TextSpan).ToString(), ignoreLineEndingDifferences: true);
},
c =>
{
Assert.Equal(ClassificationTypeNames.Keyword, c.ClassificationType);
Assert.Equal("string", result.Value.Content.GetSubText(c.TextSpan).ToString());
},
c =>
{
Assert.Equal(ClassificationTypeNames.Text, c.ClassificationType);
Assert.Equal(" ", result.Value.Content.GetSubText(c.TextSpan).ToString());
},
c =>
{
Assert.Equal(ClassificationTypeNames.LocalName, c.ClassificationType);
Assert.Equal("bigString", result.Value.Content.GetSubText(c.TextSpan).ToString());
},
c =>
{
Assert.Equal(ClassificationTypeNames.Text, c.ClassificationType);
Assert.Equal(" ", result.Value.Content.GetSubText(c.TextSpan).ToString());
},
c =>
{
Assert.Equal(ClassificationTypeNames.Operator, c.ClassificationType);
Assert.Equal("=", result.Value.Content.GetSubText(c.TextSpan).ToString());
},
c =>
{
Assert.Equal(ClassificationTypeNames.Text, c.ClassificationType);
Assert.Equal(" ", result.Value.Content.GetSubText(c.TextSpan).ToString());
},
c =>
{
Assert.Equal(ClassificationTypeNames.VerbatimStringLiteral, c.ClassificationType);
Assert.Equal("""
@"
Razor shows 3 lines in a
tooltip maximum, so this
multi-line verbatim
""", result.Value.Content.GetSubText(c.TextSpan).ToString());
});
}

[Fact]
public async Task TryGetExcerptInternalAsync_SingleLine_MultilineString()
{
// Arrange
var razorSource = """
<html>
@{
[|string|] bigString = @"
This is a
multi-line verbatim
string.
";
}
</html>
""";

var (primary, secondary, secondarySpan) = await InitializeWithSnapshotAsync(razorSource);

var service = CreateExcerptService(primary);

// Act
var options = RazorClassificationOptionsWrapper.Default;
var result = await service.TryGetExcerptInternalAsync(secondary, secondarySpan, ExcerptModeInternal.SingleLine, options, DisposalToken);

// Assert
Assert.NotNull(result);
Assert.Equal(secondarySpan, result.Value.Span);
Assert.Same(secondary, result.Value.Document);

// Verifies that the right part of the primary document will be highlighted.
Assert.Equal(
(await secondary.GetTextAsync()).GetSubText(secondarySpan).ToString(),
result.Value.Content.GetSubText(result.Value.MappedSpan).ToString(),
ignoreLineEndingDifferences: true);

Assert.Equal("string bigString = @\"", result.Value.Content.ToString(), ignoreLineEndingDifferences: true);

Assert.Collection(
result.Value.ClassifiedSpans,
c =>
{
Assert.Equal(ClassificationTypeNames.Keyword, c.ClassificationType);
Assert.Equal("string", result.Value.Content.GetSubText(c.TextSpan).ToString());
},
c =>
{
Assert.Equal(ClassificationTypeNames.Text, c.ClassificationType);
Assert.Equal(" ", result.Value.Content.GetSubText(c.TextSpan).ToString());
},
c =>
{
Assert.Equal(ClassificationTypeNames.LocalName, c.ClassificationType);
Assert.Equal("bigString", result.Value.Content.GetSubText(c.TextSpan).ToString());
},
c =>
{
Assert.Equal(ClassificationTypeNames.Text, c.ClassificationType);
Assert.Equal(" ", result.Value.Content.GetSubText(c.TextSpan).ToString());
},
c =>
{
Assert.Equal(ClassificationTypeNames.Operator, c.ClassificationType);
Assert.Equal("=", result.Value.Content.GetSubText(c.TextSpan).ToString());
},
c =>
{
Assert.Equal(ClassificationTypeNames.Text, c.ClassificationType);
Assert.Equal(" ", result.Value.Content.GetSubText(c.TextSpan).ToString());
},
c =>
{
Assert.Equal(ClassificationTypeNames.VerbatimStringLiteral, c.ClassificationType);
Assert.Equal("@\"", result.Value.Content.GetSubText(c.TextSpan).ToString());
});
}

[Fact]
public async Task TryGetExcerptInternalAsync_MultiLine_CanClassifyCSharp()
{
Expand Down