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

Introduces a new hover provider, under V2 of the protocol, that uses Roslyn's QuickInfoService #1860

Merged
merged 6 commits into from
Jul 25, 2020
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
2 changes: 1 addition & 1 deletion .pipelines/init.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parameters:
# Configuration: Release
Verbosity: Normal
DotNetVersion: "3.1.201"
DotNetVersion: "3.1.302"
CakeVersion: "0.32.1"
NuGetVersion: "4.9.2"
steps:
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ resources:

variables:
Verbosity: Diagnostic
DotNetVersion: "3.1.201"
DotNetVersion: "3.1.302"
CakeVersion: "0.32.1"
NuGetVersion: "4.9.2"
GitVersionVersion: "5.0.1"
Expand Down
2 changes: 1 addition & 1 deletion build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"DotNetInstallScriptURL": "https://dot.net/v1",
"DotNetChannel": "Preview",
"DotNetVersions": [
"3.1.201",
"3.1.302",
"5.0.100-preview.7.20366.6"
],
"RequiredMonoVersion": "6.6.0",
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "3.1.201"
"version": "3.1.302"
}
}
9 changes: 9 additions & 0 deletions src/OmniSharp.Abstractions/Models/v2/QuickInfoRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using OmniSharp.Mef;

namespace OmniSharp.Models.v2
{
[OmniSharpEndpoint(OmniSharpEndpoints.V2.QuickInfo, typeof(QuickInfoRequest), typeof(QuickInfoResponse))]
public class QuickInfoRequest : Request
{
}
}
35 changes: 35 additions & 0 deletions src/OmniSharp.Abstractions/Models/v2/QuickInfoResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#nullable enable
namespace OmniSharp.Models.v2
{
public class QuickInfoResponse
{
/// <summary>
/// Description of the symbol under the cursor. This is expected to be rendered as a C# codeblock
/// </summary>
public string? Description { get; set; }

/// <summary>
/// Documentation of the symbol under the cursor, if present. It is expected to be rendered as markdown.
/// </summary>
public string? Summary { get; set; }
333fred marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Other relevant information to the symbol under the cursor.
/// </summary>
public QuickInfoResponseSection[]? RemainingSections { get; set; }
}

public struct QuickInfoResponseSection
{
/// <summary>
/// If true, the text should be rendered as C# code. If false, the text should be rendered as markdown.
/// </summary>
public bool IsCSharpCode { get; set; }
public string Text { get; set; }

public override string ToString()
{
return $@"{{ IsCSharpCode = {IsCSharpCode}, Text = ""{Text}"" }}";
}
333fred marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 2 additions & 0 deletions src/OmniSharp.Abstractions/OmniSharpEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public static class V2
public const string CodeStructure = "/v2/codestructure";

public const string Highlight = "/v2/highlight";

public const string QuickInfo = "/v2/quickinfo";
333fred marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
197 changes: 197 additions & 0 deletions src/OmniSharp.Roslyn.CSharp/Services/QuickInfoProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.QuickInfo;
using Microsoft.CodeAnalysis.Text;
using OmniSharp.Mef;
using OmniSharp.Models.v2;
using OmniSharp.Options;

#nullable enable

namespace OmniSharp.Roslyn.CSharp.Services
{
[OmniSharpHandler(OmniSharpEndpoints.V2.QuickInfo, LanguageNames.CSharp)]
public class QuickInfoProvider : IRequestHandler<QuickInfoRequest, QuickInfoResponse>
{
// Based on https://github.com/dotnet/roslyn/blob/master/src/Features/LanguageServer/Protocol/Handler/Hover/HoverHandler.cs
333fred marked this conversation as resolved.
Show resolved Hide resolved

// These are internal tag values taken from https://github.com/dotnet/roslyn/blob/master/src/Features/Core/Portable/Common/TextTags.cs
// They're copied here so that we can ensure we render blocks correctly in the markdown

/// <summary>
/// Indicates the start of a text container. The elements after <see cref="ContainerStart"/> through (but not
/// including) the matching <see cref="ContainerEnd"/> are rendered in a rectangular block which is positioned
/// as an inline element relative to surrounding elements. The text of the <see cref="ContainerStart"/> element
/// itself precedes the content of the container, and is typically a bullet or number header for an item in a
/// list.
/// </summary>
private const string ContainerStart = nameof(ContainerStart);
/// <summary>
/// Indicates the end of a text container. See <see cref="ContainerStart"/>.
/// </summary>
private const string ContainerEnd = nameof(ContainerEnd);

private readonly OmniSharpWorkspace _workspace;
private readonly FormattingOptions _formattingOptions;

[ImportingConstructor]
public QuickInfoProvider(OmniSharpWorkspace workspace, FormattingOptions formattingOptions)
{
_workspace = workspace;
_formattingOptions = formattingOptions;
}

public async Task<QuickInfoResponse> Handle(QuickInfoRequest request)
{
var document = _workspace.GetDocument(request.FileName);
var response = new QuickInfoResponse();

if (document is null)
{
return response;
}

var quickInfoService = QuickInfoService.GetService(document);
333fred marked this conversation as resolved.
Show resolved Hide resolved
if (quickInfoService is null)
{
return response;
}

var sourceText = await document.GetTextAsync();
var position = sourceText.Lines.GetPosition(new LinePosition(request.Line, request.Column));

var quickInfo = await quickInfoService.GetQuickInfoAsync(document, position);
if (quickInfo is null)
{
return response;
}


var sb = new StringBuilder();
response.Description = quickInfo.Sections.FirstOrDefault(s => s.Kind == QuickInfoSectionKinds.Description)?.Text;

var documentation = quickInfo.Sections.FirstOrDefault(s => s.Kind == QuickInfoSectionKinds.DocumentationComments);
if (documentation is object)
{
response.Summary = getMarkdown(documentation.TaggedParts);
}

response.RemainingSections = quickInfo.Sections
.Where(s => s.Kind != QuickInfoSectionKinds.Description && s.Kind != QuickInfoSectionKinds.DocumentationComments)
.Select(s =>
{
switch (s.Kind)
{
case QuickInfoSectionKinds.AnonymousTypes:
case QuickInfoSectionKinds.TypeParameters:
return new QuickInfoResponseSection { IsCSharpCode = true, Text = s.Text };

default:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we special case any example as well and emit them as full markdown code blocks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe there is an examples section in quickinfo.

return new QuickInfoResponseSection { IsCSharpCode = false, Text = getMarkdown(s.TaggedParts) };
}
})
.ToArray();

return response;

string getMarkdown(ImmutableArray<TaggedText> taggedTexts)
333fred marked this conversation as resolved.
Show resolved Hide resolved
{
bool isInCodeBlock = false;
var sb = new StringBuilder();
for (int i = 0; i < taggedTexts.Length; i++)
{
var current = taggedTexts[i];

switch (current.Tag)
{
case TextTags.Text when !isInCodeBlock:
sb.Append(current.Text);
break;

case TextTags.Text:
endBlock();
sb.Append(current.Text);
break;

case TextTags.Space when isInCodeBlock:
if (nextIsTag(TextTags.Text, i))
{
endBlock();
}

sb.Append(current.Text);
break;

case TextTags.Space:
case TextTags.Punctuation:
sb.Append(current.Text);
break;

case ContainerStart:
// Markdown needs 2 linebreaks to make a new paragraph
addNewline();
addNewline();
sb.Append(current.Text);
break;

case ContainerEnd:
// Markdown needs 2 linebreaks to make a new paragraph
addNewline();
addNewline();
break;

case TextTags.LineBreak:
if (!nextIsTag(ContainerStart, i) && !nextIsTag(ContainerEnd, i))
{
addNewline();
addNewline();
}
break;

default:
if (!isInCodeBlock)
{
isInCodeBlock = true;
sb.Append('`');
}
sb.Append(current.Text);
break;
}
}

if (isInCodeBlock)
{
endBlock();
}

return sb.ToString().Trim();

void addNewline()
{
if (isInCodeBlock)
{
endBlock();
}

sb.Append(_formattingOptions.NewLine);
}

void endBlock()
{
sb.Append('`');
isInCodeBlock = false;
}

bool nextIsTag(string tag, int i)
{
int nextI = i + 1;
return nextI < taggedTexts.Length && taggedTexts[nextI].Tag == tag;
}
}
}
}
}
2 changes: 1 addition & 1 deletion test-assets/test-projects/global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "3.1.201"
"version": "3.1.302"
}
}
2 changes: 1 addition & 1 deletion tests/OmniSharp.MSBuild.Tests/ProjectLoadListenerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public async Task The_correct_sdk_version_is_emitted()
using (var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory)))
{
Assert.Single(emitter.ReceivedMessages);
Assert.Equal(GetHashedFileExtension("3.1.201"), emitter.ReceivedMessages[0].SdkVersion);
Assert.Equal(GetHashedFileExtension("3.1.302"), emitter.ReceivedMessages[0].SdkVersion);
}
}

Expand Down
Loading