Skip to content

Commit

Permalink
RazorProjectService cleanup (#10989)
Browse files Browse the repository at this point in the history
Noticed while working on my previous PR. There were a few methods in
RazorProjectService that were only used by tests, and also resulted in
some tests validating things that could never happen in the product
(like passing `null` for a project file path in an update). This PR
removes one unused method, moves one to a test accessor where it rightly
should be, and updates the tests that were calling `UpdateProjectAsync`
to instead call through `IRazorProjectServiceListener.UpdatedAsync`.

I don't love that these tests have to call through that interface, but I
didn't change the method to be implicitly implemented because I don't
think the name makes sense when written that way.

There are, or at least should be, no functionality changes in this PR.
  • Loading branch information
davidwengier authored Oct 9, 2024
2 parents ccd2034 + 365b5cc commit 6f378ca
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 223 deletions.
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;

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 @@ -344,53 +327,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

0 comments on commit 6f378ca

Please sign in to comment.