From 074043b5163041beaaf1adf8a9acd75c0bf0a856 Mon Sep 17 00:00:00 2001 From: David Wengier Date: Wed, 4 Sep 2024 13:35:33 +1000 Subject: [PATCH 1/3] Remove IsImportDocument Was only used in one place, and that place could never have any result except false due to a FileKind check above --- .../IDocumentSnapshotExtensions.cs | 6 --- .../ProjectSystem/IProjectSnapshot.cs | 1 - .../ProjectSystem/ProjectSnapshot.cs | 10 ----- .../ProjectSystem/RemoteProjectSnapshot.cs | 6 --- .../EphemeralProjectSnapshot.cs | 10 ----- .../DefaultProjectSnapshotTest.cs | 37 ------------------- .../CohostUriPresentationEndpointTest.cs | 20 ++++++++++ 7 files changed, 20 insertions(+), 70 deletions(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IDocumentSnapshotExtensions.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IDocumentSnapshotExtensions.cs index 6b6ec6a936..62a90f1143 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IDocumentSnapshotExtensions.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IDocumentSnapshotExtensions.cs @@ -27,12 +27,6 @@ internal static class IDocumentSnapshotExtensions var project = documentSnapshot.Project; - // If the document is an import document, then it can't be a component - if (project.IsImportDocument(documentSnapshot)) - { - return null; - } - // If we got this far, we can check for tag helpers var tagHelpers = await project.GetTagHelpersAsync(cancellationToken).ConfigureAwait(false); foreach (var tagHelper in tagHelpers) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IProjectSnapshot.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IProjectSnapshot.cs index 56459d179b..070e8a325e 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IProjectSnapshot.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IProjectSnapshot.cs @@ -39,7 +39,6 @@ internal interface IProjectSnapshot RazorProjectEngine GetProjectEngine(); IDocumentSnapshot? GetDocument(string filePath); bool TryGetDocument(string filePath, [NotNullWhen(true)] out IDocumentSnapshot? document); - bool IsImportDocument(IDocumentSnapshot document); /// /// If the provided document is an import document, gets the other documents in the project diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshot.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshot.cs index ff80c8c93a..066808d200 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshot.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshot.cs @@ -79,16 +79,6 @@ internal class ProjectSnapshot : IProjectSnapshot return document is not null; } - public bool IsImportDocument(IDocumentSnapshot document) - { - if (document is null) - { - throw new ArgumentNullException(nameof(document)); - } - - return document.TargetPath is { } targetPath && State.ImportsToRelatedDocuments.ContainsKey(targetPath); - } - public ImmutableArray GetRelatedDocuments(IDocumentSnapshot document) { if (document is null) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteProjectSnapshot.cs b/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteProjectSnapshot.cs index cfd8e70171..8e59270f35 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteProjectSnapshot.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteProjectSnapshot.cs @@ -176,12 +176,6 @@ internal class RemoteProjectSnapshot : IProjectSnapshot return builder.DrainToImmutable(); } - public bool IsImportDocument(IDocumentSnapshot document) - { - return document.TargetPath is { } targetPath && - _importsToRelatedDocumentsLazy.Value.ContainsKey(targetPath); - } - private RazorConfiguration CreateRazorConfiguration() { // See RazorSourceGenerator.RazorProviders.cs diff --git a/src/Razor/src/Microsoft.VisualStudio.LegacyEditor.Razor/EphemeralProjectSnapshot.cs b/src/Razor/src/Microsoft.VisualStudio.LegacyEditor.Razor/EphemeralProjectSnapshot.cs index e0d3b2670a..16b4140836 100644 --- a/src/Razor/src/Microsoft.VisualStudio.LegacyEditor.Razor/EphemeralProjectSnapshot.cs +++ b/src/Razor/src/Microsoft.VisualStudio.LegacyEditor.Razor/EphemeralProjectSnapshot.cs @@ -72,16 +72,6 @@ internal class EphemeralProjectSnapshot : IProjectSnapshot return false; } - public bool IsImportDocument(IDocumentSnapshot document) - { - if (document is null) - { - throw new ArgumentNullException(nameof(document)); - } - - return false; - } - public ImmutableArray GetRelatedDocuments(IDocumentSnapshot document) { if (document is null) diff --git a/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/ProjectSystem/DefaultProjectSnapshotTest.cs b/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/ProjectSystem/DefaultProjectSnapshotTest.cs index 4c8734c90c..b661352b3e 100644 --- a/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/ProjectSystem/DefaultProjectSnapshotTest.cs +++ b/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/ProjectSystem/DefaultProjectSnapshotTest.cs @@ -61,43 +61,6 @@ public class DefaultProjectSnapshotTest : WorkspaceTestBase d => Assert.Same(d.Value, snapshot.GetDocument(d.Key))); } - [Fact] - public void IsImportDocument_NonImportDocument_ReturnsFalse() - { - // Arrange - var state = ProjectState.Create(ProjectEngineFactoryProvider, _hostProject, _projectWorkspaceState) - .WithAddedHostDocument(_documents[0], DocumentState.EmptyLoader); - var snapshot = new ProjectSnapshot(state); - - var document = snapshot.GetDocument(_documents[0].FilePath); - Assert.NotNull(document); - - // Act - var result = snapshot.IsImportDocument(document); - - // Assert - Assert.False(result); - } - - [Fact] - public void IsImportDocument_ImportDocument_ReturnsTrue() - { - // Arrange - var state = ProjectState.Create(ProjectEngineFactoryProvider, _hostProject, _projectWorkspaceState) - .WithAddedHostDocument(_documents[0], DocumentState.EmptyLoader) - .WithAddedHostDocument(TestProjectData.SomeProjectImportFile, DocumentState.EmptyLoader); - var snapshot = new ProjectSnapshot(state); - - var document = snapshot.GetDocument(TestProjectData.SomeProjectImportFile.FilePath); - Assert.NotNull(document); - - // Act - var result = snapshot.IsImportDocument(document); - - // Assert - Assert.True(result); - } - [Fact] public void GetRelatedDocuments_NonImportDocument_ReturnsEmpty() { diff --git a/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/CohostUriPresentationEndpointTest.cs b/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/CohostUriPresentationEndpointTest.cs index 981e8f7a8b..d1f965f0b7 100644 --- a/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/CohostUriPresentationEndpointTest.cs +++ b/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/CohostUriPresentationEndpointTest.cs @@ -97,6 +97,26 @@ public class CohostUriPresentationEndpointTest(ITestOutputHelper testOutputHelpe expected: ""); } + [Fact] + public async Task ImportsFile() + { + await VerifyUriPresentationAsync( + input: """ + This is a Razor document. + +
+ [||] +
+ + The end. + """, + additionalFiles: [ + (File("_Imports.razor"), "") + ], + uris: [FileUri("_Imports.razor")], + expected: null); + } + [Fact] public async Task Html_IntoCSharp_NoTag() { From 7107165cd47ce9ccd41a7bc006a231fb76b9d99e Mon Sep 17 00:00:00 2001 From: David Wengier Date: Wed, 4 Sep 2024 13:58:57 +1000 Subject: [PATCH 2/3] Remove GetRelatedDocuments from IProjectSnapshot This is only called from the LSP server, and shouldn't be called from cohosting, so now it can't be! --- .../ProjectSystem/IProjectSnapshot.cs | 9 ----- .../ProjectSystem/ProjectChangeEventArgs.cs | 10 +++--- .../ProjectSystem/ProjectSnapshot.cs | 5 +++ .../ProjectSnapshotManager.Entry.cs | 4 +-- .../ProjectSystem/ProjectSnapshotManager.cs | 6 ++-- .../ProjectSystem/RemoteProjectSnapshot.cs | 36 ------------------- .../EphemeralProjectSnapshot.cs | 10 ------ .../VisualStudioDocumentTrackerTest.cs | 11 +++--- 8 files changed, 22 insertions(+), 69 deletions(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IProjectSnapshot.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IProjectSnapshot.cs index 070e8a325e..5201a44810 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IProjectSnapshot.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IProjectSnapshot.cs @@ -39,13 +39,4 @@ internal interface IProjectSnapshot RazorProjectEngine GetProjectEngine(); IDocumentSnapshot? GetDocument(string filePath); bool TryGetDocument(string filePath, [NotNullWhen(true)] out IDocumentSnapshot? document); - - /// - /// If the provided document is an import document, gets the other documents in the project - /// that include directives specified by the provided document. Otherwise returns an empty - /// list. - /// - /// The document. - /// A list of related documents. - ImmutableArray GetRelatedDocuments(IDocumentSnapshot document); } diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectChangeEventArgs.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectChangeEventArgs.cs index 9d2843994e..be4ce51e13 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectChangeEventArgs.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectChangeEventArgs.cs @@ -8,12 +8,12 @@ namespace Microsoft.CodeAnalysis.Razor.ProjectSystem; internal class ProjectChangeEventArgs : EventArgs { - public ProjectChangeEventArgs(IProjectSnapshot older, IProjectSnapshot newer, ProjectChangeKind kind) + public ProjectChangeEventArgs(ProjectSnapshot older, ProjectSnapshot newer, ProjectChangeKind kind) : this(older, newer, null, kind, false) { } - public ProjectChangeEventArgs(IProjectSnapshot? older, IProjectSnapshot? newer, string? documentFilePath, ProjectChangeKind kind, bool solutionIsClosing) + public ProjectChangeEventArgs(ProjectSnapshot? older, ProjectSnapshot? newer, string? documentFilePath, ProjectChangeKind kind, bool solutionIsClosing) { if (older is null && newer is null) { @@ -29,9 +29,9 @@ internal class ProjectChangeEventArgs : EventArgs ProjectKey = (older ?? newer)!.Key; } - public IProjectSnapshot? Older { get; } + public ProjectSnapshot? Older { get; } - public IProjectSnapshot? Newer { get; } + public ProjectSnapshot? Newer { get; } public ProjectKey ProjectKey { get; } @@ -43,6 +43,6 @@ internal class ProjectChangeEventArgs : EventArgs public bool SolutionIsClosing { get; } - public static ProjectChangeEventArgs CreateTestInstance(IProjectSnapshot older, IProjectSnapshot newer, string documentFilePath, ProjectChangeKind kind, bool solutionIsClosing = false) => + public static ProjectChangeEventArgs CreateTestInstance(ProjectSnapshot older, ProjectSnapshot newer, string documentFilePath, ProjectChangeKind kind, bool solutionIsClosing = false) => new(older, newer, documentFilePath, kind, solutionIsClosing); } diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshot.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshot.cs index 066808d200..1278e741d6 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshot.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshot.cs @@ -79,6 +79,11 @@ internal class ProjectSnapshot : IProjectSnapshot return document is not null; } + /// + /// If the provided document is an import document, gets the other documents in the project + /// that include directives specified by the provided document. Otherwise returns an empty + /// list. + /// public ImmutableArray GetRelatedDocuments(IDocumentSnapshot document) { if (document is null) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.Entry.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.Entry.cs index 48341f3dd5..c02de7a1ba 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.Entry.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.Entry.cs @@ -7,9 +7,9 @@ internal partial class ProjectSnapshotManager { private sealed record Entry(ProjectState State) { - private IProjectSnapshot? _snapshotUnsafe; + private ProjectSnapshot? _snapshotUnsafe; - public IProjectSnapshot GetSnapshot() + public ProjectSnapshot GetSnapshot() { return _snapshotUnsafe ??= new ProjectSnapshot(State); } diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.cs index 40f7d96740..96c5e4b8d6 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.cs @@ -383,7 +383,7 @@ internal partial class ProjectSnapshotManager : IProjectSnapshotManager, IDispos } } - private void NotifyListeners(IProjectSnapshot? older, IProjectSnapshot? newer, string? documentFilePath, ProjectChangeKind kind) + private void NotifyListeners(ProjectSnapshot? older, ProjectSnapshot? newer, string? documentFilePath, ProjectChangeKind kind) { if (!_initialized) { @@ -421,8 +421,8 @@ internal partial class ProjectSnapshotManager : IProjectSnapshotManager, IDispos ProjectKey projectKey, string? documentFilePath, IUpdateProjectAction action, - [NotNullWhen(true)] out IProjectSnapshot? oldSnapshot, - [NotNullWhen(true)] out IProjectSnapshot? newSnapshot) + [NotNullWhen(true)] out ProjectSnapshot? oldSnapshot, + [NotNullWhen(true)] out ProjectSnapshot? newSnapshot) { using var upgradeableLock = _readerWriterLock.DisposableUpgradeableRead(); diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteProjectSnapshot.cs b/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteProjectSnapshot.cs index 8e59270f35..6d5d2ec604 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteProjectSnapshot.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteProjectSnapshot.cs @@ -11,7 +11,6 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Razor; using Microsoft.AspNetCore.Razor.Language; -using Microsoft.AspNetCore.Razor.PooledObjects; using Microsoft.AspNetCore.Razor.ProjectEngineHost; using Microsoft.AspNetCore.Razor.ProjectSystem; using Microsoft.AspNetCore.Razor.Telemetry; @@ -32,7 +31,6 @@ internal class RemoteProjectSnapshot : IProjectSnapshot private readonly ITelemetryReporter _telemetryReporter; private readonly Lazy _lazyConfiguration; private readonly Lazy _lazyProjectEngine; - private readonly Lazy>> _importsToRelatedDocumentsLazy; private ImmutableArray _tagHelpers; @@ -56,18 +54,6 @@ internal class RemoteProjectSnapshot : IProjectSnapshot builder.SetSupportLocalizedComponentNames(); }); }); - - _importsToRelatedDocumentsLazy = new Lazy>>(() => - { - var importsToRelatedDocuments = ImmutableDictionary.Create>(FilePathNormalizingComparer.Instance); - foreach (var documentFilePath in DocumentFilePaths) - { - var importTargetPaths = ProjectState.GetImportDocumentTargetPaths(documentFilePath, FileKinds.GetFileKindFromFilePath(documentFilePath), _lazyProjectEngine.Value); - importsToRelatedDocuments = ProjectState.AddToImportsToRelatedDocuments(importsToRelatedDocuments, documentFilePath, importTargetPaths); - } - - return importsToRelatedDocuments; - }); } public RazorConfiguration Configuration => throw new InvalidOperationException("Should not be called for cohosted projects."); @@ -154,28 +140,6 @@ internal class RemoteProjectSnapshot : IProjectSnapshot /// internal RazorProjectEngine GetProjectEngine_CohostOnly() => _lazyProjectEngine.Value; - public ImmutableArray GetRelatedDocuments(IDocumentSnapshot document) - { - var targetPath = document.TargetPath.AssumeNotNull(); - - if (!_importsToRelatedDocumentsLazy.Value.TryGetValue(targetPath, out var relatedDocuments)) - { - return []; - } - - using var builder = new PooledArrayBuilder(relatedDocuments.Length); - - foreach (var relatedDocumentFilePath in relatedDocuments) - { - if (TryGetDocument(relatedDocumentFilePath, out var relatedDocument)) - { - builder.Add(relatedDocument); - } - } - - return builder.DrainToImmutable(); - } - private RazorConfiguration CreateRazorConfiguration() { // See RazorSourceGenerator.RazorProviders.cs diff --git a/src/Razor/src/Microsoft.VisualStudio.LegacyEditor.Razor/EphemeralProjectSnapshot.cs b/src/Razor/src/Microsoft.VisualStudio.LegacyEditor.Razor/EphemeralProjectSnapshot.cs index 16b4140836..ebc897de40 100644 --- a/src/Razor/src/Microsoft.VisualStudio.LegacyEditor.Razor/EphemeralProjectSnapshot.cs +++ b/src/Razor/src/Microsoft.VisualStudio.LegacyEditor.Razor/EphemeralProjectSnapshot.cs @@ -72,16 +72,6 @@ internal class EphemeralProjectSnapshot : IProjectSnapshot return false; } - public ImmutableArray GetRelatedDocuments(IDocumentSnapshot document) - { - if (document is null) - { - throw new ArgumentNullException(nameof(document)); - } - - return ImmutableArray.Empty; - } - public RazorProjectEngine GetProjectEngine() { return _projectEngine.Value; diff --git a/src/Razor/test/Microsoft.VisualStudio.LegacyEditor.Razor.Test/VisualStudioDocumentTrackerTest.cs b/src/Razor/test/Microsoft.VisualStudio.LegacyEditor.Razor.Test/VisualStudioDocumentTrackerTest.cs index ea936cb99b..1ce0405fbd 100644 --- a/src/Razor/test/Microsoft.VisualStudio.LegacyEditor.Razor.Test/VisualStudioDocumentTrackerTest.cs +++ b/src/Razor/test/Microsoft.VisualStudio.LegacyEditor.Razor.Test/VisualStudioDocumentTrackerTest.cs @@ -161,7 +161,8 @@ public class VisualStudioDocumentTrackerTest : VisualStudioWorkspaceTestBase updater.ProjectAdded(_hostProject); }); - var e = new ProjectChangeEventArgs(null!, _projectManager.GetLoadedProject(_hostProject.Key), ProjectChangeKind.ProjectAdded); + var projectSnapshot = (ProjectSnapshot)_projectManager.GetLoadedProject(_hostProject.Key); + var e = new ProjectChangeEventArgs(null!, projectSnapshot, ProjectChangeKind.ProjectAdded); var called = false; _documentTracker.ContextChanged += (sender, args) => @@ -187,7 +188,8 @@ public class VisualStudioDocumentTrackerTest : VisualStudioWorkspaceTestBase updater.ProjectAdded(_hostProject); }); - var e = new ProjectChangeEventArgs(null!, _projectManager.GetLoadedProject(_hostProject.Key), ProjectChangeKind.ProjectChanged); + var projectSnapshot = (ProjectSnapshot)_projectManager.GetLoadedProject(_hostProject.Key); + var e = new ProjectChangeEventArgs(null!, projectSnapshot, ProjectChangeKind.ProjectChanged); var called = false; _documentTracker.ContextChanged += (sender, args) => @@ -213,7 +215,7 @@ public class VisualStudioDocumentTrackerTest : VisualStudioWorkspaceTestBase updater.ProjectAdded(_hostProject); }); - var project = _projectManager.GetLoadedProject(_hostProject.Key); + var project = (ProjectSnapshot)_projectManager.GetLoadedProject(_hostProject.Key); await _projectManager.UpdateAsync(updater => { @@ -247,7 +249,8 @@ public class VisualStudioDocumentTrackerTest : VisualStudioWorkspaceTestBase updater.ProjectAdded(_otherHostProject); }); - var e = new ProjectChangeEventArgs(null!, _projectManager.GetLoadedProject(_otherHostProject.Key), ProjectChangeKind.ProjectChanged); + var projectSnapshot = (ProjectSnapshot)_projectManager.GetLoadedProject(_otherHostProject.Key); + var e = new ProjectChangeEventArgs(null!, projectSnapshot, ProjectChangeKind.ProjectChanged); var called = false; _documentTracker.ContextChanged += (sender, args) => called = true; From 42cc39e4a95bc135b06f83b9fd3e08367247ec96 Mon Sep 17 00:00:00 2001 From: David Wengier Date: Thu, 5 Sep 2024 08:34:32 +1000 Subject: [PATCH 3/3] PR Feedback --- .../ProjectSystem/IProjectSnapshotExtensions.cs | 11 +++++++++++ .../ProjectSystem/ProjectChangeEventArgs.cs | 10 +++++----- .../ProjectSystem/ProjectSnapshotManager.Entry.cs | 4 ++-- .../ProjectSystem/ProjectSnapshotManager.cs | 6 +++--- .../VisualStudioDocumentTrackerTest.cs | 11 ++++------- 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IProjectSnapshotExtensions.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IProjectSnapshotExtensions.cs index 3d48ba9d84..4735509191 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IProjectSnapshotExtensions.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IProjectSnapshotExtensions.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT license. See License.txt in the project root for license information. +using System; using System.Collections.Immutable; using System.Diagnostics; using System.Threading; @@ -50,4 +51,14 @@ internal static class IProjectSnapshotExtensions return tagHelperTask.Result; #pragma warning restore VSTHRD002 // Avoid problematic synchronous waits } + + public static ImmutableArray GetRelatedDocuments(this IProjectSnapshot projectSnapshot, IDocumentSnapshot document) + { + if (projectSnapshot is not ProjectSnapshot project) + { + throw new InvalidOperationException("This method can only be called with a ProjectSnapshot."); + } + + return project.GetRelatedDocuments(document); + } } diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectChangeEventArgs.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectChangeEventArgs.cs index be4ce51e13..9d2843994e 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectChangeEventArgs.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectChangeEventArgs.cs @@ -8,12 +8,12 @@ namespace Microsoft.CodeAnalysis.Razor.ProjectSystem; internal class ProjectChangeEventArgs : EventArgs { - public ProjectChangeEventArgs(ProjectSnapshot older, ProjectSnapshot newer, ProjectChangeKind kind) + public ProjectChangeEventArgs(IProjectSnapshot older, IProjectSnapshot newer, ProjectChangeKind kind) : this(older, newer, null, kind, false) { } - public ProjectChangeEventArgs(ProjectSnapshot? older, ProjectSnapshot? newer, string? documentFilePath, ProjectChangeKind kind, bool solutionIsClosing) + public ProjectChangeEventArgs(IProjectSnapshot? older, IProjectSnapshot? newer, string? documentFilePath, ProjectChangeKind kind, bool solutionIsClosing) { if (older is null && newer is null) { @@ -29,9 +29,9 @@ internal class ProjectChangeEventArgs : EventArgs ProjectKey = (older ?? newer)!.Key; } - public ProjectSnapshot? Older { get; } + public IProjectSnapshot? Older { get; } - public ProjectSnapshot? Newer { get; } + public IProjectSnapshot? Newer { get; } public ProjectKey ProjectKey { get; } @@ -43,6 +43,6 @@ internal class ProjectChangeEventArgs : EventArgs public bool SolutionIsClosing { get; } - public static ProjectChangeEventArgs CreateTestInstance(ProjectSnapshot older, ProjectSnapshot newer, string documentFilePath, ProjectChangeKind kind, bool solutionIsClosing = false) => + public static ProjectChangeEventArgs CreateTestInstance(IProjectSnapshot older, IProjectSnapshot newer, string documentFilePath, ProjectChangeKind kind, bool solutionIsClosing = false) => new(older, newer, documentFilePath, kind, solutionIsClosing); } diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.Entry.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.Entry.cs index c02de7a1ba..48341f3dd5 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.Entry.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.Entry.cs @@ -7,9 +7,9 @@ internal partial class ProjectSnapshotManager { private sealed record Entry(ProjectState State) { - private ProjectSnapshot? _snapshotUnsafe; + private IProjectSnapshot? _snapshotUnsafe; - public ProjectSnapshot GetSnapshot() + public IProjectSnapshot GetSnapshot() { return _snapshotUnsafe ??= new ProjectSnapshot(State); } diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.cs index 96c5e4b8d6..40f7d96740 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshotManager.cs @@ -383,7 +383,7 @@ internal partial class ProjectSnapshotManager : IProjectSnapshotManager, IDispos } } - private void NotifyListeners(ProjectSnapshot? older, ProjectSnapshot? newer, string? documentFilePath, ProjectChangeKind kind) + private void NotifyListeners(IProjectSnapshot? older, IProjectSnapshot? newer, string? documentFilePath, ProjectChangeKind kind) { if (!_initialized) { @@ -421,8 +421,8 @@ internal partial class ProjectSnapshotManager : IProjectSnapshotManager, IDispos ProjectKey projectKey, string? documentFilePath, IUpdateProjectAction action, - [NotNullWhen(true)] out ProjectSnapshot? oldSnapshot, - [NotNullWhen(true)] out ProjectSnapshot? newSnapshot) + [NotNullWhen(true)] out IProjectSnapshot? oldSnapshot, + [NotNullWhen(true)] out IProjectSnapshot? newSnapshot) { using var upgradeableLock = _readerWriterLock.DisposableUpgradeableRead(); diff --git a/src/Razor/test/Microsoft.VisualStudio.LegacyEditor.Razor.Test/VisualStudioDocumentTrackerTest.cs b/src/Razor/test/Microsoft.VisualStudio.LegacyEditor.Razor.Test/VisualStudioDocumentTrackerTest.cs index 1ce0405fbd..ea936cb99b 100644 --- a/src/Razor/test/Microsoft.VisualStudio.LegacyEditor.Razor.Test/VisualStudioDocumentTrackerTest.cs +++ b/src/Razor/test/Microsoft.VisualStudio.LegacyEditor.Razor.Test/VisualStudioDocumentTrackerTest.cs @@ -161,8 +161,7 @@ public class VisualStudioDocumentTrackerTest : VisualStudioWorkspaceTestBase updater.ProjectAdded(_hostProject); }); - var projectSnapshot = (ProjectSnapshot)_projectManager.GetLoadedProject(_hostProject.Key); - var e = new ProjectChangeEventArgs(null!, projectSnapshot, ProjectChangeKind.ProjectAdded); + var e = new ProjectChangeEventArgs(null!, _projectManager.GetLoadedProject(_hostProject.Key), ProjectChangeKind.ProjectAdded); var called = false; _documentTracker.ContextChanged += (sender, args) => @@ -188,8 +187,7 @@ public class VisualStudioDocumentTrackerTest : VisualStudioWorkspaceTestBase updater.ProjectAdded(_hostProject); }); - var projectSnapshot = (ProjectSnapshot)_projectManager.GetLoadedProject(_hostProject.Key); - var e = new ProjectChangeEventArgs(null!, projectSnapshot, ProjectChangeKind.ProjectChanged); + var e = new ProjectChangeEventArgs(null!, _projectManager.GetLoadedProject(_hostProject.Key), ProjectChangeKind.ProjectChanged); var called = false; _documentTracker.ContextChanged += (sender, args) => @@ -215,7 +213,7 @@ public class VisualStudioDocumentTrackerTest : VisualStudioWorkspaceTestBase updater.ProjectAdded(_hostProject); }); - var project = (ProjectSnapshot)_projectManager.GetLoadedProject(_hostProject.Key); + var project = _projectManager.GetLoadedProject(_hostProject.Key); await _projectManager.UpdateAsync(updater => { @@ -249,8 +247,7 @@ public class VisualStudioDocumentTrackerTest : VisualStudioWorkspaceTestBase updater.ProjectAdded(_otherHostProject); }); - var projectSnapshot = (ProjectSnapshot)_projectManager.GetLoadedProject(_otherHostProject.Key); - var e = new ProjectChangeEventArgs(null!, projectSnapshot, ProjectChangeKind.ProjectChanged); + var e = new ProjectChangeEventArgs(null!, _projectManager.GetLoadedProject(_otherHostProject.Key), ProjectChangeKind.ProjectChanged); var called = false; _documentTracker.ContextChanged += (sender, args) => called = true;