Migrate `textDocument/implementation` to new use delegation APIs.

- Updated the tests to reflect the new dependency on document snapshots and their virtual documents.

Part of #5017
This commit is contained in:
N. Taylor Mullen 2021-10-19 12:37:16 -07:00
Родитель c0a89a0e96
Коммит 0c38c4be65
2 изменённых файлов: 35 добавлений и 17 удалений

Просмотреть файл

@ -104,17 +104,25 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor.HtmlCSharp
}
};
var languageServerName = projectionResult.LanguageKind.ToContainedLanguageServerName();
var serverKind = projectionResult.LanguageKind.ToLanguageServerKind();
var languageServerName = serverKind.ToLanguageServerName();
_logger.LogInformation($"Requesting {languageServerName} implementation for {projectionResult.Uri}.");
var textBuffer = serverKind.GetTextBuffer(documentSnapshot);
var response = await _requestInvoker.ReinvokeRequestOnServerAsync<TextDocumentPositionParams, Location[]>(
textBuffer,
Methods.TextDocumentImplementationName,
languageServerName,
textDocumentPositionParams,
cancellationToken).ConfigureAwait(false);
var locations = response.Result;
if (locations is null || locations.Length == 0)
if (!ReinvocationResponseHelper.TryExtractResultOrLog(response, _logger, languageServerName, out var locations))
{
return null;
}
if (locations.Length == 0)
{
_logger.LogInformation("Received no results.");
return locations;

Просмотреть файл

@ -7,6 +7,8 @@ using System.Threading.Tasks;
using Microsoft.VisualStudio.LanguageServer.Client;
using Microsoft.VisualStudio.LanguageServer.ContainedLanguage;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Microsoft.VisualStudio.Test;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Threading;
using Moq;
using Xunit;
@ -19,10 +21,22 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor.HtmlCSharp
public GoToImplementationHandlerTest()
{
Uri = new Uri("C:/path/to/file.razor");
var csharpVirtualDocument = new CSharpVirtualDocumentSnapshot(
new Uri("C:/path/to/file.razor.g.cs"),
new TestTextBuffer(new StringTextSnapshot(string.Empty)).CurrentSnapshot,
hostDocumentSyncVersion: 0);
var htmlVirtualDocument = new HtmlVirtualDocumentSnapshot(
new Uri("C:/path/to/file.razor__virtual.html"),
new TestTextBuffer(new StringTextSnapshot(string.Empty)).CurrentSnapshot,
hostDocumentSyncVersion: 0);
LSPDocumentSnapshot documentSnapshot = new TestLSPDocumentSnapshot(Uri, version: 0, htmlVirtualDocument, csharpVirtualDocument);
DocumentManager = new TestDocumentManager();
DocumentManager.AddDocument(Uri, documentSnapshot);
}
private Uri Uri { get; }
private readonly ILanguageClient _languageClient = Mock.Of<ILanguageClient>(MockBehavior.Strict);
private TestDocumentManager DocumentManager { get; }
[Fact]
public async Task HandleRequestAsync_DocumentNotFound_ReturnsNull()
@ -50,14 +64,12 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor.HtmlCSharp
public async Task HandleRequestAsync_ProjectionNotFound_ReturnsNull()
{
// Arrange
var documentManager = new TestDocumentManager();
documentManager.AddDocument(Uri, Mock.Of<LSPDocumentSnapshot>(MockBehavior.Strict));
var requestInvoker = Mock.Of<LSPRequestInvoker>(MockBehavior.Strict);
var projectionProvider = new Mock<LSPProjectionProvider>(MockBehavior.Strict).Object;
Mock.Get(projectionProvider).Setup(projectionProvider => projectionProvider.GetProjectionAsync(It.IsAny<LSPDocumentSnapshot>(), It.IsAny<Position>(), CancellationToken.None))
.Returns(Task.FromResult<ProjectionResult>(null));
var documentMappingProvider = Mock.Of<LSPDocumentMappingProvider>(MockBehavior.Strict);
var implementationHandler = new GoToImplementationHandler(requestInvoker, documentManager, projectionProvider, documentMappingProvider, LoggerProvider);
var implementationHandler = new GoToImplementationHandler(requestInvoker, DocumentManager, projectionProvider, documentMappingProvider, LoggerProvider);
var implementationRequest = new TextDocumentPositionParams()
{
TextDocument = new TextDocumentIdentifier() { Uri = Uri },
@ -78,25 +90,24 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor.HtmlCSharp
var invokedLSPRequest = false;
var invokedRemapRequest = false;
var expectedLocation = GetLocation(5, 5, 5, 5, Uri);
var documentManager = new TestDocumentManager();
documentManager.AddDocument(Uri, Mock.Of<LSPDocumentSnapshot>(MockBehavior.Strict));
var virtualHtmlUri = new Uri("C:/path/to/file.razor__virtual.html");
var htmlLocation = GetLocation(100, 100, 100, 100, virtualHtmlUri);
var requestInvoker = new Mock<LSPRequestInvoker>(MockBehavior.Strict);
requestInvoker
.Setup(r => r.ReinvokeRequestOnServerAsync<TextDocumentPositionParams, Location[]>(
It.IsAny<ITextBuffer>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<TextDocumentPositionParams>(),
It.IsAny<CancellationToken>()))
.Callback<string, string, TextDocumentPositionParams, CancellationToken>((method, clientName, implementationParams, ct) =>
.Callback<ITextBuffer, string, string, TextDocumentPositionParams, CancellationToken>((textBuffer, method, clientName, implementationParams, ct) =>
{
Assert.Equal(Methods.TextDocumentImplementationName, method);
Assert.Equal(RazorLSPConstants.HtmlLanguageServerName, clientName);
invokedLSPRequest = true;
})
.Returns(Task.FromResult(new ReinvokeResponse<Location[]>(_languageClient, new[] { htmlLocation })));
.Returns(Task.FromResult(new ReinvocationResponse<Location[]>("LanguageClientName", new[] { htmlLocation })));
var projectionResult = new ProjectionResult()
{
@ -115,7 +126,7 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor.HtmlCSharp
})
.Returns(Task.FromResult(Array.Empty<Location>()));
var implementationHandler = new GoToImplementationHandler(requestInvoker.Object, documentManager, projectionProvider.Object, documentMappingProvider.Object, LoggerProvider);
var implementationHandler = new GoToImplementationHandler(requestInvoker.Object, DocumentManager, projectionProvider.Object, documentMappingProvider.Object, LoggerProvider);
var implementationRequest = new TextDocumentPositionParams()
{
TextDocument = new TextDocumentIdentifier() { Uri = Uri },
@ -139,25 +150,24 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor.HtmlCSharp
var invokedLSPRequest = false;
var invokedRemapRequest = false;
var expectedLocation = GetLocation(5, 5, 5, 5, Uri);
var documentManager = new TestDocumentManager();
documentManager.AddDocument(Uri, Mock.Of<LSPDocumentSnapshot>(MockBehavior.Strict));
var virtualCSharpUri = new Uri("C:/path/to/file.razor.g.cs");
var csharpLocation = GetLocation(100, 100, 100, 100, virtualCSharpUri);
var requestInvoker = new Mock<LSPRequestInvoker>(MockBehavior.Strict);
requestInvoker
.Setup(r => r.ReinvokeRequestOnServerAsync<TextDocumentPositionParams, Location[]>(
It.IsAny<ITextBuffer>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<TextDocumentPositionParams>(),
It.IsAny<CancellationToken>()))
.Callback<string, string, TextDocumentPositionParams, CancellationToken>((method, clientName, implementationParams, ct) =>
.Callback<ITextBuffer, string, string, TextDocumentPositionParams, CancellationToken>((textBuffer, method, clientName, implementationParams, ct) =>
{
Assert.Equal(Methods.TextDocumentImplementationName, method);
Assert.Equal(RazorLSPConstants.RazorCSharpLanguageServerName, clientName);
invokedLSPRequest = true;
})
.Returns(Task.FromResult(new ReinvokeResponse<Location[]>(_languageClient, new[] { csharpLocation })));
.Returns(Task.FromResult(new ReinvocationResponse<Location[]>("LanguageClientName", new[] { csharpLocation })));
var projectionResult = new ProjectionResult()
{
@ -176,7 +186,7 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor.HtmlCSharp
})
.Returns(Task.FromResult(Array.Empty<Location>()));
var implementationHandler = new GoToImplementationHandler(requestInvoker.Object, documentManager, projectionProvider.Object, documentMappingProvider.Object, LoggerProvider);
var implementationHandler = new GoToImplementationHandler(requestInvoker.Object, DocumentManager, projectionProvider.Object, documentMappingProvider.Object, LoggerProvider);
var implementationRequest = new TextDocumentPositionParams()
{
TextDocument = new TextDocumentIdentifier() { Uri = Uri },