@@ -930,47 +930,54 @@ public void AddAnalyzerReference(string fullPath)
930930 foreach ( var mappedFullPath in mappedPaths )
931931 {
932932 if ( _analyzerPathsToAnalyzers . ContainsKey ( mappedFullPath ) )
933- {
934933 throw new ArgumentException ( $ "'{ fullPath } ' has already been added to this project.", nameof ( fullPath ) ) ;
935- }
936934 }
937935
938- foreach ( var mappedFullPath in mappedPaths )
936+ if ( _activeBatchScopes > 0 )
939937 {
940- // Are we adding one we just recently removed? If so, we can just keep using that one, and avoid removing
941- // it once we apply the batch
942- var analyzerPendingRemoval = _analyzersRemovedInBatch . FirstOrDefault ( a => a . FullPath == mappedFullPath ) ;
943- if ( analyzerPendingRemoval != null )
944- {
945- _analyzersRemovedInBatch . Remove ( analyzerPendingRemoval ) ;
946- _analyzerPathsToAnalyzers . Add ( mappedFullPath , analyzerPendingRemoval ) ;
947- }
948- else
938+ foreach ( var mappedFullPath in mappedPaths )
949939 {
950- // Nope, we actually need to make a new one.
951- var analyzerReference = new AnalyzerFileReference ( mappedFullPath , _analyzerAssemblyLoader ) ;
952-
953- _analyzerPathsToAnalyzers . Add ( mappedFullPath , analyzerReference ) ;
954-
955- if ( _activeBatchScopes > 0 )
940+ // Are we adding one we just recently removed? If so, we can just keep using that one, and avoid removing
941+ // it once we apply the batch
942+ var analyzerPendingRemoval = _analyzersRemovedInBatch . FirstOrDefault ( a => a . FullPath == mappedFullPath ) ;
943+ if ( analyzerPendingRemoval != null )
956944 {
957- _analyzersAddedInBatch . Add ( analyzerReference ) ;
945+ _analyzersRemovedInBatch . Remove ( analyzerPendingRemoval ) ;
946+ _analyzerPathsToAnalyzers . Add ( mappedFullPath , analyzerPendingRemoval ) ;
958947 }
959948 else
960949 {
961- _projectSystemProjectFactory . ApplyChangeToWorkspace ( w => w . OnAnalyzerReferenceAdded ( Id , analyzerReference ) ) ;
950+ // Nope, we actually need to make a new one.
951+ var analyzerReference = new AnalyzerFileReference ( mappedFullPath , _analyzerAssemblyLoader ) ;
952+
953+ _analyzersAddedInBatch . Add ( analyzerReference ) ;
954+ _analyzerPathsToAnalyzers . Add ( mappedFullPath , analyzerReference ) ;
962955 }
963956 }
964957 }
958+ else
959+ {
960+ _projectSystemProjectFactory . ApplyChangeToWorkspaceWithProjectUpdateState ( ( w , projectUpdateState ) =>
961+ {
962+ foreach ( var mappedFullPath in mappedPaths )
963+ {
964+ var analyzerReference = new AnalyzerFileReference ( mappedFullPath , _analyzerAssemblyLoader ) ;
965+ _analyzerPathsToAnalyzers . Add ( mappedFullPath , analyzerReference ) ;
966+ w . OnAnalyzerReferenceAdded ( Id , analyzerReference ) ;
967+
968+ projectUpdateState = projectUpdateState . WithIncrementalAnalyzerReferenceAdded ( analyzerReference ) ;
969+ }
970+
971+ return projectUpdateState ;
972+ } ) ;
973+ }
965974 }
966975 }
967976
968977 public void RemoveAnalyzerReference ( string fullPath )
969978 {
970979 if ( string . IsNullOrEmpty ( fullPath ) )
971- {
972980 throw new ArgumentException ( "message" , nameof ( fullPath ) ) ;
973- }
974981
975982 var mappedPaths = GetMappedAnalyzerPaths ( fullPath ) ;
976983
@@ -980,28 +987,38 @@ public void RemoveAnalyzerReference(string fullPath)
980987 foreach ( var mappedFullPath in mappedPaths )
981988 {
982989 if ( ! _analyzerPathsToAnalyzers . ContainsKey ( mappedFullPath ) )
983- {
984990 throw new ArgumentException ( $ "'{ fullPath } ' is not an analyzer of this project.", nameof ( fullPath ) ) ;
985- }
986991 }
987992
988- foreach ( var mappedFullPath in mappedPaths )
993+ if ( _activeBatchScopes > 0 )
989994 {
990- var analyzerReference = _analyzerPathsToAnalyzers [ mappedFullPath ] ;
995+ foreach ( var mappedFullPath in mappedPaths )
996+ {
997+ var analyzerReference = _analyzerPathsToAnalyzers [ mappedFullPath ] ;
991998
992- _analyzerPathsToAnalyzers . Remove ( mappedFullPath ) ;
999+ _analyzerPathsToAnalyzers . Remove ( mappedFullPath ) ;
9931000
994- if ( _activeBatchScopes > 0 )
995- {
9961001 // This analyzer may be one we've just added in the same batch; in that case, just don't add it in
9971002 // the first place.
9981003 if ( ! _analyzersAddedInBatch . Remove ( analyzerReference ) )
9991004 _analyzersRemovedInBatch . Add ( analyzerReference ) ;
10001005 }
1001- else
1006+ }
1007+ else
1008+ {
1009+ _projectSystemProjectFactory . ApplyChangeToWorkspaceWithProjectUpdateState ( ( w , projectUpdateState ) =>
10021010 {
1003- _projectSystemProjectFactory . ApplyChangeToWorkspace ( w => w . OnAnalyzerReferenceRemoved ( Id , analyzerReference ) ) ;
1004- }
1011+ foreach ( var mappedFullPath in mappedPaths )
1012+ {
1013+ var analyzerReference = _analyzerPathsToAnalyzers [ mappedFullPath ] ;
1014+ _analyzerPathsToAnalyzers . Remove ( mappedFullPath ) ;
1015+
1016+ w . OnAnalyzerReferenceRemoved ( Id , analyzerReference ) ;
1017+ projectUpdateState = projectUpdateState . WithIncrementalAnalyzerReferenceRemoved ( analyzerReference ) ;
1018+ }
1019+
1020+ return projectUpdateState ;
1021+ } ) ;
10051022 }
10061023 }
10071024 }
0 commit comments