-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dynamic registration for razor and cshtml files (#73369)
- Loading branch information
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...rosoft.CodeAnalysis.LanguageServer/LanguageServer/RazorDynamicDocumentSyncRegistration.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,84 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Composition; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
using Microsoft.CodeAnalysis.LanguageServer; | ||
using Microsoft.CodeAnalysis.LanguageServer.Handler; | ||
using Microsoft.CodeAnalysis.Options; | ||
using Roslyn.LanguageServer.Protocol; | ||
|
||
namespace Microsoft.CodeAnalysis.LanguageServer.LanguageServer; | ||
|
||
[ExportCSharpVisualBasicLspServiceFactory(typeof(OnInitialized)), Shared] | ||
[method: ImportingConstructor] | ||
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] | ||
internal sealed class RazorDynamicDocumentSyncRegistration(IGlobalOptionService globalOptionService) : ILspServiceFactory | ||
{ | ||
public ILspService CreateILspService(LspServices lspServices, WellKnownLspServerKinds serverKind) | ||
=> new OnInitialized(globalOptionService); | ||
|
||
public class OnInitialized : IOnInitialized, ILspService | ||
{ | ||
private readonly IGlobalOptionService _globalOptionService; | ||
|
||
public OnInitialized(IGlobalOptionService globalOptionService) | ||
{ | ||
_globalOptionService = globalOptionService; | ||
} | ||
|
||
public async Task OnInitializedAsync(ClientCapabilities clientCapabilities, RequestContext context, CancellationToken cancellationToken) | ||
{ | ||
// Hot reload only works in devkit scenarios. Without, there is no need to register for dynamic document sync. | ||
if (!_globalOptionService.GetOption(LspOptionsStorage.LspUsingDevkitFeatures)) | ||
{ | ||
return; | ||
} | ||
|
||
// If dynamic registration for text document synchronization is supported, register for .razor and .cshtml files | ||
// so that they are up to date for hot reload scenarios rather than depending on the file watchers to update | ||
// the contents. | ||
if (clientCapabilities.TextDocument?.Synchronization?.DynamicRegistration is true) | ||
{ | ||
var languageServerManager = context.GetRequiredLspService<IClientLanguageServerManager>(); | ||
|
||
var documentFilters = new[] { new DocumentFilter() { Pattern = "**/*.razor" }, new DocumentFilter() { Pattern = "**/*.cshtml" } }; | ||
var registrationOptions = new TextDocumentRegistrationOptions() | ||
{ | ||
DocumentSelector = documentFilters | ||
}; | ||
|
||
await languageServerManager.SendRequestAsync(Methods.ClientRegisterCapabilityName, | ||
new RegistrationParams() | ||
{ | ||
Registrations = [ | ||
new() | ||
{ | ||
Id = Guid.NewGuid().ToString(), // No need to save this for unregistering | ||
Method = Methods.TextDocumentDidOpenName, | ||
RegisterOptions = registrationOptions | ||
}, | ||
new() | ||
{ | ||
Id = Guid.NewGuid().ToString(), // No need to save this for unregistering | ||
Method = Methods.TextDocumentDidChangeName, | ||
RegisterOptions = registrationOptions | ||
}, | ||
new() | ||
{ | ||
Id = Guid.NewGuid().ToString(), // No need to save this for unregistering | ||
Method = Methods.TextDocumentDidCloseName, | ||
RegisterOptions = registrationOptions | ||
} | ||
] | ||
}, | ||
cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
} | ||
} | ||
|