-
Notifications
You must be signed in to change notification settings - Fork 426
Introduces a new hover provider, under V2 of the protocol, that uses Roslyn's QuickInfoService #1860
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
Merged
Merged
Introduces a new hover provider, under V2 of the protocol, that uses Roslyn's QuickInfoService #1860
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
af15858
Updated .NET SDK.
333fred 017a909
Introduces a new hover provider, under V2 of the protocol, that uses …
333fred 2794241
PR Feedback
333fred 8d0dbc4
Simplify the API by sending a pre-markdowned string, rather than send…
333fred 4682884
Update dotnet tests for 3.1.302.
333fred f51549f
Merge branch 'master' into quickinfo
333fred File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "sdk": { | ||
| "version": "3.1.201" | ||
| "version": "3.1.302" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
|
|
||
| /// <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.
Outdated
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
src/OmniSharp.Roslyn.CSharp/Services/QuickInfoProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
Outdated
|
||
|
|
||
| // 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.
|
||
| 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: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we special case any
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Outdated
|
||
| { | ||
| 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; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "sdk": { | ||
| "version": "3.1.201" | ||
| "version": "3.1.302" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.