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

Enable rich LSIF hover information. #64580

Merged
merged 3 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -69,6 +69,7 @@ public HoverHandler(IGlobalOptionService globalOptions)
int position,
SymbolDescriptionOptions options,
LanguageServices languageServices,
ClientCapabilities clientCapabilities,
CancellationToken cancellationToken)
{
Debug.Assert(semanticModel.Language is LanguageNames.CSharp or LanguageNames.VisualBasic);
Expand All @@ -83,7 +84,7 @@ public HoverHandler(IGlobalOptionService globalOptions)
}

var text = await semanticModel.SyntaxTree.GetTextAsync(cancellationToken).ConfigureAwait(false);
return await GetHoverAsync(info, text, semanticModel.Language, document: null, classificationOptions: null, clientCapabilities: null, cancellationToken).ConfigureAwait(false);
return await GetHoverAsync(info, text, semanticModel.Language, document: null, classificationOptions: null, clientCapabilities, cancellationToken).ConfigureAwait(false);
}

private static async Task<Hover> GetHoverAsync(
Expand Down
17 changes: 16 additions & 1 deletion src/Features/Lsif/Generator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;
using LspProtocol = Microsoft.VisualStudio.LanguageServer.Protocol;
using Methods = Microsoft.VisualStudio.LanguageServer.Protocol.Methods;

namespace Microsoft.CodeAnalysis.LanguageServerIndexFormat.Generator
Expand All @@ -33,6 +34,20 @@ internal sealed class Generator
private const bool FoldingRangeProvider = true;
private const bool DiagnosticProvider = false;

private static readonly LspProtocol.ClientCapabilities LspClientCapabilities = new()
NTaylorMullen marked this conversation as resolved.
Show resolved Hide resolved
{
TextDocument = new LspProtocol.TextDocumentClientCapabilities()
{
Hover = new LspProtocol.HoverSetting()
{
ContentFormat = new[]
{
LspProtocol.MarkupKind.PlainText,
LspProtocol.MarkupKind.Markdown,
}
}
}
};
NTaylorMullen marked this conversation as resolved.
Show resolved Hide resolved
private readonly ILsifJsonWriter _lsifJsonWriter;
private readonly IdFactory _idFactory = new IdFactory();

Expand Down Expand Up @@ -252,7 +267,7 @@ SymbolKind.RangeVariable or
// See /~https://github.com/Microsoft/language-server-protocol/blob/main/indexFormat/specification.md#resultset for an example.
if (symbolResultsTracker.ResultSetNeedsInformationalEdgeAdded(symbolForLinkedResultSet, Methods.TextDocumentHoverName))
{
var hover = await HoverHandler.GetHoverAsync(semanticModel, syntaxToken.SpanStart, options.SymbolDescriptionOptions, languageServices, CancellationToken.None);
var hover = await HoverHandler.GetHoverAsync(semanticModel, syntaxToken.SpanStart, options.SymbolDescriptionOptions, languageServices, LspClientCapabilities, CancellationToken.None);
if (hover != null)
{
var hoverResult = new HoverResult(hover, idFactory);
Expand Down
78 changes: 60 additions & 18 deletions src/Features/Lsif/GeneratorTest/HoverTests.vb
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,46 @@ class C
Dim expectedHoverContents As String
Select Case code
Case "class [|C|] { string s; }"
expectedHoverContents = "class C"
expectedHoverContents = "```csharp
class C
```
"
NTaylorMullen marked this conversation as resolved.
Show resolved Hide resolved
Case "class C { void [|M|]() { } }"
expectedHoverContents = "void C.M()"
expectedHoverContents = "```csharp
void C.M()
```
"
Case "class C { string [|s|]; }"
expectedHoverContents = $"({FeaturesResources.field}) string C.s"
expectedHoverContents = $"```csharp
({FeaturesResources.field}) string C.s
```
"
Case "class C { void M(string [|s|]) { M(s); } }"
expectedHoverContents = $"({FeaturesResources.parameter}) string s"
expectedHoverContents = $"```csharp
({FeaturesResources.parameter}) string s
```
"
Case "class C { void M(string s) { string [|local|] = """"; } }"
expectedHoverContents = $"({FeaturesResources.local_variable}) string local"
expectedHoverContents = $"```csharp
({FeaturesResources.local_variable}) string local
```
"
Case "
class C
{
/// <summary>Doc Comment</summary>
void [|M|]() { }
}"
expectedHoverContents = "void C.M()
Doc Comment"
expectedHoverContents = "```csharp
void C.M()
```

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

Assert.Equal(MarkupKind.PlainText, hoverMarkupContent.Kind)
Assert.Equal(MarkupKind.Markdown, hoverMarkupContent.Kind)
Assert.Equal(expectedHoverContents + Environment.NewLine, hoverMarkupContent.Value)
End Function

Expand Down Expand Up @@ -99,29 +117,50 @@ class C
Dim expectedHoverContents As String
Select Case code
Case "class C { [|string|] s; }"
expectedHoverContents = "class System.String"
expectedHoverContents = "```csharp
class System.String
```
"
Case "class C { void M() { [|M|](); } }"
expectedHoverContents = "void C.M()"
expectedHoverContents = "```csharp
void C.M()
```
"
Case "class C { void M(string s) { M([|s|]); } }"
expectedHoverContents = $"({FeaturesResources.parameter}) string s"
expectedHoverContents = $"```csharp
({FeaturesResources.parameter}) string s
```
"
Case "class C { void M(string s) { string local = """"; M([|local|]); } }"
expectedHoverContents = $"({FeaturesResources.local_variable}) string local"
expectedHoverContents = $"```csharp
({FeaturesResources.local_variable}) string local
```
"
Case "using [|S|] = System.String;"
expectedHoverContents = "class System.String"
expectedHoverContents = "```csharp
class System.String
```
"
Case "class C { [|global|]::System.String s; }"
expectedHoverContents = "<global namespace>"
expectedHoverContents = "```csharp
<global namespace>
```
"
Case "
class C
{
/// <see cref=""C.[|M|]()"" />
void M() { }
}"
expectedHoverContents = "void C.M()"
expectedHoverContents = "```csharp
void C.M()
```
"
Case Else
Throw TestExceptionUtilities.UnexpectedValue(code)
End Select

Assert.Equal(MarkupKind.PlainText, hoverMarkupContent.Kind)
Assert.Equal(MarkupKind.Markdown, hoverMarkupContent.Kind)
Assert.Equal(expectedHoverContents + Environment.NewLine, hoverMarkupContent.Value)
End Function

Expand Down Expand Up @@ -161,8 +200,11 @@ class C
Next

Dim hoverMarkupContent = DirectCast(hoverVertex.Result.Contents.Value.Fourth, MarkupContent)
Assert.Equal(MarkupKind.PlainText, hoverMarkupContent.Kind)
Assert.Equal("class System.String" + Environment.NewLine, hoverMarkupContent.Value)
Assert.Equal(MarkupKind.Markdown, hoverMarkupContent.Kind)
Assert.Equal("```csharp
class System.String
```
" + Environment.NewLine, hoverMarkupContent.Value)
End Function
End Class
End Namespace