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 ;
54using System . Collections . Generic ;
65using System . Collections . Immutable ;
76using System . IO ;
@@ -20,11 +19,6 @@ namespace Microsoft.CodeAnalysis.Razor.ProjectSystem;
2019// Internal tracker for DefaultProjectSnapshot
2120internal class ProjectState
2221{
23- private const ProjectDifference ClearDocumentCollectionVersionMask =
24- ProjectDifference . ConfigurationChanged |
25- ProjectDifference . DocumentAdded |
26- ProjectDifference . DocumentRemoved ;
27-
2822 private static readonly ImmutableDictionary < string , DocumentState > s_emptyDocuments = ImmutableDictionary . Create < string , DocumentState > ( FilePathNormalizingComparer . Instance ) ;
2923 private static readonly ImmutableDictionary < string , ImmutableArray < string > > s_emptyImportsToRelatedDocuments = ImmutableDictionary . Create < string , ImmutableArray < string > > ( FilePathNormalizingComparer . Instance ) ;
3024 private readonly object _lock ;
@@ -63,7 +57,7 @@ private ProjectState(
6357
6458 private ProjectState (
6559 ProjectState older ,
66- ProjectDifference difference ,
60+ bool numberOfDocumentsMayHaveChanged ,
6761 HostProject hostProject ,
6862 ProjectWorkspaceState projectWorkspaceState ,
6963 ImmutableDictionary < string , DocumentState > documents ,
@@ -80,14 +74,14 @@ private ProjectState(
8074
8175 _lock = new object ( ) ;
8276
83- if ( ( difference & ClearDocumentCollectionVersionMask ) == 0 )
77+ if ( numberOfDocumentsMayHaveChanged )
8478 {
85- // Document collection hasn't changed
86- DocumentCollectionVersion = older . DocumentCollectionVersion ;
79+ DocumentCollectionVersion = Version ;
8780 }
8881 else
8982 {
90- DocumentCollectionVersion = Version ;
83+ // Document collection hasn't changed
84+ DocumentCollectionVersion = older . DocumentCollectionVersion ;
9185 }
9286
9387 if ( older . _projectEngine != null &&
@@ -179,16 +173,6 @@ RazorProjectEngine CreateProjectEngine()
179173
180174 public ProjectState WithAddedHostDocument ( HostDocument hostDocument , TextLoader loader )
181175 {
182- if ( hostDocument is null )
183- {
184- throw new ArgumentNullException ( nameof ( hostDocument ) ) ;
185- }
186-
187- if ( loader is null )
188- {
189- throw new ArgumentNullException ( nameof ( loader ) ) ;
190- }
191-
192176 // Ignore attempts to 'add' a document with different data, we only
193177 // care about one, so it might as well be the one we have.
194178 if ( Documents . ContainsKey ( hostDocument . FilePath ) )
@@ -212,17 +196,12 @@ public ProjectState WithAddedHostDocument(HostDocument hostDocument, TextLoader
212196 }
213197 }
214198
215- var state = new ProjectState ( this , ProjectDifference . DocumentAdded , HostProject , ProjectWorkspaceState , documents , importsToRelatedDocuments ) ;
199+ var state = new ProjectState ( this , numberOfDocumentsMayHaveChanged : true , HostProject , ProjectWorkspaceState , documents , importsToRelatedDocuments ) ;
216200 return state ;
217201 }
218202
219203 public ProjectState WithRemovedHostDocument ( HostDocument hostDocument )
220204 {
221- if ( hostDocument is null )
222- {
223- throw new ArgumentNullException ( nameof ( hostDocument ) ) ;
224- }
225-
226205 if ( ! Documents . ContainsKey ( hostDocument . FilePath ) )
227206 {
228207 return this ;
@@ -244,17 +223,12 @@ public ProjectState WithRemovedHostDocument(HostDocument hostDocument)
244223 var importTargetPaths = GetImportDocumentTargetPaths ( hostDocument ) ;
245224 var importsToRelatedDocuments = RemoveFromImportsToRelatedDocuments ( ImportsToRelatedDocuments , hostDocument , importTargetPaths ) ;
246225
247- var state = new ProjectState ( this , ProjectDifference . DocumentRemoved , HostProject , ProjectWorkspaceState , documents , importsToRelatedDocuments ) ;
226+ var state = new ProjectState ( this , numberOfDocumentsMayHaveChanged : true , HostProject , ProjectWorkspaceState , documents , importsToRelatedDocuments ) ;
248227 return state ;
249228 }
250229
251230 public ProjectState WithChangedHostDocument ( HostDocument hostDocument , SourceText sourceText , VersionStamp textVersion )
252231 {
253- if ( hostDocument is null )
254- {
255- throw new ArgumentNullException ( nameof ( hostDocument ) ) ;
256- }
257-
258232 if ( ! Documents . TryGetValue ( hostDocument . FilePath , out var document ) )
259233 {
260234 return this ;
@@ -270,17 +244,12 @@ public ProjectState WithChangedHostDocument(HostDocument hostDocument, SourceTex
270244 }
271245 }
272246
273- var state = new ProjectState ( this , ProjectDifference . DocumentChanged , HostProject , ProjectWorkspaceState , documents , ImportsToRelatedDocuments ) ;
247+ var state = new ProjectState ( this , numberOfDocumentsMayHaveChanged : false , HostProject , ProjectWorkspaceState , documents , ImportsToRelatedDocuments ) ;
274248 return state ;
275249 }
276250
277251 public ProjectState WithChangedHostDocument ( HostDocument hostDocument , TextLoader loader )
278252 {
279- if ( hostDocument is null )
280- {
281- throw new ArgumentNullException ( nameof ( hostDocument ) ) ;
282- }
283-
284253 if ( ! Documents . TryGetValue ( hostDocument . FilePath , out var document ) )
285254 {
286255 return this ;
@@ -296,7 +265,7 @@ public ProjectState WithChangedHostDocument(HostDocument hostDocument, TextLoade
296265 }
297266 }
298267
299- var state = new ProjectState ( this , ProjectDifference . DocumentChanged , HostProject , ProjectWorkspaceState , documents , ImportsToRelatedDocuments ) ;
268+ var state = new ProjectState ( this , numberOfDocumentsMayHaveChanged : false , HostProject , ProjectWorkspaceState , documents , ImportsToRelatedDocuments ) ;
300269 return state ;
301270 }
302271
@@ -320,7 +289,7 @@ public ProjectState WithHostProjectAndWorkspaceState(HostProject hostProject, Pr
320289 importsToRelatedDocuments = AddToImportsToRelatedDocuments ( importsToRelatedDocuments , document . Value . HostDocument . FilePath , importTargetPaths ) ;
321290 }
322291
323- var state = new ProjectState ( this , ProjectDifference . ConfigurationChanged , hostProject , projectWorkspaceState , documents , importsToRelatedDocuments ) ;
292+ var state = new ProjectState ( this , numberOfDocumentsMayHaveChanged : true , hostProject , projectWorkspaceState , documents , importsToRelatedDocuments ) ;
324293 return state ;
325294 }
326295
@@ -333,7 +302,7 @@ internal static ImmutableDictionary<string, ImmutableArray<string>> AddToImports
333302 {
334303 if ( ! importsToRelatedDocuments . TryGetValue ( importTargetPath , out var relatedDocuments ) )
335304 {
336- relatedDocuments = ImmutableArray . Create < string > ( ) ;
305+ relatedDocuments = [ ] ;
337306 }
338307
339308 relatedDocuments = relatedDocuments . Add ( documentFilePath ) ;
0 commit comments