コード例 #1
0
        public async Task HandleRequestAsync_InvokesServerWithCorrectKey()
        {
            // Arrange
            var documentManager = new TestDocumentManager();

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

            var invokedServer  = false;
            var requestInvoker = new Mock <LSPRequestInvoker>();

            requestInvoker
            .Setup(r => r.ReinvokeRequestOnServerAsync <DocumentOnTypeFormattingParams, TextEdit[]>(It.IsAny <string>(), It.IsAny <LanguageServerKind>(), It.IsAny <DocumentOnTypeFormattingParams>(), It.IsAny <CancellationToken>()))
            .Callback <string, LanguageServerKind, DocumentOnTypeFormattingParams, CancellationToken>((method, serverKind, formattingParams, ct) =>
            {
                Assert.True(formattingParams.Options.OtherOptions.ContainsKey(LanguageServerConstants.ExpectsCursorPlaceholderKey));
                invokedServer = true;
            })
            .Returns(Task.FromResult <TextEdit[]>(new[] { new TextEdit() }));

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

            projectionProvider.Setup(p => p.GetProjectionAsync(It.IsAny <LSPDocumentSnapshot>(), It.IsAny <Position>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(projectionResult));
            var documentMappingProvider = Mock.Of <LSPDocumentMappingProvider>();
            var editorService           = Mock.Of <LSPEditorService>();

            var handler = new TestOnTypeFormattingHandler(JoinableTaskContext, documentManager, requestInvoker.Object, projectionProvider.Object, documentMappingProvider, editorService);
            var request = new DocumentOnTypeFormattingParams()
            {
                Character    = ">",
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                Options = new FormattingOptions()
                {
                    OtherOptions = new Dictionary <string, object>()
                },
                Position = new Position(0, 0)
            };

            // Act
            var edits = await handler.HandleRequestAsync(request, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.True(invokedServer);
            Assert.Empty(edits);
        }
コード例 #2
0
        public async Task HandleRequestAsync_UnknownTriggerCharacter_DoesNotInvokeServer()
        {
            // Arrange
            var documentManager = new TestDocumentManager();

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

            var invokedServer  = false;
            var requestInvoker = new Mock <LSPRequestInvoker>();

            requestInvoker
            .Setup(r => r.ReinvokeRequestOnServerAsync <DocumentOnTypeFormattingParams, TextEdit[]>(It.IsAny <string>(), It.IsAny <LanguageServerKind>(), It.IsAny <DocumentOnTypeFormattingParams>(), It.IsAny <CancellationToken>()))
            .Callback <string, LanguageServerKind, DocumentOnTypeFormattingParams, CancellationToken>((method, serverKind, formattingParams, ct) =>
            {
                invokedServer = true;
            })
            .Returns(Task.FromResult <TextEdit[]>(new[] { new TextEdit() }));

            var projectionProvider      = Mock.Of <LSPProjectionProvider>();
            var documentMappingProvider = Mock.Of <LSPDocumentMappingProvider>();
            var editorService           = Mock.Of <LSPEditorService>();

            var handler = new TestOnTypeFormattingHandler(JoinableTaskContext, documentManager, requestInvoker.Object, projectionProvider, documentMappingProvider, editorService);
            var request = new DocumentOnTypeFormattingParams()
            {
                Character    = "?",
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                Options = new FormattingOptions()
                {
                    OtherOptions = new Dictionary <string, object>()
                },
                Position = new Position(0, 0)
            };

            // Act
            var edits = await handler.HandleRequestAsync(request, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.False(invokedServer);
            Assert.Empty(edits);
        }
コード例 #3
0
        public async Task HandleRequestAsync_InvokesServer_RemapsAndAppliesEdits()
        {
            // Arrange
            var documentManager = new TestDocumentManager();

            documentManager.AddDocument(Uri, Mock.Of <LSPDocumentSnapshot>(s => s.Uri == Uri && s.Snapshot == Mock.Of <ITextSnapshot>()));

            var invokedServer    = false;
            var mappedTextEdits  = false;
            var appliedTextEdits = false;
            var requestInvoker   = new Mock <LSPRequestInvoker>(MockBehavior.Strict);

            requestInvoker
            .Setup(r => r.ReinvokeRequestOnServerAsync <DocumentOnTypeFormattingParams, TextEdit[]>(Methods.TextDocumentOnTypeFormattingName, It.IsAny <LanguageServerKind>(), It.IsAny <DocumentOnTypeFormattingParams>(), It.IsAny <CancellationToken>()))
            .Callback <string, LanguageServerKind, DocumentOnTypeFormattingParams, CancellationToken>((method, serverKind, formattingParams, ct) =>
            {
                Assert.True(formattingParams.Options.OtherOptions.ContainsKey(LanguageServerConstants.ExpectsCursorPlaceholderKey));
                invokedServer = true;
            })
            .Returns(Task.FromResult <TextEdit[]>(new[] { new TextEdit()
                                                          {
                                                              Range = new Range(), NewText = "sometext"
                                                          } }));

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

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

            var documentMappingProvider = new Mock <LSPDocumentMappingProvider>(MockBehavior.Strict);

            documentMappingProvider
            .Setup(d => d.MapToDocumentRangeAsync(RazorLanguageKind.Html, Uri, It.IsAny <Range>(), It.IsAny <CancellationToken>()))
            .Callback(() => { mappedTextEdits = true; })
            .Returns(Task.FromResult(new RazorMapToDocumentRangeResponse()));

            var editorService = new Mock <LSPEditorService>(MockBehavior.Strict);

            editorService.Setup(e => e.ApplyTextEditsAsync(Uri, It.IsAny <ITextSnapshot>(), It.IsAny <IEnumerable <TextEdit> >())).Callback(() => { appliedTextEdits = true; })
            .Returns(Task.CompletedTask);

            var handler = new TestOnTypeFormattingHandler(JoinableTaskContext, documentManager, requestInvoker.Object, projectionProvider.Object, documentMappingProvider.Object, editorService.Object);
            var request = new DocumentOnTypeFormattingParams()
            {
                Character    = "=",
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                Options = new FormattingOptions()
                {
                    OtherOptions = new Dictionary <string, object>()
                },
                Position = new Position(1, 4)
            };

            // Act
            var edits = await handler.HandleRequestAsync(request, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.True(invokedServer);
            Assert.True(mappedTextEdits);
            Assert.True(appliedTextEdits);
            Assert.Empty(edits);
        }