-
Notifications
You must be signed in to change notification settings - Fork 196
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
Cohost document highlighting #10656
Merged
davidwengier
merged 10 commits into
dotnet:main
from
davidwengier:CohostDocumentHighlight
Jul 23, 2024
Merged
Cohost document highlighting #10656
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e47b5c6
Create endpoint and remote service interface
davidwengier e6a914d
Add tests to ensure RazorServices and Services.props are correct
davidwengier 1ebf1de
Create remote service and register etc.
davidwengier 6831461
Turn off old endpoint in cohosting
davidwengier 626a8ab
Tests!
davidwengier 31936ef
Another test, which found a product bug, and a fix for the product bug
davidwengier fb77765
Make RemoteResponse re-usable and implement
davidwengier 97c0acd
Rework remote response
davidwengier 1371cb5
Update src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensio…
davidwengier 761afd2
Nullability
davidwengier 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 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 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 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 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 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 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
28 changes: 28 additions & 0 deletions
28
...osoft.CodeAnalysis.Razor.Workspaces/Protocol/DocumentHighlight/RemoteDocumentHighlight.cs
This file contains 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,28 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Runtime.Serialization; | ||
using Microsoft.CodeAnalysis.Razor.Workspaces; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Microsoft.VisualStudio.LanguageServer.Protocol; | ||
using RLSP = Roslyn.LanguageServer.Protocol; | ||
|
||
namespace Microsoft.CodeAnalysis.Razor.Protocol.DocumentHighlight; | ||
|
||
using DocumentHighlight = VisualStudio.LanguageServer.Protocol.DocumentHighlight; | ||
|
||
[DataContract] | ||
internal readonly record struct RemoteDocumentHighlight( | ||
[property: DataMember(Order = 0)] LinePositionSpan Position, | ||
[property: DataMember(Order = 1)] DocumentHighlightKind Kind) | ||
{ | ||
public static RemoteDocumentHighlight FromRLSPDocumentHighlight(RLSP.DocumentHighlight h) | ||
=> new RemoteDocumentHighlight(h.Range.ToLinePositionSpan(), (DocumentHighlightKind)h.Kind); | ||
|
||
public static DocumentHighlight ToLspDocumentHighlight(RemoteDocumentHighlight r) | ||
=> new DocumentHighlight | ||
{ | ||
Range = r.Position.ToRange(), | ||
Kind = r.Kind | ||
}; | ||
} |
15 changes: 15 additions & 0 deletions
15
...zor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Remote/IRemoteDocumentHighlightService.cs
This file contains 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,15 @@ | ||
// 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 Microsoft.CodeAnalysis.Razor.Protocol.DocumentHighlight; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace Microsoft.CodeAnalysis.Razor.Remote; | ||
|
||
internal interface IRemoteDocumentHighlightService | ||
{ | ||
ValueTask<RemoteResponse<RemoteDocumentHighlight[]?>> GetHighlightsAsync(RazorPinnedSolutionInfoWrapper solutionInfo, DocumentId documentId, LinePosition position, CancellationToken cancellationToken); | ||
} |
This file contains 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
44 changes: 29 additions & 15 deletions
44
src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Remote/RazorServices.cs
This file contains 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,37 +1,51 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Razor.Serialization.MessagePack.Resolvers; | ||
using Microsoft.CodeAnalysis.ExternalAccess.Razor; | ||
|
||
namespace Microsoft.CodeAnalysis.Razor.Remote; | ||
|
||
internal static class RazorServices | ||
{ | ||
private const string ComponentName = "Razor"; | ||
|
||
public static readonly RazorServiceDescriptorsWrapper Descriptors = new( | ||
ComponentName, | ||
featureDisplayNameProvider: feature => $"Razor {feature} Feature", | ||
additionalFormatters: [], | ||
additionalResolvers: TopLevelResolvers.All, | ||
interfaces: | ||
// Internal for testing | ||
internal static readonly IEnumerable<(Type, Type?)> MessagePackServices = | ||
[ | ||
(typeof(IRemoteLinkedEditingRangeService), null), | ||
(typeof(IRemoteTagHelperProviderService), null), | ||
(typeof(IRemoteClientInitializationService), null), | ||
(typeof(IRemoteSemanticTokensService), null), | ||
(typeof(IRemoteHtmlDocumentService), null), | ||
(typeof(IRemoteUriPresentationService), null), | ||
(typeof(IRemoteFoldingRangeService), null) | ||
]); | ||
(typeof(IRemoteFoldingRangeService), null), | ||
(typeof(IRemoteDocumentHighlightService), null), | ||
]; | ||
|
||
// Internal for testing | ||
internal static readonly IEnumerable<(Type, Type?)> JsonServices = | ||
[ | ||
(typeof(IRemoteSignatureHelpService), null), | ||
]; | ||
|
||
private const string ComponentName = "Razor"; | ||
|
||
public static readonly RazorServiceDescriptorsWrapper Descriptors = new( | ||
ComponentName, | ||
featureDisplayNameProvider: GetFeatureDisplayName, | ||
additionalFormatters: [], | ||
additionalResolvers: TopLevelResolvers.All, | ||
interfaces: MessagePackServices); | ||
|
||
public static readonly RazorServiceDescriptorsWrapper JsonDescriptors = new( | ||
ComponentName, // Needs to match the above because so much of our ServiceHub infrastructure is convention based | ||
featureDisplayNameProvider: feature => $"Razor {feature} Feature", | ||
featureDisplayNameProvider: GetFeatureDisplayName, | ||
jsonConverters: RazorServiceDescriptorsWrapper.GetLspConverters(), | ||
interfaces: | ||
[ | ||
(typeof(IRemoteSignatureHelpService), null), | ||
]); | ||
interfaces: JsonServices); | ||
|
||
private static string GetFeatureDisplayName(string feature) | ||
{ | ||
return $"Razor {feature} Feature"; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Remote/RemoteResponse.cs
This file contains 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,16 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Runtime.Serialization; | ||
|
||
namespace Microsoft.CodeAnalysis.Razor.Remote; | ||
|
||
[DataContract] | ||
internal record struct RemoteResponse<T>( | ||
[property: DataMember(Order = 0)] bool StopHandling, | ||
[property: DataMember(Order = 1)] T Result) | ||
{ | ||
public static RemoteResponse<T> CallHtml => new(StopHandling: false, Result: default!); | ||
public static RemoteResponse<T> NoFurtherHandling => new(StopHandling: true, Result: default!); | ||
public static RemoteResponse<T> Results(T result) => new(StopHandling: false, Result: result); | ||
} |
92 changes: 92 additions & 0 deletions
92
...c/Microsoft.CodeAnalysis.Remote.Razor/DocumentHighlight/RemoteDocumentHighlightService.cs
This file contains 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,92 @@ | ||
// 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.AspNetCore.Razor.Language; | ||
using Microsoft.AspNetCore.Razor.PooledObjects; | ||
using Microsoft.CodeAnalysis.ExternalAccess.Razor; | ||
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost.Handlers; | ||
using Microsoft.CodeAnalysis.Razor.DocumentMapping; | ||
using Microsoft.CodeAnalysis.Razor.Protocol; | ||
using Microsoft.CodeAnalysis.Razor.Protocol.DocumentHighlight; | ||
using Microsoft.CodeAnalysis.Razor.Remote; | ||
using Microsoft.CodeAnalysis.Razor.Workspaces; | ||
using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Response = Microsoft.CodeAnalysis.Razor.Remote.RemoteResponse<Microsoft.CodeAnalysis.Razor.Protocol.DocumentHighlight.RemoteDocumentHighlight[]?>; | ||
|
||
namespace Microsoft.CodeAnalysis.Remote.Razor; | ||
|
||
internal sealed partial class RemoteDocumentHighlightService(in ServiceArgs args) : RazorDocumentServiceBase(in args), IRemoteDocumentHighlightService | ||
{ | ||
internal sealed class Factory : FactoryBase<IRemoteDocumentHighlightService> | ||
{ | ||
protected override IRemoteDocumentHighlightService CreateService(in ServiceArgs args) | ||
=> new RemoteDocumentHighlightService(in args); | ||
} | ||
|
||
private readonly IRazorDocumentMappingService _documentMappingService = args.ExportProvider.GetExportedValue<IRazorDocumentMappingService>(); | ||
private readonly IFilePathService _filePathService = args.ExportProvider.GetExportedValue<IFilePathService>(); | ||
|
||
public ValueTask<Response> GetHighlightsAsync( | ||
RazorPinnedSolutionInfoWrapper solutionInfo, | ||
DocumentId razorDocumentId, | ||
LinePosition position, | ||
CancellationToken cancellationToken) | ||
=> RunServiceAsync( | ||
solutionInfo, | ||
razorDocumentId, | ||
context => GetHighlightsAsync(context, position, cancellationToken), | ||
cancellationToken); | ||
|
||
private async ValueTask<Response> GetHighlightsAsync( | ||
RemoteDocumentContext context, | ||
LinePosition position, | ||
CancellationToken cancellationToken) | ||
{ | ||
var sourceText = await context.GetSourceTextAsync(cancellationToken).ConfigureAwait(false); | ||
if (!sourceText.TryGetAbsoluteIndex(position.Line, position.Character, out var index)) | ||
{ | ||
return Response.NoFurtherHandling; | ||
} | ||
|
||
var codeDocument = await context.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
var languageKind = _documentMappingService.GetLanguageKind(codeDocument, index, rightAssociative: true); | ||
if (languageKind is RazorLanguageKind.Html) | ||
{ | ||
return Response.CallHtml; | ||
} | ||
else if (languageKind is RazorLanguageKind.Razor) | ||
{ | ||
return Response.NoFurtherHandling; | ||
} | ||
|
||
var csharpDocument = codeDocument.GetCSharpDocument(); | ||
if (_documentMappingService.TryMapToGeneratedDocumentPosition(csharpDocument, index, out var mappedPosition, out _)) | ||
{ | ||
var generatedDocument = await context.GetGeneratedDocumentAsync(_filePathService, cancellationToken).ConfigureAwait(false); | ||
|
||
var highlights = await DocumentHighlights.GetHighlightsAsync(generatedDocument, mappedPosition, cancellationToken).ConfigureAwait(false); | ||
|
||
if (highlights is not null) | ||
{ | ||
using var results = new PooledArrayBuilder<RemoteDocumentHighlight>(); | ||
|
||
foreach (var highlight in highlights) | ||
{ | ||
if (_documentMappingService.TryMapToHostDocumentRange(csharpDocument, highlight.Range.ToLinePositionSpan(), out var mappedRange)) | ||
{ | ||
highlight.Range = mappedRange.ToRLSPRange(); | ||
results.Add(RemoteDocumentHighlight.FromRLSPDocumentHighlight(highlight)); | ||
} | ||
} | ||
|
||
return Response.Results(results.ToArray()); | ||
} | ||
} | ||
|
||
return Response.NoFurtherHandling; | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love elevating this concept so that any service can use it. I have a few ideas and thoughts that you can take or leave:
Data
or something else is a better name. To me, the wordsResult
andResponse
are bit too close semantically and are pretty much interchangeable.T : class
constraint?HasData
and/orTryGetData(...)
APIs. IfT
is constrained toclass
, declareData
asT?
and then add[MemberNotNullWhen(true, Data)]
toHasData
and[NotNullWhen(true)]
toTryGetData
.StopHandling
feels a little awkward to "handle" on the client side. I wonder if there's a better name or combination of signals. In general, it feels strange that the remote service would have so much knowledge about what the client does with the response. It also feels a bit odd to me that aRemoteResponse
might have aResult
andStopHandling == false
. I don't have any great suggestions, so please don't make any changes related to this unless you have better ideas. However, I suggest that we keep thinking on this logic.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type is the epitomy of "naming is hard". Trying to work out names for the parameters, and the helper properties, that didn't clash and meant something etc. I do not disagree with any of your feedback.
I'd like to re-use part of this PR in the inlay hint work I'm currently doing though, so if you don't mind I think I'll merge this as is, but will do another pass on this class specifically in a separate PR. I think making it not a record might be the key, so we can separate out consructor params, properties etc. and allow some more nuance in these.
I will say for your item number 2, at the moment no, the first use of this was with Roslyn's
TextChange
which is a struct. It certainly would have made the nullability bits easier though :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no problem with merging this as is. I just wanted to write my thoughts down. We can definitely get back to them later as we go.