Example #1
0
        public void Should_DealWithClassesThatImplementMultipleHandlers_WithoutConflictingRegistrations(string method, IJsonRpcHandler sub)
        {
            var handler = new SharedHandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue, new TextDocumentIdentifiers(), Substitute.For <IResolverContext>(),
                                                      new LspHandlerTypeDescriptorProvider(new [] { typeof(FoundationTests).Assembly, typeof(LanguageServer).Assembly, typeof(LanguageClient).Assembly, typeof(IRegistrationManager).Assembly, typeof(LspRequestRouter).Assembly }));

            handler.Initialize();
            handler.Add(sub);

            var descriptor = handler.OfType <LspHandlerDescriptor>().First(x => x.Method == method);

            descriptor.Key.Should().Be("default");
            handler.Initialize();
        }
Example #2
0
        public void Should_Contain_AllDefinedMethods_OnLanguageServer_WithDifferentKeys(Type requestHandler, Type type2, string key, string key2, int count)
        {
            var handler = new SharedHandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue, new TextDocumentIdentifiers(), Substitute.For <IResolverContext>(),
                                                      new LspHandlerTypeDescriptorProvider(new [] { typeof(FoundationTests).Assembly, typeof(LanguageServer).Assembly, typeof(LanguageClient).Assembly, typeof(IRegistrationManager).Assembly, typeof(LspRequestRouter).Assembly }));            handler.Initialize();

            handler.Initialize();
            var sub = (IJsonRpcHandler)Substitute.For(new[] { requestHandler, type2 }, new object[0]);

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

            if (sub2 is IRegistration <ITextDocumentRegistrationOptions> reg2)
            {
                reg2.GetRegistrationOptions(Arg.Any <ClientCapabilities>())
                .Returns(
                    new TextDocumentSyncRegistrationOptions {
                    DocumentSelector = new DocumentSelector()
                }
                    );
            }
            handler.Add(sub);
            handler.Add(sub2);
            handler.Should().Contain(x => x.Method == key);
            handler.Should().Contain(x => x.Method == key2);
            handler.Should().HaveCount(count);
        }
Example #3
0
        public void Should_Return_Code_Lens_Descriptor()
        {
            // Given
            var textDocumentSyncHandler =
                TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cs"), "csharp");
            var textDocumentIdentifiers = new TextDocumentIdentifiers();

            AutoSubstitute.Provide(textDocumentIdentifiers);
            var collection = new SharedHandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue, textDocumentIdentifiers, Substitute.For <IResolverContext>(),
                                                         new LspHandlerTypeDescriptorProvider(new [] { typeof(FoundationTests).Assembly, typeof(LanguageServer).Assembly, typeof(LanguageClient).Assembly, typeof(IRegistrationManager).Assembly, typeof(LspRequestRouter).Assembly }))
            {
                textDocumentSyncHandler
            };

            AutoSubstitute.Provide <IHandlerCollection>(collection);
            AutoSubstitute.Provide <IEnumerable <ILspHandlerDescriptor> >(collection);
            var handlerMatcher = AutoSubstitute.Resolve <TextDocumentMatcher>();

            var codeLensHandler = (ICodeLensHandler)Substitute.For(new[] { typeof(ICodeLensHandler), typeof(ICodeLensResolveHandler) }, new object[0]);

            codeLensHandler.GetRegistrationOptions(Arg.Any <CodeLensCapability>(), Arg.Any <ClientCapabilities>())
            .Returns(
                new CodeLensRegistrationOptions {
                DocumentSelector = new DocumentSelector(new DocumentFilter {
                    Pattern = "**/*.cs"
                })
            }
                );

            var codeLensHandler2 = (ICodeLensHandler)Substitute.For(new[] { typeof(ICodeLensHandler), typeof(ICodeLensResolveHandler) }, new object[0]);

            codeLensHandler2.GetRegistrationOptions(Arg.Any <CodeLensCapability>(), Arg.Any <ClientCapabilities>())
            .Returns(
                new CodeLensRegistrationOptions {
                DocumentSelector = new DocumentSelector(new DocumentFilter {
                    Pattern = "**/*.cake"
                })
            }
                );
            collection.Add(codeLensHandler, codeLensHandler2);
            collection.Initialize();

            // When
            var result = handlerMatcher.FindHandler(
                new CodeLensParams {
                TextDocument = new OptionalVersionedTextDocumentIdentifier {
                    Uri = new Uri("file:///abc/123/d.cs"), Version = 1
                }
            },
                collection.Where(x => x.Method == TextDocumentNames.CodeLens)
                );

            // Then
            var lspHandlerDescriptors = result as ILspHandlerDescriptor[] ?? result.ToArray();

            lspHandlerDescriptors.Should().NotBeNullOrEmpty();
            lspHandlerDescriptors.Should().Contain(x => x.Method == TextDocumentNames.CodeLens);
            lspHandlerDescriptors.Should().Contain(x => ((LspHandlerDescriptor)x).Key == "[**/*.cs]");
        }
Example #4
0
        public void Should_Contain_AllDefinedTextDocumentSyncMethods(string key, int count)
        {
            var handler = new SharedHandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue, new TextDocumentIdentifiers(), Substitute.For <IResolverContext>(),
                                                      new LspHandlerTypeDescriptorProvider(new [] { typeof(FoundationTests).Assembly, typeof(LanguageServer).Assembly, typeof(LanguageClient).Assembly, typeof(IRegistrationManager).Assembly, typeof(LspRequestRouter).Assembly }));

            handler.Initialize();
            var sub = (IJsonRpcHandler)TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.something"), "csharp");

            handler.Add(sub);
            handler.Should().Contain(x => x.Method == key);
            handler.Should().HaveCount(count);
        }
Example #5
0
        public void Should_Contain_AllDefinedMethods(Type requestHandler, string key, int count)
        {
            var handler = new SharedHandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue, new TextDocumentIdentifiers(), Substitute.For <IResolverContext>(),
                                                      new LspHandlerTypeDescriptorProvider(new [] { typeof(FoundationTests).Assembly, typeof(LanguageServer).Assembly, typeof(LanguageClient).Assembly, typeof(IRegistrationManager).Assembly, typeof(LspRequestRouter).Assembly }));

            handler.Initialize();
            var sub = (IJsonRpcHandler)Substitute.For(new[] { requestHandler }, new object[0]);

            handler.Add(sub);
            handler.Should().Contain(x => x.Method == key);
            handler.Should().HaveCount(count);
        }
Example #6
0
        public async Task ShouldRouteToCorrect_Notification_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 textDocumentIdentifiers = new TextDocumentIdentifiers();

            AutoSubstitute.Provide(textDocumentIdentifiers);
            var collection =
                new SharedHandlerCollection(
                    SupportedCapabilitiesFixture.AlwaysTrue, textDocumentIdentifiers, Substitute.For <IResolverContext>(),
                    new LspHandlerTypeDescriptorProvider(
                        new[] {
                typeof(FoundationTests).Assembly, typeof(LanguageServer).Assembly, typeof(LanguageClient).Assembly, typeof(IRegistrationManager).Assembly,
                typeof(LspRequestRouter).Assembly
            }
                        )
                    )
            {
                textDocumentSyncHandler, textDocumentSyncHandler2
            };

            collection.Initialize();
            AutoSubstitute.Provide <IHandlerCollection>(collection);
            AutoSubstitute.Provide <IEnumerable <ILspHandlerDescriptor> >(collection);
            AutoSubstitute.Provide <IHandlerMatcher>(new TextDocumentMatcher(LoggerFactory.CreateLogger <TextDocumentMatcher>(), textDocumentIdentifiers));
            var mediator = AutoSubstitute.Resolve <LspRequestRouter>();

            var @params = new DidSaveTextDocumentParams {
                TextDocument = new TextDocumentIdentifier(new Uri("file:///c:/test/123.cake"))
            };

            var request = new Notification(
                TextDocumentNames.DidSave,
                JObject.Parse(JsonConvert.SerializeObject(@params, new LspSerializer(ClientVersion.Lsp3).Settings))
                );

            await mediator.RouteNotification(mediator.GetDescriptors(request), request, CancellationToken.None);

            await textDocumentSyncHandler.Received(0)
            .Handle(Arg.Any <DidSaveTextDocumentParams>(), Arg.Any <CancellationToken>());

            await textDocumentSyncHandler2.Received(1)
            .Handle(Arg.Any <DidSaveTextDocumentParams>(), Arg.Any <CancellationToken>());
        }
Example #7
0
        public void Should_AllowSpecificHandlers_ToBeAdded(string method, Type handlerType)
        {
            var handler = new SharedHandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue, new TextDocumentIdentifiers(), Substitute.For <IResolverContext>(),
                                                      new LspHandlerTypeDescriptorProvider(new [] { typeof(FoundationTests).Assembly, typeof(LanguageServer).Assembly, typeof(LanguageClient).Assembly, typeof(IRegistrationManager).Assembly, typeof(LspRequestRouter).Assembly }));

            handler.Initialize();

            var sub  = (IJsonRpcHandler)Substitute.For(new[] { handlerType }, new object[0]);
            var sub2 = (IJsonRpcHandler)Substitute.For(new[] { handlerType }, new object[0]);

            handler.Add(method, sub, null);
            handler.Add(method, sub2, null);
            handler.Should().Contain(x => x.Method == method);
            handler.Should().Contain(x => x.Method == method);
            handler.Should().HaveCount(1);
        }
Example #8
0
        public void Should_Contain_AllConcreteDefinedMethods()
        {
            var handler = new SharedHandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue, new TextDocumentIdentifiers(), Substitute.For <IResolverContext>(),
                                                      new LspHandlerTypeDescriptorProvider(new [] { typeof(FoundationTests).Assembly, typeof(LanguageServer).Assembly, typeof(LanguageClient).Assembly, typeof(IRegistrationManager).Assembly, typeof(LspRequestRouter).Assembly }));

            handler.Initialize();

            handler.Add(
                Substitute.For <IExitHandler>(),
                Substitute.For <ILanguageProtocolInitializeHandler>(),
                Substitute.For <ILanguageProtocolInitializedHandler>(),
                Substitute.For <IShutdownHandler>()
                );

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

            ((ICodeLensHandler)codeLensHandler).GetRegistrationOptions(Arg.Any <CodeLensCapability>(), Arg.Any <ClientCapabilities>())
            .Returns(
                new CodeLensRegistrationOptions {
                DocumentSelector = new DocumentSelector(DocumentFilter.ForLanguage("foo"))
            }
                );

            var handler = new SharedHandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue, new TextDocumentIdentifiers(), Substitute.For <IResolverContext>(),
                                                      new LspHandlerTypeDescriptorProvider(new [] { typeof(FoundationTests).Assembly, typeof(LanguageServer).Assembly, typeof(LanguageClient).Assembly, typeof(IRegistrationManager).Assembly, typeof(LspRequestRouter).Assembly }));

            handler.Initialize();
            handler.Add((codeLensHandler as IJsonRpcHandler) !);

            var descriptor = handler.OfType <LspHandlerDescriptor>().Select(x => x.Key);

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