Esempio n. 1
0
        public async Task RazorMapToDocumentRangeAsync_InvokesLanguageServer()
        {
            // Arrange
            var uri = new Uri("file:///some/folder/to/file.razor");

            var response = new RazorMapToDocumentRangeResponse()
            {
                Range = new Range()
                {
                    Start = new Position(1, 1),
                    End   = new Position(3, 3),
                },
                HostDocumentVersion = 1
            };
            var requestInvoker = new Mock <LSPRequestInvoker>(MockBehavior.Strict);

            requestInvoker
            .Setup(r => r.RequestServerAsync <RazorMapToDocumentRangeParams, RazorMapToDocumentRangeResponse>(LanguageServerConstants.RazorMapToDocumentRangeEndpoint, LanguageServerKind.Razor, It.IsAny <RazorMapToDocumentRangeParams>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(response));

            var mappingProvider = new DefaultLSPDocumentMappingProvider(requestInvoker.Object);
            var projectedRange  = new Range()
            {
                Start = new Position(10, 10),
                End   = new Position(15, 15)
            };

            // Act
            var result = await mappingProvider.MapToDocumentRangeAsync(RazorLanguageKind.CSharp, uri, projectedRange, CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(1, result.HostDocumentVersion);
            Assert.Equal(new Position(1, 1), result.Range.Start);
            Assert.Equal(new Position(3, 3), result.Range.End);
        }
Esempio n. 2
0
        public async Task HandleRequestAsync_CSharpProjection_InvokesCSharpLanguageServer()
        {
            // Arrange
            var called = false;

            var expectedContents = new SumType <SumType <string, MarkedString>, SumType <string, MarkedString>[], MarkupContent>(
                new MarkedString()
            {
                Language = "markdown",
                Value    = "Hover Details"
            }
                );

            var lspResponse = new Hover()
            {
                Range = new Range()
                {
                    Start = new Position(10, 0),
                    End   = new Position(10, 1)
                },
                Contents = expectedContents
            };

            var expectedItem = new Hover()
            {
                Range = new Range()
                {
                    Start = new Position(0, 0),
                    End   = new Position(0, 1)
                },
                Contents = expectedContents
            };

            var hoverRequest = new TextDocumentPositionParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                Position = new Position(0, 1)
            };

            var documentManager = new TestDocumentManager();

            documentManager.AddDocument(Uri, Mock.Of <LSPDocumentSnapshot>());

            var requestInvoker = new Mock <LSPRequestInvoker>();

            requestInvoker
            .Setup(r => r.ReinvokeRequestOnServerAsync <TextDocumentPositionParams, Hover>(It.IsAny <string>(), It.IsAny <LanguageServerKind>(), It.IsAny <TextDocumentPositionParams>(), It.IsAny <CancellationToken>()))
            .Callback <string, LanguageServerKind, TextDocumentPositionParams, CancellationToken>((method, serverKind, hoverParams, ct) =>
            {
                Assert.Equal(Methods.TextDocumentHoverName, method);
                Assert.Equal(LanguageServerKind.CSharp, serverKind);
                called = true;
            })
            .Returns(Task.FromResult(lspResponse));

            var projectionResult = new ProjectionResult()
            {
                LanguageKind = RazorLanguageKind.CSharp,
            };
            var projectionProvider = new Mock <LSPProjectionProvider>();

            projectionProvider.Setup(p => p.GetProjectionAsync(It.IsAny <LSPDocumentSnapshot>(), It.IsAny <Position>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(projectionResult));

            var remappingResult = new RazorMapToDocumentRangeResponse()
            {
                Range = new Range()
                {
                    Start = new Position(0, 0),
                    End   = new Position(0, 1)
                }
            };
            var documentMappingProvider = new Mock <LSPDocumentMappingProvider>();

            documentMappingProvider.Setup(d => d.MapToDocumentRangeAsync(RazorLanguageKind.CSharp, It.IsAny <Uri>(), It.IsAny <Range>(), It.IsAny <CancellationToken>())).
            Returns(Task.FromResult(remappingResult));

            var hoverHandler = new HoverHandler(JoinableTaskContext, requestInvoker.Object, documentManager, projectionProvider.Object, documentMappingProvider.Object);

            // Act
            var result = await hoverHandler.HandleRequestAsync(hoverRequest, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.True(called);
            Assert.Equal(expectedItem.Contents, result.Contents);
            Assert.Equal(expectedItem.Range, result.Range);
        }