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

Razor EA changes to allow better tests in Razor #74402

Merged
merged 3 commits into from
Jul 16, 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
5 changes: 0 additions & 5 deletions src/LanguageServer/Protocol/WellKnownLspServerKinds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ namespace Microsoft.CodeAnalysis.LanguageServer;

internal enum WellKnownLspServerKinds
{
/// <summary>
/// Razor LSP server for Razor document requests (.razor and .cshtml files)
/// </summary>
RazorCohostServer,

/// <summary>
/// Roslyn LSP server for razor c# requests.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ internal abstract class AbstractRazorCohostRequestHandler<TRequestType, TRespons

Task<TResponseType> IRequestHandler<TRequestType, TResponseType, RequestContext>.HandleRequestAsync(TRequestType request, RequestContext context, CancellationToken cancellationToken)
{
// We have to wrap the RequestContext in order to expose it to Roslyn. We could create our own (by exporting
// and IRequestContextFactory) but that would not be possible if/when we live in the same server as Roslyn
// so may as well deal with it now.
// This does mean we can't nicely pass through the original Uri, which would have ProjectContext info, but
// we get the Project so that will have to do.

var razorRequestContext = new RazorCohostRequestContext(context);
return HandleRequestAsync(request, razorRequestContext, cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,29 @@

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor
{
[Export(typeof(AbstractRazorLanguageServerFactoryWrapper))]
[Shared]
internal class RazorLanguageServerFactoryWrapper : AbstractRazorLanguageServerFactoryWrapper
[Export(typeof(RazorTestLanguageServerFactory))]
[Export(typeof(AbstractRazorLanguageServerFactoryWrapper))]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The abstract class is only used for this export, and was probably just copied from Razor where we used to use abstract classes a lot. I'm exporting it as itself now, so that we can remove the abstract class in future, but we have to wait until these changes make it to VS Preview images for that to happen, or the Roslyn integration tests will fail.

[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
[method: ImportingConstructor]
internal class RazorTestLanguageServerFactory(ILanguageServerFactory languageServerFactory) : AbstractRazorLanguageServerFactoryWrapper
{
private readonly ILanguageServerFactory _languageServerFactory;
private readonly ILanguageServerFactory _languageServerFactory = languageServerFactory;

[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
[ImportingConstructor]
public RazorLanguageServerFactoryWrapper(ILanguageServerFactory languageServerFactory)
internal override IRazorLanguageServerTarget CreateLanguageServer(JsonRpc jsonRpc, JsonSerializerOptions options, IRazorTestCapabilitiesProvider razorCapabilitiesProvider, HostServices hostServices)
{
if (languageServerFactory is null)
{
throw new ArgumentNullException(nameof(languageServerFactory));
}
return CreateLanguageServerCore(jsonRpc, options, razorCapabilitiesProvider, hostServices, WellKnownLspServerKinds.RazorLspServer);
}

_languageServerFactory = languageServerFactory;
internal IRazorLanguageServerTarget CreateAlwaysActiveVSLanguageServer(JsonRpc jsonRpc, JsonSerializerOptions options, IRazorTestCapabilitiesProvider razorCapabilitiesProvider, HostServices hostServices)
{
return CreateLanguageServerCore(jsonRpc, options, razorCapabilitiesProvider, hostServices, WellKnownLspServerKinds.AlwaysActiveVSLspServer);
}

internal override IRazorLanguageServerTarget CreateLanguageServer(JsonRpc jsonRpc, JsonSerializerOptions options, IRazorTestCapabilitiesProvider razorCapabilitiesProvider, HostServices hostServices)
private IRazorLanguageServerTarget CreateLanguageServerCore(JsonRpc jsonRpc, JsonSerializerOptions options, IRazorTestCapabilitiesProvider razorCapabilitiesProvider, HostServices hostServices, WellKnownLspServerKinds serverKind)
{
var capabilitiesProvider = new RazorCapabilitiesProvider(razorCapabilitiesProvider, options);
var languageServer = _languageServerFactory.Create(jsonRpc, options, capabilitiesProvider, WellKnownLspServerKinds.RazorLspServer, NoOpLspLogger.Instance, hostServices);
var languageServer = _languageServerFactory.Create(jsonRpc, options, capabilitiesProvider, serverKind, NoOpLspLogger.Instance, hostServices);

return new RazorLanguageServerTargetWrapper(languageServer);
}
Expand Down
43 changes: 43 additions & 0 deletions src/Tools/ExternalAccess/Razor/Testing/TestSolutionStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor;

internal sealed class TestSolutionStore
{
private readonly Dictionary<Checksum, Solution> _solutions = [];

internal async Task<RazorPinnedSolutionInfoWrapper> AddAsync(Solution solution, CancellationToken cancellationToken)
{
// Using compilation state, since that is what is used in the real SolutionAssetStorage class
// Compilation state is the SolutionState checksum, plus source generator info, which seems pretty relevant :)
var checksum = await solution.CompilationState.GetChecksumAsync(cancellationToken).ConfigureAwait(false);

lock (_solutions)
{
if (_solutions.TryGetValue(checksum, out var existingSolution))
{
return checksum;
}

_solutions.Add(checksum, solution);
}

return checksum;
}

internal Solution? Get(RazorPinnedSolutionInfoWrapper solutionInfo)
{
lock (_solutions)
{
_solutions.TryGetValue(solutionInfo.UnderlyingObject, out var solution);

return solution;
}
}
}
Loading