public void Should_Contain_AllDefinedMethods_OnLanguageServer_WithDifferentKeys(Type requestHandler, Type type2, string key, string key2, int count)
        {
            var handler = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue, new TextDocumentIdentifiers());
            var sub     = (IJsonRpcHandler)Substitute.For(new Type[] { requestHandler, type2 }, new object[0]);

            if (sub is IRegistration <TextDocumentRegistrationOptions> reg)
            {
                reg.GetRegistrationOptions()
                .Returns(new TextDocumentRegistrationOptions()
                {
                    DocumentSelector = new DocumentSelector()
                    {
                    }
                });
            }
            var sub2 = (IJsonRpcHandler)Substitute.For(new Type[] { requestHandler, type2 }, new object[0]);

            if (sub2 is IRegistration <TextDocumentRegistrationOptions> reg2)
            {
                reg2.GetRegistrationOptions()
                .Returns(new TextDocumentRegistrationOptions()
                {
                    DocumentSelector = new DocumentSelector()
                });
            }
            handler.Add(sub);
            handler.Add(sub2);
            handler._handlers.Should().Contain(x => x.Method == key);
            handler._handlers.Should().Contain(x => x.Method == key2);
            handler._handlers.Count.Should().Be(count);
        }
        public void Should_AllowSpecificHandlers_ToBeAdded(string method, Type handlerType)
        {
            var handler = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue, new TextDocumentIdentifiers());
            var sub     = (IJsonRpcHandler)Substitute.For(new Type[] { handlerType }, new object[0]);
            var sub2    = (IJsonRpcHandler)Substitute.For(new Type[] { handlerType }, new object[0]);

            handler.Add(method, sub);
            handler.Add(method, sub2);
            handler._handlers.Should().Contain(x => x.Method == method);
            handler._handlers.Should().Contain(x => x.Method == method);
            handler._handlers.Count.Should().Be(1);
        }
        public void Should_Contain_AllDefinedMethods_ForDifferentKeys(string key, int count)
        {
            var handler = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue, new TextDocumentIdentifiers());
            var sub     = TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cs"), "csharp");

            var sub2 = TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cake"), "csharp");

            handler.Add(sub);
            handler.Add(sub2);
            handler._handlers.Should().Contain(x => x.Method == key);
            handler._handlers.Count.Should().Be(count);
        }
        public async Task ShouldRouteToCorrect_Request_WithManyHandlers()
        {
            var textDocumentSyncHandler  = TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cs"), "csharp");
            var textDocumentSyncHandler2 = TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cake"), "csharp");

            textDocumentSyncHandler.Handle(Arg.Any <DidSaveTextDocumentParams>(), Arg.Any <CancellationToken>()).Returns(Unit.Value);
            textDocumentSyncHandler2.Handle(Arg.Any <DidSaveTextDocumentParams>(), Arg.Any <CancellationToken>()).Returns(Unit.Value);

            var codeActionHandler = Substitute.For <ICodeActionHandler>();

            codeActionHandler.GetRegistrationOptions().Returns(new CodeActionRegistrationOptions()
            {
                DocumentSelector = DocumentSelector.ForPattern("**/*.cs")
            });
            codeActionHandler
            .Handle(Arg.Any <CodeActionParams>(), Arg.Any <CancellationToken>())
            .Returns(new CommandOrCodeActionContainer());

            var registry           = new TestLanguageServerRegistry();
            var codeActionDelegate = Substitute.For <Func <CodeActionParams, CancellationToken, Task <CommandOrCodeActionContainer> > >();

            codeActionDelegate.Invoke(Arg.Any <CodeActionParams>(), Arg.Any <CancellationToken>())
            .Returns(new CommandOrCodeActionContainer());
            registry.OnCodeAction(
                codeActionDelegate,
                new CodeActionRegistrationOptions()
            {
                DocumentSelector = DocumentSelector.ForPattern("**/*.cake")
            }
                );

            var textDocumentIdentifiers = new TextDocumentIdentifiers();

            AutoSubstitute.Provide(textDocumentIdentifiers);
            var handlerCollection = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue, textDocumentIdentifiers)
            {
                textDocumentSyncHandler, textDocumentSyncHandler2, codeActionHandler
            };

            handlerCollection.Add(registry.Handlers);
            AutoSubstitute.Provide <IHandlerCollection>(handlerCollection);
            AutoSubstitute.Provide <IEnumerable <ILspHandlerDescriptor> >(handlerCollection);
            var mediator = AutoSubstitute.Resolve <LspRequestRouter>();

            var id      = Guid.NewGuid().ToString();
            var @params = new CodeActionParams()
            {
                TextDocument = new TextDocumentIdentifier(new Uri("file:///c:/test/123.cake"))
            };

            var request = new Request(id, DocumentNames.CodeAction, JObject.Parse(JsonConvert.SerializeObject(@params, new Serializer(ClientVersion.Lsp3).Settings)));

            await mediator.RouteRequest(mediator.GetDescriptor(request), request, CancellationToken.None);

            await codeActionHandler.Received(0).Handle(Arg.Any <CodeActionParams>(), Arg.Any <CancellationToken>());

            await codeActionDelegate.Received(1).Invoke(Arg.Any <CodeActionParams>(), Arg.Any <CancellationToken>());
        }
        public void Should_Contain_AllDefinedMethods(Type requestHandler, string key, int count)
        {
            var handler = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue, new TextDocumentIdentifiers());
            var sub     = (IJsonRpcHandler)Substitute.For(new Type[] { requestHandler }, new object[0]);

            handler.Add(sub);
            handler._handlers.Should().Contain(x => x.Method == key);
            handler._handlers.Count.Should().Be(count);
        }
        public void Should_DealWithClassesThatImplementMultipleHandlers_WithoutConflictingRegistrations(string method, IJsonRpcHandler sub)
        {
            var handler = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue, new TextDocumentIdentifiers());

            handler.Add(sub);

            var descriptor = handler._handlers.First(x => x.Method == method);

            descriptor.Key.Should().Be("default");
        }
        public IDisposable AddHandler(string method, IJsonRpcHandler handler)
        {
            var handlerDisposable = _collection.Add(method, handler);

            return(new ImmutableDisposable(
                       handlerDisposable,
                       new Disposable(() => {
                var foundItems = _collection
                                 .Where(x => handler == x.Handler)
                                 .Where(x => x.AllowsDynamicRegistration)
                                 .Select(x => x.Registration)
                                 .Where(x => x != null)
                                 .ToArray();

                Task.Run(() => this.UnregisterCapability(new UnregistrationParams()
                {
                    Unregisterations = foundItems
                }));
            })));
        }
        public void Should_Contain_AllDefinedLanguageServerMethods(string key, int count)
        {
            var handler = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue, new TextDocumentIdentifiers());

            handler.Add(
                Substitute.For <IInitializeHandler>(),
                Substitute.For <IInitializedHandler>(),
                Substitute.For <IExitHandler>(),
                Substitute.For <IShutdownHandler>()
                );
            handler._handlers.Should().Contain(x => x.Method == key);
            handler._handlers.Count.Should().Be(count);
        }
        public void Should_Contain_AllConcreteDefinedMethods()
        {
            var handler = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue, new TextDocumentIdentifiers());

            handler.Add(
                Substitute.For <IExitHandler>(),
                Substitute.For <IInitializeHandler>(),
                Substitute.For <IInitializedHandler>(),
                Substitute.For <IShutdownHandler>()
                );

            handler._handlers.Should().Contain(x => x.Method == "exit");
            handler._handlers.Should().Contain(x => x.Method == "shutdown");
            handler._handlers.Count.Should().Be(4);
        }
        public void Should_DealWithClassesThatImplementMultipleHandlers_BySettingKeyAccordingly()
        {
            var codeLensHandler = Substitute.For(new Type[] { typeof(ICodeLensHandler), typeof(ICodeLensResolveHandler) }, new object[0]);

            ((ICodeLensHandler)codeLensHandler).GetRegistrationOptions()
            .Returns(new CodeLensRegistrationOptions()
            {
                DocumentSelector = new DocumentSelector(DocumentFilter.ForLanguage("foo"))
            });

            var handler = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue, new TextDocumentIdentifiers());

            handler.Add(codeLensHandler as IJsonRpcHandler);

            var descriptor = handler._handlers.Select(x => x.Key);

            descriptor.Should().BeEquivalentTo(new [] { "[foo]", "[foo]" });
        }