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

RazorProjectService cleanup #10989

Merged
merged 5 commits into from
Oct 9, 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 @@ -18,31 +18,4 @@ internal interface IRazorProjectService
Task UpdateDocumentAsync(string filePath, SourceText sourceText, CancellationToken cancellationToken);
Task CloseDocumentAsync(string filePath, CancellationToken cancellationToken);
Task RemoveDocumentAsync(string filePath, CancellationToken cancellationToken);

Task<ProjectKey> AddProjectAsync(
string filePath,
string intermediateOutputPath,
RazorConfiguration? configuration,
string? rootNamespace,
string? displayName,
CancellationToken cancellationToken);

Task UpdateProjectAsync(
ProjectKey projectKey,
RazorConfiguration? configuration,
string? rootNamespace,
string displayName,
ProjectWorkspaceState projectWorkspaceState,
ImmutableArray<DocumentSnapshotHandle> documents,
CancellationToken cancellationToken);

Task AddOrUpdateProjectAsync(
ProjectKey projectKey,
string filePath,
RazorConfiguration? configuration,
string? rootNamespace,
string displayName,
ProjectWorkspaceState projectWorkspaceState,
ImmutableArray<DocumentSnapshotHandle> documents,
CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// 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.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.ProjectSystem;
using Microsoft.AspNetCore.Razor.Serialization;

namespace Microsoft.AspNetCore.Razor.LanguageServer.ProjectSystem;

Expand All @@ -13,5 +18,24 @@ internal readonly struct TestAccessor(RazorProjectService instance)
{
public ValueTask WaitForInitializationAsync()
=> instance.WaitForInitializationAsync();

public async Task<ProjectKey> AddProjectAsync(
string filePath,
string intermediateOutputPath,
RazorConfiguration? configuration,
string? rootNamespace,
string? displayName,
CancellationToken cancellationToken)
{
var service = instance;
Copy link
Contributor

Choose a reason for hiding this comment

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

❓ why?

Copy link
Member Author

@davidwengier davidwengier Oct 9, 2024

Choose a reason for hiding this comment

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

Short answer: it makes the compiler happy

Long answer: The test accessor is a struct, and a lambda in a struct can't access a member of the struct. Or at least that's what I took the error to mean when the compiler complained at me :)

Copy link
Contributor

Choose a reason for hiding this comment

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

ah interesting, so this is causing it to box? Will have to look more, but at first glance it was weird. Newfangled C#

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it would have to box the instance, and I guess they want that to be explicit and not hidden. I don't begin to understand this low level stuff :)

The error was CS1673 IIRC and you wanted to look up more about it.

Copy link
Member

@DustinCampbell DustinCampbell Oct 9, 2024

Choose a reason for hiding this comment

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

Yes, this is a problem because you're capturing a primary constructor parameter that is ultimately backed by a field on the struct. So, you're really capturing an instance field which means that the lambda must capture this, which requires boxing the struct. Otherwise, the closure would contain a copy of the struct.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Yea, this definitely makes sense. In a PR it looks weird but wholistically in the language is 👍


await service.WaitForInitializationAsync().ConfigureAwait(false);

return await instance._projectManager
.UpdateAsync(
updater => service.AddProjectCore(updater, filePath, intermediateOutputPath, configuration, rootNamespace, displayName),
cancellationToken)
.ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,23 +313,6 @@ private void ActOnDocumentInMultipleProjects(string filePath, Action<IProjectSna
}
}

public async Task<ProjectKey> AddProjectAsync(
string filePath,
string intermediateOutputPath,
RazorConfiguration? configuration,
string? rootNamespace,
string? displayName,
CancellationToken cancellationToken)
{
await WaitForInitializationAsync().ConfigureAwait(false);

return await _projectManager
.UpdateAsync(
updater => AddProjectCore(updater, filePath, intermediateOutputPath, configuration, rootNamespace, displayName),
cancellationToken)
.ConfigureAwait(false);
}

private ProjectKey AddProjectCore(ProjectSnapshotManager.Updater updater, string filePath, string intermediateOutputPath, RazorConfiguration? configuration, string? rootNamespace, string? displayName)
{
var normalizedPath = FilePathNormalizer.Normalize(filePath);
Expand All @@ -346,53 +329,6 @@ private ProjectKey AddProjectCore(ProjectSnapshotManager.Updater updater, string
return hostProject.Key;
}

public async Task UpdateProjectAsync(
ProjectKey projectKey,
RazorConfiguration? configuration,
string? rootNamespace,
string? displayName,
ProjectWorkspaceState projectWorkspaceState,
ImmutableArray<DocumentSnapshotHandle> documents,
CancellationToken cancellationToken)
{
await WaitForInitializationAsync().ConfigureAwait(false);

await AddOrUpdateProjectCoreAsync(
projectKey,
filePath: null,
configuration,
rootNamespace,
displayName,
projectWorkspaceState,
documents,
cancellationToken)
.ConfigureAwait(false);
}

public async Task AddOrUpdateProjectAsync(
ProjectKey projectKey,
string filePath,
RazorConfiguration? configuration,
string? rootNamespace,
string? displayName,
ProjectWorkspaceState projectWorkspaceState,
ImmutableArray<DocumentSnapshotHandle> documents,
CancellationToken cancellationToken)
{
await WaitForInitializationAsync().ConfigureAwait(false);

await AddOrUpdateProjectCoreAsync(
projectKey,
filePath,
configuration,
rootNamespace,
displayName,
projectWorkspaceState,
documents,
cancellationToken)
.ConfigureAwait(false);
}

private Task AddOrUpdateProjectCoreAsync(
ProjectKey projectKey,
string? filePath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected override async Task InitializeAsync()
LoggerFactory);
AddDisposable(projectService);

await projectService.AddProjectAsync(
await projectService.GetTestAccessor().AddProjectAsync(
s_projectFilePath1,
s_intermediateOutputPath1,
RazorConfiguration.Default,
Expand All @@ -80,7 +80,7 @@ await projectService.AddProjectAsync(
await projectService.AddDocumentToPotentialProjectsAsync(s_componentFilePath2, DisposalToken);
await projectService.UpdateDocumentAsync(s_componentFilePath2, SourceText.From("@namespace Test"), DisposalToken);

await projectService.AddProjectAsync(
await projectService.GetTestAccessor().AddProjectAsync(
s_projectFilePath2,
s_intermediateOutputPath2,
RazorConfiguration.Default,
Expand Down
Loading