-
Notifications
You must be signed in to change notification settings - Fork 229
Add co-hosting support for hover #11150
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
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c670a4e
Add RemoteHoverService to MS.CA.Remote.Razor
DustinCampbell 157ce7f
Add CohostHoverEndpoint to MS.VS.LS.Razor
DustinCampbell 4b9ae46
A couple of small tweaks
DustinCampbell 78865f9
Fix CohostHoverEndpoint registration
DustinCampbell 22ce317
Acquire hover display options from client capabilities
DustinCampbell a51d3db
Add first cohosting hover test
DustinCampbell db1ccec
Add second cohosting hover test
DustinCampbell 2273026
Add HTML cohosting hover test
DustinCampbell b4c31c6
Merge branch 'main' into cohost-hover
DustinCampbell a3a4ff5
Simplify RemoteHoverService logic
DustinCampbell 3b2adab
Debug.Fail(...) if we encounter a language that we don't expect
DustinCampbell 2358aa3
Merge branch 'main' into cohost-hover
DustinCampbell be89fc9
Update CohostHoverEndpoint.GetRegistrations signature
DustinCampbell 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
31 changes: 31 additions & 0 deletions
31
...or/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/ClientCapabilitiesExtensions.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,31 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
|
||
| using System; | ||
|
|
||
| namespace Microsoft.VisualStudio.LanguageServer.Protocol; | ||
|
|
||
| internal static class ClientCapabilitiesExtensions | ||
| { | ||
| public static MarkupKind GetMarkupKind(this ClientCapabilities clientCapabilities) | ||
| { | ||
| // If MarkDown is supported, we'll use that. | ||
| if (clientCapabilities.TextDocument?.Hover?.ContentFormat is MarkupKind[] contentFormat && | ||
| Array.IndexOf(contentFormat, MarkupKind.Markdown) >= 0) | ||
| { | ||
| return MarkupKind.Markdown; | ||
| } | ||
|
|
||
| return MarkupKind.PlainText; | ||
| } | ||
|
|
||
| public static bool SupportsMarkdown(this ClientCapabilities clientCapabilities) | ||
| { | ||
| return clientCapabilities.GetMarkupKind() == MarkupKind.Markdown; | ||
| } | ||
|
|
||
| public static bool SupportsVisualStudioExtensions(this ClientCapabilities clientCapabilities) | ||
| { | ||
| return (clientCapabilities as VSInternalClientCapabilities)?.SupportsVisualStudioExtensions ?? false; | ||
| } | ||
| } |
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
19 changes: 19 additions & 0 deletions
19
src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Remote/IRemoteHoverService.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,19 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
|
||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeAnalysis.ExternalAccess.Razor; | ||
| using RoslynHover = Roslyn.LanguageServer.Protocol.Hover; | ||
| using RoslynPosition = Roslyn.LanguageServer.Protocol.Position; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.Razor.Remote; | ||
|
|
||
| internal interface IRemoteHoverService : IRemoteJsonService | ||
| { | ||
| ValueTask<RemoteResponse<RoslynHover?>> GetHoverAsync( | ||
| JsonSerializableRazorPinnedSolutionInfoWrapper solutionInfo, | ||
| JsonSerializableDocumentId documentId, | ||
| RoslynPosition position, | ||
| CancellationToken cancellationToken); | ||
| } |
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
219 changes: 219 additions & 0 deletions
219
src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Hover/RemoteHoverService.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,219 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
|
||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Razor; | ||
| using Microsoft.AspNetCore.Razor.Language; | ||
| using Microsoft.CodeAnalysis.ExternalAccess.Razor; | ||
| using Microsoft.CodeAnalysis.Razor.DocumentMapping; | ||
| using Microsoft.CodeAnalysis.Razor.Hover; | ||
| using Microsoft.CodeAnalysis.Razor.Protocol; | ||
| using Microsoft.CodeAnalysis.Razor.Remote; | ||
| using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem; | ||
| using Roslyn.LanguageServer.Protocol; | ||
| using static Microsoft.CodeAnalysis.Razor.Remote.RemoteResponse<Roslyn.LanguageServer.Protocol.Hover?>; | ||
| using static Microsoft.VisualStudio.LanguageServer.Protocol.ClientCapabilitiesExtensions; | ||
| using static Microsoft.VisualStudio.LanguageServer.Protocol.VsLspExtensions; | ||
| using static Roslyn.LanguageServer.Protocol.RoslynLspExtensions; | ||
| using ExternalHandlers = Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost.Handlers; | ||
| using Range = Roslyn.LanguageServer.Protocol.Range; | ||
| using VsLsp = Microsoft.VisualStudio.LanguageServer.Protocol; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.Remote.Razor; | ||
|
|
||
| internal sealed class RemoteHoverService(in ServiceArgs args) : RazorDocumentServiceBase(in args), IRemoteHoverService | ||
| { | ||
| internal sealed class Factory : FactoryBase<IRemoteHoverService> | ||
| { | ||
| protected override IRemoteHoverService CreateService(in ServiceArgs args) | ||
| => new RemoteHoverService(in args); | ||
| } | ||
|
|
||
| private readonly IClientCapabilitiesService _clientCapabilitiesService = args.ExportProvider.GetExportedValue<IClientCapabilitiesService>(); | ||
|
|
||
| protected override IDocumentPositionInfoStrategy DocumentPositionInfoStrategy => PreferAttributeNameDocumentPositionInfoStrategy.Instance; | ||
|
|
||
| public ValueTask<RemoteResponse<Hover?>> GetHoverAsync( | ||
| JsonSerializableRazorPinnedSolutionInfoWrapper solutionInfo, | ||
| JsonSerializableDocumentId documentId, | ||
| Position position, | ||
| CancellationToken cancellationToken) | ||
| => RunServiceAsync( | ||
| solutionInfo, | ||
| documentId, | ||
| context => GetHoverAsync(context, position, cancellationToken), | ||
| cancellationToken); | ||
|
|
||
| private async ValueTask<RemoteResponse<Hover?>> GetHoverAsync( | ||
| RemoteDocumentContext context, | ||
| Position position, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| var codeDocument = await context.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false); | ||
|
|
||
| if (!codeDocument.Source.Text.TryGetAbsoluteIndex(position, out var hostDocumentIndex)) | ||
| { | ||
| return NoFurtherHandling; | ||
| } | ||
|
|
||
| var clientCapabilities = _clientCapabilitiesService.ClientCapabilities; | ||
|
|
||
| var positionInfo = GetPositionInfo(codeDocument, hostDocumentIndex, preferCSharpOverHtml: true); | ||
|
|
||
| if (positionInfo.LanguageKind is RazorLanguageKind.Html or RazorLanguageKind.Razor) | ||
| { | ||
| // Sometimes what looks like a html attribute can actually map to C#, in which case its better to let Roslyn try to handle this. | ||
| if (!DocumentMappingService.TryMapToGeneratedDocumentPosition(codeDocument.GetCSharpDocument(), positionInfo.HostDocumentIndex, out _, out _)) | ||
| { | ||
| // Acquire from client capabilities | ||
| var options = HoverDisplayOptions.From(clientCapabilities); | ||
|
|
||
| var razorHover = await HoverFactory | ||
| .GetHoverAsync(codeDocument, hostDocumentIndex, options, context.GetSolutionQueryOperations(), cancellationToken) | ||
| .ConfigureAwait(false); | ||
|
|
||
| if (razorHover is null) | ||
| { | ||
| return CallHtml; | ||
| } | ||
|
|
||
| // Ensure that we convert our Hover to a Roslyn Hover. | ||
| var resultHover = ConvertHover(razorHover); | ||
|
|
||
| return Results(resultHover); | ||
| } | ||
| } | ||
|
|
||
| var csharpDocument = codeDocument.GetCSharpDocument(); | ||
| if (!DocumentMappingService.TryMapToGeneratedDocumentPosition(csharpDocument, positionInfo.HostDocumentIndex, out var mappedPosition, out _)) | ||
| { | ||
| // If we can't map to the generated C# file, we're done. | ||
| return NoFurtherHandling; | ||
| } | ||
|
|
||
| // Finally, call into C#. | ||
| var generatedDocument = await context.Snapshot | ||
| .GetGeneratedDocumentAsync(cancellationToken) | ||
| .ConfigureAwait(false); | ||
|
|
||
| var csharpHover = await ExternalHandlers.Hover | ||
| .GetHoverAsync( | ||
| generatedDocument, | ||
| mappedPosition, | ||
| clientCapabilities.SupportsVisualStudioExtensions(), | ||
| clientCapabilities.SupportsMarkdown(), | ||
| cancellationToken) | ||
| .ConfigureAwait(false); | ||
|
|
||
| if (csharpHover is null) | ||
| { | ||
| return NoFurtherHandling; | ||
| } | ||
|
|
||
| // Map range back to host document | ||
| if (csharpHover.Range is { } range && | ||
| DocumentMappingService.TryMapToHostDocumentRange(csharpDocument, range.ToLinePositionSpan(), out var hostDocumentSpan)) | ||
| { | ||
| csharpHover.Range = RoslynLspFactory.CreateRange(hostDocumentSpan); | ||
| } | ||
|
|
||
| return Results(csharpHover); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Converts a <see cref="VsLsp.Hover"/> to a <see cref="Hover"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Once Razor moves wholly over to Roslyn.LanguageServer.Protocol, this method can be removed. | ||
| /// </remarks> | ||
| private Hover ConvertHover(VsLsp.Hover hover) | ||
| { | ||
| // Note: Razor only ever produces a Hover with MarkupContent or a VSInternalHover with RawContents. | ||
| // Both variants return a Range. | ||
|
|
||
| return hover switch | ||
| { | ||
| VsLsp.VSInternalHover { Range: var range, RawContent: { } rawContent } => new VSInternalHover() | ||
| { | ||
| Range = ConvertRange(range), | ||
| Contents = string.Empty, | ||
| RawContent = ConvertVsContent(rawContent) | ||
| }, | ||
| VsLsp.Hover { Range: var range, Contents.Fourth: VsLsp.MarkupContent contents } => new Hover() | ||
| { | ||
| Range = ConvertRange(range), | ||
| Contents = ConvertMarkupContent(contents) | ||
| }, | ||
| _ => Assumed.Unreachable<Hover>(), | ||
| }; | ||
|
|
||
| static Range? ConvertRange(VsLsp.Range? range) | ||
| { | ||
| return range is not null | ||
| ? RoslynLspFactory.CreateRange(range.ToLinePositionSpan()) | ||
| : null; | ||
| } | ||
|
|
||
| static object ConvertVsContent(object obj) | ||
| { | ||
| return obj switch | ||
| { | ||
| VisualStudio.Core.Imaging.ImageId imageId => ConvertImageId(imageId), | ||
| VisualStudio.Text.Adornments.ImageElement element => ConvertImageElement(element), | ||
| VisualStudio.Text.Adornments.ClassifiedTextRun run => ConvertClassifiedTextRun(run), | ||
| VisualStudio.Text.Adornments.ClassifiedTextElement element => ConvertClassifiedTextElement(element), | ||
| VisualStudio.Text.Adornments.ContainerElement element => ConvertContainerElement(element), | ||
| _ => Assumed.Unreachable<object>() | ||
| }; | ||
|
|
||
| static Roslyn.Core.Imaging.ImageId ConvertImageId(VisualStudio.Core.Imaging.ImageId imageId) | ||
| { | ||
| return new(imageId.Guid, imageId.Id); | ||
| } | ||
|
|
||
| static Roslyn.Text.Adornments.ImageElement ConvertImageElement(VisualStudio.Text.Adornments.ImageElement element) | ||
| { | ||
| return new(ConvertImageId(element.ImageId), element.AutomationName); | ||
| } | ||
|
|
||
| static Roslyn.Text.Adornments.ClassifiedTextRun ConvertClassifiedTextRun(VisualStudio.Text.Adornments.ClassifiedTextRun run) | ||
| { | ||
| return new( | ||
| run.ClassificationTypeName, | ||
| run.Text, | ||
| (Roslyn.Text.Adornments.ClassifiedTextRunStyle)run.Style, | ||
| run.MarkerTagType, | ||
| run.NavigationAction, | ||
| run.Tooltip); | ||
| } | ||
|
|
||
| static Roslyn.Text.Adornments.ClassifiedTextElement ConvertClassifiedTextElement(VisualStudio.Text.Adornments.ClassifiedTextElement element) | ||
| { | ||
| return new(element.Runs.Select(ConvertClassifiedTextRun)); | ||
| } | ||
|
|
||
| static Roslyn.Text.Adornments.ContainerElement ConvertContainerElement(VisualStudio.Text.Adornments.ContainerElement element) | ||
| { | ||
| return new((Roslyn.Text.Adornments.ContainerElementStyle)element.Style, element.Elements.Select(ConvertVsContent)); | ||
| } | ||
| } | ||
|
|
||
| static MarkupContent ConvertMarkupContent(VsLsp.MarkupContent value) | ||
| { | ||
| return new() | ||
| { | ||
| Kind = ConvertMarkupKind(value.Kind), | ||
| Value = value.Value | ||
| }; | ||
| } | ||
|
|
||
| static MarkupKind ConvertMarkupKind(VsLsp.MarkupKind value) | ||
| { | ||
| return value == VsLsp.MarkupKind.Markdown | ||
| ? MarkupKind.Markdown | ||
| : MarkupKind.PlainText; | ||
| } | ||
| } | ||
| } | ||
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.