Skip to content
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

Co-hosting: Add API for mapping a generated document URI and range back to the host document #10712

Merged
merged 17 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public enum FileTypes
public async Task SetupAsync()
{
CodeActionEndpoint = new CodeActionEndpoint(
documentMappingService: RazorLanguageServerHost.GetRequiredService<IRazorDocumentMappingService>(),
documentMappingService: RazorLanguageServerHost.GetRequiredService<IDocumentMappingService>(),
razorCodeActionProviders: RazorLanguageServerHost.GetRequiredService<IEnumerable<IRazorCodeActionProvider>>(),
csharpCodeActionProviders: RazorLanguageServerHost.GetRequiredService<IEnumerable<ICSharpCodeActionProvider>>(),
htmlCodeActionProviders: RazorLanguageServerHost.GetRequiredService<IEnumerable<IHtmlCodeActionProvider>>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task SetupAsync()
var razorCompletionListProvider = RazorLanguageServerHost.GetRequiredService<RazorCompletionListProvider>();
var lspServices = RazorLanguageServerHost.GetRequiredService<ILspServices>();
var responseRewriters = lspServices.GetRequiredServices<DelegatedCompletionResponseRewriter>();
var documentMappingService = lspServices.GetRequiredService<IRazorDocumentMappingService>();
var documentMappingService = lspServices.GetRequiredService<IDocumentMappingService>();
var clientConnection = lspServices.GetRequiredService<IClientConnection>();
var completionListCache = lspServices.GetRequiredService<CompletionListCache>();
var loggerFactory = lspServices.GetRequiredService<ILoggerFactory>();
Expand Down Expand Up @@ -141,7 +141,7 @@ public async Task RazorCompletionAsync()

private class TestDelegatedCompletionListProvider : DelegatedCompletionListProvider
{
public TestDelegatedCompletionListProvider(IEnumerable<DelegatedCompletionResponseRewriter> responseRewriters, IRazorDocumentMappingService documentMappingService, IClientConnection clientConnection, CompletionListCache completionListCache)
public TestDelegatedCompletionListProvider(IEnumerable<DelegatedCompletionResponseRewriter> responseRewriters, IDocumentMappingService documentMappingService, IClientConnection clientConnection, CompletionListCache completionListCache)
: base(responseRewriters, documentMappingService, clientConnection, completionListCache)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ private protected override LanguageServerFeatureOptions BuildFeatureOptions()
MockBehavior.Strict);
}

private IRazorDocumentMappingService BuildRazorDocumentMappingService()
private IDocumentMappingService BuildRazorDocumentMappingService()
{
var razorDocumentMappingService = new Mock<IRazorDocumentMappingService>(MockBehavior.Strict);
var razorDocumentMappingService = new Mock<IDocumentMappingService>(MockBehavior.Strict);

Range? hostDocumentRange;
razorDocumentMappingService.Setup(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class RazorDocumentMappingBenchmark : RazorLanguageServerBenchmarkBase
{
private string _filePath;

private IRazorDocumentMappingService DocumentMappingService { get; set; }
private IDocumentMappingService DocumentMappingService { get; set; }

private IDocumentSnapshot DocumentSnapshot { get; set; }

Expand Down Expand Up @@ -171,6 +171,6 @@ public async Task CleanupServerAsync()

private void EnsureServicesInitialized()
{
DocumentMappingService = RazorLanguageServerHost.GetRequiredService<IRazorDocumentMappingService>();
DocumentMappingService = RazorLanguageServerHost.GetRequiredService<IDocumentMappingService>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ internal class TestRazorSemanticTokensInfoService : RazorSemanticTokensInfoServi
{
public TestRazorSemanticTokensInfoService(
LanguageServerFeatureOptions languageServerFeatureOptions,
IRazorDocumentMappingService documentMappingService,
IDocumentMappingService documentMappingService,
RazorSemanticTokensLegendService razorSemanticTokensLegendService,
ILoggerFactory loggerFactory)
: base(documentMappingService, razorSemanticTokensLegendService, csharpSemanticTokensProvider: null!, languageServerFeatureOptions, loggerFactory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ internal class TestCustomizableRazorSemanticTokensInfoService : RazorSemanticTok
{
public TestCustomizableRazorSemanticTokensInfoService(
LanguageServerFeatureOptions languageServerFeatureOptions,
IRazorDocumentMappingService documentMappingService,
IDocumentMappingService documentMappingService,
RazorSemanticTokensLegendService razorSemanticTokensLegendService,
ILoggerFactory loggerFactory)
: base(documentMappingService, razorSemanticTokensLegendService, csharpSemanticTokensProvider: null!, languageServerFeatureOptions, loggerFactory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ internal abstract class AbstractRazorDelegatingEndpoint<TRequest, TResponse> : I
where TRequest : ITextDocumentPositionParams
{
private readonly LanguageServerFeatureOptions _languageServerFeatureOptions;
private readonly IRazorDocumentMappingService _documentMappingService;
private readonly IDocumentMappingService _documentMappingService;
private readonly IClientConnection _clientConnection;
protected readonly ILogger Logger;

protected AbstractRazorDelegatingEndpoint(
LanguageServerFeatureOptions languageServerFeatureOptions,
IRazorDocumentMappingService documentMappingService,
IDocumentMappingService documentMappingService,
IClientConnection clientConnection,
ILogger logger)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.AutoInsert;
[RazorLanguageServerEndpoint(VSInternalMethods.OnAutoInsertName)]
internal class OnAutoInsertEndpoint(
LanguageServerFeatureOptions languageServerFeatureOptions,
IRazorDocumentMappingService documentMappingService,
IDocumentMappingService documentMappingService,
IClientConnection clientConnection,
IEnumerable<IOnAutoInsertProvider> onAutoInsertProvider,
RazorLSPOptionsMonitor optionsMonitor,
Expand Down Expand Up @@ -170,7 +170,7 @@ public void ApplyCapabilities(VSInternalServerCapabilities serverCapabilities, V
}

return Task.FromResult<IDelegatedParams?>(new DelegatedOnAutoInsertParams(
documentContext.Identifier,
documentContext.GetTextDocumentIdentifierAndVersion(),
positionInfo.Position,
positionInfo.LanguageKind,
request.Character,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class PreferHtmlInAttributeValuesDocumentPositionInfoStrategy : IDocume
{
public static IDocumentPositionInfoStrategy Instance { get; } = new PreferHtmlInAttributeValuesDocumentPositionInfoStrategy();

public async Task<DocumentPositionInfo?> TryGetPositionInfoAsync(IRazorDocumentMappingService documentMappingService, DocumentContext documentContext, Position position, CancellationToken cancellationToken)
public async Task<DocumentPositionInfo?> TryGetPositionInfoAsync(IDocumentMappingService documentMappingService, DocumentContext documentContext, Position position, CancellationToken cancellationToken)
{
var defaultDocumentPositionInfo = await DefaultDocumentPositionInfoStrategy.Instance.TryGetPositionInfoAsync(documentMappingService, documentContext, position, cancellationToken).ConfigureAwait(false);
if (defaultDocumentPositionInfo is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.CodeActions;
internal sealed class UnformattedRemappingCSharpCodeActionResolver(
IDocumentContextFactory documentContextFactory,
IClientConnection clientConnection,
IRazorDocumentMappingService documentMappingService) : CSharpCodeActionResolver(clientConnection)
IDocumentMappingService documentMappingService) : CSharpCodeActionResolver(clientConnection)
{
private readonly IDocumentContextFactory _documentContextFactory = documentContextFactory;
private readonly IRazorDocumentMappingService _documentMappingService = documentMappingService;
private readonly IDocumentMappingService _documentMappingService = documentMappingService;

public override string Action => LanguageServerConstants.CodeActions.UnformattedRemap;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.CodeActions;

[RazorLanguageServerEndpoint(LspEndpointName)]
internal sealed class CodeActionEndpoint(
IRazorDocumentMappingService documentMappingService,
IDocumentMappingService documentMappingService,
IEnumerable<IRazorCodeActionProvider> razorCodeActionProviders,
IEnumerable<ICSharpCodeActionProvider> csharpCodeActionProviders,
IEnumerable<IHtmlCodeActionProvider> htmlCodeActionProviders,
Expand All @@ -42,7 +42,7 @@ internal sealed class CodeActionEndpoint(
{
private const string LspEndpointName = Methods.TextDocumentCodeActionName;

private readonly IRazorDocumentMappingService _documentMappingService = documentMappingService ?? throw new ArgumentNullException(nameof(documentMappingService));
private readonly IDocumentMappingService _documentMappingService = documentMappingService ?? throw new ArgumentNullException(nameof(documentMappingService));
private readonly IEnumerable<IRazorCodeActionProvider> _razorCodeActionProviders = razorCodeActionProviders ?? throw new ArgumentNullException(nameof(razorCodeActionProviders));
private readonly IEnumerable<ICSharpCodeActionProvider> _csharpCodeActionProviders = csharpCodeActionProviders ?? throw new ArgumentNullException(nameof(csharpCodeActionProviders));
private readonly IEnumerable<IHtmlCodeActionProvider> _htmlCodeActionProviders = htmlCodeActionProviders ?? throw new ArgumentNullException(nameof(htmlCodeActionProviders));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -14,9 +13,9 @@

namespace Microsoft.AspNetCore.Razor.LanguageServer.CodeActions;

internal sealed class DefaultHtmlCodeActionProvider(IRazorDocumentMappingService documentMappingService) : IHtmlCodeActionProvider
internal sealed class DefaultHtmlCodeActionProvider(IEditMappingService editMappingService) : IHtmlCodeActionProvider
{
private readonly IRazorDocumentMappingService _documentMappingService = documentMappingService;
private readonly IEditMappingService _editMappingService = editMappingService;

public async Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(
RazorCodeActionContext context,
Expand All @@ -28,7 +27,7 @@ public async Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(
{
if (codeAction.Edit is not null)
{
await RemapAndFixHtmlCodeActionEditAsync(_documentMappingService, context.CodeDocument, codeAction, cancellationToken).ConfigureAwait(false);
await RemapAndFixHtmlCodeActionEditAsync(_editMappingService, context.CodeDocument, codeAction, cancellationToken).ConfigureAwait(false);

results.Add(codeAction);
}
Expand All @@ -41,11 +40,11 @@ public async Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(
return results.ToImmutable();
}

public static async Task RemapAndFixHtmlCodeActionEditAsync(IRazorDocumentMappingService documentMappingService, RazorCodeDocument codeDocument, CodeAction codeAction, CancellationToken cancellationToken)
public static async Task RemapAndFixHtmlCodeActionEditAsync(IEditMappingService editMappingService, RazorCodeDocument codeDocument, CodeAction codeAction, CancellationToken cancellationToken)
{
Assumes.NotNull(codeAction.Edit);

codeAction.Edit = await documentMappingService.RemapWorkspaceEditAsync(codeAction.Edit, cancellationToken).ConfigureAwait(false);
codeAction.Edit = await editMappingService.RemapWorkspaceEditAsync(codeAction.Edit, cancellationToken).ConfigureAwait(false);

if (codeAction.Edit.TryGetDocumentChanges(out var documentEdits) == true)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.CodeActions;
internal sealed class DefaultHtmlCodeActionResolver(
IDocumentContextFactory documentContextFactory,
IClientConnection clientConnection,
IRazorDocumentMappingService documentMappingService) : HtmlCodeActionResolver(clientConnection)
IEditMappingService editMappingService) : HtmlCodeActionResolver(clientConnection)
{
private readonly IDocumentContextFactory _documentContextFactory = documentContextFactory;
private readonly IRazorDocumentMappingService _documentMappingService = documentMappingService;
private readonly IEditMappingService _editMappingService = editMappingService;

public override string Action => LanguageServerConstants.CodeActions.Default;

Expand All @@ -41,7 +41,7 @@ public async override Task<CodeAction> ResolveAsync(
}

var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
await DefaultHtmlCodeActionProvider.RemapAndFixHtmlCodeActionEditAsync(_documentMappingService, codeDocument, resolvedCodeAction, cancellationToken).ConfigureAwait(false);
await DefaultHtmlCodeActionProvider.RemapAndFixHtmlCodeActionEditAsync(_editMappingService, codeDocument, resolvedCodeAction, cancellationToken).ConfigureAwait(false);

return resolvedCodeAction;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ internal sealed class GenerateMethodCodeActionResolver(
IDocumentContextFactory documentContextFactory,
RazorLSPOptionsMonitor razorLSPOptionsMonitor,
IClientConnection clientConnection,
IRazorDocumentMappingService razorDocumentMappingService,
IDocumentMappingService documentMappingService,
IRazorFormattingService razorFormattingService) : IRazorCodeActionResolver
{
private readonly IDocumentContextFactory _documentContextFactory = documentContextFactory;
private readonly RazorLSPOptionsMonitor _razorLSPOptionsMonitor = razorLSPOptionsMonitor;
private readonly IClientConnection _clientConnection = clientConnection;
private readonly IRazorDocumentMappingService _documentMappingService = razorDocumentMappingService;
private readonly IDocumentMappingService _documentMappingService = documentMappingService;
private readonly IRazorFormattingService _razorFormattingService = razorFormattingService;

private const string ReturnType = "$$ReturnType$$";
Expand Down Expand Up @@ -166,7 +166,7 @@ private async Task<WorkspaceEdit> GenerateMethodInCodeBlockAsync(
character: 0,
editToSendToRoslyn.NewText);

var delegatedParams = new DelegatedSimplifyMethodParams(documentContext.Identifier, RequiresVirtualDocument: true, tempTextEdit);
var delegatedParams = new DelegatedSimplifyMethodParams(documentContext.GetTextDocumentIdentifierAndVersion(), RequiresVirtualDocument: true, tempTextEdit);
var result = await _clientConnection.SendRequestAsync<DelegatedSimplifyMethodParams, TextEdit[]?>(
CustomMessageNames.RazorSimplifyMethodEndpointName,
delegatedParams,
Expand Down Expand Up @@ -194,7 +194,7 @@ private async Task<WorkspaceEdit> GenerateMethodInCodeBlockAsync(

var remappedEdit = VsLspFactory.CreateTextEdit(remappedRange, unformattedMethodSignature);

var delegatedParams = new DelegatedSimplifyMethodParams(documentContext.Identifier, RequiresVirtualDocument: true, remappedEdit);
var delegatedParams = new DelegatedSimplifyMethodParams(documentContext.GetTextDocumentIdentifierAndVersion(), RequiresVirtualDocument: true, remappedEdit);
var result = await _clientConnection.SendRequestAsync<DelegatedSimplifyMethodParams, TextEdit[]?>(
CustomMessageNames.RazorSimplifyMethodEndpointName,
delegatedParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ private async Task<VSInternalCompletionItem> PostProcessCompletionItemAsync(
return resolvedCompletionItem;
}

var formattingOptions = await _clientConnection.SendRequestAsync<TextDocumentIdentifierAndVersion, FormattingOptions?>(CodeAnalysis.Razor.Protocol.LanguageServerConstants.RazorGetFormattingOptionsEndpointName, documentContext.Identifier, cancellationToken).ConfigureAwait(false);
var formattingOptions = await _clientConnection
.SendRequestAsync<TextDocumentIdentifierAndVersion, FormattingOptions?>(
LanguageServerConstants.RazorGetFormattingOptionsEndpointName,
documentContext.GetTextDocumentIdentifierAndVersion(),
cancellationToken)
.ConfigureAwait(false);

if (formattingOptions is null)
{
return resolvedCompletionItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ internal class DelegatedCompletionListProvider
.Union(s_razorTriggerCharacters);

private readonly ImmutableArray<DelegatedCompletionResponseRewriter> _responseRewriters;
private readonly IRazorDocumentMappingService _documentMappingService;
private readonly IDocumentMappingService _documentMappingService;
private readonly IClientConnection _clientConnection;
private readonly CompletionListCache _completionListCache;

public DelegatedCompletionListProvider(
IEnumerable<DelegatedCompletionResponseRewriter> responseRewriters,
IRazorDocumentMappingService documentMappingService,
IDocumentMappingService documentMappingService,
IClientConnection clientConnection,
CompletionListCache completionListCache)
{
Expand Down Expand Up @@ -77,7 +77,7 @@ public DelegatedCompletionListProvider(
var shouldIncludeSnippets = await ShouldIncludeSnippetsAsync(documentContext, absoluteIndex, cancellationToken).ConfigureAwait(false);

var delegatedParams = new DelegatedCompletionParams(
documentContext.Identifier,
documentContext.GetTextDocumentIdentifierAndVersion(),
positionInfo.Position,
positionInfo.LanguageKind,
completionContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Debugging;
[RazorLanguageServerEndpoint(LanguageServerConstants.RazorBreakpointSpanEndpoint)]
internal class RazorBreakpointSpanEndpoint : IRazorDocumentlessRequestHandler<RazorBreakpointSpanParams, RazorBreakpointSpanResponse?>, ITextDocumentIdentifierHandler<RazorBreakpointSpanParams, Uri>
{
private readonly IRazorDocumentMappingService _documentMappingService;
private readonly IDocumentMappingService _documentMappingService;
private readonly ILogger _logger;

public bool MutatesSolutionState => false;

public RazorBreakpointSpanEndpoint(
IRazorDocumentMappingService documentMappingService,
IDocumentMappingService documentMappingService,
ILoggerFactory loggerFactory)
{
if (loggerFactory is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Debugging;
[RazorLanguageServerEndpoint(LanguageServerConstants.RazorProximityExpressionsEndpoint)]
internal class RazorProximityExpressionsEndpoint : IRazorDocumentlessRequestHandler<RazorProximityExpressionsParams, RazorProximityExpressionsResponse?>, ITextDocumentIdentifierHandler<RazorProximityExpressionsParams, Uri>
{
private readonly IRazorDocumentMappingService _documentMappingService;
private readonly IDocumentMappingService _documentMappingService;
private readonly ILogger _logger;

public RazorProximityExpressionsEndpoint(
IRazorDocumentMappingService documentMappingService,
IDocumentMappingService documentMappingService,
ILoggerFactory loggerFactory)
{
if (documentMappingService is null)
Expand Down
Loading