Skip to content

Commit 855b86c

Browse files
Use ImmutableArray<T> in ProjectRazorJson
1 parent 8795985 commit 855b86c

File tree

9 files changed

+36
-36
lines changed

9 files changed

+36
-36
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.ExternalAccess.RoslynWorkspace/RazorProjectJsonSerializer.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

4+
using System.Collections.Immutable;
45
using System.Diagnostics.CodeAnalysis;
56
using System.Runtime.InteropServices;
67
using Microsoft.AspNetCore.Razor.Language;
8+
using Microsoft.AspNetCore.Razor.PooledObjects;
79
using Microsoft.AspNetCore.Razor.ProjectEngineHost;
810
using Microsoft.AspNetCore.Razor.ProjectSystem;
911
using Microsoft.AspNetCore.Razor.Serialization;
@@ -60,7 +62,7 @@ public static async Task SerializeAsync(Project project, string projectRazorJson
6062
var documents = GetDocuments(project, projectPath);
6163

6264
// Not a razor project
63-
if (documents.Count == 0)
65+
if (documents.Length == 0)
6466
{
6567
return;
6668
}
@@ -164,9 +166,9 @@ private static void WriteJsonFile(string publishFilePath, ProjectRazorJson proje
164166
File.Move(tempFileInfo.FullName, publishFilePath);
165167
}
166168

167-
private static IReadOnlyList<DocumentSnapshotHandle> GetDocuments(Project project, string projectPath)
169+
private static ImmutableArray<DocumentSnapshotHandle> GetDocuments(Project project, string projectPath)
168170
{
169-
var documents = new List<DocumentSnapshotHandle>(project.DocumentIds.Count);
171+
using var documents = new PooledArrayBuilder<DocumentSnapshotHandle>();
170172

171173
var normalizedProjectPath = FilePathNormalizer.NormalizeDirectory(projectPath);
172174

@@ -182,7 +184,7 @@ private static IReadOnlyList<DocumentSnapshotHandle> GetDocuments(Project projec
182184
}
183185
}
184186

185-
return documents;
187+
return documents.DrainToImmutable();
186188
}
187189

188190
private static string GetTargetPath(string documentFilePath, string normalizedProjectPath)
@@ -191,7 +193,7 @@ private static string GetTargetPath(string documentFilePath, string normalizedPr
191193
if (targetFilePath.StartsWith(normalizedProjectPath, s_stringComparison))
192194
{
193195
// Make relative
194-
targetFilePath = documentFilePath.Substring(normalizedProjectPath.Length);
196+
targetFilePath = documentFilePath[normalizedProjectPath.Length..];
195197
}
196198

197199
// Representing all of our host documents with a re-normalized target path to workaround GetRelatedDocument limitations.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void UpdateProject(string projectFilePath, ProjectRazorJson? projectRazorJson)
129129
}
130130

131131
var projectWorkspaceState = projectRazorJson.ProjectWorkspaceState ?? ProjectWorkspaceState.Default;
132-
var documents = projectRazorJson.Documents ?? Array.Empty<DocumentSnapshotHandle>();
132+
var documents = projectRazorJson.Documents;
133133
_projectService.UpdateProject(
134134
projectRazorJson.FilePath,
135135
projectRazorJson.Configuration,

src/Razor/src/Microsoft.AspNetCore.Razor.ProjectEngineHost/ProjectSystem/ProjectRazorJson.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

4-
using System.Collections.Generic;
4+
using System.Collections.Immutable;
55
using Microsoft.AspNetCore.Razor.Language;
66
using Microsoft.AspNetCore.Razor.Serialization;
77

@@ -14,21 +14,21 @@ internal sealed class ProjectRazorJson
1414
public RazorConfiguration? Configuration { get; }
1515
public string? RootNamespace { get; }
1616
public ProjectWorkspaceState? ProjectWorkspaceState { get; }
17-
public IReadOnlyList<DocumentSnapshotHandle> Documents { get; }
17+
public ImmutableArray<DocumentSnapshotHandle> Documents { get; }
1818

1919
public ProjectRazorJson(
2020
string serializedFilePath,
2121
string filePath,
2222
RazorConfiguration? configuration,
2323
string? rootNamespace,
2424
ProjectWorkspaceState? projectWorkspaceState,
25-
IReadOnlyList<DocumentSnapshotHandle> documents)
25+
ImmutableArray<DocumentSnapshotHandle> documents)
2626
{
2727
SerializedFilePath = serializedFilePath;
2828
FilePath = filePath;
2929
Configuration = configuration;
3030
RootNamespace = rootNamespace;
3131
ProjectWorkspaceState = projectWorkspaceState;
32-
Documents = documents;
32+
Documents = documents.NullToEmpty();
3333
}
3434
}

src/Razor/src/Microsoft.AspNetCore.Razor.ProjectEngineHost/Serialization/ObjectReaders.ProjectRazorJsonData.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

4-
using System;
4+
using System.Collections.Immutable;
55
using Microsoft.AspNetCore.Razor.Language;
66
using Microsoft.AspNetCore.Razor.ProjectSystem;
77

@@ -15,7 +15,7 @@ private record struct ProjectRazorJsonData(
1515
RazorConfiguration? Configuration,
1616
string? RootNamespace,
1717
ProjectWorkspaceState? ProjectWorkspaceState,
18-
DocumentSnapshotHandle[] Documents,
18+
ImmutableArray<DocumentSnapshotHandle> Documents,
1919
string? SerializationFormat)
2020
{
2121
public static readonly PropertyMap<ProjectRazorJsonData> PropertyMap = new(
@@ -43,8 +43,7 @@ private static void ReadProjectWorkspaceState(JsonDataReader reader, ref Project
4343
=> data.ProjectWorkspaceState = reader.ReadObject(ReadProjectWorkspaceStateFromProperties);
4444

4545
private static void ReadDocuments(JsonDataReader reader, ref ProjectRazorJsonData data)
46-
=> data.Documents = reader.ReadArray(static r => r.ReadNonNullObject(ReadDocumentSnapshotHandleFromProperties))
47-
?? Array.Empty<DocumentSnapshotHandle>();
46+
=> data.Documents = reader.ReadImmutableArray(static r => r.ReadNonNullObject(ReadDocumentSnapshotHandleFromProperties));
4847

4948
private static void ReadSerializationFormat(JsonDataReader reader, ref ProjectRazorJsonData data)
5049
=> data.SerializationFormat = reader.ReadString();

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/IProjectSnapshotExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

4-
using System.Collections.Generic;
4+
using Microsoft.AspNetCore.Razor.PooledObjects;
55
using Microsoft.AspNetCore.Razor.ProjectSystem;
66
using Microsoft.AspNetCore.Razor.Serialization;
77
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
@@ -12,7 +12,8 @@ internal static class IProjectSnapshotExtensions
1212
{
1313
public static ProjectRazorJson ToProjectRazorJson(this IProjectSnapshot project, string serializedFilePath)
1414
{
15-
var documents = new List<DocumentSnapshotHandle>();
15+
using var documents = new PooledArrayBuilder<DocumentSnapshotHandle>();
16+
1617
foreach (var documentFilePath in project.DocumentFilePaths)
1718
{
1819
if (project.GetDocument(documentFilePath) is { } document)
@@ -29,6 +30,6 @@ public static ProjectRazorJson ToProjectRazorJson(this IProjectSnapshot project,
2930
configuration: project.Configuration,
3031
rootNamespace: project.RootNamespace,
3132
projectWorkspaceState: project.ProjectWorkspaceState,
32-
documents: documents);
33+
documents: documents.DrainToImmutable());
3334
}
3435
}

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Common.Test/SerializationTest.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

4-
using System;
54
using System.Collections.Immutable;
65
using System.Linq;
76
using Microsoft.AspNetCore.Razor.Language;
@@ -46,7 +45,7 @@ public void ProjectRazorJson_InvalidSerializationFormat_SerializesToNull()
4645
Configuration,
4746
rootNamespace: "TestProject",
4847
ProjectWorkspaceState,
49-
Array.Empty<DocumentSnapshotHandle>());
48+
ImmutableArray<DocumentSnapshotHandle>.Empty);
5049

5150
var serializedHandle = JsonConvert.SerializeObject(projectRazorJson, ProjectRazorJsonJsonConverter.Instance);
5251
Assert.NotNull(serializedHandle);
@@ -73,7 +72,7 @@ public void ProjectRazorJson_MissingSerializationFormat_SerializesToNull()
7372
Configuration,
7473
rootNamespace: "TestProject",
7574
ProjectWorkspaceState,
76-
Array.Empty<DocumentSnapshotHandle>());
75+
ImmutableArray<DocumentSnapshotHandle>.Empty);
7776

7877
var serializedHandle = JsonConvert.SerializeObject(projectRazorJson, ProjectRazorJsonJsonConverter.Instance);
7978
Assert.NotNull(serializedHandle);
@@ -103,7 +102,7 @@ public void ProjectRazorJson_CanRoundTrip()
103102
Configuration,
104103
rootNamespace: "TestProject",
105104
ProjectWorkspaceState,
106-
new[] { legacyDocument, componentDocument });
105+
ImmutableArray.Create(legacyDocument, componentDocument));
107106

108107
var serializedHandle = JsonConvert.SerializeObject(projectRazorJson, ProjectRazorJsonJsonConverter.Instance);
109108
Assert.NotNull(serializedHandle);

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
6-
using System;
4+
using System.Collections.Immutable;
75
using Microsoft.AspNetCore.Razor.LanguageServer.Common;
86
using Microsoft.AspNetCore.Razor.LanguageServer.Serialization;
97
using Microsoft.AspNetCore.Razor.ProjectSystem;
@@ -34,7 +32,7 @@ public void TryDeserialize_RemovedKind_ReturnsFalse()
3432
configuration: null,
3533
rootNamespace: null,
3634
projectWorkspaceState: null,
37-
documents: Array.Empty<DocumentSnapshotHandle>()));
35+
documents: ImmutableArray<DocumentSnapshotHandle>.Empty));
3836
var args = new ProjectConfigurationFileChangeEventArgs("/path/to/obj/project.razor.json", RazorFileChangeKind.Removed, jsonFileDeserializer.Object);
3937

4038
// Act
@@ -57,7 +55,7 @@ public void TryDeserialize_DifferingSerializationPaths_ReturnsFalse()
5755
configuration: null,
5856
rootNamespace: null,
5957
projectWorkspaceState: null,
60-
documents: Array.Empty<DocumentSnapshotHandle>());
58+
documents: ImmutableArray<DocumentSnapshotHandle>.Empty);
6159
jsonFileDeserializer.Setup(deserializer => deserializer.Deserialize<ProjectRazorJson>(It.IsAny<string>()))
6260
.Returns(projectRazorJson);
6361
var args = new ProjectConfigurationFileChangeEventArgs("/path/to/DIFFERENT/obj/project.razor.json", RazorFileChangeKind.Added, jsonFileDeserializer.Object);
@@ -81,7 +79,7 @@ public void TryDeserialize_MemoizesResults()
8179
configuration: null,
8280
rootNamespace: null,
8381
projectWorkspaceState: null,
84-
documents: Array.Empty<DocumentSnapshotHandle>());
82+
documents: ImmutableArray<DocumentSnapshotHandle>.Empty);
8583
jsonFileDeserializer.Setup(deserializer => deserializer.Deserialize<ProjectRazorJson>(It.IsAny<string>()))
8684
.Returns(projectRazorJson);
8785
var args = new ProjectConfigurationFileChangeEventArgs("/path/to/obj/project.razor.json", RazorFileChangeKind.Added, jsonFileDeserializer.Object);

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public async Task ProjectConfigurationFileChanged_Removed_NonNormalizedPaths()
5555
RazorConfiguration.Default,
5656
rootNamespace: "TestRootNamespace",
5757
new ProjectWorkspaceState(ImmutableArray<TagHelperDescriptor>.Empty, LanguageVersion.CSharp5),
58-
Array.Empty<DocumentSnapshotHandle>());
58+
ImmutableArray<DocumentSnapshotHandle>.Empty);
5959
var projectService = new Mock<RazorProjectService>(MockBehavior.Strict);
6060
projectService.Setup(service => service.AddProject(projectRazorJson.FilePath, projectRazorJson.RootNamespace)).Verifiable();
6161
projectService.Setup(service => service.UpdateProject(
@@ -69,7 +69,7 @@ public async Task ProjectConfigurationFileChanged_Removed_NonNormalizedPaths()
6969
null,
7070
null,
7171
ProjectWorkspaceState.Default,
72-
Array.Empty<DocumentSnapshotHandle>())).Verifiable();
72+
ImmutableArray<DocumentSnapshotHandle>.Empty)).Verifiable();
7373
var synchronizer = GetSynchronizer(projectService.Object);
7474
var jsonFileDeserializer = CreateJsonFileDeserializer(projectRazorJson);
7575
var addArgs = new ProjectConfigurationFileChangeEventArgs("/path/to\\obj/project.razor.json", RazorFileChangeKind.Added, jsonFileDeserializer);
@@ -121,7 +121,7 @@ public async Task ProjectConfigurationFileChanged_Added_AddAndUpdatesProject()
121121
RazorConfiguration.Default,
122122
rootNamespace: "TestRootNamespace",
123123
new ProjectWorkspaceState(ImmutableArray<TagHelperDescriptor>.Empty, LanguageVersion.CSharp5),
124-
Array.Empty<DocumentSnapshotHandle>());
124+
ImmutableArray<DocumentSnapshotHandle>.Empty);
125125
var projectService = new Mock<RazorProjectService>(MockBehavior.Strict);
126126
projectService.Setup(service => service.AddProject(projectRazorJson.FilePath, projectRazorJson.RootNamespace)).Verifiable();
127127
projectService.Setup(service => service.UpdateProject(
@@ -156,7 +156,7 @@ public async Task ProjectConfigurationFileChanged_Removed_ResetsProject()
156156
RazorConfiguration.Default,
157157
rootNamespace: "TestRootNamespace",
158158
new ProjectWorkspaceState(ImmutableArray<TagHelperDescriptor>.Empty, LanguageVersion.CSharp5),
159-
Array.Empty<DocumentSnapshotHandle>());
159+
ImmutableArray<DocumentSnapshotHandle>.Empty);
160160
var projectService = new Mock<RazorProjectService>(MockBehavior.Strict);
161161
projectService.Setup(service => service.AddProject(projectRazorJson.FilePath, projectRazorJson.RootNamespace)).Verifiable();
162162
projectService.Setup(service => service.UpdateProject(
@@ -205,7 +205,7 @@ public async Task ProjectConfigurationFileChanged_Changed_UpdatesProject()
205205
RazorConfiguration.Default,
206206
rootNamespace: "TestRootNamespace",
207207
new ProjectWorkspaceState(ImmutableArray<TagHelperDescriptor>.Empty, LanguageVersion.CSharp5),
208-
Array.Empty<DocumentSnapshotHandle>());
208+
ImmutableArray<DocumentSnapshotHandle>.Empty);
209209
var projectService = new Mock<RazorProjectService>(MockBehavior.Strict);
210210
projectService.Setup(service => service.AddProject(initialProjectRazorJson.FilePath, initialProjectRazorJson.RootNamespace)).Verifiable();
211211
projectService.Setup(service => service.UpdateProject(
@@ -223,7 +223,7 @@ public async Task ProjectConfigurationFileChanged_Changed_UpdatesProject()
223223
Array.Empty<RazorExtension>()),
224224
rootNamespace: "TestRootNamespace2",
225225
new ProjectWorkspaceState(ImmutableArray<TagHelperDescriptor>.Empty, LanguageVersion.CSharp6),
226-
Array.Empty<DocumentSnapshotHandle>());
226+
ImmutableArray<DocumentSnapshotHandle>.Empty);
227227
projectService.Setup(service => service.UpdateProject(
228228
changedProjectRazorJson.FilePath,
229229
changedProjectRazorJson.Configuration,
@@ -266,7 +266,7 @@ public async Task ProjectConfigurationFileChanged_Changed_CantDeserialize_Resets
266266
RazorConfiguration.Default,
267267
rootNamespace: "TestRootNamespace",
268268
new ProjectWorkspaceState(ImmutableArray<TagHelperDescriptor>.Empty, LanguageVersion.CSharp5),
269-
Array.Empty<DocumentSnapshotHandle>());
269+
ImmutableArray<DocumentSnapshotHandle>.Empty);
270270
var projectService = new Mock<RazorProjectService>(MockBehavior.Strict);
271271
projectService.Setup(service => service.AddProject(initialProjectRazorJson.FilePath, initialProjectRazorJson.RootNamespace)).Verifiable();
272272
projectService.Setup(service => service.UpdateProject(
@@ -284,7 +284,7 @@ public async Task ProjectConfigurationFileChanged_Changed_CantDeserialize_Resets
284284
Array.Empty<RazorExtension>()),
285285
rootNamespace: "TestRootNamespace2",
286286
new ProjectWorkspaceState(ImmutableArray<TagHelperDescriptor>.Empty, LanguageVersion.CSharp6),
287-
Array.Empty<DocumentSnapshotHandle>());
287+
ImmutableArray<DocumentSnapshotHandle>.Empty);
288288

289289
// This is the request that happens when the server is reset
290290
projectService.Setup(service => service.UpdateProject(
@@ -349,7 +349,7 @@ public async Task ProjectConfigurationFileChanged_RemoveThenAdd_OnlyAdds()
349349
RazorConfiguration.Default,
350350
rootNamespace: "TestRootNamespace",
351351
new ProjectWorkspaceState(ImmutableArray<TagHelperDescriptor>.Empty, LanguageVersion.CSharp5),
352-
Array.Empty<DocumentSnapshotHandle>());
352+
ImmutableArray<DocumentSnapshotHandle>.Empty);
353353

354354
var filePath = "path/to/project.csproj";
355355
var projectService = new Mock<RazorProjectService>(MockBehavior.Strict);

src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Razor.Test.Common.Compiler, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
1717

1818
// Razor tooling assemblies
19+
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Razor.ExternalAccess.RoslynWorkspace, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
1920
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Razor.LanguageServer, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
2021
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Razor.LanguageServer.Common, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
2122
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Razor.LanguageServer.Protocol, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]

0 commit comments

Comments
 (0)