-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add textDocument/inlayHints support (#9904)
Adds inlay hint support for Razor in VS and VS Code, cohosting and regular. Goes with dotnet/vscode-csharp#6857 for the VS Code side.
- Loading branch information
Showing
20 changed files
with
671 additions
and
53 deletions.
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
15 changes: 15 additions & 0 deletions
15
src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/InlayHints/IInlayHintService.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.VisualStudio.LanguageServer.Protocol; | ||
|
||
namespace Microsoft.AspNetCore.Razor.LanguageServer.InlayHints; | ||
|
||
internal interface IInlayHintService | ||
{ | ||
Task<InlayHint[]?> GetInlayHintsAsync(IClientConnection clientConnection, VersionedDocumentContext documentContext, Range range, CancellationToken cancellationToken); | ||
|
||
Task<InlayHint?> ResolveInlayHintAsync(IClientConnection clientConnection, InlayHint inlayHint, CancellationToken cancellationToken); | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/InlayHints/InlayHintEndpoint.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,37 @@ | ||
// 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.LanguageServer.Common; | ||
using Microsoft.AspNetCore.Razor.LanguageServer.EndpointContracts; | ||
using Microsoft.CodeAnalysis.Razor.Workspaces; | ||
using Microsoft.CommonLanguageServerProtocol.Framework; | ||
using Microsoft.VisualStudio.LanguageServer.Protocol; | ||
|
||
namespace Microsoft.AspNetCore.Razor.LanguageServer.InlayHints; | ||
|
||
[LanguageServerEndpoint(Methods.TextDocumentInlayHintName)] | ||
internal sealed class InlayHintEndpoint(LanguageServerFeatureOptions featureOptions, IInlayHintService inlayHintService, IClientConnection clientConnection) | ||
: IRazorRequestHandler<InlayHintParams, InlayHint[]?>, ICapabilitiesProvider | ||
{ | ||
private readonly LanguageServerFeatureOptions _featureOptions = featureOptions; | ||
private readonly IInlayHintService _inlayHintService = inlayHintService; | ||
private readonly IClientConnection _clientConnection = clientConnection; | ||
|
||
public bool MutatesSolutionState => false; | ||
|
||
public void ApplyCapabilities(VSInternalServerCapabilities serverCapabilities, VSInternalClientCapabilities clientCapabilities) | ||
{ | ||
serverCapabilities.EnableInlayHints(); | ||
} | ||
|
||
public TextDocumentIdentifier GetTextDocumentIdentifier(InlayHintParams request) | ||
=> request.TextDocument; | ||
|
||
public Task<InlayHint[]?> HandleRequestAsync(InlayHintParams request, RazorRequestContext context, CancellationToken cancellationToken) | ||
{ | ||
var documentContext = context.GetRequiredDocumentContext(); | ||
return _inlayHintService.GetInlayHintsAsync(_clientConnection, documentContext, request.Range, cancellationToken); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...azor/src/Microsoft.AspNetCore.Razor.LanguageServer/InlayHints/InlayHintResolveEndpoint.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,25 @@ | ||
// 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.LanguageServer.EndpointContracts; | ||
using Microsoft.CommonLanguageServerProtocol.Framework; | ||
using Microsoft.VisualStudio.LanguageServer.Protocol; | ||
|
||
namespace Microsoft.AspNetCore.Razor.LanguageServer.InlayHints; | ||
|
||
[LanguageServerEndpoint(Methods.InlayHintResolveName)] | ||
internal sealed class InlayHintResolveEndpoint(IInlayHintService inlayHintService, IClientConnection clientConnection) | ||
: IRazorDocumentlessRequestHandler<InlayHint, InlayHint?> | ||
{ | ||
private readonly IInlayHintService _inlayHintService = inlayHintService; | ||
private readonly IClientConnection _clientConnection = clientConnection; | ||
|
||
public bool MutatesSolutionState => false; | ||
|
||
public Task<InlayHint?> HandleRequestAsync(InlayHint request, RazorRequestContext context, CancellationToken cancellationToken) | ||
{ | ||
return _inlayHintService.ResolveInlayHintAsync(_clientConnection, request, cancellationToken); | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/InlayHints/InlayHintService.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,125 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.ComponentModel.Composition; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Razor.Language; | ||
using Microsoft.AspNetCore.Razor.LanguageServer.Common; | ||
using Microsoft.AspNetCore.Razor.LanguageServer.Extensions; | ||
using Microsoft.AspNetCore.Razor.LanguageServer.Protocol; | ||
using Microsoft.AspNetCore.Razor.PooledObjects; | ||
using Microsoft.CodeAnalysis.Razor.Workspaces.Extensions; | ||
using Microsoft.VisualStudio.LanguageServer.Protocol; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Microsoft.AspNetCore.Razor.LanguageServer.InlayHints; | ||
|
||
[Export(typeof(IInlayHintService))] | ||
[method: ImportingConstructor] | ||
internal sealed class InlayHintService(IRazorDocumentMappingService documentMappingService) : IInlayHintService | ||
{ | ||
private readonly IRazorDocumentMappingService _documentMappingService = documentMappingService; | ||
|
||
public async Task<InlayHint[]?> GetInlayHintsAsync(IClientConnection clientConnection, VersionedDocumentContext documentContext, Range range, CancellationToken cancellationToken) | ||
{ | ||
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false); | ||
var csharpDocument = codeDocument.GetCSharpDocument(); | ||
|
||
// We are given a range by the client, but our mapping only succeeds if the start and end of the range can both be mapped | ||
// to C#. Since that doesn't logically match what we want from inlay hints, we instead get the minimum range of mappable | ||
// C# to get hints for. We'll filter that later, to remove the sections that can't be mapped back. | ||
if (!_documentMappingService.TryMapToGeneratedDocumentRange(csharpDocument, range, out var projectedRange) && | ||
!codeDocument.TryGetMinimalCSharpRange(range, out projectedRange)) | ||
{ | ||
// There's no C# in the range. | ||
return null; | ||
} | ||
|
||
// For now we only support C# inlay hints. Once Web Tools adds support we'll need to request from both servers and combine | ||
// the results, much like folding ranges. | ||
var delegatedRequest = new DelegatedInlayHintParams( | ||
Identifier: documentContext.Identifier, | ||
ProjectedRange: projectedRange, | ||
ProjectedKind: RazorLanguageKind.CSharp | ||
); | ||
|
||
var inlayHints = await clientConnection.SendRequestAsync<DelegatedInlayHintParams, InlayHint[]?>( | ||
CustomMessageNames.RazorInlayHintEndpoint, | ||
delegatedRequest, | ||
cancellationToken).ConfigureAwait(false); | ||
|
||
if (inlayHints is null) | ||
{ | ||
return null; | ||
} | ||
|
||
var csharpSourceText = codeDocument.GetCSharpSourceText(); | ||
using var _1 = ArrayBuilderPool<InlayHint>.GetPooledObject(out var inlayHintsBuilder); | ||
foreach (var hint in inlayHints) | ||
{ | ||
if (hint.Position.TryGetAbsoluteIndex(csharpSourceText, null, out var absoluteIndex) && | ||
_documentMappingService.TryMapToHostDocumentPosition(csharpDocument, absoluteIndex, out Position? hostDocumentPosition, out _)) | ||
{ | ||
if (hint.TextEdits is not null) | ||
{ | ||
hint.TextEdits = _documentMappingService.GetHostDocumentEdits(csharpDocument, hint.TextEdits); | ||
} | ||
|
||
hint.Data = new RazorInlayHintWrapper | ||
{ | ||
TextDocument = documentContext.Identifier, | ||
OriginalData = hint.Data, | ||
OriginalPosition = hint.Position | ||
}; | ||
hint.Position = hostDocumentPosition; | ||
inlayHintsBuilder.Add(hint); | ||
} | ||
} | ||
|
||
return inlayHintsBuilder.ToArray(); | ||
} | ||
|
||
public async Task<InlayHint?> ResolveInlayHintAsync(IClientConnection clientConnection, InlayHint inlayHint, CancellationToken cancellationToken) | ||
{ | ||
var inlayHintWrapper = inlayHint.Data as RazorInlayHintWrapper; | ||
if (inlayHintWrapper is null && | ||
inlayHint.Data is JObject dataObj) | ||
{ | ||
inlayHintWrapper = dataObj.ToObject<RazorInlayHintWrapper>(); | ||
} | ||
|
||
if (inlayHintWrapper is null) | ||
{ | ||
return null; | ||
} | ||
|
||
var razorPosition = inlayHint.Position; | ||
inlayHint.Position = inlayHintWrapper.OriginalPosition; | ||
inlayHint.Data = inlayHintWrapper.OriginalData; | ||
|
||
// For now we only support C# inlay hints. Once Web Tools adds support we'll need to inlayHint from both servers and combine | ||
// the results, much like folding ranges. | ||
var delegatedRequest = new DelegatedInlayHintResolveParams( | ||
Identifier: inlayHintWrapper.TextDocument, | ||
InlayHint: inlayHint, | ||
ProjectedKind: RazorLanguageKind.CSharp | ||
); | ||
|
||
var resolvedHint = await clientConnection.SendRequestAsync<DelegatedInlayHintResolveParams, InlayHint?>( | ||
CustomMessageNames.RazorInlayHintResolveEndpoint, | ||
delegatedRequest, | ||
cancellationToken).ConfigureAwait(false); | ||
|
||
if (resolvedHint is null) | ||
{ | ||
return null; | ||
} | ||
|
||
Debug.Assert(inlayHint.Position == resolvedHint.Position, "Resolving inlay hints should not change the position of them."); | ||
resolvedHint.Position = razorPosition; | ||
|
||
return resolvedHint; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/InlayHints/RazorInlayHintWrapper.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,14 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.Razor.LanguageServer.Protocol; | ||
using Microsoft.VisualStudio.LanguageServer.Protocol; | ||
|
||
namespace Microsoft.AspNetCore.Razor.LanguageServer.InlayHints; | ||
|
||
internal class RazorInlayHintWrapper | ||
{ | ||
public required TextDocumentIdentifierAndVersion TextDocument { get; set; } | ||
public required object? OriginalData { get; set; } | ||
public required Position OriginalPosition { get; set; } | ||
} |
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
Oops, something went wrong.