Skip to content

Commit 79ee154

Browse files
authored
Merge pull request #8775 from davidwengier/CleanupDocumentMapping
2 parents 280593f + 4cf4d52 commit 79ee154

File tree

78 files changed

+1422
-1407
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+1422
-1407
lines changed

src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/LanguageServer/RazorCodeActionsBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public async Task SetupAsync()
4747
var languageServer = RazorLanguageServer.GetInnerLanguageServerForTesting();
4848

4949
CodeActionEndpoint = new CodeActionEndpoint(
50-
documentMappingService: languageServer.GetRequiredService<RazorDocumentMappingService>(),
50+
documentMappingService: languageServer.GetRequiredService<IRazorDocumentMappingService>(),
5151
razorCodeActionProviders: languageServer.GetRequiredService<IEnumerable<IRazorCodeActionProvider>>(),
5252
csharpCodeActionProviders: languageServer.GetRequiredService<IEnumerable<ICSharpCodeActionProvider>>(),
5353
htmlCodeActionProviders: languageServer.GetRequiredService<IEnumerable<IHtmlCodeActionProvider>>(),

src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/LanguageServer/RazorCompletionBenchmark.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public async Task SetupAsync()
3737
var razorCompletionListProvider = languageServer.GetRequiredService<RazorCompletionListProvider>();
3838
var lspServices = languageServer.GetLspServices();
3939
var responseRewriters = lspServices.GetRequiredServices<DelegatedCompletionResponseRewriter>();
40-
var documentMappingService = lspServices.GetRequiredService<RazorDocumentMappingService>();
40+
var documentMappingService = lspServices.GetRequiredService<IRazorDocumentMappingService>();
4141
var clientNotifierServiceBase = lspServices.GetRequiredService<ClientNotifierServiceBase>();
4242
var completionListCache = lspServices.GetRequiredService<CompletionListCache>();
4343

@@ -142,7 +142,7 @@ public async Task RazorCompletionAsync()
142142

143143
private class TestDelegatedCompletionListProvider : DelegatedCompletionListProvider
144144
{
145-
public TestDelegatedCompletionListProvider(IEnumerable<DelegatedCompletionResponseRewriter> responseRewriters, RazorDocumentMappingService documentMappingService, ClientNotifierServiceBase languageServer, CompletionListCache completionListCache)
145+
public TestDelegatedCompletionListProvider(IEnumerable<DelegatedCompletionResponseRewriter> responseRewriters, IRazorDocumentMappingService documentMappingService, ClientNotifierServiceBase languageServer, CompletionListCache completionListCache)
146146
: base(responseRewriters, documentMappingService, languageServer, completionListCache)
147147
{
148148
}

src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/LanguageServer/RazorSemanticTokensBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ internal class TestRazorSemanticTokensInfoService : RazorSemanticTokensInfoServi
126126
{
127127
public TestRazorSemanticTokensInfoService(
128128
ClientNotifierServiceBase languageServer,
129-
RazorDocumentMappingService documentMappingService,
129+
IRazorDocumentMappingService documentMappingService,
130130
ILoggerFactory loggerFactory)
131131
: base(languageServer, documentMappingService, loggerFactory)
132132
{

src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/LanguageServer/RazorSemanticTokensRangeEndpointBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ internal class TestCustomizableRazorSemanticTokensInfoService : RazorSemanticTok
151151
{
152152
public TestCustomizableRazorSemanticTokensInfoService(
153153
ClientNotifierServiceBase languageServer,
154-
RazorDocumentMappingService documentMappingService,
154+
IRazorDocumentMappingService documentMappingService,
155155
ILoggerFactory loggerFactory)
156156
: base(languageServer, documentMappingService, loggerFactory)
157157
{

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/AbstractRazorDelegatingEndpoint.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ internal abstract class AbstractRazorDelegatingEndpoint<TRequest, TResponse> : I
2020
where TRequest : ITextDocumentPositionParams
2121
{
2222
private readonly LanguageServerFeatureOptions _languageServerFeatureOptions;
23-
private readonly RazorDocumentMappingService _documentMappingService;
23+
private readonly IRazorDocumentMappingService _documentMappingService;
2424
private readonly ClientNotifierServiceBase _languageServer;
2525
protected readonly ILogger Logger;
2626

2727
protected AbstractRazorDelegatingEndpoint(
2828
LanguageServerFeatureOptions languageServerFeatureOptions,
29-
RazorDocumentMappingService documentMappingService,
29+
IRazorDocumentMappingService documentMappingService,
3030
ClientNotifierServiceBase languageServer,
3131
ILogger logger)
3232
{
@@ -40,7 +40,7 @@ protected AbstractRazorDelegatingEndpoint(
4040
/// <summary>
4141
/// The strategy to use to project the incoming caret position onto the generated C#/Html document
4242
/// </summary>
43-
protected virtual IProjectionStrategy ProjectionStrategy { get; } = DefaultProjectionStrategy.Instance;
43+
protected virtual IDocumentPositionInfoStrategy DocumentPositionInfoStrategy { get; } = DefaultDocumentPositionInfoStrategy.Instance;
4444

4545
protected bool SingleServerSupport => _languageServerFeatureOptions.SingleServerSupport;
4646

@@ -67,20 +67,20 @@ protected AbstractRazorDelegatingEndpoint(
6767
/// <summary>
6868
/// The delegated object to send to the <see cref="CustomMessageTarget"/>
6969
/// </summary>
70-
protected abstract Task<IDelegatedParams?> CreateDelegatedParamsAsync(TRequest request, RazorRequestContext requestContext, Projection projection, CancellationToken cancellationToken);
70+
protected abstract Task<IDelegatedParams?> CreateDelegatedParamsAsync(TRequest request, RazorRequestContext requestContext, DocumentPositionInfo positionInfo, CancellationToken cancellationToken);
7171

7272
/// <summary>
7373
/// If the response needs to be handled, such as for remapping positions back, override and handle here
7474
/// </summary>
75-
protected virtual Task<TResponse> HandleDelegatedResponseAsync(TResponse delegatedResponse, TRequest originalRequest, RazorRequestContext requestContext, Projection projection, CancellationToken cancellationToken)
75+
protected virtual Task<TResponse> HandleDelegatedResponseAsync(TResponse delegatedResponse, TRequest originalRequest, RazorRequestContext requestContext, DocumentPositionInfo positionInfo, CancellationToken cancellationToken)
7676
=> Task.FromResult(delegatedResponse);
7777

7878
/// <summary>
7979
/// If the request can be handled without delegation, override this to provide a response. If a null
8080
/// value is returned the request will be delegated to C#/HTML servers, otherwise the response
8181
/// will be used in <see cref="HandleRequestAsync(TRequest, RazorRequestContext, CancellationToken)"/>
8282
/// </summary>
83-
protected virtual Task<TResponse?> TryHandleAsync(TRequest request, RazorRequestContext requestContext, Projection projection, CancellationToken cancellationToken)
83+
protected virtual Task<TResponse?> TryHandleAsync(TRequest request, RazorRequestContext requestContext, DocumentPositionInfo positionInfo, CancellationToken cancellationToken)
8484
=> Task.FromResult<TResponse?>(default);
8585

8686
/// <summary>
@@ -111,13 +111,13 @@ protected virtual Task<TResponse> HandleDelegatedResponseAsync(TResponse delegat
111111
return default;
112112
}
113113

114-
var projection = await ProjectionStrategy.TryGetProjectionAsync(_documentMappingService, documentContext, request.Position, requestContext.Logger, cancellationToken).ConfigureAwait(false);
115-
if (projection is null)
114+
var positionInfo = await DocumentPositionInfoStrategy.TryGetPositionInfoAsync(_documentMappingService, documentContext, request.Position, requestContext.Logger, cancellationToken).ConfigureAwait(false);
115+
if (positionInfo is null)
116116
{
117117
return default;
118118
}
119119

120-
var response = await TryHandleAsync(request, requestContext, projection, cancellationToken).ConfigureAwait(false);
120+
var response = await TryHandleAsync(request, requestContext, positionInfo, cancellationToken).ConfigureAwait(false);
121121
if (response is not null && response is not ISumType { Value: null })
122122
{
123123
return response;
@@ -128,27 +128,27 @@ protected virtual Task<TResponse> HandleDelegatedResponseAsync(TResponse delegat
128128
return default;
129129
}
130130

131-
if (projection.LanguageKind == RazorLanguageKind.Razor)
131+
if (positionInfo.LanguageKind == RazorLanguageKind.Razor)
132132
{
133133
// We can only delegate to C# and HTML, so if we're in a Razor context and our inheritor didn't want to provide
134134
// any response then that's all we can do.
135135
return default;
136136
}
137-
else if (projection.LanguageKind == RazorLanguageKind.Html && PreferCSharpOverHtmlIfPossible)
137+
else if (positionInfo.LanguageKind == RazorLanguageKind.Html && PreferCSharpOverHtmlIfPossible)
138138
{
139139
// Sometimes Html can actually be mapped to C#, like for example component attributes, which map to
140140
// C# properties, even though they appear entirely in a Html context. Since remapping is pretty cheap
141141
// it's easier to just try mapping, and see what happens, rather than checking for specific syntax nodes.
142142
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
143-
if (_documentMappingService.TryMapToProjectedDocumentPosition(codeDocument.GetCSharpDocument(), projection.AbsoluteIndex, out var csharpPosition, out _))
143+
if (_documentMappingService.TryMapToGeneratedDocumentPosition(codeDocument.GetCSharpDocument(), positionInfo.HostDocumentIndex, out var csharpPosition, out _))
144144
{
145-
// We're just gonna pretend this mapped perfectly normally onto C#. Moving this logic to the actual projection
145+
// We're just gonna pretend this mapped perfectly normally onto C#. Moving this logic to the actual position info
146146
// calculating code is possible, but could have untold effects, so opt-in is better (for now?)
147-
projection = new Projection(RazorLanguageKind.CSharp, csharpPosition, projection.AbsoluteIndex);
147+
positionInfo = new DocumentPositionInfo(RazorLanguageKind.CSharp, csharpPosition, positionInfo.HostDocumentIndex);
148148
}
149149
}
150150

151-
var delegatedParams = await CreateDelegatedParamsAsync(request, requestContext, projection, cancellationToken).ConfigureAwait(false);
151+
var delegatedParams = await CreateDelegatedParamsAsync(request, requestContext, positionInfo, cancellationToken).ConfigureAwait(false);
152152

153153
if (delegatedParams is null)
154154
{
@@ -171,7 +171,7 @@ protected virtual Task<TResponse> HandleDelegatedResponseAsync(TResponse delegat
171171
throw;
172172
}
173173

174-
var remappedResponse = await HandleDelegatedResponseAsync(delegatedRequest, request, requestContext, projection, cancellationToken).ConfigureAwait(false);
174+
var remappedResponse = await HandleDelegatedResponseAsync(delegatedRequest, request, requestContext, positionInfo, cancellationToken).ConfigureAwait(false);
175175

176176
return remappedResponse;
177177
}

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/AutoInsert/OnAutoInsertEndpoint.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal class OnAutoInsertEndpoint : AbstractRazorDelegatingEndpoint<VSInternal
3232

3333
public OnAutoInsertEndpoint(
3434
LanguageServerFeatureOptions languageServerFeatureOptions,
35-
RazorDocumentMappingService documentMappingService,
35+
IRazorDocumentMappingService documentMappingService,
3636
ClientNotifierServiceBase languageServer,
3737
IEnumerable<IOnAutoInsertProvider> onAutoInsertProvider,
3838
IOptionsMonitor<RazorLSPOptions> optionsMonitor,
@@ -65,7 +65,7 @@ public RegistrationExtensionResult GetRegistration(VSInternalClientCapabilities
6565
return new RegistrationExtensionResult(AssociatedServerCapability, registrationOptions);
6666
}
6767

68-
protected override async Task<VSInternalDocumentOnAutoInsertResponseItem?> TryHandleAsync(VSInternalDocumentOnAutoInsertParams request, RazorRequestContext requestContext, Projection projection, CancellationToken cancellationToken)
68+
protected override async Task<VSInternalDocumentOnAutoInsertResponseItem?> TryHandleAsync(VSInternalDocumentOnAutoInsertParams request, RazorRequestContext requestContext, DocumentPositionInfo positionInfo, CancellationToken cancellationToken)
6969
{
7070
var documentContext = requestContext.GetRequiredDocumentContext();
7171
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
@@ -118,16 +118,16 @@ public RegistrationExtensionResult GetRegistration(VSInternalClientCapabilities
118118
return null;
119119
}
120120

121-
protected override Task<IDelegatedParams?> CreateDelegatedParamsAsync(VSInternalDocumentOnAutoInsertParams request, RazorRequestContext requestContext, Projection projection, CancellationToken cancellationToken)
121+
protected override Task<IDelegatedParams?> CreateDelegatedParamsAsync(VSInternalDocumentOnAutoInsertParams request, RazorRequestContext requestContext, DocumentPositionInfo positionInfo, CancellationToken cancellationToken)
122122
{
123123
var documentContext = requestContext.GetRequiredDocumentContext();
124-
if (projection.LanguageKind == RazorLanguageKind.Html &&
124+
if (positionInfo.LanguageKind == RazorLanguageKind.Html &&
125125
!s_htmlAllowedTriggerCharacters.Contains(request.Character))
126126
{
127127
Logger.LogInformation("Inapplicable HTML trigger char {request.Character}.", request.Character);
128128
return Task.FromResult<IDelegatedParams?>(null);
129129
}
130-
else if (projection.LanguageKind == RazorLanguageKind.CSharp)
130+
else if (positionInfo.LanguageKind == RazorLanguageKind.CSharp)
131131
{
132132
if (!s_cSharpAllowedTriggerCharacters.Contains(request.Character))
133133
{
@@ -155,8 +155,8 @@ public RegistrationExtensionResult GetRegistration(VSInternalClientCapabilities
155155

156156
return Task.FromResult<IDelegatedParams?>(new DelegatedOnAutoInsertParams(
157157
documentContext.Identifier,
158-
projection.Position,
159-
projection.LanguageKind,
158+
positionInfo.Position,
159+
positionInfo.LanguageKind,
160160
request.Character,
161161
request.Options));
162162
}
@@ -165,7 +165,7 @@ public RegistrationExtensionResult GetRegistration(VSInternalClientCapabilities
165165
VSInternalDocumentOnAutoInsertResponseItem? delegatedResponse,
166166
VSInternalDocumentOnAutoInsertParams originalRequest,
167167
RazorRequestContext requestContext,
168-
Projection projection,
168+
DocumentPositionInfo positionInfo,
169169
CancellationToken cancellationToken)
170170
{
171171
if (delegatedResponse is null)
@@ -176,7 +176,7 @@ public RegistrationExtensionResult GetRegistration(VSInternalClientCapabilities
176176
var documentContext = requestContext.GetRequiredDocumentContext();
177177

178178
// For Html we just return the edit as is
179-
if (projection.LanguageKind == RazorLanguageKind.Html)
179+
if (positionInfo.LanguageKind == RazorLanguageKind.Html)
180180
{
181181
return delegatedResponse;
182182
}
@@ -188,11 +188,11 @@ public RegistrationExtensionResult GetRegistration(VSInternalClientCapabilities
188188
TextEdit[] mappedEdits;
189189
if (delegatedResponse.TextEditFormat == InsertTextFormat.Snippet)
190190
{
191-
mappedEdits = await razorFormattingService.FormatSnippetAsync(documentContext, projection.LanguageKind, edits, originalRequest.Options, cancellationToken).ConfigureAwait(false);
191+
mappedEdits = await razorFormattingService.FormatSnippetAsync(documentContext, positionInfo.LanguageKind, edits, originalRequest.Options, cancellationToken).ConfigureAwait(false);
192192
}
193193
else
194194
{
195-
mappedEdits = await razorFormattingService.FormatOnTypeAsync(documentContext, projection.LanguageKind, edits, originalRequest.Options, hostDocumentIndex: 0, triggerCharacter: '\0', cancellationToken).ConfigureAwait(false);
195+
mappedEdits = await razorFormattingService.FormatOnTypeAsync(documentContext, positionInfo.LanguageKind, edits, originalRequest.Options, hostDocumentIndex: 0, triggerCharacter: '\0', cancellationToken).ConfigureAwait(false);
196196
}
197197

198198
if (mappedEdits.Length != 1)

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/CSharp/UnformattedRemappingCSharpCodeActionResolver.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.CodeActions;
2121
internal sealed class UnformattedRemappingCSharpCodeActionResolver : CSharpCodeActionResolver
2222
{
2323
private readonly DocumentContextFactory _documentContextFactory;
24-
private readonly RazorDocumentMappingService _documentMappingService;
24+
private readonly IRazorDocumentMappingService _documentMappingService;
2525

2626
public UnformattedRemappingCSharpCodeActionResolver(
2727
DocumentContextFactory documentContextFactory,
2828
ClientNotifierServiceBase languageServer,
29-
RazorDocumentMappingService documentMappingService)
29+
IRazorDocumentMappingService documentMappingService)
3030
: base(languageServer)
3131
{
3232
_documentContextFactory = documentContextFactory ?? throw new ArgumentNullException(nameof(documentContextFactory));
@@ -92,7 +92,7 @@ public async override Task<CodeAction> ResolveAsync(
9292
return codeAction;
9393
}
9494

95-
if (!_documentMappingService.TryMapFromProjectedDocumentRange(codeDocument.GetCSharpDocument(), textEdit.Range, MappingBehavior.Inclusive, out var originalRange))
95+
if (!_documentMappingService.TryMapToHostDocumentRange(codeDocument.GetCSharpDocument(), textEdit.Range, MappingBehavior.Inclusive, out var originalRange))
9696
{
9797
// Text edit failed to map
9898
return codeAction;

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/CodeActionEndpoint.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.CodeActions;
2929
[LanguageServerEndpoint(Methods.TextDocumentCodeActionName)]
3030
internal sealed class CodeActionEndpoint : IRazorRequestHandler<VSCodeActionParams, SumType<Command, CodeAction>[]?>, IRegistrationExtension
3131
{
32-
private readonly RazorDocumentMappingService _documentMappingService;
32+
private readonly IRazorDocumentMappingService _documentMappingService;
3333
private readonly IEnumerable<IRazorCodeActionProvider> _razorCodeActionProviders;
3434
private readonly IEnumerable<ICSharpCodeActionProvider> _csharpCodeActionProviders;
3535
private readonly IEnumerable<IHtmlCodeActionProvider> _htmlCodeActionProviders;
@@ -43,7 +43,7 @@ internal sealed class CodeActionEndpoint : IRazorRequestHandler<VSCodeActionPara
4343
public bool MutatesSolutionState { get; } = false;
4444

4545
public CodeActionEndpoint(
46-
RazorDocumentMappingService documentMappingService,
46+
IRazorDocumentMappingService documentMappingService,
4747
IEnumerable<IRazorCodeActionProvider> razorCodeActionProviders,
4848
IEnumerable<ICSharpCodeActionProvider> csharpCodeActionProviders,
4949
IEnumerable<IHtmlCodeActionProvider> htmlCodeActionProviders,
@@ -274,14 +274,14 @@ internal async Task<RazorVSInternalCodeAction[]> GetCodeActionsFromLanguageServe
274274
if (languageKind == RazorLanguageKind.CSharp)
275275
{
276276
// For C# we have to map the ranges to the generated document
277-
if (!_documentMappingService.TryMapToProjectedDocumentRange(context.CodeDocument.GetCSharpDocument(), context.Request.Range, out var projectedRange))
277+
if (!_documentMappingService.TryMapToGeneratedDocumentRange(context.CodeDocument.GetCSharpDocument(), context.Request.Range, out var projectedRange))
278278
{
279279
return Array.Empty<RazorVSInternalCodeAction>();
280280
}
281281

282282
var newContext = context.Request.Context;
283283
if (context.Request.Context is VSInternalCodeActionContext { SelectionRange: not null } vsContext &&
284-
_documentMappingService.TryMapToProjectedDocumentRange(context.CodeDocument.GetCSharpDocument(), vsContext.SelectionRange, out var selectionRange))
284+
_documentMappingService.TryMapToGeneratedDocumentRange(context.CodeDocument.GetCSharpDocument(), vsContext.SelectionRange, out var selectionRange))
285285
{
286286
vsContext.SelectionRange = selectionRange;
287287
newContext = vsContext;

0 commit comments

Comments
 (0)