Skip to content

Commit

Permalink
Merge pull request #69893 from dibarbet/hover_fixes
Browse files Browse the repository at this point in the history
Fix escaping and wrapping in vscode hover
  • Loading branch information
dibarbet authored Sep 13, 2023
2 parents 938295b + d385fab commit f868ca1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -892,15 +892,17 @@ static string GetCodeBlockLanguageName(string language)

static string GetStyledText(TaggedText taggedText, bool isInCodeBlock)
{
var text = isInCodeBlock ? taggedText.Text : s_markdownEscapeRegex.Replace(taggedText.Text, @"\$1");
var isCode = isInCodeBlock || taggedText.Style is TaggedTextStyle.Code;
var text = isCode ? taggedText.Text : s_markdownEscapeRegex.Replace(taggedText.Text, @"\$1");

// For non-cref links, the URI is present in both the hint and target.
if (!string.IsNullOrEmpty(taggedText.NavigationHint) && taggedText.NavigationHint == taggedText.NavigationTarget)
return $"[{text}]({taggedText.NavigationHint})";

// Markdown ignores spaces at the start of lines outside of code blocks,
// so to get indented lines we replace the spaces with these.
if (!isInCodeBlock)
// so we replace regular spaces with non-breaking spaces to ensure structural space is retained.
// We want to use regular spaces everywhere else to allow the client to wrap long text.
if (!isCode && taggedText.Tag is TextTags.Space or TextTags.ContainerStart)
text = text.Replace(" ", " ");

return taggedText.Style switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,13 @@ void M()
void A.AMethod(int i)
```
A cref A\.AMethod\(int\)
**strong text**
_italic text_
<u>underline&nbsp;text</u>
A cref&nbsp;A\.AMethod\(int\)
**strong text**
_italic text_
<u>underline text</u>
•&nbsp;Item&nbsp;1\.
•&nbsp;Item&nbsp;2\.
•&nbsp;Item 1\.
•&nbsp;Item 2\.
[link text](https://google.com)";

Expand Down
61 changes: 49 additions & 12 deletions src/Features/LanguageServer/ProtocolUnitTests/Hover/HoverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,20 +245,20 @@ public async Task TestGetHoverAsync_UsingMarkupContent(bool mutatingLspWorkspace
void A.AMethod(int i)
```
A&nbsp;cref&nbsp;A\.AMethod\(int\)
**strong&nbsp;text**
_italic&nbsp;text_
<u>underline&nbsp;text</u>
A cref&nbsp;A\.AMethod\(int\)
**strong text**
_italic text_
<u>underline text</u>
•&nbsp;Item&nbsp;1\.
•&nbsp;Item&nbsp;2\.
•&nbsp;Item 1\.
•&nbsp;Item 2\.
[link text](https://google.com)
Remarks&nbsp;are&nbsp;cool&nbsp;too\.
Remarks are cool too\.
{FeaturesResources.Returns_colon}
&nbsp;&nbsp;a&nbsp;string
&nbsp;&nbsp;a string
{FeaturesResources.Exceptions_colon}
&nbsp;&nbsp;System\.NullReferenceException
Expand Down Expand Up @@ -375,13 +375,13 @@ public async Task TestGetHoverAsync_UsingMarkupContentProperlyEscapes(bool mutat
void A.AMethod(int i)
```
Some&nbsp;\{curly\}&nbsp;\[braces\]&nbsp;and&nbsp;\(parens\)
Some \{curly\} \[braces\] and \(parens\)
\#Hashtag
1&nbsp;\+&nbsp;1&nbsp;\-&nbsp;1
1 \+ 1 \- 1
Period\.
Exclaim\!
**strong\\\*\*&nbsp;text**
_italic\_&nbsp;\*\*text\*\*_
**strong\\\*\* text**
_italic\_ \*\*text\*\*_
[closing\] link](https://google.com)
";

Expand All @@ -391,6 +391,43 @@ void A.AMethod(int i)
Assert.Equal(expectedMarkdown, results.Contents.Value.Fourth.Value);
}

[Theory, CombinatorialData]
public async Task TestGetHoverAsync_UsingMarkupContentDoesNotEscapeCode(bool mutatingLspWorkspace)
{
var markup =
@"class A
{
/// <summary>
/// <c>
/// if (true) {
/// Console.WriteLine(""hello"");
/// }
/// </c>
/// </summary>
void {|caret:AMethod|}(int i)
{
}
}";
var clientCapabilities = new LSP.ClientCapabilities
{
TextDocument = new LSP.TextDocumentClientCapabilities { Hover = new LSP.HoverSetting { ContentFormat = new LSP.MarkupKind[] { LSP.MarkupKind.Markdown } } }
};
await using var testLspServer = await CreateTestLspServerAsync(markup, mutatingLspWorkspace, clientCapabilities);
var expectedLocation = testLspServer.GetLocations("caret").Single();

var expectedMarkdown = @"```csharp
void A.AMethod(int i)
```
`if (true) { Console.WriteLine(""hello""); }`
";

var results = await RunGetHoverAsync(
testLspServer,
expectedLocation).ConfigureAwait(false);
Assert.Equal(expectedMarkdown, results.Contents.Value.Fourth.Value);
}

private static async Task<LSP.Hover> RunGetHoverAsync(
TestLspServer testLspServer,
LSP.Location caret,
Expand Down
2 changes: 1 addition & 1 deletion src/Features/Lsif/GeneratorTest/HoverTests.vb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class C
void C.M()
```

Doc&nbsp;Comment "
Doc Comment "
Case Else
Throw TestExceptionUtilities.UnexpectedValue(code)
End Select
Expand Down

0 comments on commit f868ca1

Please sign in to comment.