From e4dca37173194ed8e02f54be53a0e868169210ca Mon Sep 17 00:00:00 2001 From: Dustin Campbell Date: Thu, 7 Nov 2024 08:43:11 -0800 Subject: [PATCH 1/7] Make method private The comment explaining why this DocumentState method is internal seems to be out of date. It's currently only used within DocumentState, so this change removes the comment and makes it private. --- .../ProjectSystem/DocumentState.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs index 129bc593aa4..41e4826b1bd 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs @@ -193,8 +193,7 @@ public virtual DocumentState WithTextLoader(TextLoader textLoader) return new DocumentState(HostDocument, Version + 1, textAndVersion: null, textLoader); } - // Internal, because we are temporarily sharing code with CohostDocumentSnapshot - internal static ImmutableArray GetImportsCore(IProjectSnapshot project, RazorProjectEngine projectEngine, string filePath, string fileKind) + private static ImmutableArray GetImportsCore(IProjectSnapshot project, RazorProjectEngine projectEngine, string filePath, string fileKind) { var projectItem = projectEngine.FileSystem.GetItem(filePath, fileKind); From c6ab8196a0ed816108854ebca3908aad95fca058 Mon Sep 17 00:00:00 2001 From: Dustin Campbell Date: Thu, 7 Nov 2024 09:58:49 -0800 Subject: [PATCH 2/7] Clean and refactor code within GenerateCodeDocumentAsync a bit --- .../ProjectSystem/DocumentState.cs | 57 ++++++++++++------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs index 41e4826b1bd..121ce425507 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs @@ -244,24 +244,12 @@ internal static async Task GenerateCodeDocumentAsync( bool forceRuntimeCodeGeneration, CancellationToken cancellationToken) { - // OK we have to generate the code. - using var importSources = new PooledArrayBuilder(imports.Length); - foreach (var item in imports) - { - var importProjectItem = item.FilePath is null ? null : projectEngine.FileSystem.GetItem(item.FilePath, item.FileKind); - var sourceDocument = await GetRazorSourceDocumentAsync(item.Document, importProjectItem, cancellationToken).ConfigureAwait(false); - importSources.Add(sourceDocument); - } - - var projectItem = document.FilePath is null ? null : projectEngine.FileSystem.GetItem(document.FilePath, document.FileKind); - var documentSource = await GetRazorSourceDocumentAsync(document, projectItem, cancellationToken).ConfigureAwait(false); + var importSources = await GetImportSourcesAsync(imports, projectEngine, cancellationToken).ConfigureAwait(false); + var source = await GetSourceAsync(document, projectEngine, cancellationToken).ConfigureAwait(false); - if (forceRuntimeCodeGeneration) - { - return projectEngine.Process(documentSource, fileKind: document.FileKind, importSources.DrainToImmutable(), tagHelpers); - } - - return projectEngine.ProcessDesignTime(documentSource, fileKind: document.FileKind, importSources.DrainToImmutable(), tagHelpers); + return forceRuntimeCodeGeneration + ? projectEngine.Process(source, fileKind: document.FileKind, importSources, tagHelpers) + : projectEngine.ProcessDesignTime(source, fileKind: document.FileKind, importSources, tagHelpers); } internal static async Task> GetImportsAsync(IDocumentSnapshot document, RazorProjectEngine projectEngine, CancellationToken cancellationToken) @@ -278,12 +266,37 @@ internal static async Task> GetImportsAsync(IDocument return result.DrainToImmutable(); } - private static async Task GetRazorSourceDocumentAsync( - IDocumentSnapshot document, - RazorProjectItem? projectItem, + private static async Task> GetImportSourcesAsync( + ImmutableArray importItems, + RazorProjectEngine projectEngine, CancellationToken cancellationToken) { - var sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); - return RazorSourceDocument.Create(sourceText, RazorSourceDocumentProperties.Create(document.FilePath, projectItem?.RelativePhysicalPath)); + using var importSources = new PooledArrayBuilder(importItems.Length); + + foreach (var importItem in importItems) + { + var importProjectItem = importItem is { FilePath: string filePath, FileKind: var fileKind } + ? projectEngine.FileSystem.GetItem(filePath, fileKind) + : null; + + var text = await importItem.Document.GetTextAsync(cancellationToken).ConfigureAwait(false); + var properties = RazorSourceDocumentProperties.Create(importItem.FilePath, importProjectItem?.RelativePhysicalPath); + var importSource = RazorSourceDocument.Create(text, properties); + + importSources.Add(importSource); + } + + return importSources.DrainToImmutable(); + } + + private static async Task GetSourceAsync(IDocumentSnapshot document, RazorProjectEngine projectEngine, CancellationToken cancellationToken) + { + var projectItem = document is { FilePath: string filePath, FileKind: var fileKind } + ? projectEngine.FileSystem.GetItem(filePath, fileKind) + : null; + + var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); + var properties = RazorSourceDocumentProperties.Create(document.FilePath, projectItem?.RelativePhysicalPath); + return RazorSourceDocument.Create(text, properties); } } From bd735d380ee45f237dc60eff900d2136b3cfc4a0 Mon Sep 17 00:00:00 2001 From: Dustin Campbell Date: Thu, 7 Nov 2024 10:25:25 -0800 Subject: [PATCH 3/7] Don't produce intermediary array of document snapshots When computing the ImportItems for a given document, we shouldn't need to compute an intermediary array of document snapshots. --- .../DocumentState.ComputedStateTracker.cs | 6 +- .../ProjectSystem/DocumentState.ImportItem.cs | 12 ++- .../ProjectSystem/DocumentState.cs | 92 +++++++++---------- 3 files changed, 56 insertions(+), 54 deletions(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.ComputedStateTracker.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.ComputedStateTracker.cs index ffbd02c1127..caedcf0bd5e 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.ComputedStateTracker.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.ComputedStateTracker.cs @@ -186,7 +186,7 @@ static void PropagateToTaskCompletionSource( var configurationVersion = project.ConfigurationVersion; var projectWorkspaceStateVersion = project.ProjectWorkspaceStateVersion; var documentCollectionVersion = project.DocumentCollectionVersion; - var imports = await GetImportsAsync(document, project.GetProjectEngine(), cancellationToken).ConfigureAwait(false); + var importItems = await GetImportsAsync(document, project.GetProjectEngine(), cancellationToken).ConfigureAwait(false); var documentVersion = await document.GetTextVersionAsync(cancellationToken).ConfigureAwait(false); // OK now that have the previous output and all of the versions, we can see if anything @@ -207,7 +207,7 @@ static void PropagateToTaskCompletionSource( inputVersion = documentCollectionVersion; } - foreach (var import in imports) + foreach (var import in importItems) { var importVersion = import.Version; if (inputVersion.GetNewerVersion(importVersion) == importVersion) @@ -234,7 +234,7 @@ static void PropagateToTaskCompletionSource( var tagHelpers = await project.GetTagHelpersAsync(cancellationToken).ConfigureAwait(false); var forceRuntimeCodeGeneration = project.LanguageServerFeatureOptions.ForceRuntimeCodeGeneration; - var codeDocument = await GenerateCodeDocumentAsync(document, project.GetProjectEngine(), imports, tagHelpers, forceRuntimeCodeGeneration, cancellationToken).ConfigureAwait(false); + var codeDocument = await GenerateCodeDocumentAsync(document, project.GetProjectEngine(), importItems, tagHelpers, forceRuntimeCodeGeneration, cancellationToken).ConfigureAwait(false); return (codeDocument, inputVersion); } diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.ImportItem.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.ImportItem.cs index a7b37f733bd..683c14426a9 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.ImportItem.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.ImportItem.cs @@ -1,12 +1,20 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT license. See License.txt in the project root for license information. +using Microsoft.CodeAnalysis.Text; + namespace Microsoft.CodeAnalysis.Razor.ProjectSystem; internal partial class DocumentState { - internal record struct ImportItem(string? FilePath, VersionStamp Version, IDocumentSnapshot Document) + internal readonly record struct ImportItem( + string? FilePath, + string? FileKind, + SourceText Text, + VersionStamp Version) { - public readonly string? FileKind => Document.FileKind; + // Note: The default import does not have file path, file kind, or version stamp. + public static ImportItem CreateDefault(SourceText text) + => new(FilePath: null, FileKind: null, text, Version: default); } } diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs index 121ce425507..b077fca1697 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs @@ -193,83 +193,78 @@ public virtual DocumentState WithTextLoader(TextLoader textLoader) return new DocumentState(HostDocument, Version + 1, textAndVersion: null, textLoader); } - private static ImmutableArray GetImportsCore(IProjectSnapshot project, RazorProjectEngine projectEngine, string filePath, string fileKind) + internal static async Task GenerateCodeDocumentAsync( + IDocumentSnapshot document, + RazorProjectEngine projectEngine, + ImmutableArray imports, + ImmutableArray tagHelpers, + bool forceRuntimeCodeGeneration, + CancellationToken cancellationToken) { + var importSources = GetImportSources(imports, projectEngine); + var source = await GetSourceAsync(document, projectEngine, cancellationToken).ConfigureAwait(false); + + return forceRuntimeCodeGeneration + ? projectEngine.Process(source, fileKind: document.FileKind, importSources, tagHelpers) + : projectEngine.ProcessDesignTime(source, fileKind: document.FileKind, importSources, tagHelpers); + } + + internal static async Task> GetImportsAsync(IDocumentSnapshot document, RazorProjectEngine projectEngine, CancellationToken cancellationToken) + { + var filePath = document.FilePath.AssumeNotNull(); + var fileKind = document.FileKind.AssumeNotNull(); + var projectItem = projectEngine.FileSystem.GetItem(filePath, fileKind); - using var importItems = new PooledArrayBuilder(); + using var importProjectItems = new PooledArrayBuilder(); foreach (var feature in projectEngine.ProjectFeatures.OfType()) { if (feature.GetImports(projectItem) is { } featureImports) { - importItems.AddRange(featureImports); + importProjectItems.AddRange(featureImports); } } - if (importItems.Count == 0) + if (importProjectItems.Count == 0) { return []; } - using var imports = new PooledArrayBuilder(capacity: importItems.Count); + var project = document.Project; + + using var importItems = new PooledArrayBuilder(capacity: importProjectItems.Count); - foreach (var item in importItems) + foreach (var importProjectItem in importProjectItems) { - if (item is NotFoundProjectItem) + if (importProjectItem is NotFoundProjectItem) { continue; } - if (item.PhysicalPath is null) + if (importProjectItem.PhysicalPath is null) { // This is a default import. - var defaultImport = new ImportDocumentSnapshot(project, item); - imports.Add(defaultImport); + using var stream = importProjectItem.Read(); + var text = SourceText.From(stream); + var defaultImport = ImportItem.CreateDefault(text); + + importItems.Add(defaultImport); } - else if (project.TryGetDocument(item.PhysicalPath, out var import)) + else if (project.TryGetDocument(importProjectItem.PhysicalPath, out var importDocument)) { - imports.Add(import); - } - } - - return imports.DrainToImmutable(); - } + var text = await importDocument.GetTextAsync(cancellationToken).ConfigureAwait(false); + var versionStamp = await importDocument.GetTextVersionAsync(cancellationToken).ConfigureAwait(false); + var importItem = new ImportItem(importDocument.FilePath, importDocument.FileKind, text, versionStamp); - internal static async Task GenerateCodeDocumentAsync( - IDocumentSnapshot document, - RazorProjectEngine projectEngine, - ImmutableArray imports, - ImmutableArray tagHelpers, - bool forceRuntimeCodeGeneration, - CancellationToken cancellationToken) - { - var importSources = await GetImportSourcesAsync(imports, projectEngine, cancellationToken).ConfigureAwait(false); - var source = await GetSourceAsync(document, projectEngine, cancellationToken).ConfigureAwait(false); - - return forceRuntimeCodeGeneration - ? projectEngine.Process(source, fileKind: document.FileKind, importSources, tagHelpers) - : projectEngine.ProcessDesignTime(source, fileKind: document.FileKind, importSources, tagHelpers); - } - - internal static async Task> GetImportsAsync(IDocumentSnapshot document, RazorProjectEngine projectEngine, CancellationToken cancellationToken) - { - var imports = GetImportsCore(document.Project, projectEngine, document.FilePath.AssumeNotNull(), document.FileKind.AssumeNotNull()); - using var result = new PooledArrayBuilder(imports.Length); - - foreach (var snapshot in imports) - { - var versionStamp = await snapshot.GetTextVersionAsync(cancellationToken).ConfigureAwait(false); - result.Add(new ImportItem(snapshot.FilePath, versionStamp, snapshot)); + importItems.Add(importItem); + } } - return result.DrainToImmutable(); + return importItems.DrainToImmutable(); } - private static async Task> GetImportSourcesAsync( - ImmutableArray importItems, - RazorProjectEngine projectEngine, - CancellationToken cancellationToken) + private static ImmutableArray GetImportSources(ImmutableArray importItems, RazorProjectEngine projectEngine) { using var importSources = new PooledArrayBuilder(importItems.Length); @@ -279,9 +274,8 @@ private static async Task> GetImportSourcesA ? projectEngine.FileSystem.GetItem(filePath, fileKind) : null; - var text = await importItem.Document.GetTextAsync(cancellationToken).ConfigureAwait(false); var properties = RazorSourceDocumentProperties.Create(importItem.FilePath, importProjectItem?.RelativePhysicalPath); - var importSource = RazorSourceDocument.Create(text, properties); + var importSource = RazorSourceDocument.Create(importItem.Text, properties); importSources.Add(importSource); } From ff8a76077cafcdad09e7a1454f52e4f82d265e97 Mon Sep 17 00:00:00 2001 From: Dustin Campbell Date: Thu, 7 Nov 2024 10:43:47 -0800 Subject: [PATCH 4/7] Don't expose DocumentState.GetImportsAsync This change removes several parameters from DocumentState.GenerateCodeDocumentAsync(...) and computes them internally. So, there's no longer a need to expose GetImportsAsync for GenerateCodeDocumetnAsync callers. --- .../ProjectSystem/DocumentSnapshot.cs | 11 ++--------- .../DocumentState.ComputedStateTracker.cs | 5 ++--- .../ProjectSystem/DocumentState.cs | 16 ++++++++++++++-- .../ProjectSystem/RemoteDocumentSnapshot.cs | 4 +--- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentSnapshot.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentSnapshot.cs index a69c96dfbc0..6943e4badc7 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentSnapshot.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentSnapshot.cs @@ -82,15 +82,8 @@ public async ValueTask GetGeneratedOutputAsync(bool forceDesi return output; } - private async Task GetDesignTimeGeneratedOutputAsync(CancellationToken cancellationToken) - { - var tagHelpers = await Project.GetTagHelpersAsync(cancellationToken).ConfigureAwait(false); - var projectEngine = Project.GetProjectEngine(); - var imports = await DocumentState.GetImportsAsync(this, projectEngine, cancellationToken).ConfigureAwait(false); - return await DocumentState - .GenerateCodeDocumentAsync(this, projectEngine, imports, tagHelpers, forceRuntimeCodeGeneration: false, cancellationToken) - .ConfigureAwait(false); - } + private Task GetDesignTimeGeneratedOutputAsync(CancellationToken cancellationToken) + => DocumentState.GenerateCodeDocumentAsync(this, Project.GetProjectEngine(), forceRuntimeCodeGeneration: false, cancellationToken); /// /// Retrieves a cached Roslyn from the generated C# document. diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.ComputedStateTracker.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.ComputedStateTracker.cs index caedcf0bd5e..4b74b384683 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.ComputedStateTracker.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.ComputedStateTracker.cs @@ -186,7 +186,7 @@ static void PropagateToTaskCompletionSource( var configurationVersion = project.ConfigurationVersion; var projectWorkspaceStateVersion = project.ProjectWorkspaceStateVersion; var documentCollectionVersion = project.DocumentCollectionVersion; - var importItems = await GetImportsAsync(document, project.GetProjectEngine(), cancellationToken).ConfigureAwait(false); + var importItems = await GetImportItemsAsync(document, project.GetProjectEngine(), cancellationToken).ConfigureAwait(false); var documentVersion = await document.GetTextVersionAsync(cancellationToken).ConfigureAwait(false); // OK now that have the previous output and all of the versions, we can see if anything @@ -232,9 +232,8 @@ static void PropagateToTaskCompletionSource( } } - var tagHelpers = await project.GetTagHelpersAsync(cancellationToken).ConfigureAwait(false); var forceRuntimeCodeGeneration = project.LanguageServerFeatureOptions.ForceRuntimeCodeGeneration; - var codeDocument = await GenerateCodeDocumentAsync(document, project.GetProjectEngine(), importItems, tagHelpers, forceRuntimeCodeGeneration, cancellationToken).ConfigureAwait(false); + var codeDocument = await GenerateCodeDocumentAsync(document, project.GetProjectEngine(), importItems, forceRuntimeCodeGeneration, cancellationToken).ConfigureAwait(false); return (codeDocument, inputVersion); } diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs index b077fca1697..7ed858571d7 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs @@ -194,14 +194,26 @@ public virtual DocumentState WithTextLoader(TextLoader textLoader) } internal static async Task GenerateCodeDocumentAsync( + IDocumentSnapshot document, + RazorProjectEngine projectEngine, + bool forceRuntimeCodeGeneration, + CancellationToken cancellationToken) + { + var importItems = await GetImportItemsAsync(document, projectEngine, cancellationToken).ConfigureAwait(false); + + return await GenerateCodeDocumentAsync( + document, projectEngine, importItems, forceRuntimeCodeGeneration, cancellationToken).ConfigureAwait(false); + } + + private static async Task GenerateCodeDocumentAsync( IDocumentSnapshot document, RazorProjectEngine projectEngine, ImmutableArray imports, - ImmutableArray tagHelpers, bool forceRuntimeCodeGeneration, CancellationToken cancellationToken) { var importSources = GetImportSources(imports, projectEngine); + var tagHelpers = await document.Project.GetTagHelpersAsync(cancellationToken).ConfigureAwait(false); var source = await GetSourceAsync(document, projectEngine, cancellationToken).ConfigureAwait(false); return forceRuntimeCodeGeneration @@ -209,7 +221,7 @@ internal static async Task GenerateCodeDocumentAsync( : projectEngine.ProcessDesignTime(source, fileKind: document.FileKind, importSources, tagHelpers); } - internal static async Task> GetImportsAsync(IDocumentSnapshot document, RazorProjectEngine projectEngine, CancellationToken cancellationToken) + private static async Task> GetImportItemsAsync(IDocumentSnapshot document, RazorProjectEngine projectEngine, CancellationToken cancellationToken) { var filePath = document.FilePath.AssumeNotNull(); var fileKind = document.FileKind.AssumeNotNull(); diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteDocumentSnapshot.cs b/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteDocumentSnapshot.cs index 39f2e9d2830..aaebd5df650 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteDocumentSnapshot.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteDocumentSnapshot.cs @@ -99,11 +99,9 @@ async Task GetRazorCodeDocumentAsync(bool forceRuntimeCodeGen // but since we don't expect users to ever use cohosting without source generators, it's fine for now. var projectEngine = await ProjectSnapshot.GetProjectEngine_CohostOnlyAsync(cancellationToken).ConfigureAwait(false); - var tagHelpers = await ProjectSnapshot.GetTagHelpersAsync(cancellationToken).ConfigureAwait(false); - var imports = await DocumentState.GetImportsAsync(this, projectEngine, cancellationToken).ConfigureAwait(false); return await DocumentState - .GenerateCodeDocumentAsync(this, projectEngine, imports, tagHelpers, forceRuntimeCodeGeneration, cancellationToken) + .GenerateCodeDocumentAsync(this, projectEngine, forceRuntimeCodeGeneration, cancellationToken) .ConfigureAwait(false); } } From f6761de0cd5f13977dcd1ee0b34e68ea5cd3829c Mon Sep 17 00:00:00 2001 From: Dustin Campbell Date: Thu, 7 Nov 2024 10:51:17 -0800 Subject: [PATCH 5/7] Make DocumentState.ImportItem private --- .../ProjectSystem/DocumentState.ImportItem.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.ImportItem.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.ImportItem.cs index 683c14426a9..73cca9ac7d1 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.ImportItem.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.ImportItem.cs @@ -7,7 +7,7 @@ namespace Microsoft.CodeAnalysis.Razor.ProjectSystem; internal partial class DocumentState { - internal readonly record struct ImportItem( + private readonly record struct ImportItem( string? FilePath, string? FileKind, SourceText Text, From 8de0cf0002de7e43c91df21393e1ac8e8584024e Mon Sep 17 00:00:00 2001 From: Dustin Campbell Date: Thu, 7 Nov 2024 10:53:17 -0800 Subject: [PATCH 6/7] Delete ImportDocumentSnapshot now that it is unused --- .../ProjectSystem/ImportDocumentSnapshot.cs | 72 ------------------- 1 file changed, 72 deletions(-) delete mode 100644 src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ImportDocumentSnapshot.cs diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ImportDocumentSnapshot.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ImportDocumentSnapshot.cs deleted file mode 100644 index ae94a5e8026..00000000000 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ImportDocumentSnapshot.cs +++ /dev/null @@ -1,72 +0,0 @@ -// 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.Diagnostics.CodeAnalysis; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Razor; -using Microsoft.AspNetCore.Razor.Language; -using Microsoft.CodeAnalysis.Text; - -namespace Microsoft.CodeAnalysis.Razor.ProjectSystem; - -internal sealed class ImportDocumentSnapshot(IProjectSnapshot project, RazorProjectItem item) : IDocumentSnapshot -{ - public IProjectSnapshot Project { get; } = project; - - private readonly RazorProjectItem _importItem = item; - private SourceText? _text; - - // The default import file does not have a kind or paths. - public string? FileKind => null; - public string? FilePath => null; - public string? TargetPath => null; - - public int Version => 1; - - public ValueTask GetTextAsync(CancellationToken cancellationToken) - { - return TryGetText(out var text) - ? new(text) - : ReadTextAsync(); - - ValueTask ReadTextAsync() - { - using var stream = _importItem.Read(); - var sourceText = SourceText.From(stream); - - var result = _text ??= InterlockedOperations.Initialize(ref _text, sourceText); - return new(result); - } - } - - public ValueTask GetGeneratedOutputAsync( - bool forceDesignTimeGeneratedOutput, - CancellationToken cancellationToken) - => throw new NotSupportedException(); - - public ValueTask GetTextVersionAsync(CancellationToken cancellationToken) - => new(VersionStamp.Default); - - public bool TryGetText([NotNullWhen(true)] out SourceText? result) - { - result = _text; - return result is not null; - } - - public bool TryGetTextVersion(out VersionStamp result) - { - result = VersionStamp.Default; - return true; - } - - public bool TryGetGeneratedOutput([NotNullWhen(true)] out RazorCodeDocument? result) - => throw new NotSupportedException(); - - public IDocumentSnapshot WithText(SourceText text) - => throw new NotSupportedException(); - - public ValueTask GetCSharpSyntaxTreeAsync(CancellationToken cancellationToken) - => throw new NotSupportedException(); -} From b31ace04efc21ee77757bc2afdddd90ab15da85e Mon Sep 17 00:00:00 2001 From: Dustin Campbell Date: Thu, 7 Nov 2024 11:05:08 -0800 Subject: [PATCH 7/7] Fix nullability of IDocumentSnapshot For a long while IDocumentSnapshot's FileKind, FilePath, and TargetPath properties have all been annotated as nullable. It turns out that the only reason for this was ImportDocumentSnapshot, which has been removed. So, we can now allow these properties to be correctly annotated as non-nullable, --- .../CodeDocumentReferenceHolder.cs | 2 +- .../RazorDiagnosticsPublisher.Comparer.cs | 5 +---- .../Diagnostics/RazorDiagnosticsPublisher.cs | 2 +- .../GeneratedDocumentSynchronizer.cs | 6 +++--- .../OpenDocumentGenerator.Comparer.cs | 2 +- .../OpenDocumentGenerator.cs | 4 ++-- .../AbstractRazorComponentDefinitionService.cs | 3 +-- .../ProjectSystem/DocumentContext.cs | 4 ++-- .../ProjectSystem/DocumentState.cs | 9 +++------ .../ProjectSystem/Extensions.cs | 3 +-- .../ProjectSystem/IDocumentSnapshot.cs | 6 +++--- .../ProjectSystem/ProjectSnapshot.cs | 3 +-- .../Rename/RenameService.cs | 6 +++--- .../ProjectSystem/RemoteDocumentSnapshot.cs | 2 +- .../BackgroundDocumentGenerator.Comparer.cs | 7 +++---- .../DynamicFiles/BackgroundDocumentGenerator.cs | 4 ++-- .../DefaultDynamicDocumentContainer.cs | 2 +- .../WindowsRazorProjectHostBase.cs | 7 +++---- .../RazorDiagnosticsPublisherTest.cs | 17 ----------------- .../FormattingContentValidationPassTest.cs | 4 ++-- .../FormattingDiagnosticValidationPassTest.cs | 2 +- .../Formatting_NetFx/FormattingTestBase.cs | 6 +++--- .../ProjectSystem/TestDocumentSnapshot.cs | 6 +++--- .../BackgroundDocumentGeneratorTest.cs | 2 +- .../RazorDynamicFileInfoProviderTest.cs | 10 +++++----- 25 files changed, 48 insertions(+), 76 deletions(-) diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeDocumentReferenceHolder.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeDocumentReferenceHolder.cs index acbd8955067..00564573173 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeDocumentReferenceHolder.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeDocumentReferenceHolder.cs @@ -30,7 +30,7 @@ public void DocumentProcessed(RazorCodeDocument codeDocument, IDocumentSnapshot // multiple parses/regenerations across LSP requests that are all for the same document version. lock (_codeDocumentCache) { - var key = new DocumentKey(documentSnapshot.Project.Key, documentSnapshot.FilePath.AssumeNotNull()); + var key = new DocumentKey(documentSnapshot.Project.Key, documentSnapshot.FilePath); _codeDocumentCache[key] = codeDocument; } } diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Diagnostics/RazorDiagnosticsPublisher.Comparer.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Diagnostics/RazorDiagnosticsPublisher.Comparer.cs index 6235e88c958..32ce5e35948 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Diagnostics/RazorDiagnosticsPublisher.Comparer.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Diagnostics/RazorDiagnosticsPublisher.Comparer.cs @@ -26,9 +26,6 @@ public bool Equals(IDocumentSnapshot? x, IDocumentSnapshot? y) } public int GetHashCode(IDocumentSnapshot obj) - { - var filePath = obj.FilePath.AssumeNotNull(); - return FilePathComparer.Instance.GetHashCode(filePath); - } + => FilePathComparer.Instance.GetHashCode(obj.FilePath); } } diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Diagnostics/RazorDiagnosticsPublisher.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Diagnostics/RazorDiagnosticsPublisher.cs index 61925ca772e..8ba6aee4cfa 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Diagnostics/RazorDiagnosticsPublisher.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Diagnostics/RazorDiagnosticsPublisher.cs @@ -121,7 +121,7 @@ private async Task PublishDiagnosticsAsync(IDocumentSnapshot document, Cancellat lock (_publishedDiagnostics) { - var filePath = document.FilePath.AssumeNotNull(); + var filePath = document.FilePath; // See if these are the same diagnostics as last time. If so, we don't need to publish. if (_publishedDiagnostics.TryGetValue(filePath, out var previousDiagnostics)) diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/GeneratedDocumentSynchronizer.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/GeneratedDocumentSynchronizer.cs index 13bd64e2d54..cf0a255f0ff 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/GeneratedDocumentSynchronizer.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/GeneratedDocumentSynchronizer.cs @@ -19,10 +19,10 @@ internal class GeneratedDocumentSynchronizer( public void DocumentProcessed(RazorCodeDocument codeDocument, IDocumentSnapshot document) { var hostDocumentVersion = document.Version; - var filePath = document.FilePath.AssumeNotNull(); + var filePath = document.FilePath; // If the document isn't open, and we're not updating buffers for closed documents, then we don't need to do anything. - if (!_projectManager.IsDocumentOpen(document.FilePath) && + if (!_projectManager.IsDocumentOpen(filePath) && !_languageServerFeatureOptions.UpdateBuffersForClosedDocuments) { return; @@ -30,7 +30,7 @@ public void DocumentProcessed(RazorCodeDocument codeDocument, IDocumentSnapshot // If the document has been removed from the project, then don't do anything, or version numbers will be thrown off if (!_projectManager.TryGetLoadedProject(document.Project.Key, out var project) || - !project.ContainsDocument(document.FilePath)) + !project.ContainsDocument(filePath)) { return; } diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/OpenDocumentGenerator.Comparer.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/OpenDocumentGenerator.Comparer.cs index 142654381b7..b339ddfbc9a 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/OpenDocumentGenerator.Comparer.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/OpenDocumentGenerator.Comparer.cs @@ -37,7 +37,7 @@ public int GetHashCode(IDocumentSnapshot obj) { var hash = HashCodeCombiner.Start(); hash.Add(obj.Project.Key.Id, FilePathComparer.Instance); - hash.Add(obj.FileKind.AssumeNotNull(), FilePathComparer.Instance); + hash.Add(obj.FileKind, FilePathComparer.Instance); return hash.CombinedHash; } } diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/OpenDocumentGenerator.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/OpenDocumentGenerator.cs index 4923d948a15..dafdc7840fb 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/OpenDocumentGenerator.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/OpenDocumentGenerator.cs @@ -148,7 +148,7 @@ private void ProjectManager_Changed(object? sender, ProjectChangeEventArgs args) { foreach (var relatedDocument in oldProject.GetRelatedDocuments(document)) { - var relatedDocumentFilePath = relatedDocument.FilePath.AssumeNotNull(); + var relatedDocumentFilePath = relatedDocument.FilePath; if (newProject.TryGetDocument(relatedDocumentFilePath, out var newRelatedDocument)) { @@ -169,7 +169,7 @@ private void ProjectManager_Changed(object? sender, ProjectChangeEventArgs args) void EnqueueIfNecessary(IDocumentSnapshot document) { - if (!_projectManager.IsDocumentOpen(document.FilePath.AssumeNotNull()) && + if (!_projectManager.IsDocumentOpen(document.FilePath) && !_options.UpdateBuffersForClosedDocuments) { return; diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/GoToDefinition/AbstractRazorComponentDefinitionService.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/GoToDefinition/AbstractRazorComponentDefinitionService.cs index 486713db242..ccab0374023 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/GoToDefinition/AbstractRazorComponentDefinitionService.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/GoToDefinition/AbstractRazorComponentDefinitionService.cs @@ -3,7 +3,6 @@ using System.Threading; using System.Threading.Tasks; -using Microsoft.AspNetCore.Razor; using Microsoft.AspNetCore.Razor.Language; using Microsoft.CodeAnalysis.Razor.DocumentMapping; using Microsoft.CodeAnalysis.Razor.Logging; @@ -62,7 +61,7 @@ internal abstract class AbstractRazorComponentDefinitionService( return null; } - var componentFilePath = componentDocument.FilePath.AssumeNotNull(); + var componentFilePath = componentDocument.FilePath; _logger.LogInformation($"Definition found at file path: {componentFilePath}"); diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentContext.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentContext.cs index e9b24b02cbb..e5bc8836804 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentContext.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentContext.cs @@ -23,8 +23,8 @@ internal class DocumentContext(Uri uri, IDocumentSnapshot snapshot, VSProjectCon public Uri Uri { get; } = uri; public IDocumentSnapshot Snapshot { get; } = snapshot; - public string FilePath => Snapshot.FilePath.AssumeNotNull(); - public string FileKind => Snapshot.FileKind.AssumeNotNull(); + public string FilePath => Snapshot.FilePath; + public string FileKind => Snapshot.FileKind; public IProjectSnapshot Project => Snapshot.Project; public TextDocumentIdentifier GetTextDocumentIdentifier() diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs index 7ed858571d7..7c09f3bfc4e 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/DocumentState.cs @@ -217,16 +217,13 @@ private static async Task GenerateCodeDocumentAsync( var source = await GetSourceAsync(document, projectEngine, cancellationToken).ConfigureAwait(false); return forceRuntimeCodeGeneration - ? projectEngine.Process(source, fileKind: document.FileKind, importSources, tagHelpers) - : projectEngine.ProcessDesignTime(source, fileKind: document.FileKind, importSources, tagHelpers); + ? projectEngine.Process(source, document.FileKind, importSources, tagHelpers) + : projectEngine.ProcessDesignTime(source, document.FileKind, importSources, tagHelpers); } private static async Task> GetImportItemsAsync(IDocumentSnapshot document, RazorProjectEngine projectEngine, CancellationToken cancellationToken) { - var filePath = document.FilePath.AssumeNotNull(); - var fileKind = document.FileKind.AssumeNotNull(); - - var projectItem = projectEngine.FileSystem.GetItem(filePath, fileKind); + var projectItem = projectEngine.FileSystem.GetItem(document.FilePath, document.FileKind); using var importProjectItems = new PooledArrayBuilder(); diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/Extensions.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/Extensions.cs index 34b050477a0..bc90ad47731 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/Extensions.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/Extensions.cs @@ -6,7 +6,6 @@ #endif using System.Diagnostics; -using Microsoft.AspNetCore.Razor; using Microsoft.AspNetCore.Razor.ProjectSystem; using Microsoft.AspNetCore.Razor.Serialization; using Microsoft.AspNetCore.Razor.Utilities; @@ -16,7 +15,7 @@ namespace Microsoft.CodeAnalysis.Razor.ProjectSystem; internal static class Extensions { public static DocumentSnapshotHandle ToHandle(this IDocumentSnapshot snapshot) - => new(snapshot.FilePath.AssumeNotNull(), snapshot.TargetPath.AssumeNotNull(), snapshot.FileKind.AssumeNotNull()); + => new(snapshot.FilePath, snapshot.TargetPath, snapshot.FileKind); public static ProjectKey ToProjectKey(this Project project) { diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IDocumentSnapshot.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IDocumentSnapshot.cs index 82840ac3bd9..b99ccab0f3a 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IDocumentSnapshot.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IDocumentSnapshot.cs @@ -11,9 +11,9 @@ namespace Microsoft.CodeAnalysis.Razor.ProjectSystem; internal interface IDocumentSnapshot { - string? FileKind { get; } - string? FilePath { get; } - string? TargetPath { get; } + string FileKind { get; } + string FilePath { get; } + string TargetPath { get; } IProjectSnapshot Project { get; } int Version { get; } 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 01182938d7b..1ec202ec509 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshot.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshot.cs @@ -6,7 +6,6 @@ using System.Diagnostics.CodeAnalysis; 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.ProjectSystem; @@ -104,7 +103,7 @@ public bool TryGetDocument(string filePath, [NotNullWhen(true)] out IDocumentSna /// public ImmutableArray GetRelatedDocuments(IDocumentSnapshot document) { - var targetPath = document.TargetPath.AssumeNotNull(); + var targetPath = document.TargetPath; if (!_state.ImportsToRelatedDocuments.TryGetValue(targetPath, out var relatedDocuments)) { diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Rename/RenameService.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Rename/RenameService.cs index 15283267027..2dd67f4a6ed 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Rename/RenameService.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Rename/RenameService.cs @@ -66,7 +66,7 @@ internal class RenameService( return null; } - var originComponentDocumentFilePath = originComponentDocumentSnapshot.FilePath.AssumeNotNull(); + var originComponentDocumentFilePath = originComponentDocumentSnapshot.FilePath; var newPath = MakeNewPath(originComponentDocumentFilePath, newName); if (File.Exists(newPath)) { @@ -137,7 +137,7 @@ private static ImmutableArray GetAllDocumentSnapshots(string private RenameFile GetFileRenameForComponent(IDocumentSnapshot documentSnapshot, string newPath) => new RenameFile { - OldUri = BuildUri(documentSnapshot.FilePath.AssumeNotNull()), + OldUri = BuildUri(documentSnapshot.FilePath), NewUri = BuildUri(newPath), }; @@ -182,7 +182,7 @@ private async Task AddEditsForCodeDocumentAsync( } // VS Code in Windows expects path to start with '/' - var uri = BuildUri(documentSnapshot.FilePath.AssumeNotNull()); + var uri = BuildUri(documentSnapshot.FilePath); AddEditsForCodeDocument(documentChanges, originTagHelpers, newName, uri, codeDocument); } diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteDocumentSnapshot.cs b/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteDocumentSnapshot.cs index aaebd5df650..be35efe147e 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteDocumentSnapshot.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteDocumentSnapshot.cs @@ -149,7 +149,7 @@ private async Task HACK_GenerateDocumentAsync(CancellationToken cancel var solution = TextDocument.Project.Solution; var filePathService = ProjectSnapshot.SolutionSnapshot.SnapshotManager.FilePathService; - var generatedFilePath = filePathService.GetRazorCSharpFilePath(Project.Key, FilePath.AssumeNotNull()); + var generatedFilePath = filePathService.GetRazorCSharpFilePath(Project.Key, FilePath); var projectId = TextDocument.Project.Id; var generatedDocumentId = solution.GetDocumentIdsWithFilePath(generatedFilePath).First(d => d.ProjectId == projectId); var generatedDocument = solution.GetRequiredDocument(generatedDocumentId); diff --git a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/DynamicFiles/BackgroundDocumentGenerator.Comparer.cs b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/DynamicFiles/BackgroundDocumentGenerator.Comparer.cs index 47ac7f7aae0..16d8b09f3b4 100644 --- a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/DynamicFiles/BackgroundDocumentGenerator.Comparer.cs +++ b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/DynamicFiles/BackgroundDocumentGenerator.Comparer.cs @@ -2,7 +2,6 @@ // Licensed under the MIT license. See License.txt in the project root for license information. using System.Collections.Generic; -using Microsoft.AspNetCore.Razor; using Microsoft.CodeAnalysis.Razor; using Microsoft.CodeAnalysis.Razor.ProjectSystem; @@ -23,8 +22,8 @@ public bool Equals((IProjectSnapshot, IDocumentSnapshot) x, (IProjectSnapshot, I var (projectX, documentX) = x; var (projectY, documentY) = y; - var documentKeyX = new DocumentKey(projectX.Key, documentX.FilePath.AssumeNotNull()); - var documentKeyY = new DocumentKey(projectY.Key, documentY.FilePath.AssumeNotNull()); + var documentKeyX = new DocumentKey(projectX.Key, documentX.FilePath); + var documentKeyY = new DocumentKey(projectY.Key, documentY.FilePath); return documentKeyX.Equals(documentKeyY); } @@ -32,7 +31,7 @@ public bool Equals((IProjectSnapshot, IDocumentSnapshot) x, (IProjectSnapshot, I public int GetHashCode((IProjectSnapshot, IDocumentSnapshot) obj) { var (project, document) = obj; - var documentKey = new DocumentKey(project.Key, document.FilePath.AssumeNotNull()); + var documentKey = new DocumentKey(project.Key, document.FilePath); return documentKey.GetHashCode(); } diff --git a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/DynamicFiles/BackgroundDocumentGenerator.cs b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/DynamicFiles/BackgroundDocumentGenerator.cs index 3cecdf51d6f..aaf4c5e92c6 100644 --- a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/DynamicFiles/BackgroundDocumentGenerator.cs +++ b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/DynamicFiles/BackgroundDocumentGenerator.cs @@ -136,7 +136,7 @@ protected virtual async ValueTask ProcessBatchAsync(ImmutableArray<(IProjectSnap private bool Suppressed(IProjectSnapshot project, IDocumentSnapshot document) { - var filePath = document.FilePath.AssumeNotNull(); + var filePath = document.FilePath; if (_projectManager.IsDocumentOpen(filePath)) { @@ -151,7 +151,7 @@ private bool Suppressed(IProjectSnapshot project, IDocumentSnapshot document) private void UpdateFileInfo(IProjectSnapshot project, IDocumentSnapshot document) { - var filePath = document.FilePath.AssumeNotNull(); + var filePath = document.FilePath; if (!_suppressedDocuments.Contains(filePath)) { diff --git a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/DynamicFiles/DefaultDynamicDocumentContainer.cs b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/DynamicFiles/DefaultDynamicDocumentContainer.cs index 5e950ede79b..7f9c55d27b5 100644 --- a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/DynamicFiles/DefaultDynamicDocumentContainer.cs +++ b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/DynamicFiles/DefaultDynamicDocumentContainer.cs @@ -19,7 +19,7 @@ internal sealed class DefaultDynamicDocumentContainer(IDocumentSnapshot document private RazorDocumentExcerptService? _excerptService; private RazorSpanMappingService? _mappingService; - public string FilePath => _documentSnapshot.FilePath.AssumeNotNull(); + public string FilePath => _documentSnapshot.FilePath; public bool SupportsDiagnostics => false; diff --git a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/ProjectSystem/WindowsRazorProjectHostBase.cs b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/ProjectSystem/WindowsRazorProjectHostBase.cs index 9c1f525174d..de2e60c0077 100644 --- a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/ProjectSystem/WindowsRazorProjectHostBase.cs +++ b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/ProjectSystem/WindowsRazorProjectHostBase.cs @@ -9,7 +9,6 @@ using System.Threading; using System.Threading.Tasks; using System.Threading.Tasks.Dataflow; -using Microsoft.AspNetCore.Razor; using Microsoft.AspNetCore.Razor.ProjectSystem; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Razor.ProjectSystem; @@ -230,10 +229,10 @@ internal Task OnProjectRenamingAsync(string oldProjectFilePath, string newProjec { var documentSnapshot = current.GetDocument(documentFilePath); Assumes.NotNull(documentSnapshot); - // TODO: The creation of the HostProject here is silly + var hostDocument = new HostDocument( - documentSnapshot.FilePath.AssumeNotNull(), - documentSnapshot.TargetPath.AssumeNotNull(), + documentSnapshot.FilePath, + documentSnapshot.TargetPath, documentSnapshot.FileKind); updater.DocumentAdded(projectKey, hostDocument, new FileTextLoader(hostDocument.FilePath, null)); } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Diagnostics/RazorDiagnosticsPublisherTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Diagnostics/RazorDiagnosticsPublisherTest.cs index 01a8e65ea6f..8b1471d4e22 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Diagnostics/RazorDiagnosticsPublisherTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Diagnostics/RazorDiagnosticsPublisherTest.cs @@ -92,7 +92,6 @@ await testProjectManager.UpdateAsync(updater => public async Task DocumentProcessed_NewWorkQueued_RestartsTimer() { // Arrange - Assert.NotNull(_openedDocument.FilePath); var codeDocument = CreateCodeDocument(s_singleRazorDiagnostic); var processedOpenDocument = TestDocumentSnapshot.Create(_openedDocument.FilePath, codeDocument); @@ -161,10 +160,8 @@ public async Task PublishDiagnosticsAsync_NewDocumentDiagnosticsGetPublished(boo } }; - Assert.NotNull(_openedDocument.FilePath); var codeDocument = CreateCodeDocument(shouldContainRazorDiagnostic ? s_singleRazorDiagnostic : []); var processedOpenDocument = TestDocumentSnapshot.Create(_openedDocument.FilePath, codeDocument); - Assert.NotNull(processedOpenDocument.FilePath); var clientConnectionMock = new StrictMock(); var requestResult = new FullDocumentDiagnosticReport(); @@ -237,10 +234,8 @@ public async Task PublishDiagnosticsAsync_NewDocumentDiagnosticsGetPublished(boo public async Task PublishDiagnosticsAsync_NewRazorDiagnosticsGetPublished() { // Arrange - Assert.NotNull(_openedDocument.FilePath); var codeDocument = CreateCodeDocument(s_singleRazorDiagnostic); var processedOpenDocument = TestDocumentSnapshot.Create(_openedDocument.FilePath, codeDocument); - Assert.NotNull(processedOpenDocument.FilePath); var clientConnectionMock = new StrictMock(); clientConnectionMock @@ -295,10 +290,8 @@ public async Task PublishDiagnosticsAsync_NewRazorDiagnosticsGetPublished() public async Task PublishDiagnosticsAsync_NewCSharpDiagnosticsGetPublished() { // Arrange - Assert.NotNull(_openedDocument.FilePath); var codeDocument = CreateCodeDocument([]); var processedOpenDocument = TestDocumentSnapshot.Create(_openedDocument.FilePath, codeDocument); - Assert.NotNull(processedOpenDocument.FilePath); var arranging = true; var clientConnectionMock = new StrictMock(); @@ -368,10 +361,8 @@ public async Task PublishDiagnosticsAsync_NoopsIfRazorDiagnosticsAreSameAsPrevio }) .Returns(Task.FromResult(new SumType?(new FullDocumentDiagnosticReport()))); - Assert.NotNull(_openedDocument.FilePath); var codeDocument = CreateCodeDocument(s_singleRazorDiagnostic); var processedOpenDocument = TestDocumentSnapshot.Create(_openedDocument.FilePath, codeDocument); - Assert.NotNull(processedOpenDocument.FilePath); var documentContextFactory = new TestDocumentContextFactory(_openedDocument.FilePath, codeDocument); var filePathService = new LSPFilePathService(TestLanguageServerFeatureOptions.Instance); @@ -390,10 +381,8 @@ public async Task PublishDiagnosticsAsync_NoopsIfRazorDiagnosticsAreSameAsPrevio public async Task PublishDiagnosticsAsync_NoopsIfCSharpDiagnosticsAreSameAsPreviousPublish() { // Arrange - Assert.NotNull(_openedDocument.FilePath); var codeDocument = CreateCodeDocument([]); var processedOpenDocument = TestDocumentSnapshot.Create(_openedDocument.FilePath, codeDocument); - Assert.NotNull(processedOpenDocument.FilePath); var clientConnectionMock = new StrictMock(); var arranging = true; @@ -452,7 +441,6 @@ public void ClearClosedDocuments_ClearsDiagnosticsForClosedDocument() It.IsAny())) .Callback((string method, PublishDiagnosticParams @params, CancellationToken cancellationToken) => { - Assert.NotNull(_closedDocument.FilePath); Assert.Equal(_closedDocument.FilePath.TrimStart('/'), @params.Uri.AbsolutePath); Assert.Empty(@params.Diagnostics); }) @@ -463,7 +451,6 @@ public void ClearClosedDocuments_ClearsDiagnosticsForClosedDocument() using var publisher = new TestRazorDiagnosticsPublisher(_projectManager, clientConnectionMock.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory); var publisherAccessor = publisher.GetTestAccessor(); - Assert.NotNull(_closedDocument.FilePath); publisherAccessor.SetPublishedDiagnostics(_closedDocument.FilePath, s_singleRazorDiagnostic, s_singleCSharpDiagnostic); // Act @@ -483,7 +470,6 @@ public void ClearClosedDocuments_NoopsIfDocumentIsStillOpen() using var publisher = new TestRazorDiagnosticsPublisher(_projectManager, clientConnectionMock.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory); var publisherAccessor = publisher.GetTestAccessor(); - Assert.NotNull(_openedDocument.FilePath); publisherAccessor.SetPublishedDiagnostics(_openedDocument.FilePath, s_singleRazorDiagnostic, s_singleCSharpDiagnostic); // Act & Assert @@ -500,7 +486,6 @@ public void ClearClosedDocuments_NoopsIfDocumentIsClosedButNoDiagnostics() using var publisher = new TestRazorDiagnosticsPublisher(_projectManager, clientConnectionMock.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory); var publisherAccessor = publisher.GetTestAccessor(); - Assert.NotNull(_closedDocument.FilePath); publisherAccessor.SetPublishedDiagnostics(_closedDocument.FilePath, razorDiagnostics: [], csharpDiagnostics: []); // Act & Assert @@ -517,8 +502,6 @@ public void ClearClosedDocuments_RestartsTimerIfDocumentsStillOpen() using var publisher = new TestRazorDiagnosticsPublisher(_projectManager, clientConnectionMock.Object, TestLanguageServerFeatureOptions.Instance, translateDiagnosticsService, documentContextFactory, LoggerFactory); var publisherAccessor = publisher.GetTestAccessor(); - Assert.NotNull(_closedDocument.FilePath); - Assert.NotNull(_openedDocument.FilePath); publisherAccessor.SetPublishedDiagnostics(_closedDocument.FilePath, razorDiagnostics: [], csharpDiagnostics: []); publisherAccessor.SetPublishedDiagnostics(_openedDocument.FilePath, razorDiagnostics: [], csharpDiagnostics: []); diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingContentValidationPassTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingContentValidationPassTest.cs index ee58d38dcd0..934601ed357 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingContentValidationPassTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingContentValidationPassTest.cs @@ -92,7 +92,7 @@ private static FormattingContext CreateFormattingContext(TestCode input, int tab return context; } - private static (RazorCodeDocument, IDocumentSnapshot) CreateCodeDocumentAndSnapshot(SourceText text, string path, ImmutableArray tagHelpers = default, string? fileKind = default) + private static (RazorCodeDocument, IDocumentSnapshot) CreateCodeDocumentAndSnapshot(SourceText text, string path, ImmutableArray tagHelpers = default, string? fileKind = null) { fileKind ??= FileKinds.Component; tagHelpers = tagHelpers.NullToEmpty(); @@ -113,7 +113,7 @@ private static (RazorCodeDocument, IDocumentSnapshot) CreateCodeDocumentAndSnaps .Returns(path); documentSnapshot .Setup(d => d.Project.GetTagHelpersAsync(It.IsAny())) - .Returns(new ValueTask>(tagHelpers)); + .ReturnsAsync(tagHelpers); documentSnapshot .Setup(d => d.FileKind) .Returns(fileKind); diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingDiagnosticValidationPassTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingDiagnosticValidationPassTest.cs index 736d19ac580..f5a0c708a79 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingDiagnosticValidationPassTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingDiagnosticValidationPassTest.cs @@ -90,7 +90,7 @@ private static FormattingContext CreateFormattingContext(TestCode input, int tab return context; } - private static (RazorCodeDocument, IDocumentSnapshot) CreateCodeDocumentAndSnapshot(SourceText text, string path, ImmutableArray tagHelpers = default, string? fileKind = default) + private static (RazorCodeDocument, IDocumentSnapshot) CreateCodeDocumentAndSnapshot(SourceText text, string path, ImmutableArray tagHelpers = default, string? fileKind = null) { fileKind ??= FileKinds.Component; tagHelpers = tagHelpers.NullToEmpty(); diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs index 9c50af020b9..727bb08b3e6 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs @@ -246,7 +246,7 @@ protected async Task RunCodeActionFormattingTestAsync( protected static TextEdit Edit(int startLine, int startChar, int endLine, int endChar, string newText) => VsLspFactory.CreateTextEdit(startLine, startChar, endLine, endChar, newText); - private static (RazorCodeDocument, IDocumentSnapshot) CreateCodeDocumentAndSnapshot(SourceText text, string path, ImmutableArray tagHelpers = default, string? fileKind = default, bool allowDiagnostics = false, bool inGlobalNamespace = false) + private static (RazorCodeDocument, IDocumentSnapshot) CreateCodeDocumentAndSnapshot(SourceText text, string path, ImmutableArray tagHelpers = default, string? fileKind = null, bool allowDiagnostics = false, bool inGlobalNamespace = false) { fileKind ??= FileKinds.Component; tagHelpers = tagHelpers.NullToEmpty(); @@ -313,7 +313,7 @@ @using Microsoft.AspNetCore.Components.Web return (codeDocument, documentSnapshot); } - internal static IDocumentSnapshot CreateDocumentSnapshot(string path, ImmutableArray tagHelpers, string? fileKind, ImmutableArray importsDocuments, ImmutableArray imports, RazorProjectEngine projectEngine, RazorCodeDocument codeDocument, bool inGlobalNamespace = false) + internal static IDocumentSnapshot CreateDocumentSnapshot(string path, ImmutableArray tagHelpers, string fileKind, ImmutableArray importsDocuments, ImmutableArray imports, RazorProjectEngine projectEngine, RazorCodeDocument codeDocument, bool inGlobalNamespace = false) { var documentSnapshot = new StrictMock(); documentSnapshot @@ -336,7 +336,7 @@ internal static IDocumentSnapshot CreateDocumentSnapshot(string path, ImmutableA .ReturnsAsync(codeDocument.Source.Text); documentSnapshot .Setup(d => d.Project.GetTagHelpersAsync(It.IsAny())) - .Returns(new ValueTask>(tagHelpers)); + .ReturnsAsync(tagHelpers); documentSnapshot .Setup(d => d.Project.GetProjectEngine()) .Returns(projectEngine); diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/TestDocumentSnapshot.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/TestDocumentSnapshot.cs index 09ddd504297..a5afd0fc36c 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/TestDocumentSnapshot.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/ProjectSystem/TestDocumentSnapshot.cs @@ -61,9 +61,9 @@ public static TestDocumentSnapshot Create(string filePath, RazorCodeDocument cod public HostDocument HostDocument => RealSnapshot.HostDocument; - public string? FileKind => RealSnapshot.FileKind; - public string? FilePath => RealSnapshot.FilePath; - public string? TargetPath => RealSnapshot.TargetPath; + public string FileKind => RealSnapshot.FileKind; + public string FilePath => RealSnapshot.FilePath; + public string TargetPath => RealSnapshot.TargetPath; public IProjectSnapshot Project => RealSnapshot.Project; public int Version => RealSnapshot.Version; diff --git a/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/DocumentGenerator/BackgroundDocumentGeneratorTest.cs b/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/DocumentGenerator/BackgroundDocumentGeneratorTest.cs index b4d91bec7e6..9586c91ec45 100644 --- a/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/DocumentGenerator/BackgroundDocumentGeneratorTest.cs +++ b/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/DocumentGenerator/BackgroundDocumentGeneratorTest.cs @@ -383,7 +383,7 @@ public void UnblockBatchProcessing() } private static DocumentKey GetKey(IProjectSnapshot project, IDocumentSnapshot document) - => new(project.Key, document.FilePath.AssumeNotNull()); + => new(project.Key, document.FilePath); protected override async ValueTask ProcessBatchAsync(ImmutableArray<(IProjectSnapshot, IDocumentSnapshot)> items, CancellationToken token) { diff --git a/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/ProjectSystem/RazorDynamicFileInfoProviderTest.cs b/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/ProjectSystem/RazorDynamicFileInfoProviderTest.cs index ff1fa77965d..847194757fc 100644 --- a/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/ProjectSystem/RazorDynamicFileInfoProviderTest.cs +++ b/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/ProjectSystem/RazorDynamicFileInfoProviderTest.cs @@ -113,7 +113,7 @@ public void UpdateLSPFileInfo_UnknownFile_Noops() public async Task GetDynamicFileInfoAsync_IncludesProjectToken() { // Arrange - var info = await _testAccessor.GetDynamicFileInfoAsync(_projectId, _document1.FilePath.AssumeNotNull(), DisposalToken); + var info = await _testAccessor.GetDynamicFileInfoAsync(_projectId, _document1.FilePath, DisposalToken); Assert.NotNull(info); Assert.Equal(@"C:\document1.razor.fJcYlbdqjCXiWYY1.ide.g.cs", info.FilePath); @@ -123,7 +123,7 @@ public async Task GetDynamicFileInfoAsync_IncludesProjectToken() public async Task UpdateLSPFileInfo_Updates() { // Arrange - await _testAccessor.GetDynamicFileInfoAsync(_projectId, _document1.FilePath.AssumeNotNull(), DisposalToken); + await _testAccessor.GetDynamicFileInfoAsync(_projectId, _document1.FilePath, DisposalToken); var called = false; _provider.Updated += (sender, args) => called = true; @@ -138,7 +138,7 @@ public async Task UpdateLSPFileInfo_Updates() public async Task UpdateLSPFileInfo_ProjectRemoved_Noops() { // Arrange - await _testAccessor.GetDynamicFileInfoAsync(_projectId, _document1.FilePath.AssumeNotNull(), DisposalToken); + await _testAccessor.GetDynamicFileInfoAsync(_projectId, _document1.FilePath, DisposalToken); var called = false; _provider.Updated += (sender, args) => called = true; @@ -158,8 +158,8 @@ await _projectManager.UpdateAsync(updater => public async Task UpdateLSPFileInfo_SolutionClosing_ClearsAllDocuments() { // Arrange - await _testAccessor.GetDynamicFileInfoAsync(_projectId, _document1.FilePath.AssumeNotNull(), DisposalToken); - await _testAccessor.GetDynamicFileInfoAsync(_projectId, _document2.FilePath.AssumeNotNull(), DisposalToken); + await _testAccessor.GetDynamicFileInfoAsync(_projectId, _document1.FilePath, DisposalToken); + await _testAccessor.GetDynamicFileInfoAsync(_projectId, _document2.FilePath, DisposalToken); _provider.Updated += (sender, documentFilePath) => throw new InvalidOperationException("Should not have been called!"); await _projectManager.UpdateAsync(updater =>