|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.CodeAnalysis.LanguageServer.Handler; |
| 9 | +using Microsoft.CommonLanguageServerProtocol.Framework; |
| 10 | +using Roslyn.LanguageServer.Protocol; |
| 11 | + |
| 12 | +namespace Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost; |
| 13 | + |
| 14 | +internal interface IRazorCohostTextDocumentSyncHandler |
| 15 | +{ |
| 16 | + Task HandleAsync(int version, RazorCohostRequestContext context, CancellationToken cancellationToken); |
| 17 | +} |
| 18 | + |
| 19 | +internal static class IRazorCohostTextDocumentSyncHandlerExtensions |
| 20 | +{ |
| 21 | + public static async Task NotifyRazorAsync(this IRazorCohostTextDocumentSyncHandler? openOrChangeHandler, Uri uri, int version, RequestContext context, CancellationToken cancellationToken) |
| 22 | + { |
| 23 | + if (openOrChangeHandler is null) |
| 24 | + return; |
| 25 | + |
| 26 | + // Razor is a little special here, because when a .razor or .cshtml document is opened/changed, which is what this request is for, |
| 27 | + // they need to generate a C# and/or Html document. To do this they use the Razor Source Generator, but to run the generator they |
| 28 | + // need a TextDocument for the Razor file that this request is for. To facilitate that need we create a RequestContext here |
| 29 | + // and pass it to Razor, which is essentially the same as the RequestContext they would get on the next request, but by providing it |
| 30 | + // early here, they can do their generation before the didOpen/didChange/didClose mutating request is finished. |
| 31 | + // This is a little hacky, but it's the best we can do for now. In future hopefully we can switch to a pull model where the source |
| 32 | + // generator just provides C# source to the project/compilation as normal. Whether we need to maintain this system for the Html |
| 33 | + // generated documents remains to be seen. |
| 34 | + var clientCapabilitiesManager = context.GetRequiredService<IInitializeManager>(); |
| 35 | + var clientCapabilities = clientCapabilitiesManager.TryGetClientCapabilities(); |
| 36 | + var logger = context.GetRequiredService<AbstractLspLogger>(); |
| 37 | + var serverInfoProvider = context.GetRequiredService<ServerInfoProvider>(); |
| 38 | + var supportedLanguages = serverInfoProvider.SupportedLanguages; |
| 39 | + var lspServices = context.GetRequiredService<ILspServices>(); |
| 40 | + |
| 41 | + var newContext = await RequestContext.CreateAsync(false, true, new TextDocumentIdentifier() { Uri = uri }, LanguageServer.WellKnownLspServerKinds.RazorCohostServer, clientCapabilities, supportedLanguages, lspServices, logger, context.Method, cancellationToken).ConfigureAwait(false); |
| 42 | + |
| 43 | + var razorContext = new RazorCohostRequestContext(newContext); |
| 44 | + |
| 45 | + await openOrChangeHandler.HandleAsync(version, razorContext, cancellationToken).ConfigureAwait(false); |
| 46 | + } |
| 47 | +} |
0 commit comments