-
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 all 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 | ||
| { | ||
| [OmniSharpEndpoint(OmniSharpEndpoints.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,13 @@ | ||
| #nullable enable | ||
| using System.Collections.Immutable; | ||
|
|
||
| namespace OmniSharp.Models | ||
| { | ||
| public class QuickInfoResponse | ||
| { | ||
| /// <summary> | ||
| /// QuickInfo for the given position, rendered as markdown. | ||
| /// </summary> | ||
| public string Markdown { get; set; } = string.Empty; | ||
| } | ||
| } |
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
275 changes: 275 additions & 0 deletions
275
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,275 @@ | ||
| 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 Microsoft.Extensions.Logging; | ||
| using OmniSharp.Mef; | ||
| using OmniSharp.Models; | ||
| using OmniSharp.Options; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace OmniSharp.Roslyn.CSharp.Services | ||
| { | ||
| [OmniSharpHandler(OmniSharpEndpoints.QuickInfo, LanguageNames.CSharp)] | ||
| public class QuickInfoProvider : IRequestHandler<QuickInfoRequest, QuickInfoResponse> | ||
| { | ||
| // Based on https://github.com/dotnet/roslyn/blob/7dc32a952e77c96c31cae6a2ba6d253a558fc7ff/src/Features/LanguageServer/Protocol/Handler/Hover/HoverHandler.cs | ||
|
|
||
| // 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 | ||
| // https://github.com/dotnet/roslyn/issues/46254 tracks making these public | ||
|
|
||
| /// <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); | ||
| /// <summary> | ||
| /// Section kind for nullability analysis. | ||
| /// </summary> | ||
| internal const string NullabilityAnalysis = nameof(NullabilityAnalysis); | ||
|
|
||
| private readonly OmniSharpWorkspace _workspace; | ||
| private readonly FormattingOptions _formattingOptions; | ||
| private readonly ILogger<QuickInfoProvider>? _logger; | ||
|
|
||
| [ImportingConstructor] | ||
| public QuickInfoProvider(OmniSharpWorkspace workspace, FormattingOptions formattingOptions, ILoggerFactory? loggerFactory) | ||
| { | ||
| _workspace = workspace; | ||
| _formattingOptions = formattingOptions; | ||
| _logger = loggerFactory?.CreateLogger<QuickInfoProvider>(); | ||
| } | ||
|
|
||
| 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); | ||
| if (quickInfoService is null) | ||
| { | ||
| _logger?.LogWarning($"QuickInfo service was null for {document.FilePath}"); | ||
| 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) | ||
| { | ||
| _logger?.LogTrace($"No QuickInfo found for {document.FilePath}:{request.Line},{request.Column}"); | ||
| return response; | ||
| } | ||
|
|
||
| var finalTextBuilder = new StringBuilder(); | ||
| var sectionTextBuilder = new StringBuilder(); | ||
|
|
||
| var description = quickInfo.Sections.FirstOrDefault(s => s.Kind == QuickInfoSectionKinds.Description); | ||
| if (description is object) | ||
| { | ||
| appendSectionAsCsharp(description, finalTextBuilder, _formattingOptions, includeSpaceAtStart: false); | ||
| } | ||
|
|
||
| var summary = quickInfo.Sections.FirstOrDefault(s => s.Kind == QuickInfoSectionKinds.DocumentationComments); | ||
| if (summary is object) | ||
| { | ||
| buildSectionAsMarkdown(summary, sectionTextBuilder, _formattingOptions, out _); | ||
| appendBuiltSection(finalTextBuilder, sectionTextBuilder, _formattingOptions); | ||
| } | ||
|
|
||
| foreach (var section in quickInfo.Sections) | ||
| { | ||
| switch (section.Kind) | ||
| { | ||
| case QuickInfoSectionKinds.Description: | ||
| case QuickInfoSectionKinds.DocumentationComments: | ||
| continue; | ||
|
|
||
| case QuickInfoSectionKinds.TypeParameters: | ||
| appendSectionAsCsharp(section, finalTextBuilder, _formattingOptions); | ||
| break; | ||
|
|
||
| case QuickInfoSectionKinds.AnonymousTypes: | ||
| // The first line is "Anonymous Types:" | ||
| buildSectionAsMarkdown(section, sectionTextBuilder, _formattingOptions, out int lastIndex, untilLineBreak: true); | ||
| appendBuiltSection(finalTextBuilder, sectionTextBuilder, _formattingOptions); | ||
|
|
||
| // Then we want all anonymous types to be C# highlighted | ||
| appendSectionAsCsharp(section, finalTextBuilder, _formattingOptions, lastIndex + 1); | ||
| break; | ||
|
|
||
| case NullabilityAnalysis: | ||
| // Italicize the nullable analysis for emphasis. | ||
| buildSectionAsMarkdown(section, sectionTextBuilder, _formattingOptions, out _); | ||
| appendBuiltSection(finalTextBuilder, sectionTextBuilder, _formattingOptions, italicize: true); | ||
| break; | ||
|
|
||
| default: | ||
| buildSectionAsMarkdown(section, sectionTextBuilder, _formattingOptions, out _); | ||
| appendBuiltSection(finalTextBuilder, sectionTextBuilder, _formattingOptions); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| response.Markdown = finalTextBuilder.ToString().Trim(); | ||
|
|
||
| return response; | ||
|
|
||
| static void appendBuiltSection(StringBuilder finalTextBuilder, StringBuilder stringBuilder, FormattingOptions formattingOptions, bool italicize = false) | ||
| { | ||
| // Two newlines to trigger a markdown new paragraph | ||
| finalTextBuilder.Append(formattingOptions.NewLine); | ||
| finalTextBuilder.Append(formattingOptions.NewLine); | ||
| if (italicize) | ||
| { | ||
| finalTextBuilder.Append("_"); | ||
| } | ||
| finalTextBuilder.Append(stringBuilder); | ||
| if (italicize) | ||
| { | ||
| finalTextBuilder.Append("_"); | ||
| } | ||
| stringBuilder.Clear(); | ||
| } | ||
|
|
||
| static void appendSectionAsCsharp(QuickInfoSection section, StringBuilder builder, FormattingOptions formattingOptions, int startingIndex = 0, bool includeSpaceAtStart = true) | ||
| { | ||
| if (includeSpaceAtStart) | ||
| { | ||
| builder.Append(formattingOptions.NewLine); | ||
| } | ||
| builder.Append("```csharp"); | ||
| builder.Append(formattingOptions.NewLine); | ||
| for (int i = startingIndex; i < section.TaggedParts.Length; i++) | ||
| { | ||
| TaggedText part = section.TaggedParts[i]; | ||
| if (part.Tag == TextTags.LineBreak && i + 1 != section.TaggedParts.Length) | ||
| { | ||
| builder.Append(formattingOptions.NewLine); | ||
| } | ||
| else | ||
| { | ||
| builder.Append(part.Text); | ||
| } | ||
| } | ||
| builder.Append(formattingOptions.NewLine); | ||
| builder.Append("```"); | ||
| } | ||
|
|
||
| static void buildSectionAsMarkdown(QuickInfoSection section, StringBuilder stringBuilder, FormattingOptions formattingOptions, out int lastIndex, bool untilLineBreak = false) | ||
| { | ||
| bool isInCodeBlock = false; | ||
| lastIndex = 0; | ||
| for (int i = 0; i < section.TaggedParts.Length; i++) | ||
| { | ||
| var current = section.TaggedParts[i]; | ||
| lastIndex = i; | ||
|
|
||
| switch (current.Tag) | ||
| { | ||
| case TextTags.Text when !isInCodeBlock: | ||
| stringBuilder.Append(current.Text); | ||
| break; | ||
|
|
||
| case TextTags.Text: | ||
| endBlock(); | ||
| stringBuilder.Append(current.Text); | ||
| break; | ||
|
|
||
| case TextTags.Space when isInCodeBlock: | ||
| if (nextIsTag(i, TextTags.Text)) | ||
| { | ||
| endBlock(); | ||
| } | ||
|
|
||
| stringBuilder.Append(current.Text); | ||
| break; | ||
|
|
||
| case TextTags.Space: | ||
| case TextTags.Punctuation: | ||
| stringBuilder.Append(current.Text); | ||
| break; | ||
|
|
||
| case ContainerStart: | ||
| addNewline(); | ||
| stringBuilder.Append(current.Text); | ||
| break; | ||
|
|
||
| case ContainerEnd: | ||
| addNewline(); | ||
| break; | ||
|
|
||
| case TextTags.LineBreak when untilLineBreak && stringBuilder.Length != 0: | ||
| // The section will end and another newline will be appended, no need to add yet another newline. | ||
| return; | ||
|
|
||
| case TextTags.LineBreak: | ||
| if (stringBuilder.Length != 0 && !nextIsTag(i, ContainerStart, ContainerEnd) && i + 1 != section.TaggedParts.Length) | ||
| { | ||
| addNewline(); | ||
| } | ||
| break; | ||
|
|
||
| default: | ||
| if (!isInCodeBlock) | ||
| { | ||
| isInCodeBlock = true; | ||
| stringBuilder.Append('`'); | ||
| } | ||
| stringBuilder.Append(current.Text); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (isInCodeBlock) | ||
| { | ||
| endBlock(); | ||
| } | ||
|
|
||
| return; | ||
|
|
||
| void addNewline() | ||
| { | ||
| if (isInCodeBlock) | ||
| { | ||
| endBlock(); | ||
| } | ||
|
|
||
| // Markdown needs 2 linebreaks to make a new paragraph | ||
| stringBuilder.Append(formattingOptions.NewLine); | ||
| stringBuilder.Append(formattingOptions.NewLine); | ||
| } | ||
|
|
||
| void endBlock() | ||
| { | ||
| stringBuilder.Append('`'); | ||
| isInCodeBlock = false; | ||
| } | ||
|
|
||
| bool nextIsTag(int i, params string[] tags) | ||
| { | ||
| int nextI = i + 1; | ||
| return nextI < section.TaggedParts.Length && tags.Contains(section.TaggedParts[nextI].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.