csharp-language-server-protocol
csharp-language-server-protocol copied to clipboard
Enabling/disabling handler based on client configuration
Is there a way to enable and disable handlers dynamically based on client configuration?
For example, VS Code recommends that a formatter provide a client setting to enable or disable it.
If this is possible to do, I'm guessing it's probably something like this:
- Enable synchronization of the relevant configuration section (.WithConfigurationSection, I think)
- Either pass configuration at startup (for clients that don't support dynamic configuration), or delay enabling the handler until after Initialize has completed and then retrieve configuration and add the handler if enabled. (Per the spec, the server isn't allowed to use the configuration request message before initialization has completed).
- Listen to configuration changes, and remove and add the handler as appropriate.
Questions: Are there APIs to add or remove handlers after the server has completed initialization? What's the best approach to listen for a change to a specific configuration setting?
Thanks,
David
On the settings sync part: I confirmed that using .WithConfigurationSection allows pulling in the client settings. However, I can't find a way to get notifications of settings changes - using .OnDidChangeConfiguration() or .WithHandler for a class of type IDidChangeConfigurationHandler seems like it should be the right option (the latter is what Razor did), but for some reason I can't get the server to send a message registering a change configuration handler capability with any of these approaches.
(The add/remove handlers dynamically question is still open as well.)
I figured out how to register and unregister dynamically - use .Register on the server after it has started to register dynamically and then .Dispose on the result to unregister:
LanguageServer server = await LanguageServer.From(options =>
options
.WithInput(Console.OpenStandardInput())
.WithOutput(Console.OpenStandardOutput())
.WithHandler<TextDocumentSyncHandler>()
.WithServices(ConfigureServices)
);
using (IDisposable temp = server.Register(r => r.AddHandler<FormattingHandler>()))
{
System.Threading.Thread.Sleep(10000);
}
await server.WaitForExit;
I just can't figure out how to trigger the server to send client/registerCapability for workspace/didChangeConfiguration.
I noticed that DidChangeConfigurationFeature, unlike TextDocumentSyncFeature, does not specify a RegistrationOptions attribute. And IDidChangeConfigurationHandler, unlike ITextDocumentSyncHandler, does not inherit from IRegistration. The same applies to IDocumentFormattingHandler and DocumentFormattingFeature.
Are change configuration handlers intended to be different from text document sync handlers and document formatting handlers in this way?
I also have similar issue. Could it be that VSCode doesn't actually send the notifications? I've noticed, that razor's lsp client tracks the changes and sends the notification itself: https://github.com/dotnet/aspnetcore-tooling/blob/main/src/Razor/src/Microsoft.VisualStudio.LanguageServerClient.Razor/RazorLSPTextViewConnectionListener.cs#L242