Skip to content

Commit

Permalink
Merge branch 'main' into GenerationFromTextDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwengier authored Jan 22, 2024
2 parents 2efe53c + 4e331a0 commit d94b8f6
Show file tree
Hide file tree
Showing 147 changed files with 1,766 additions and 2,443 deletions.
8 changes: 4 additions & 4 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<Uri>https://dev.azure.com/dnceng/internal/_git/dotnet-runtime</Uri>
<Sha>839cdfb0ecca5e0be3dbccd926e7651ef50fdf10</Sha>
</Dependency>
<Dependency Name="Microsoft.SourceBuild.Intermediate.source-build-reference-packages" Version="9.0.0-alpha.1.24059.3">
<Dependency Name="Microsoft.SourceBuild.Intermediate.source-build-reference-packages" Version="9.0.0-alpha.1.24067.1">
<Uri>https://github.com/dotnet/source-build-reference-packages</Uri>
<Sha>f2c3fed62861b918dfe300f01b497551813a56df</Sha>
<Sha>549aadff1660b230bdfffa562eea3edf59dd0bb4</Sha>
<SourceBuild RepoName="source-build-reference-packages" ManagedOnly="true" />
</Dependency>
<Dependency Name="Microsoft.Net.Compilers.Toolset" Version="4.10.0-1.24069.13">
Expand Down Expand Up @@ -90,9 +90,9 @@
<Uri>https://dev.azure.com/dnceng/internal/_git/dotnet-runtime</Uri>
<Sha>3a25a7f1cc446b60678ed25c9d829420d6321eba</Sha>
</Dependency>
<Dependency Name="Microsoft.DotNet.Arcade.Sdk" Version="8.0.0-beta.24059.4">
<Dependency Name="Microsoft.DotNet.Arcade.Sdk" Version="8.0.0-beta.24060.4">
<Uri>https://github.com/dotnet/arcade</Uri>
<Sha>61ae141d2bf3534619265c8f691fd55dc3e75147</Sha>
<Sha>888985fb9a9ae4cb30bca75f98af9126c839e660</Sha>
<SourceBuild RepoName="arcade" ManagedOnly="true" />
</Dependency>
<Dependency Name="Microsoft.DotNet.XliffTasks" Version="1.0.0-beta.23475.1" CoherentParentDependency="Microsoft.DotNet.Arcade.Sdk">
Expand Down
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<PropertyGroup Label="Automated">
<MicrosoftNETCoreBrowserDebugHostTransportPackageVersion>6.0.2-servicing.22064.6</MicrosoftNETCoreBrowserDebugHostTransportPackageVersion>
<MicrosoftNETCorePlatformsPackageVersion>6.0.1</MicrosoftNETCorePlatformsPackageVersion>
<MicrosoftSourceBuildIntermediatesourcebuildreferencepackagesPackageVersion>9.0.0-alpha.1.24059.3</MicrosoftSourceBuildIntermediatesourcebuildreferencepackagesPackageVersion>
<MicrosoftSourceBuildIntermediatesourcebuildreferencepackagesPackageVersion>9.0.0-alpha.1.24067.1</MicrosoftSourceBuildIntermediatesourcebuildreferencepackagesPackageVersion>
<MicrosoftNetCompilersToolsetPackageVersion>4.10.0-1.24069.13</MicrosoftNetCompilersToolsetPackageVersion>
<MicrosoftCommonLanguageServerProtocolFrameworkPackageVersion>4.10.0-1.24069.13</MicrosoftCommonLanguageServerProtocolFrameworkPackageVersion>
<MicrosoftCodeAnalysisExternalAccessRazorPackageVersion>4.10.0-1.24069.13</MicrosoftCodeAnalysisExternalAccessRazorPackageVersion>
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"rollForward": "latestPatch"
},
"msbuild-sdks": {
"Microsoft.DotNet.Arcade.Sdk": "8.0.0-beta.24059.4"
"Microsoft.DotNet.Arcade.Sdk": "8.0.0-beta.24060.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,35 @@
using System;
using Microsoft.AspNetCore.Mvc.Razor.Extensions;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis.Razor;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.AspNetCore.Razor.ProjectEngineHost;

namespace Microsoft.AspNetCore.Razor.Microbenchmarks;

public abstract partial class ProjectSnapshotManagerBenchmarkBase
{
private class StaticProjectSnapshotProjectEngineFactory : ProjectSnapshotProjectEngineFactory
private class StaticProjectEngineFactoryProvider : IProjectEngineFactoryProvider
{
public override IProjectEngineFactory FindFactory(IProjectSnapshot project)
=> throw new NotImplementedException();
public static readonly StaticProjectEngineFactoryProvider Instance = new();

public override IProjectEngineFactory FindSerializableFactory(IProjectSnapshot project)
=> throw new NotImplementedException();
private StaticProjectEngineFactoryProvider()
{
}

public override RazorProjectEngine Create(
RazorConfiguration configuration,
RazorProjectFileSystem fileSystem,
Action<RazorProjectEngineBuilder> configure)
=> RazorProjectEngine.Create(configuration, fileSystem, RazorExtensions.Register);
public IProjectEngineFactory GetFactory(RazorConfiguration configuration)
=> Factory.Instance;

private sealed class Factory : IProjectEngineFactory
{
public static readonly Factory Instance = new();

private Factory()
{
}

public string ConfigurationName => "Static";

public RazorProjectEngine Create(RazorConfiguration configuration, RazorProjectFileSystem fileSystem, Action<RazorProjectEngineBuilder>? configure)
=> RazorProjectEngine.Create(configuration, fileSystem, RazorExtensions.Register);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,15 @@ protected ProjectSnapshotManagerBenchmarkBase(int documentCount = 100)
internal DefaultProjectSnapshotManager CreateProjectSnapshotManager()
{
var services = TestServices.Create(
new IWorkspaceService[]
{
TagHelperResolver,
new StaticProjectSnapshotProjectEngineFactory(),
},
[TagHelperResolver],
Array.Empty<ILanguageService>());

return new DefaultProjectSnapshotManager(
new TestErrorReporter(),
Array.Empty<IProjectSnapshotChangeTrigger>(),
#pragma warning disable CA2000 // Dispose objects before losing scope
new AdhocWorkspace(services),
StaticProjectEngineFactoryProvider.Instance,
new TestProjectSnapshotManagerDispatcher());
#pragma warning restore CA2000 // Dispose objects before losing scope
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,13 @@ namespace Microsoft.AspNetCore.Razor.ExternalAccess.RoslynWorkspace;

internal static class RazorProjectInfoSerializer
{
private static readonly EmptyProjectEngineFactory s_fallbackProjectEngineFactory;
private static readonly StringComparison s_stringComparison;
private static readonly (IProjectEngineFactory Value, ICustomProjectEngineFactoryMetadata)[] s_projectEngineFactories;

static RazorProjectInfoSerializer()
{
s_fallbackProjectEngineFactory = new EmptyProjectEngineFactory();
s_stringComparison = RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
? StringComparison.Ordinal
: StringComparison.OrdinalIgnoreCase;

s_projectEngineFactories = ProjectEngineFactories.Factories.Select(f => (f.Item1.Value, f.Item2)).ToArray();
}

public static async Task SerializeAsync(Project project, string configurationFileName, CancellationToken cancellationToken)
Expand Down Expand Up @@ -75,12 +70,12 @@ public static async Task SerializeAsync(Project project, string configurationFil
builder.SetSupportLocalizedComponentNames(); // ProjectState in MS.CA.Razor.Workspaces does this, so I'm doing it too!
};

var engine = DefaultProjectEngineFactory.Create(
var engineFactory = ProjectEngineFactories.DefaultProvider.GetFactory(configuration);

var engine = engineFactory.Create(
configuration,
fileSystem: fileSystem,
configure: defaultConfigure,
fallback: s_fallbackProjectEngineFactory,
factories: s_projectEngineFactories);
fileSystem,
configure: defaultConfigure);

var resolver = new CompilationTagHelperResolver(NoOpTelemetryReporter.Instance);
var tagHelpers = await resolver.GetTagHelpersAsync(project, engine, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.AspNetCore.Razor.LanguageServer.Common;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Host;

namespace Microsoft.AspNetCore.Razor.LanguageServer;

internal abstract class AdhocWorkspaceFactory
internal sealed class AdhocWorkspaceFactory(HostServicesProvider hostServicesProvider) : IAdhocWorkspaceFactory
{
public abstract AdhocWorkspace Create();
public AdhocWorkspace Create(params IWorkspaceService[] workspaceServices)
{
workspaceServices ??= [];

public abstract AdhocWorkspace Create(IEnumerable<IWorkspaceService> workspaceServices);
var fallbackServices = hostServicesProvider.GetServices();
var services = AdhocServices.Create(
workspaceServices: workspaceServices.ToImmutableArray(),
languageServices: ImmutableArray<ILanguageService>.Empty,
fallbackServices);

return new AdhocWorkspace(services);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void ApplyCapabilities(VSInternalServerCapabilities serverCapabilities, V
var uri = request.TextDocument.Uri;
var position = request.Position;

var workspaceFactory = requestContext.GetRequiredService<AdhocWorkspaceFactory>();
var workspaceFactory = requestContext.GetRequiredService<IAdhocWorkspaceFactory>();
using (var formattingContext = FormattingContext.Create(uri, documentContext.Snapshot, codeDocument, request.Options, workspaceFactory))
{
for (var i = 0; i < applicableProviders.Count; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,12 @@ public bool TryGetGeneratedOutput([NotNullWhen(true)] out RazorCodeDocument? res
result = _codeDocument;
return result is not null;
}

public IDocumentSnapshot WithText(SourceText text)
{
var id = _textDocument.Id;
var newDocument = _textDocument.Project.Solution.WithAdditionalDocumentText(id, text).GetAdditionalDocument(id).AssumeNotNull();

return new CohostDocumentSnapshot(newDocument, _projectSnapshot);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Cohost;

internal class CohostProjectSnapshot : IProjectSnapshot
{
private static readonly EmptyProjectEngineFactory s_fallbackProjectEngineFactory = new();
private static readonly (IProjectEngineFactory Value, ICustomProjectEngineFactoryMetadata)[] s_projectEngineFactories = ProjectEngineFactories.Factories.Select(f => (f.Item1.Value, f.Item2)).ToArray();

private readonly Project _project;
private readonly DocumentSnapshotFactory _documentSnapshotFactory;
private readonly ITelemetryReporter _telemetryReporter;
Expand All @@ -44,17 +41,18 @@ public CohostProjectSnapshot(Project project, DocumentSnapshotFactory documentSn
_projectKey = ProjectKey.From(_project).AssumeNotNull();

_lazyConfiguration = new Lazy<RazorConfiguration>(CreateRazorConfiguration);
_lazyProjectEngine = new Lazy<RazorProjectEngine>(() => DefaultProjectEngineFactory.Create(
Configuration,
fileSystem: RazorProjectFileSystem.Create(Path.GetDirectoryName(FilePath)),
configure: builder =>
{
builder.SetRootNamespace(RootNamespace);
builder.SetCSharpLanguageVersion(CSharpLanguageVersion);
builder.SetSupportLocalizedComponentNames();
},
fallback: s_fallbackProjectEngineFactory,
factories: s_projectEngineFactories));
_lazyProjectEngine = new Lazy<RazorProjectEngine>(() =>
{
return ProjectEngineFactories.DefaultProvider.Create(
Configuration,
rootDirectoryPath: Path.GetDirectoryName(FilePath).AssumeNotNull(),
configure: builder =>
{
builder.SetRootNamespace(RootNamespace);
builder.SetCSharpLanguageVersion(CSharpLanguageVersion);
builder.SetSupportLocalizedComponentNames();
});
});

_tagHelpersLazy = new AsyncLazy<ImmutableArray<TagHelperDescriptor>>(() =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,31 @@
// Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Razor;

namespace Microsoft.AspNetCore.Razor.LanguageServer.Common;

internal class AdhocLanguageServices : HostLanguageServices
internal sealed class AdhocLanguageServices(
AdhocWorkspaceServices workspaceServices,
ImmutableArray<ILanguageService> languageServices)
: HostLanguageServices
{
private readonly HostWorkspaceServices _workspaceServices;
private readonly IEnumerable<ILanguageService> _languageServices;

public AdhocLanguageServices(HostWorkspaceServices workspaceServices, IEnumerable<ILanguageService> languageServices)
{
if (workspaceServices is null)
{
throw new ArgumentNullException(nameof(workspaceServices));
}

if (languageServices is null)
{
throw new ArgumentNullException(nameof(languageServices));
}

_workspaceServices = workspaceServices;
_languageServices = languageServices;
}

public override HostWorkspaceServices WorkspaceServices => _workspaceServices;

public override string Language => RazorLanguage.Name;

public override HostWorkspaceServices WorkspaceServices => workspaceServices;

public override TLanguageService GetService<TLanguageService>()
{
var service = _languageServices.OfType<TLanguageService>().FirstOrDefault();

if (service is null)
foreach (var service in languageServices)
{
throw new InvalidOperationException(SR.FormatLanguage_Services_Missing_Service(typeof(TLanguageService).FullName));
if (service is TLanguageService languageService)
{
return languageService;
}
}

return service;
throw new InvalidOperationException(SR.FormatLanguage_Services_Missing_Service(typeof(TLanguageService).FullName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,35 @@
// Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Host;

namespace Microsoft.AspNetCore.Razor.LanguageServer.Common;

internal class AdhocServices : HostServices
internal sealed class AdhocServices : HostServices
{
private readonly IEnumerable<IWorkspaceService> _workspaceServices;
private readonly IEnumerable<ILanguageService> _razorLanguageServices;
private readonly ImmutableArray<IWorkspaceService> _workspaceServices;
private readonly ImmutableArray<ILanguageService> _languageServices;
private readonly HostServices _fallbackHostServices;
private readonly MethodInfo _createWorkspaceServicesMethod;

private AdhocServices(
IEnumerable<IWorkspaceService> workspaceServices,
IEnumerable<ILanguageService> razorLanguageServices,
ImmutableArray<IWorkspaceService> workspaceServices,
ImmutableArray<ILanguageService> languageServices,
HostServices fallbackHostServices)
{
if (workspaceServices is null)
{
throw new ArgumentNullException(nameof(workspaceServices));
}

if (razorLanguageServices is null)
{
throw new ArgumentNullException(nameof(razorLanguageServices));
}

if (fallbackHostServices is null)
{
throw new ArgumentNullException(nameof(fallbackHostServices));
}

_workspaceServices = workspaceServices;
_razorLanguageServices = razorLanguageServices;
_languageServices = languageServices;
_fallbackHostServices = fallbackHostServices;

// We need to create workspace services from the provided fallback host services. To do that we need to invoke into Roslyn's
// CreateWorkspaceServices method. Ultimately the reason behind this is to ensure that any services created by this class are
// truly isolated from the passed in fallback services host workspace.
var createWorkspaceServicesMethod = typeof(HostServices).GetMethod(nameof(CreateWorkspaceServices), BindingFlags.Instance | BindingFlags.NonPublic);
Assumes.NotNull(createWorkspaceServicesMethod);

_createWorkspaceServicesMethod = createWorkspaceServicesMethod;
_createWorkspaceServicesMethod = typeof(HostServices)
.GetMethod(nameof(CreateWorkspaceServices), BindingFlags.Instance | BindingFlags.NonPublic)
.AssumeNotNull();
}

protected override HostWorkspaceServices CreateWorkspaceServices(Workspace workspace)
Expand All @@ -57,20 +41,19 @@ protected override HostWorkspaceServices CreateWorkspaceServices(Workspace works
}

var fallbackServices = CreateFallbackWorkspaceServices(workspace);
return new AdhocWorkspaceServices(this, _workspaceServices, _razorLanguageServices, workspace, fallbackServices);
return new AdhocWorkspaceServices(this, _workspaceServices, _languageServices, workspace, fallbackServices);
}

public static HostServices Create(
IEnumerable<IWorkspaceService> workspaceServices,
IEnumerable<ILanguageService> razorLanguageServices,
ImmutableArray<IWorkspaceService> workspaceServices,
ImmutableArray<ILanguageService> languageServices,
HostServices fallbackServices)
=> new AdhocServices(workspaceServices, razorLanguageServices, fallbackServices);
=> new AdhocServices(workspaceServices, languageServices, fallbackServices);

private HostWorkspaceServices CreateFallbackWorkspaceServices(Workspace workspace)
{
var result = _createWorkspaceServicesMethod.Invoke(_fallbackHostServices, new[] { workspace }) as HostWorkspaceServices;
Assumes.NotNull(result);

return result;
return result.AssumeNotNull();
}
}
Loading

0 comments on commit d94b8f6

Please sign in to comment.