Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remap Html uris in cohost Go To Def #10826

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost;
using Microsoft.CodeAnalysis.Razor.Remote;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.VisualStudio.LanguageServer.ContainedLanguage;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using static Roslyn.LanguageServer.Protocol.RoslynLspExtensions;
Expand All @@ -30,12 +31,14 @@ namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;
internal sealed class CohostGoToDefinitionEndpoint(
IRemoteServiceInvoker remoteServiceInvoker,
IHtmlDocumentSynchronizer htmlDocumentSynchronizer,
LSPRequestInvoker requestInvoker)
LSPRequestInvoker requestInvoker,
IFilePathService filePathService)
: AbstractRazorCohostDocumentRequestHandler<TextDocumentPositionParams, SumType<RoslynLocation, RoslynLocation[], RoslynDocumentLink[]>?>, IDynamicRegistrationProvider
{
private readonly IRemoteServiceInvoker _remoteServiceInvoker = remoteServiceInvoker;
private readonly IHtmlDocumentSynchronizer _htmlDocumentSynchronizer = htmlDocumentSynchronizer;
private readonly LSPRequestInvoker _requestInvoker = requestInvoker;
private readonly IFilePathService _filePathService = filePathService;

protected override bool MutatesSolutionState => false;

Expand Down Expand Up @@ -115,11 +118,11 @@ internal sealed class CohostGoToDefinitionEndpoint(

if (response.TryGetFirst(out var singleLocation))
{
return RoslynLspFactory.CreateLocation(singleLocation.Uri, singleLocation.Range.ToLinePositionSpan());
return RoslynLspFactory.CreateLocation(RemapVirtualHtmlUri(singleLocation.Uri), singleLocation.Range.ToLinePositionSpan());
}
else if (response.TryGetSecond(out var multipleLocations))
{
return Array.ConvertAll(multipleLocations, static l => RoslynLspFactory.CreateLocation(l.Uri, l.Range.ToLinePositionSpan()));
return Array.ConvertAll(multipleLocations, l => RoslynLspFactory.CreateLocation(RemapVirtualHtmlUri(l.Uri), l.Range.ToLinePositionSpan()));
}
else if (response.TryGetThird(out var documentLinks))
{
Expand All @@ -129,7 +132,7 @@ internal sealed class CohostGoToDefinitionEndpoint(
{
if (documentLink.Target is Uri target)
{
builder.Add(RoslynLspFactory.CreateDocumentLink(target, documentLink.Range.ToLinePositionSpan()));
builder.Add(RoslynLspFactory.CreateDocumentLink(RemapVirtualHtmlUri(target), documentLink.Range.ToLinePositionSpan()));
}
}

Expand All @@ -139,6 +142,17 @@ internal sealed class CohostGoToDefinitionEndpoint(
return null;
}

private Uri RemapVirtualHtmlUri(Uri uri)
{
if (_filePathService.IsVirtualHtmlFile(uri))
{
return _filePathService.GetRazorDocumentUri(uri);
}

return uri;
}


internal TestAccessor GetTestAccessor() => new(this);

internal readonly struct TestAccessor(CohostGoToDefinitionEndpoint instance)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Test.Common;
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
using Microsoft.CodeAnalysis.Remote.Razor;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Xunit;
using Xunit.Abstractions;
using RoslynDocumentLink = Roslyn.LanguageServer.Protocol.DocumentLink;
using RoslynLocation = Roslyn.LanguageServer.Protocol.Location;
using RoslynLspExtensions = Roslyn.LanguageServer.Protocol.RoslynLspExtensions;
using TextDocument = Microsoft.CodeAnalysis.TextDocument;

namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;

Expand Down Expand Up @@ -295,26 +298,74 @@ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.
Assert.Equal(range, location.Range);
}

[Fact]
public async Task Html()
{
// This really just validates Uri remapping, the actual response is largely arbitrary

TestCode input = """
<div></div>

<script>
function [|foo|]() {
f$$oo();
}
</script>
""";

var document = CreateProjectAndRazorDocument(input.Text);
var inputText = await document.GetTextAsync(DisposalToken);

var htmlResponse = new SumType<Location, Location[], DocumentLink[]>?(new Location[]
{
new Location
{
Uri = new Uri(document.CreateUri(), document.Name + FeatureOptions.HtmlVirtualDocumentSuffix),
Range = inputText.GetRange(input.Span),
},
});

await VerifyGoToDefinitionAsync(input, htmlResponse: htmlResponse);
}

private static string FileName(string projectRelativeFileName)
=> Path.Combine(TestProjectData.SomeProjectPath, projectRelativeFileName);

private async Task VerifyGoToDefinitionAsync(TestCode input, string? fileKind = null, params (string fileName, string contents)[]? additionalFiles)
private async Task VerifyGoToDefinitionAsync(TestCode input, string? fileKind = null, SumType<Location, Location[], DocumentLink[]>? htmlResponse = null)
{
var result = await GetGoToDefinitionResultAsync(input, fileKind, additionalFiles);
var document = CreateProjectAndRazorDocument(input.Text, fileKind);
var result = await GetGoToDefinitionResultAsync(document, input, htmlResponse);

Assumes.NotNull(result);

Assert.NotNull(result.Value.Second);
var locations = result.Value.Second;
var location = Assert.Single(locations);

var text = SourceText.From(input.Text);
var range = RoslynLspExtensions.GetRange(text, input.Span);
Assert.Equal(range, location.Range);

Assert.Equal(document.CreateUri(), location.Uri);
}

private async Task<SumType<RoslynLocation, RoslynLocation[], RoslynDocumentLink[]>?> GetGoToDefinitionResultAsync(
private Task<SumType<RoslynLocation, RoslynLocation[], RoslynDocumentLink[]>?> GetGoToDefinitionResultAsync(
TestCode input, string? fileKind = null, params (string fileName, string contents)[]? additionalFiles)
{
var document = CreateProjectAndRazorDocument(input.Text, fileKind, additionalFiles);
return GetGoToDefinitionResultAsync(document, input, htmlResponse: null);
}

private async Task<SumType<RoslynLocation, RoslynLocation[], RoslynDocumentLink[]>?> GetGoToDefinitionResultAsync(
davidwengier marked this conversation as resolved.
Show resolved Hide resolved
TextDocument document, TestCode input, SumType<Location, Location[], DocumentLink[]>? htmlResponse)
{
var inputText = await document.GetTextAsync(DisposalToken);
var position = inputText.GetPosition(input.Position);

var requestInvoker = new TestLSPRequestInvoker([(Methods.TextDocumentDefinitionName, null)]);
var requestInvoker = new TestLSPRequestInvoker([(Methods.TextDocumentDefinitionName, htmlResponse)]);

var endpoint = new CohostGoToDefinitionEndpoint(RemoteServiceInvoker, TestHtmlDocumentSynchronizer.Instance, requestInvoker);
var filePathService = new VisualStudioFilePathService(FeatureOptions);
var endpoint = new CohostGoToDefinitionEndpoint(RemoteServiceInvoker, TestHtmlDocumentSynchronizer.Instance, requestInvoker, filePathService);

var textDocumentPositionParams = new TextDocumentPositionParams
{
Expand Down