public async Task TrySynchronizeVirtualDocumentAsync_SimultaneousEqualSynchronizationRequests_ReturnsTrue()
        {
            // Arrange
            var synchronizer = new DefaultLSPDocumentSynchronizer(DocumentManager, JoinableTaskContext);

            synchronizer._synchronizationTimeout = TimeSpan.FromMilliseconds(500);
            var originalVirtualDocument = new TestVirtualDocumentSnapshot(VirtualDocumentUri, 123);
            var originalDocument        = new TestLSPDocumentSnapshot(LSPDocumentUri, 124, originalVirtualDocument);

            // Start synchronize
            var synchronizeTask1 = synchronizer.TrySynchronizeVirtualDocumentAsync(originalDocument, originalVirtualDocument, CancellationToken.None);
            var synchronizeTask2 = synchronizer.TrySynchronizeVirtualDocumentAsync(originalDocument, originalVirtualDocument, CancellationToken.None);

            var newVirtualDocument = originalVirtualDocument.Fork(124);
            var newDocument        = originalDocument.Fork(124, newVirtualDocument);
            var args = new LSPDocumentChangeEventArgs(originalDocument, newDocument, LSPDocumentChangeKind.VirtualDocumentChanged);

            // Act
            synchronizer.DocumentManager_Changed(DocumentManager, args);
            var result1 = await synchronizeTask1.ConfigureAwait(false);

            var result2 = await synchronizeTask2.ConfigureAwait(false);

            // Assert
            Assert.True(result1);
            Assert.True(result2);
        }
        public async Task ApplyChangesAsync_VirtualHtmlDocumentNotFound_ReturnsEmptyDiagnosticResponse()
        {
            // Arrange
            var diagnosticsProvider = Mock.Of <LSPDiagnosticsTranslator>(MockBehavior.Strict);

            var testVirtualDocument          = new TestVirtualDocumentSnapshot(RazorUri, hostDocumentVersion: 0);
            LSPDocumentSnapshot testDocument = new TestLSPDocumentSnapshot(RazorUri, version: 0, testVirtualDocument);
            var documentManager = new Mock <TrackingLSPDocumentManager>(MockBehavior.Strict);

            documentManager.Setup(manager => manager.TryGetDocument(It.IsAny <Uri>(), out testDocument))
            .Returns(true);

            var htmlDiagnosticsInterceptor = new RazorHtmlPublishDiagnosticsInterceptor(documentManager.Object, diagnosticsProvider, LoggerProvider);
            var diagnosticRequest          = new VSPublishDiagnosticParams()
            {
                Diagnostics = Diagnostics,
                Mode        = null,
                Uri         = RazorVirtualHtmlUri
            };

            // Act
            var result = await htmlDiagnosticsInterceptor.ApplyChangesAsync(JToken.FromObject(diagnosticRequest), string.Empty, cancellationToken : default).ConfigureAwait(false);

            // Assert
            var updatedParams = result.UpdatedToken.ToObject <VSPublishDiagnosticParams>();

            Assert.Empty(updatedParams.Diagnostics);
            Assert.Equal(RazorUri, updatedParams.Uri);
            Assert.True(result.ChangedDocumentUri);
        }
示例#3
0
        public async Task ProvideCodeActionsAsync_ReturnsCodeActionsAsync()
        {
            // Arrange
            var testDocUri        = new Uri("C:/path/to/file.razor");
            var testVirtualDocUri = new Uri("C:/path/to/file2.razor.g");
            var testCSharpDocUri  = new Uri("C:/path/to/file.razor.g.cs");

            var testVirtualDocument          = new TestVirtualDocumentSnapshot(testVirtualDocUri, 0);
            var csharpVirtualDocument        = new CSharpVirtualDocumentSnapshot(testCSharpDocUri, Mock.Of <ITextSnapshot>(MockBehavior.Strict), 0);
            LSPDocumentSnapshot testDocument = new TestLSPDocumentSnapshot(testDocUri, 0, testVirtualDocument, csharpVirtualDocument);

            var documentManager = new Mock <TrackingLSPDocumentManager>(MockBehavior.Strict);

            documentManager.Setup(manager => manager.TryGetDocument(It.IsAny <Uri>(), out testDocument))
            .Returns(true);

            var languageServer1Response = new[] { new VSCodeAction()
                                                  {
                                                      Title = "Response 1"
                                                  } };
            var languageServer2Response = new[] { new VSCodeAction()
                                                  {
                                                      Title = "Response 2"
                                                  } };
            IEnumerable <VSCodeAction[]> expectedResults = new List <VSCodeAction[]>()
            {
                languageServer1Response, languageServer2Response
            };
            var requestInvoker = new Mock <LSPRequestInvoker>(MockBehavior.Strict);

            requestInvoker.Setup(invoker => invoker.ReinvokeRequestOnMultipleServersAsync <CodeActionParams, VSCodeAction[]>(
                                     Methods.TextDocumentCodeActionName,
                                     LanguageServerKind.CSharp.ToContentType(),
                                     It.IsAny <Func <JToken, bool> >(),
                                     It.IsAny <CodeActionParams>(),
                                     It.IsAny <CancellationToken>()
                                     )).Returns(Task.FromResult(expectedResults));

            var uIContextManager = new Mock <RazorUIContextManager>(MockBehavior.Strict);
            var disposable       = new Mock <IDisposable>(MockBehavior.Strict);

            var target  = new DefaultRazorLanguageServerCustomMessageTarget(documentManager.Object, JoinableTaskContext, requestInvoker.Object, uIContextManager.Object, disposable.Object);
            var request = new CodeActionParams()
            {
                TextDocument = new LanguageServer.Protocol.TextDocumentIdentifier()
                {
                    Uri = testDocUri
                }
            };

            // Act
            var result = await target.ProvideCodeActionsAsync(request, CancellationToken.None);

            // Assert
            Assert.Collection(result,
                              r => Assert.Equal(languageServer1Response[0].Title, r.Title),
                              r => Assert.Equal(languageServer2Response[0].Title, r.Title));
        }
示例#4
0
        private (TestLSPDocumentSnapshot, TestVirtualDocumentSnapshot) CreateDocuments(int lspDocumentVersion, long virtualDocumentSyncVersion)
        {
            var virtualDocumentUri = new Uri("C:/path/to/file.razor__virtual.cs");
            var virtualDocument    = new TestVirtualDocumentSnapshot(virtualDocumentUri, virtualDocumentSyncVersion, VirtualDocumentSnapshot);
            var documentUri        = new Uri("C:/path/to/file.razor");
            var document           = new TestLSPDocumentSnapshot(documentUri, lspDocumentVersion, virtualDocument);

            return(document, virtualDocument);
        }
        public async Task ProvideSemanticTokensAsync_ReturnsSemanticTokensAsync()
        {
            // Arrange
            var testDocUri        = new Uri("C:/path/to - project/file.razor");
            var testVirtualDocUri = new Uri("C:/path/to - project/file2.razor.g");
            var testCSharpDocUri  = new Uri("C:/path/to - project/file.razor.g.cs");

            var documentVersion              = 0;
            var testVirtualDocument          = new TestVirtualDocumentSnapshot(testVirtualDocUri, 0);
            var csharpVirtualDocument        = new CSharpVirtualDocumentSnapshot(testCSharpDocUri, TextBuffer.CurrentSnapshot, 0);
            LSPDocumentSnapshot testDocument = new TestLSPDocumentSnapshot(testDocUri, documentVersion, testVirtualDocument, csharpVirtualDocument);

            var documentManager = new Mock <TrackingLSPDocumentManager>(MockBehavior.Strict);

            documentManager.Setup(manager => manager.TryGetDocument(testDocUri, out testDocument))
            .Returns(true);

            var expectedcSharpResults = new VSSemanticTokensResponse();
            var requestInvoker        = new Mock <LSPRequestInvoker>(MockBehavior.Strict);

            requestInvoker.Setup(invoker => invoker.ReinvokeRequestOnServerAsync <OmniSharp.Extensions.LanguageServer.Protocol.Models.SemanticTokensParams, VSSemanticTokensResponse>(
                                     TextBuffer,
                                     Methods.TextDocumentSemanticTokensFullName,
                                     LanguageServerKind.CSharp.ToLanguageServerName(),
                                     It.IsAny <OmniSharp.Extensions.LanguageServer.Protocol.Models.SemanticTokensParams>(),
                                     It.IsAny <CancellationToken>()
                                     )).Returns(Task.FromResult(new ReinvocationResponse <VSSemanticTokensResponse>("languageClient", expectedcSharpResults)));

            var uIContextManager     = new Mock <RazorUIContextManager>(MockBehavior.Strict);
            var disposable           = new Mock <IDisposable>(MockBehavior.Strict);
            var clientOptionsMonitor = new Mock <RazorLSPClientOptionsMonitor>(MockBehavior.Strict);
            var documentSynchronizer = new Mock <LSPDocumentSynchronizer>(MockBehavior.Strict);

            documentSynchronizer.Setup(r => r.TrySynchronizeVirtualDocumentAsync(0, It.IsAny <CSharpVirtualDocumentSnapshot>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(true));

            var target = new DefaultRazorLanguageServerCustomMessageTarget(
                documentManager.Object, JoinableTaskContext, requestInvoker.Object,
                uIContextManager.Object, disposable.Object, clientOptionsMonitor.Object, documentSynchronizer.Object);
            var request = new ProvideSemanticTokensParams()
            {
                TextDocument = new OmniSharpTextDocumentIdentifier()
                {
                    Uri = new Uri("C:/path/to%20-%20project/file.razor")
                },
                RequiredHostDocumentVersion = 0,
            };
            var expectedResults = new ProvideSemanticTokensResponse(
                expectedcSharpResults.ResultId, expectedcSharpResults.Data, expectedcSharpResults.IsFinalized, documentVersion);

            // Act
            var result = await target.ProvideSemanticTokensAsync(request, CancellationToken.None);

            // Assert
            Assert.Equal(expectedResults, result);
        }
        private TrackingLSPDocumentManager CreateDocumentManager(int hostDocumentVersion = 0)
        {
            var testVirtualDocUri            = RazorVirtualHtmlUri;
            var testVirtualDocument          = new TestVirtualDocumentSnapshot(RazorUri, hostDocumentVersion);
            var htmlVirtualDocument          = new HtmlVirtualDocumentSnapshot(testVirtualDocUri, Mock.Of <ITextSnapshot>(MockBehavior.Strict), hostDocumentVersion);
            LSPDocumentSnapshot testDocument = new TestLSPDocumentSnapshot(RazorUri, hostDocumentVersion, testVirtualDocument, htmlVirtualDocument);
            var documentManager = new Mock <TrackingLSPDocumentManager>(MockBehavior.Strict);

            documentManager.Setup(manager => manager.TryGetDocument(It.IsAny <Uri>(), out testDocument))
            .Returns(true);
            return(documentManager.Object);
        }
        public async Task RazorServerReadyAsync_ReportsReadyAsync()
        {
            // Arrange
            var testDocUri        = new Uri("C:/path/to/file.razor");
            var testVirtualDocUri = new Uri("C:/path/to/file2.razor.g");
            var testCSharpDocUri  = new Uri("C:/path/to/file.razor.g.cs");

            var testVirtualDocument          = new TestVirtualDocumentSnapshot(testVirtualDocUri, 0);
            var csharpVirtualDocument        = new CSharpVirtualDocumentSnapshot(testCSharpDocUri, TextBuffer.CurrentSnapshot, 0);
            LSPDocumentSnapshot testDocument = new TestLSPDocumentSnapshot(testDocUri, 0, testVirtualDocument, csharpVirtualDocument);

            var documentManager = new Mock <TrackingLSPDocumentManager>(MockBehavior.Strict);

            documentManager.Setup(manager => manager.TryGetDocument(It.IsAny <Uri>(), out testDocument))
            .Returns(true);

            var expectedResults = new SemanticTokens {
            };
            var requestInvoker  = new Mock <LSPRequestInvoker>(MockBehavior.Strict);

            requestInvoker.Setup(invoker => invoker.ReinvokeRequestOnServerAsync <SemanticTokensParams, SemanticTokens>(
                                     TextBuffer,
                                     Methods.TextDocumentSemanticTokensFullName,
                                     LanguageServerKind.CSharp.ToContentType(),
                                     It.IsAny <SemanticTokensParams>(),
                                     It.IsAny <CancellationToken>()
                                     )).Returns(Task.FromResult(new ReinvocationResponse <SemanticTokens>("languageClient", expectedResults)));

            var uIContextManager = new Mock <RazorUIContextManager>(MockBehavior.Strict);

            uIContextManager.Setup(m => m.SetUIContextAsync(RazorLSPConstants.RazorActiveUIContextGuid, true, It.IsAny <CancellationToken>()))
            .Returns(() => Task.CompletedTask)
            .Verifiable();
            var disposable = new Mock <IDisposable>(MockBehavior.Strict);

            disposable
            .Setup(d => d.Dispose())
            .Verifiable();
            var clientOptionsMonitor = new Mock <RazorLSPClientOptionsMonitor>(MockBehavior.Strict);
            var documentSynchronizer = new Mock <LSPDocumentSynchronizer>(MockBehavior.Strict);

            var target = new DefaultRazorLanguageServerCustomMessageTarget(
                documentManager.Object, JoinableTaskContext, requestInvoker.Object,
                uIContextManager.Object, disposable.Object, clientOptionsMonitor.Object, documentSynchronizer.Object);

            // Act
            await target.RazorServerReadyAsync(CancellationToken.None);

            // Assert
            uIContextManager.Verify();
            disposable.Verify();
        }
        public async Task TrySynchronizeVirtualDocumentAsync_SynchronizedDocument_ReturnsTrue()
        {
            // Arrange
            var synchronizer    = new DefaultLSPDocumentSynchronizer(DocumentManager, JoinableTaskContext);
            var virtualDocument = new TestVirtualDocumentSnapshot(VirtualDocumentUri, 123);
            var document        = new TestLSPDocumentSnapshot(LSPDocumentUri, 123, virtualDocument);

            // Act
            var result = await synchronizer.TrySynchronizeVirtualDocumentAsync(document, virtualDocument, CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.True(result);
        }
        public async Task ProvideSemanticTokensAsync_ReturnsSemanticTokensAsync()
        {
            // Arrange
            var testDocUri        = new Uri("C:/path/to - project/file.razor");
            var testVirtualDocUri = new Uri("C:/path/to - project/file2.razor.g");
            var testCSharpDocUri  = new Uri("C:/path/to - project/file.razor.g.cs");

            var documentVersion              = 0;
            var testVirtualDocument          = new TestVirtualDocumentSnapshot(testVirtualDocUri, 0);
            var csharpVirtualDocument        = new CSharpVirtualDocumentSnapshot(testCSharpDocUri, Mock.Of <ITextSnapshot>(MockBehavior.Strict), 0);
            LSPDocumentSnapshot testDocument = new TestLSPDocumentSnapshot(testDocUri, documentVersion, testVirtualDocument, csharpVirtualDocument);

            var documentManager = new Mock <TrackingLSPDocumentManager>(MockBehavior.Strict);

            documentManager.Setup(manager => manager.TryGetDocument(testDocUri, out testDocument))
            .Returns(true);

            var expectedcSharpResults = new OmniSharp.Extensions.LanguageServer.Protocol.Models.Proposals.SemanticTokens();
            var requestInvoker        = new Mock <LSPRequestInvoker>(MockBehavior.Strict);

            requestInvoker.Setup(invoker => invoker.ReinvokeRequestOnServerAsync <SemanticTokensParams, OmniSharp.Extensions.LanguageServer.Protocol.Models.Proposals.SemanticTokens>(
                                     LanguageServerConstants.LegacyRazorSemanticTokensEndpoint,
                                     LanguageServerKind.CSharp.ToContentType(),
                                     It.IsAny <SemanticTokensParams>(),
                                     It.IsAny <CancellationToken>()
                                     )).Returns(Task.FromResult(expectedcSharpResults));

            var uIContextManager     = new Mock <RazorUIContextManager>(MockBehavior.Strict);
            var disposable           = new Mock <IDisposable>(MockBehavior.Strict);
            var clientOptionsMonitor = new Mock <RazorLSPClientOptionsMonitor>(MockBehavior.Strict);

            var target = new DefaultRazorLanguageServerCustomMessageTarget(
                documentManager.Object, JoinableTaskContext, requestInvoker.Object,
                uIContextManager.Object, disposable.Object, clientOptionsMonitor.Object);
            var request = new SemanticTokensParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = new Uri("C:/path/to%20-%20project/file.razor")
                }
            };
            var expectedResults = new ProvideSemanticTokensResponse(expectedcSharpResults, documentVersion);

            // Act
            var result = await target.ProvideSemanticTokensAsync(request, CancellationToken.None);

            // Assert
            Assert.Equal(expectedResults, result);
        }
        public async Task TrySynchronizeVirtualDocumentAsync_Timeout_ReturnsFalse()
        {
            // Arrange
            var synchronizer = new DefaultLSPDocumentSynchronizer(DocumentManager, JoinableTaskContext);

            synchronizer._synchronizationTimeout = TimeSpan.FromMilliseconds(10);
            var originalVirtualDocument = new TestVirtualDocumentSnapshot(VirtualDocumentUri, 123);
            var originalDocument        = new TestLSPDocumentSnapshot(LSPDocumentUri, 124, originalVirtualDocument);

            // Start synchronize
            var synchronizeTask = synchronizer.TrySynchronizeVirtualDocumentAsync(originalDocument, originalVirtualDocument, CancellationToken.None);

            // Act
            var result = await synchronizeTask.ConfigureAwait(false);

            // Assert
            Assert.False(result);
        }
        public async Task ProvideSemanticTokensAsync_ReturnsSemanticTokensAsync()
        {
            // Arrange
            var testDocUri        = new Uri("C:/path/to/file.razor");
            var testVirtualDocUri = new Uri("C:/path/to/file2.razor.g");
            var testCSharpDocUri  = new Uri("C:/path/to/file.razor.g.cs");

            var testVirtualDocument          = new TestVirtualDocumentSnapshot(testVirtualDocUri, 0);
            var csharpVirtualDocument        = new CSharpVirtualDocumentSnapshot(testCSharpDocUri, Mock.Of <ITextSnapshot>(), 0);
            LSPDocumentSnapshot testDocument = new TestLSPDocumentSnapshot(testDocUri, 0, testVirtualDocument, csharpVirtualDocument);

            var documentManager = new Mock <TrackingLSPDocumentManager>(MockBehavior.Strict);

            documentManager.Setup(manager => manager.TryGetDocument(It.IsAny <Uri>(), out testDocument))
            .Returns(true);

            var expectedResults = new SemanticTokens {
            };
            var requestInvoker  = new Mock <LSPRequestInvoker>(MockBehavior.Strict);

            requestInvoker.Setup(invoker => invoker.ReinvokeRequestOnServerAsync <SemanticTokensParams, SemanticTokens>(
                                     LanguageServerConstants.LegacyRazorSemanticTokensEndpoint,
                                     LanguageServerKind.CSharp.ToContentType(),
                                     It.IsAny <SemanticTokensParams>(),
                                     It.IsAny <CancellationToken>()
                                     )).Returns(Task.FromResult(expectedResults));

            var uIContextManager = new Mock <RazorUIContextManager>(MockBehavior.Strict);

            var target  = new DefaultRazorLanguageServerCustomMessageTarget(documentManager.Object, JoinableTaskContext, requestInvoker.Object, uIContextManager.Object);
            var request = new SemanticTokensParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = testDocUri
                }
            };

            // Act
            var result = await target.ProvideSemanticTokensAsync(request, CancellationToken.None);

            // Assert
            Assert.Equal(expectedResults, result);
        }
示例#12
0
        public async Task RazorServerReadyAsync_SetsUIContext()
        {
            // Arrange
            var testDocUri        = new Uri("C:/path/to/file.razor");
            var testVirtualDocUri = new Uri("C:/path/to/file2.razor.g");
            var testCSharpDocUri  = new Uri("C:/path/to/file.razor.g.cs");

            var testVirtualDocument          = new TestVirtualDocumentSnapshot(testVirtualDocUri, 0);
            var csharpVirtualDocument        = new CSharpVirtualDocumentSnapshot(testCSharpDocUri, Mock.Of <ITextSnapshot>(), 0);
            LSPDocumentSnapshot testDocument = new TestLSPDocumentSnapshot(testDocUri, 0, testVirtualDocument, csharpVirtualDocument);

            var documentManager = new Mock <TrackingLSPDocumentManager>(MockBehavior.Strict);

            documentManager.Setup(manager => manager.TryGetDocument(It.IsAny <Uri>(), out testDocument))
            .Returns(true);

            var expectedResults = new SemanticTokens {
            };
            var requestInvoker  = new Mock <LSPRequestInvoker>(MockBehavior.Strict);

            requestInvoker.Setup(invoker => invoker.ReinvokeRequestOnServerAsync <SemanticTokensParams, SemanticTokens>(
                                     LanguageServerConstants.LegacyRazorSemanticTokensEndpoint,
                                     LanguageServerKind.CSharp.ToContentType(),
                                     It.IsAny <SemanticTokensParams>(),
                                     It.IsAny <CancellationToken>()
                                     )).Returns(Task.FromResult(expectedResults));

            var uIContextManager = new Mock <RazorUIContextManager>(MockBehavior.Strict);

            uIContextManager.Setup(m => m.SetUIContextAsync(RazorLSPConstants.RazorActiveUIContextGuid, true, It.IsAny <CancellationToken>()))
            .Returns(() => Task.CompletedTask)
            .Verifiable();

            var target = new DefaultRazorLanguageServerCustomMessageTarget(documentManager.Object, JoinableTaskContext, requestInvoker.Object, uIContextManager.Object);

            // Act
            await target.RazorServerReadyAsync(CancellationToken.None);

            // Assert
            uIContextManager.Verify();
        }
        public async void ProvideCodeActionsAsync_ReturnsCodeActions()
        {
            // Arrange
            var testDocUri        = new Uri("C:/path/to/file.razor");
            var testVirtualDocUri = new Uri("C:/path/to/file2.razor.g");
            var testCSharpDocUri  = new Uri("C:/path/to/file.razor.g.cs");

            var testVirtualDocument          = new TestVirtualDocumentSnapshot(testVirtualDocUri, 0);
            var csharpVirtualDocument        = new CSharpVirtualDocumentSnapshot(testCSharpDocUri, Mock.Of <ITextSnapshot>(), 0);
            LSPDocumentSnapshot testDocument = new TestLSPDocumentSnapshot(testDocUri, 0, testVirtualDocument, csharpVirtualDocument);

            var documentManager = new Mock <TrackingLSPDocumentManager>();

            documentManager.Setup(manager => manager.TryGetDocument(It.IsAny <Uri>(), out testDocument))
            .Returns(true);

            var expectedResults = new[] { new VSCodeAction() };
            var requestInvoker  = new Mock <LSPRequestInvoker>();

            requestInvoker.Setup(invoker => invoker.ReinvokeRequestOnServerAsync <CodeActionParams, VSCodeAction[]>(
                                     Methods.TextDocumentCodeActionName,
                                     LanguageServerKind.CSharp.ToContentType(),
                                     It.IsAny <CodeActionParams>(),
                                     It.IsAny <CancellationToken>()
                                     )).Returns(Task.FromResult(expectedResults));
            var target  = new DefaultRazorLanguageServerCustomMessageTarget(documentManager.Object, JoinableTaskContext, requestInvoker.Object);
            var request = new CodeActionParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = testDocUri
                }
            };

            // Act
            var result = await target.ProvideCodeActionsAsync(request, CancellationToken.None);

            // Assert
            Assert.Equal(expectedResults, result);
        }
        public async Task TrySynchronizeVirtualDocumentAsync_SynchronizesAfterUpdate_ReturnsTrue()
        {
            // Arrange
            var synchronizer = new DefaultLSPDocumentSynchronizer(DocumentManager, JoinableTaskContext);

            synchronizer._synchronizationTimeout = TimeSpan.FromMilliseconds(500);
            var originalVirtualDocument = new TestVirtualDocumentSnapshot(VirtualDocumentUri, 123);
            var originalDocument        = new TestLSPDocumentSnapshot(LSPDocumentUri, 124, originalVirtualDocument);

            // Start synchronization, this will hang until we invoke a DocumentManager_Changed event because the above virtual document expects host doc version 123 but the host doc is 124
            var synchronizeTask = synchronizer.TrySynchronizeVirtualDocumentAsync(originalDocument, originalVirtualDocument, CancellationToken.None);

            // Create a virtual and host doc that are synchronized (both at version 124).
            var newVirtualDocument = originalVirtualDocument.Fork(124);
            var newDocument        = originalDocument.Fork(124, newVirtualDocument);
            var args = new LSPDocumentChangeEventArgs(originalDocument, newDocument, LSPDocumentChangeKind.VirtualDocumentChanged);

            // Act
            synchronizer.DocumentManager_Changed(DocumentManager, args);
            var result = await synchronizeTask.ConfigureAwait(false);

            // Assert
            Assert.True(result);
        }
        public async Task TrySynchronizeVirtualDocumentAsync_SimultaneousDifferentSynchronizationRequests_CancelsFirst_ReturnsFalseThenTrue()
        {
            // Arrange
            var synchronizer = new DefaultLSPDocumentSynchronizer(DocumentManager, JoinableTaskContext);

            synchronizer._synchronizationTimeout = TimeSpan.FromMilliseconds(500);
            var originalVirtualDocument = new TestVirtualDocumentSnapshot(VirtualDocumentUri, 123);
            var originalDocument        = new TestLSPDocumentSnapshot(LSPDocumentUri, 124, originalVirtualDocument);

            // Start synchronization that will hang because 123 != 124
            var synchronizeTask1 = synchronizer.TrySynchronizeVirtualDocumentAsync(originalDocument, originalVirtualDocument, CancellationToken.None);

            var newVirtualDocument = originalVirtualDocument.Fork(124);
            var newDocument        = originalDocument.Fork(125, newVirtualDocument);

            // Start another synchronization that will also hang because 124 != 125. However, this synchronization request is for the same addressable virtual document (same URI)
            // therefore requesting a second synchronization with a different host doc version expectation will cancel the original synchronization request resulting it returning
            // false.
            var synchronizeTask2 = synchronizer.TrySynchronizeVirtualDocumentAsync(newDocument, newVirtualDocument, CancellationToken.None);

            var finalVirtualDocument = newVirtualDocument.Fork(125);
            var finalDocument        = newDocument.Fork(125, finalVirtualDocument);

            var args = new LSPDocumentChangeEventArgs(newDocument, finalDocument, LSPDocumentChangeKind.VirtualDocumentChanged);


            // Act
            synchronizer.DocumentManager_Changed(DocumentManager, args);
            var result1 = await synchronizeTask1.ConfigureAwait(false);

            var result2 = await synchronizeTask2.ConfigureAwait(false);

            // Assert
            Assert.False(result1);
            Assert.True(result2);
        }
        public async Task ProvideCodeActionsAsync_ReturnsCodeActionsAsync()
        {
            // Arrange
            var testDocUri        = new Uri("C:/path/to/file.razor");
            var testVirtualDocUri = new Uri("C:/path/to/file2.razor.g");
            var testCSharpDocUri  = new Uri("C:/path/to/file.razor.g.cs");

            var testVirtualDocument          = new TestVirtualDocumentSnapshot(testVirtualDocUri, 0);
            var csharpVirtualDocument        = new CSharpVirtualDocumentSnapshot(testCSharpDocUri, TextBuffer.CurrentSnapshot, 0);
            LSPDocumentSnapshot testDocument = new TestLSPDocumentSnapshot(testDocUri, 0, testVirtualDocument, csharpVirtualDocument);

            var documentManager = new Mock <TrackingLSPDocumentManager>(MockBehavior.Strict);

            documentManager.Setup(manager => manager.TryGetDocument(It.IsAny <Uri>(), out testDocument))
            .Returns(true);

            var languageServer1Response = new[] { new VSInternalCodeAction()
                                                  {
                                                      Title = "Response 1"
                                                  } };
            var languageServer2Response = new[] { new VSInternalCodeAction()
                                                  {
                                                      Title = "Response 2"
                                                  } };

            async IAsyncEnumerable <ReinvocationResponse <IReadOnlyList <VSInternalCodeAction> > > GetExpectedResultsAsync()
            {
                yield return(new ReinvocationResponse <IReadOnlyList <VSInternalCodeAction> >("languageClient", languageServer1Response));

                yield return(new ReinvocationResponse <IReadOnlyList <VSInternalCodeAction> >("languageClient", languageServer2Response));

                await Task.CompletedTask;
            }

            var expectedResults = GetExpectedResultsAsync();
            var requestInvoker  = new Mock <LSPRequestInvoker>(MockBehavior.Strict);

            requestInvoker.Setup(invoker => invoker.ReinvokeRequestOnMultipleServersAsync <CodeActionParams, IReadOnlyList <VSInternalCodeAction> >(
                                     TextBuffer,
                                     Methods.TextDocumentCodeActionName,
                                     It.IsAny <Func <JToken, bool> >(),
                                     It.IsAny <CodeActionParams>(),
                                     It.IsAny <CancellationToken>()
                                     )).Returns(expectedResults);

            var uIContextManager     = new Mock <RazorUIContextManager>(MockBehavior.Strict);
            var disposable           = new Mock <IDisposable>(MockBehavior.Strict);
            var documentSynchronizer = new Mock <LSPDocumentSynchronizer>(MockBehavior.Strict);

            var target = new DefaultRazorLanguageServerCustomMessageTarget(
                documentManager.Object, JoinableTaskContext, requestInvoker.Object,
                uIContextManager.Object, disposable.Object, EditorSettingsManager, documentSynchronizer.Object);
            var request = new CodeActionParams()
            {
                TextDocument = new LanguageServer.Protocol.TextDocumentIdentifier()
                {
                    Uri = testDocUri
                }
            };

            // Act
            var result = await target.ProvideCodeActionsAsync(request, CancellationToken.None);

            // Assert
            Assert.Collection(result,
                              r => Assert.Equal(languageServer1Response[0].Title, r.Title),
                              r => Assert.Equal(languageServer2Response[0].Title, r.Title));
        }