From e34dc5482edbcdc2de791b8c119372a0d0dc75fc Mon Sep 17 00:00:00 2001 From: David Wengier Date: Fri, 16 Aug 2024 14:03:36 +1000 Subject: [PATCH 1/2] Don't add already known documents to the misc files project --- .../ProjectSystem/RazorProjectService.cs | 19 +++++++++++---- .../RazorProjectServiceTest.cs | 23 +++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/RazorProjectService.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/RazorProjectService.cs index 74ebb392272..1f896304173 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/RazorProjectService.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/RazorProjectService.cs @@ -159,14 +159,25 @@ await _projectManager private void AddDocumentToMiscProjectCore(ProjectSnapshotManager.Updater updater, string filePath) { var textDocumentPath = FilePathNormalizer.Normalize(filePath); + _logger.LogDebug($"Asked to add {textDocumentPath} to the miscellaneous files project, because we don't have project info (yet?)"); - _logger.LogDebug($"Adding {filePath} to the miscellaneous files project, because we don't have project info (yet?)"); - var miscFilesProject = _projectManager.GetMiscellaneousProject(); + var potentialProjects = _projectManager.FindPotentialProjects(textDocumentPath); + foreach (var project in potentialProjects) + { + if (project.DocumentFilePaths.Contains(textDocumentPath, FilePathComparer.Instance)) + { + // Already in a known project, so we don't want it in the misc files project + _logger.LogDebug($"File {textDocumentPath} is already in {project.Key} so we're not adding it to the miscellaneous files project"); + return; + } + } - if (miscFilesProject.GetDocument(FilePathNormalizer.Normalize(textDocumentPath)) is not null) + var miscFilesProject = _projectManager.GetMiscellaneousProject(); + if (miscFilesProject.GetDocument(textDocumentPath) is not null) { // Document already added. This usually occurs when VSCode has already pre-initialized // open documents and then we try to manually add all known razor documents. + _logger.LogDebug($"File {textDocumentPath} is already in the miscellaneous files project, so no-op"); return; } @@ -176,7 +187,7 @@ private void AddDocumentToMiscProjectCore(ProjectSnapshotManager.Updater updater var hostDocument = new HostDocument(textDocumentPath, normalizedTargetFilePath); var textLoader = _remoteTextLoaderFactory.Create(textDocumentPath); - _logger.LogInformation($"Adding document '{filePath}' to project '{miscFilesProject.Key}'."); + _logger.LogInformation($"Adding document '{textDocumentPath}' to project '{miscFilesProject.Key}'."); updater.DocumentAdded(miscFilesProject.Key, hostDocument, textLoader); } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorProjectServiceTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorProjectServiceTest.cs index 0802a4cf61d..0c37f082d9d 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorProjectServiceTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorProjectServiceTest.cs @@ -733,6 +733,29 @@ public async Task AddDocumentToMiscProjectAsync_AddsDocumentToMiscellaneousProje Assert.False(_projectManager.IsDocumentOpen(DocumentFilePath)); } + [Fact] + public async Task AddDocumentToMiscProjectAsync_IgnoresKnownDocument() + { + // Arrange + const string ProjectFilePath = "C:/path/to/project.csproj"; + const string IntermediateOutputPath = "C:/path/to/obj"; + const string RootNamespace = "TestRootNamespace"; + const string DocumentFilePath = "C:/path/to/document.cshtml"; + + await _projectService.AddProjectAsync( + ProjectFilePath, IntermediateOutputPath, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken); + await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken); + + // Act + using var listener = _projectManager.ListenToNotifications(); + + // Act + await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken); + + // Assert + listener.AssertNoNotifications(); + } + [Fact] public async Task RemoveDocument_RemovesDocumentFromOwnerProject() { From f1c3a700f3deee5c563f3a024d563f5f36fe0da0 Mon Sep 17 00:00:00 2001 From: David Wengier Date: Mon, 19 Aug 2024 08:49:20 +1000 Subject: [PATCH 2/2] PR feedback --- .../ProjectSystem/RazorProjectService.cs | 19 ++++--------------- .../RazorProjectServiceTest.cs | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/RazorProjectService.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/RazorProjectService.cs index 1f896304173..57e0def447f 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/RazorProjectService.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/RazorProjectService.cs @@ -161,25 +161,14 @@ private void AddDocumentToMiscProjectCore(ProjectSnapshotManager.Updater updater var textDocumentPath = FilePathNormalizer.Normalize(filePath); _logger.LogDebug($"Asked to add {textDocumentPath} to the miscellaneous files project, because we don't have project info (yet?)"); - var potentialProjects = _projectManager.FindPotentialProjects(textDocumentPath); - foreach (var project in potentialProjects) + if (_projectManager.TryResolveDocumentInAnyProject(textDocumentPath, _logger, out var document)) { - if (project.DocumentFilePaths.Contains(textDocumentPath, FilePathComparer.Instance)) - { - // Already in a known project, so we don't want it in the misc files project - _logger.LogDebug($"File {textDocumentPath} is already in {project.Key} so we're not adding it to the miscellaneous files project"); - return; - } + // Already in a known project, so we don't want it in the misc files project + _logger.LogDebug($"File {textDocumentPath} is already in {document.Project.Key}, so we're not adding it to the miscellaneous files project"); + return; } var miscFilesProject = _projectManager.GetMiscellaneousProject(); - if (miscFilesProject.GetDocument(textDocumentPath) is not null) - { - // Document already added. This usually occurs when VSCode has already pre-initialized - // open documents and then we try to manually add all known razor documents. - _logger.LogDebug($"File {textDocumentPath} is already in the miscellaneous files project, so no-op"); - return; - } // Representing all of our host documents with a re-normalized target path to workaround GetRelatedDocument limitations. var normalizedTargetFilePath = textDocumentPath.Replace('/', '\\').TrimStart('\\'); diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorProjectServiceTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorProjectServiceTest.cs index 0c37f082d9d..326170da442 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorProjectServiceTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorProjectServiceTest.cs @@ -756,6 +756,24 @@ await _projectService.AddProjectAsync( listener.AssertNoNotifications(); } + [Fact] + public async Task AddDocumentToMiscProjectAsync_IgnoresKnownDocument_InMiscFiles() + { + // Arrange + const string DocumentFilePath = "C:/path/to/document.cshtml"; + + await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken); + + // Act + using var listener = _projectManager.ListenToNotifications(); + + // Act + await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken); + + // Assert + listener.AssertNoNotifications(); + } + [Fact] public async Task RemoveDocument_RemovesDocumentFromOwnerProject() {