Skip to content

Commit 7a23d44

Browse files
authored
Minor RazorProjectService cleanup (#10342)
With recent changes, this is unnecessary because a document add event will do the same job, but better.
2 parents b09e373 + fbd66d0 commit 7a23d44

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,6 @@ private void AddDocumentToMiscProjectCore(ProjectSnapshotManager.Updater updater
6868
_logger.LogInformation($"Adding document '{filePath}' to project '{miscFilesProject.Key}'.");
6969

7070
updater.DocumentAdded(miscFilesProject.Key, hostDocument, textLoader);
71-
72-
// Adding a document to a project could also happen because a target was added to a project, or we're moving a document
73-
// from Misc Project to a real one, and means the newly added document could actually already be open.
74-
// If it is, we need to make sure we start generating it so we're ready to handle requests that could start coming in.
75-
if (_projectManager.IsDocumentOpen(textDocumentPath) &&
76-
_projectManager.TryGetLoadedProject(miscFilesProject.Key, out var project) &&
77-
project.GetDocument(textDocumentPath) is { } document)
78-
{
79-
document.GetGeneratedOutputAsync().Forget();
80-
}
8171
}
8272

8373
public Task OpenDocumentAsync(string filePath, SourceText sourceText, int version, CancellationToken cancellationToken)

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,37 @@ public class OpenDocumentGeneratorTest(ITestOutputHelper testOutput) : LanguageS
2727
private readonly HostProject _hostProject1 = new("c:/Test1/Test1.csproj", "c:/Test1/obj", RazorConfiguration.Default, "TestRootNamespace");
2828
private readonly HostProject _hostProject2 = new("c:/Test2/Test2.csproj", "c:/Test2/obj", RazorConfiguration.Default, "TestRootNamespace");
2929

30+
[Fact]
31+
public async Task DocumentAdded_ProcessesOpenDocument()
32+
{
33+
// Arrange
34+
var projectManager = CreateProjectSnapshotManager(new LspProjectEngineFactoryProvider(TestRazorLSPOptionsMonitor.Create()));
35+
var listener = new TestDocumentProcessedListener();
36+
using var generator = CreateOpenDocumentGenerator(projectManager, listener);
37+
38+
await projectManager.UpdateAsync(updater =>
39+
{
40+
updater.ProjectAdded(_hostProject1);
41+
updater.ProjectAdded(_hostProject2);
42+
updater.DocumentAdded(_hostProject1.Key, _documents[0], null!);
43+
updater.DocumentOpened(_hostProject1.Key, _documents[0].FilePath, SourceText.From(string.Empty));
44+
});
45+
46+
await listener.GetProcessedDocumentAsync(cancelAfter: TimeSpan.FromSeconds(10));
47+
listener.Reset();
48+
49+
await projectManager.UpdateAsync(updater =>
50+
{
51+
updater.DocumentRemoved(_hostProject1.Key, _documents[0]);
52+
updater.DocumentAdded(_hostProject2.Key, _documents[0], null!);
53+
});
54+
55+
// Assert
56+
var document = await listener.GetProcessedDocumentAsync(cancelAfter: TimeSpan.FromSeconds(10));
57+
Assert.Equal(_hostProject2.Key, document.Project.Key);
58+
Assert.Equal(_documents[0].FilePath, document.FilePath);
59+
}
60+
3061
[Fact]
3162
public async Task DocumentAdded_IgnoresClosedDocument()
3263
{
@@ -152,7 +183,7 @@ private OpenDocumentGenerator CreateOpenDocumentGenerator(
152183

153184
private class TestDocumentProcessedListener : IDocumentProcessedListener
154185
{
155-
private readonly TaskCompletionSource<IDocumentSnapshot> _tcs;
186+
private TaskCompletionSource<IDocumentSnapshot> _tcs;
156187

157188
public TestDocumentProcessedListener()
158189
{
@@ -178,5 +209,10 @@ public void DocumentProcessed(RazorCodeDocument codeDocument, IDocumentSnapshot
178209
{
179210
_tcs.SetResult(document);
180211
}
212+
213+
internal void Reset()
214+
{
215+
_tcs = new TaskCompletionSource<IDocumentSnapshot>();
216+
}
181217
}
182218
}

0 commit comments

Comments
 (0)