Skip to content

Commit 56356ef

Browse files
Make ISnapshotResolver.GetMiscellaneousProject synchronous
Now that the misc project is guaranteed to be in the project snapshot manager, the operation to retrieve the misc project can be made synchronous.
1 parent b95e00f commit 56356ef

File tree

9 files changed

+36
-54
lines changed

9 files changed

+36
-54
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/DocumentContextFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ internal sealed class DocumentContextFactory(
106106
// Couldn't find the document in a real project. Maybe the language server doesn't yet know about the project
107107
// that the IDE is asking us about. In that case, we might have the document in our misc files project, and we'll
108108
// move it to the real project when/if we find out about it.
109-
var miscellaneousProject = await _snapshotResolver.GetMiscellaneousProjectAsync(cancellationToken).ConfigureAwait(false);
109+
var miscellaneousProject = _snapshotResolver.GetMiscellaneousProject();
110110
var normalizedDocumentPath = FilePathNormalizer.Normalize(filePath);
111111
if (miscellaneousProject.GetDocument(normalizedDocumentPath) is { } miscDocument)
112112
{

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/ISnapshotResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal interface ISnapshotResolver
1717
/// </summary>
1818
ImmutableArray<IProjectSnapshot> FindPotentialProjects(string documentFilePath);
1919

20-
Task<IProjectSnapshot> GetMiscellaneousProjectAsync(CancellationToken cancellationToken);
20+
IProjectSnapshot GetMiscellaneousProject();
2121

2222
/// <summary>
2323
/// Finds a <see cref="IDocumentSnapshot"/> for the given document path that is contained within any project, and returns the first

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/ISnapshotResolverExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static async Task<ImmutableArray<IProjectSnapshot>> TryResolveAllProjects
3030
}
3131

3232
var normalizedDocumentPath = FilePathNormalizer.Normalize(documentFilePath);
33-
var miscProject = await snapshotResolver.GetMiscellaneousProjectAsync(cancellationToken).ConfigureAwait(false);
33+
var miscProject = snapshotResolver.GetMiscellaneousProject();
3434
if (miscProject.GetDocument(normalizedDocumentPath) is not null)
3535
{
3636
projects.Add(miscProject);

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/RazorProjectService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private async Task AddDocumentNeedsLocksAsync(string filePath, CancellationToken
6666

6767
if (!added)
6868
{
69-
var miscFilesProject = await _snapshotResolver.GetMiscellaneousProjectAsync(cancellationToken).ConfigureAwait(false);
69+
var miscFilesProject = _snapshotResolver.GetMiscellaneousProject();
7070
await AddDocumentToProjectAsync(miscFilesProject, textDocumentPath, cancellationToken).ConfigureAwait(false);
7171
}
7272

@@ -203,7 +203,7 @@ await ActOnDocumentInMultipleProjectsAsync(filePath, async (projectSnapshot, tex
203203
if (_projectManager.IsDocumentOpen(textDocumentPath))
204204
{
205205
_logger.LogInformation($"Moving document '{textDocumentPath}' from project '{projectSnapshot.Key}' to misc files because it is open.");
206-
var miscellaneousProject = await _snapshotResolver.GetMiscellaneousProjectAsync(cancellationToken).ConfigureAwait(false);
206+
var miscellaneousProject = _snapshotResolver.GetMiscellaneousProject();
207207
if (projectSnapshot != miscellaneousProject)
208208
{
209209
await MoveDocumentAsync(textDocumentPath, projectSnapshot, miscellaneousProject, cancellationToken).ConfigureAwait(false);
@@ -261,7 +261,7 @@ private async Task ActOnDocumentInMultipleProjectsAsync(
261261
var projects = await _snapshotResolver.TryResolveAllProjectsAsync(textDocumentPath, cancellationToken).ConfigureAwait(false);
262262
if (projects.IsEmpty)
263263
{
264-
var miscFilesProject = await _snapshotResolver.GetMiscellaneousProjectAsync(cancellationToken).ConfigureAwait(false);
264+
var miscFilesProject = _snapshotResolver.GetMiscellaneousProject();
265265
projects = [miscFilesProject];
266266
}
267267

@@ -373,7 +373,7 @@ private async Task UpdateProjectDocumentsAsync(ImmutableArray<DocumentSnapshotHa
373373
var currentProjectKey = project.Key;
374374
var projectDirectory = FilePathNormalizer.GetNormalizedDirectoryName(project.FilePath);
375375
var documentMap = documents.ToDictionary(document => EnsureFullPath(document.FilePath, projectDirectory), FilePathComparer.Instance);
376-
var miscellaneousProject = await _snapshotResolver.GetMiscellaneousProjectAsync(cancellationToken).ConfigureAwait(false);
376+
var miscellaneousProject = _snapshotResolver.GetMiscellaneousProject();
377377

378378
// "Remove" any unnecessary documents by putting them into the misc project
379379
foreach (var documentFilePath in project.DocumentFilePaths)
@@ -433,7 +433,7 @@ await _projectManager
433433
}
434434

435435
project = _projectManager.GetLoadedProject(project.Key);
436-
miscellaneousProject = await _snapshotResolver.GetMiscellaneousProjectAsync(cancellationToken).ConfigureAwait(false);
436+
miscellaneousProject = _snapshotResolver.GetMiscellaneousProject();
437437

438438
// Add (or migrate from misc) any new documents
439439
foreach (var documentKvp in documentMap)
@@ -508,7 +508,7 @@ private static string EnsureFullPath(string filePath, string projectDirectory)
508508

509509
private async Task TryMigrateMiscellaneousDocumentsToProjectAsync(CancellationToken cancellationToken)
510510
{
511-
var miscellaneousProject = await _snapshotResolver.GetMiscellaneousProjectAsync(cancellationToken).ConfigureAwait(false);
511+
var miscellaneousProject = _snapshotResolver.GetMiscellaneousProject();
512512

513513
foreach (var documentFilePath in miscellaneousProject.DocumentFilePaths)
514514
{

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/SnapshotResolver.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,9 @@ public ImmutableArray<IProjectSnapshot> FindPotentialProjects(string documentFil
7373
return projects.DrainToImmutable();
7474
}
7575

76-
public async Task<IProjectSnapshot> GetMiscellaneousProjectAsync(CancellationToken cancellationToken)
76+
public IProjectSnapshot GetMiscellaneousProject()
7777
{
78-
if (!_projectManager.TryGetLoadedProject(MiscellaneousHostProject.Key, out var miscellaneousProject))
79-
{
80-
await _projectManager
81-
.UpdateAsync(
82-
static (updater, miscHostProject) => updater.ProjectAdded(miscHostProject),
83-
state: MiscellaneousHostProject,
84-
cancellationToken)
85-
.ConfigureAwait(false);
86-
87-
miscellaneousProject = _projectManager.GetLoadedProject(MiscellaneousHostProject.Key);
88-
}
89-
90-
return miscellaneousProject;
78+
return _projectManager.GetLoadedProject(MiscellaneousHostProject.Key);
9179
}
9280

9381
public async Task<IDocumentSnapshot?> ResolveDocumentInAnyProjectAsync(string documentFilePath, CancellationToken cancellationToken)
@@ -112,7 +100,7 @@ await _projectManager
112100
}
113101

114102
_logger.LogTrace($"Looking for {documentFilePath} in miscellaneous project.");
115-
var miscellaneousProject = await GetMiscellaneousProjectAsync(cancellationToken).ConfigureAwait(false);
103+
var miscellaneousProject = GetMiscellaneousProject();
116104

117105
if (miscellaneousProject.GetDocument(normalizedDocumentPath) is { } miscDocument)
118106
{

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/DocumentContextFactoryTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public Task InitializeAsync(CancellationToken cancellationToken)
214214
public ImmutableArray<IProjectSnapshot> FindPotentialProjects(string documentFilePath)
215215
=> throw new NotImplementedException();
216216

217-
public Task<IProjectSnapshot> GetMiscellaneousProjectAsync(CancellationToken cancellationToken)
217+
public IProjectSnapshot GetMiscellaneousProject()
218218
=> throw new NotImplementedException();
219219

220220
public Task<IDocumentSnapshot?> ResolveDocumentInAnyProjectAsync(string documentFilePath, CancellationToken cancellationToken)

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorProjectServiceTest.cs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public async Task UpdateProject_MovesDocumentsFromMisc()
155155
var hostProject = new HostProject("C:/path/to/project.csproj", "C:/path/to/obj", RazorConfiguration.Default, "TestRootNamespace");
156156
var hostDocument = new HostDocument("C:/path/to/file.cshtml", "file.cshtml", FileKinds.Legacy);
157157

158-
var miscProject = await _snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
158+
var miscProject = _snapshotResolver.GetMiscellaneousProject();
159159

160160
await _projectManager.UpdateAsync(updater =>
161161
{
@@ -192,7 +192,7 @@ public async Task UpdateProject_MovesExistingDocumentToMisc()
192192
var hostProject = new HostProject("C:/path/to/project.csproj", "C:/path/to/obj", RazorConfiguration.Default, "TestRootNamespace");
193193
var hostDocument = new HostDocument("C:/path/to/file.cshtml", "file.cshtml", FileKinds.Legacy);
194194

195-
var miscProject = await _snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
195+
var miscProject = _snapshotResolver.GetMiscellaneousProject();
196196

197197
var project = await _projectManager.UpdateAsync(updater =>
198198
{
@@ -231,10 +231,6 @@ public async Task UpdateProject_KnownDocuments()
231231
var hostProject = new HostProject("path/to/project.csproj", "path/to/obj", RazorConfiguration.Default, "TestRootNamespace");
232232
var document = new HostDocument("path/to/file.cshtml", "file.cshtml", FileKinds.Legacy);
233233

234-
// Note: We acquire the miscellaneous project here to avoid a spurious 'ProjectAdded'
235-
// notification when it would get created by UpdateProject(...) below.
236-
_ = await _snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
237-
238234
await _projectManager.UpdateAsync(updater =>
239235
{
240236
updater.ProjectAdded(hostProject);
@@ -505,7 +501,7 @@ public async Task CloseDocument_ClosesDocumentInMiscellaneousProject()
505501
await _projectService.AddDocumentAsync(DocumentFilePath, DisposalToken);
506502
await _projectService.OpenDocumentAsync(DocumentFilePath, s_emptyText, version: 42, DisposalToken);
507503

508-
var miscProject = await _snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
504+
var miscProject = _snapshotResolver.GetMiscellaneousProject();
509505

510506
Assert.True(_projectManager.IsDocumentOpen(DocumentFilePath));
511507

@@ -592,7 +588,7 @@ public async Task OpenDocument_OpensAlreadyAddedDocumentInMiscellaneousProject()
592588

593589
await _projectService.AddDocumentAsync(DocumentFilePath, DisposalToken);
594590

595-
var miscProject = await _snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
591+
var miscProject = _snapshotResolver.GetMiscellaneousProject();
596592

597593
Assert.False(_projectManager.IsDocumentOpen(DocumentFilePath));
598594

@@ -643,7 +639,7 @@ public async Task AddDocument_NoopsIfDocumentIsAlreadyAdded()
643639

644640
await _projectService.AddDocumentAsync(DocumentFilePath, DisposalToken);
645641

646-
var miscProject = await _snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
642+
var miscProject = _snapshotResolver.GetMiscellaneousProject();
647643

648644
using var listener = _projectManager.ListenToNotifications();
649645

@@ -686,7 +682,7 @@ public async Task AddDocument_AddsDocumentToMiscellaneousProject()
686682
// Arrange
687683
const string DocumentFilePath = "document.cshtml";
688684

689-
var miscProject = await _snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
685+
var miscProject = _snapshotResolver.GetMiscellaneousProject();
690686

691687
using var listener = _projectManager.ListenToNotifications();
692688

@@ -774,7 +770,7 @@ public async Task RemoveOpenDocument_RemovesDocumentFromOwnerProject_MovesToMisc
774770
await _projectService.OpenDocumentAsync(DocumentFilePath, s_emptyText, version: 42, DisposalToken);
775771

776772
var ownerProject = _projectManager.GetLoadedProject(ownerProjectKey);
777-
var miscProject = await _snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
773+
var miscProject = _snapshotResolver.GetMiscellaneousProject();
778774

779775
Assert.True(_projectManager.IsDocumentOpen(DocumentFilePath));
780776

@@ -799,7 +795,7 @@ public async Task RemoveDocument_RemovesDocumentFromMiscellaneousProject()
799795

800796
await _projectService.AddDocumentAsync(DocumentFilePath, DisposalToken);
801797

802-
var miscProject = await _snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
798+
var miscProject = _snapshotResolver.GetMiscellaneousProject();
803799

804800
Assert.False(_projectManager.IsDocumentOpen(DocumentFilePath));
805801

@@ -842,7 +838,7 @@ public async Task RemoveDocument_NoopsIfMiscellaneousProjectDoesNotContainDocume
842838
// Arrange
843839
const string DocumentFilePath = "document.cshtml";
844840

845-
var miscProject = await _snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
841+
var miscProject = _snapshotResolver.GetMiscellaneousProject();
846842

847843
Assert.False(_projectManager.IsDocumentOpen(DocumentFilePath));
848844

@@ -929,7 +925,7 @@ public async Task UpdateDocument_ChangesDocumentInMiscProject()
929925
await _projectService.AddDocumentAsync(DocumentFilePath, DisposalToken);
930926
await _projectService.OpenDocumentAsync(DocumentFilePath, s_emptyText, version: 42, DisposalToken);
931927

932-
var miscProject = await _snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
928+
var miscProject = _snapshotResolver.GetMiscellaneousProject();
933929

934930
Assert.True(_projectManager.IsDocumentOpen(DocumentFilePath));
935931

@@ -1042,7 +1038,7 @@ public async Task AddProject_DoesNotMigrateMiscellaneousDocumentIfNewProjectNotA
10421038
const string DocumentFilePath1 = "C:/path/to/document1.cshtml";
10431039
const string DocumentFilePath2 = "C:/path/to/document2.cshtml";
10441040

1045-
var miscProject = await _snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
1041+
var miscProject = _snapshotResolver.GetMiscellaneousProject();
10461042

10471043
await _projectManager.UpdateAsync(updater =>
10481044
{
@@ -1072,7 +1068,7 @@ public async Task AddProject_MigratesMiscellaneousDocumentsToNewOwnerProject()
10721068
const string DocumentFilePath1 = "C:/path/to/document1.cshtml";
10731069
const string DocumentFilePath2 = "C:/path/to/document2.cshtml";
10741070

1075-
var miscProject = await _snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
1071+
var miscProject = _snapshotResolver.GetMiscellaneousProject();
10761072

10771073
await _projectManager.UpdateAsync(updater =>
10781074
{

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/SnapshotResolverTest.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public async Task TryResolveDocumentInAnyProject_AsksMiscellaneousProjectForDocu
4141
var snapshotResolver = new SnapshotResolver(projectManager, LoggerFactory);
4242
await snapshotResolver.InitializeAsync(DisposalToken);
4343

44-
await projectManager.UpdateAsync(async updater =>
44+
await projectManager.UpdateAsync(updater =>
4545
{
46-
var miscProject = await snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
46+
var miscProject = snapshotResolver.GetMiscellaneousProject();
4747
var hostProject = new HostProject(miscProject.FilePath, miscProject.IntermediateOutputPath, FallbackRazorConfiguration.Latest, miscProject.RootNamespace);
4848
updater.DocumentAdded(
4949
hostProject.Key,
@@ -100,8 +100,6 @@ public async Task TryResolveAllProjects_OnlyMiscellaneousProjectDoesNotContainDo
100100
var snapshotResolver = new SnapshotResolver(projectManager, LoggerFactory);
101101
await snapshotResolver.InitializeAsync(DisposalToken);
102102

103-
await snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
104-
105103
// Act
106104
var projects = await snapshotResolver.TryResolveAllProjectsAsync(documentFilePath, DisposalToken);
107105

@@ -121,7 +119,7 @@ public async Task TryResolveAllProjects_OnlyMiscellaneousProjectContainsDocument
121119
var projects = await snapshotResolver.TryResolveAllProjectsAsync(documentFilePath, DisposalToken);
122120

123121
// Assert
124-
var miscFilesProject = await snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
122+
var miscFilesProject = snapshotResolver.GetMiscellaneousProject();
125123
Assert.Single(projects, miscFilesProject);
126124
}
127125

@@ -184,9 +182,9 @@ public async Task TryResolveAllProjects_MiscellaneousOwnerProjectWithOthers_Retu
184182
var snapshotResolver = new SnapshotResolver(projectManager, LoggerFactory);
185183
await snapshotResolver.InitializeAsync(DisposalToken);
186184

187-
var miscProject = await projectManager.UpdateAsync(async updater =>
185+
var miscProject = await projectManager.UpdateAsync(updater =>
188186
{
189-
var miscProject = (ProjectSnapshot)await snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
187+
var miscProject = (ProjectSnapshot)snapshotResolver.GetMiscellaneousProject();
190188
updater.CreateAndAddDocument(miscProject, documentFilePath);
191189
updater.CreateAndAddProject("C:/path/to/project.csproj");
192190

@@ -235,7 +233,7 @@ public async Task GetMiscellaneousProject_ProjectLoaded_ReturnsExistingProject()
235233
await snapshotResolver.InitializeAsync(DisposalToken);
236234

237235
// Act
238-
var project = await snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
236+
var project = snapshotResolver.GetMiscellaneousProject();
239237
var inManager = projectManager.GetLoadedProject(snapshotResolver.MiscellaneousHostProject.Key);
240238

241239
// Assert
@@ -251,7 +249,7 @@ public async Task GetMiscellaneousProject_ProjectNotLoaded_CreatesProjectAndRetu
251249
await snapshotResolver.InitializeAsync(DisposalToken);
252250

253251
// Act
254-
var project = await snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
252+
var project = snapshotResolver.GetMiscellaneousProject();
255253

256254
// Assert
257255
Assert.Single(projectManager.GetProjects());
@@ -268,9 +266,9 @@ private async Task<SnapshotResolver> CreateSnapshotResolverAsync(string filePath
268266

269267
if (addToMiscellaneous)
270268
{
271-
await projectManager.UpdateAsync(async updater =>
269+
await projectManager.UpdateAsync(updater =>
272270
{
273-
var miscProject = (ProjectSnapshot)await snapshotResolver.GetMiscellaneousProjectAsync(DisposalToken);
271+
var miscProject = (ProjectSnapshot)snapshotResolver.GetMiscellaneousProject();
274272
updater.CreateAndAddDocument(miscProject, filePath);
275273
});
276274
}

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/TestSnapshotResolver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public ImmutableArray<IProjectSnapshot> FindPotentialProjects(string documentFil
3535
? _projects
3636
: [];
3737

38-
public Task<IProjectSnapshot> GetMiscellaneousProjectAsync(CancellationToken cancellationToken)
39-
=> Task.FromResult(_miscProject);
38+
public IProjectSnapshot GetMiscellaneousProject()
39+
=> _miscProject;
4040

4141
public Task<IDocumentSnapshot?> ResolveDocumentInAnyProjectAsync(string documentFilePath, CancellationToken cancellationToken)
4242
=> Task.FromResult<IDocumentSnapshot?>(null);

0 commit comments

Comments
 (0)