static IEnumerable <T> GetUniqueHandlers <T>(CompositeDisposable disposable)
 {
     return(GetUniqueHandlers(disposable).OfType <T>());
 }
        internal static IDisposable DynamicallyRegisterHandlers(
            IClientLanguageServer client,
            IObservable <Unit> initializeComplete,
            IServerWorkDoneManager serverWorkDoneManager,
            ISupportedCapabilities supportedCapabilities,
            IReadOnlyList <ILspHandlerDescriptor> descriptors
            )
        {
            if (descriptors.Count == 0)
            {
                return(Disposable.Empty); // No dynamic registrations supported by client.
            }
            var disposable = new CompositeDisposable();

            var result = initializeComplete
                         .LastOrDefaultAsync()
                         .Select(
                _ => {
                var registrations = new List <Registration>();
                foreach (var descriptor in descriptors)
                {
                    if (descriptor.HasCapability && supportedCapabilities.AllowsDynamicRegistration(descriptor.CapabilityType))
                    {
                        if (descriptor.RegistrationOptions is IWorkDoneProgressOptions wdpo)
                        {
                            wdpo.WorkDoneProgress = serverWorkDoneManager.IsSupported;
                        }

                        registrations.Add(
                            new Registration {
                            Id              = descriptor.Id.ToString(),
                            Method          = descriptor.Method,
                            RegisterOptions = descriptor.RegistrationOptions
                        }
                            );
                    }
                }

                return(registrations);
            }
                )
                         .SelectMany(
                registrations => Observable.FromAsync(ct => client.RegisterCapability(new RegistrationParams {
                Registrations = registrations
            }, ct)), (a, b) => a
                )
                         .Aggregate((z, b) => z)
                         .Subscribe(
                registrations => {
                disposable.Add(
                    Disposable.Create(
                        () => {
                    client.UnregisterCapability(
                        new UnregistrationParams {
                        Unregisterations = registrations
                    }
                        ).ToObservable().Subscribe();
                }
                        )
                    );
            }
                );

            disposable.Add(result);
            return(disposable);
        }