@@ -616,9 +616,11 @@ public async Task ReducePointerDimensionAsync(List<INamedTypeSymbol> symbols)
616616 }
617617
618618 // Reduce the pointer dimension of all reference locations
619- foreach ( var location in locations )
619+ // Also, do modifications in groups to prevent overwriting changes
620+ var locationsBySourcetree = locations . GroupBy ( l => l . SourceTree ) ;
621+ foreach ( var group in locationsBySourcetree )
620622 {
621- var syntaxTree = location . SourceTree ;
623+ var syntaxTree = group . Key ;
622624 if ( syntaxTree == null )
623625 {
624626 continue ;
@@ -631,18 +633,24 @@ public async Task ReducePointerDimensionAsync(List<INamedTypeSymbol> symbols)
631633 }
632634
633635 var syntaxRoot = await syntaxTree . GetRootAsync ( ct ) ;
634- var syntaxNode = syntaxRoot . FindNode ( location . SourceSpan ) ;
635636
636- var nodeToModify = GetNodeToModify ( syntaxNode ) ;
637- if ( nodeToModify == null )
637+ // Modify each location
638+ foreach ( var location in group . OrderByDescending ( l => l . SourceSpan . Start ) )
638639 {
639- continue ;
640- }
640+ var syntaxNode = syntaxRoot . FindNode ( location . SourceSpan ) ;
641641
642- var newNode = Visit ( nodeToModify ) ;
643- var newRoot = syntaxRoot . ReplaceNode ( nodeToModify , newNode ) ;
644- var newDocument = document . WithSyntaxRoot ( newRoot ) ;
642+ var nodeToModify = GetNodeToModify ( syntaxNode ) ;
643+ if ( nodeToModify == null )
644+ {
645+ continue ;
646+ }
647+
648+ var newNode = Visit ( nodeToModify ) ;
649+ syntaxRoot = syntaxRoot . ReplaceNode ( nodeToModify , newNode ) ;
650+ }
645651
652+ // Commit the changes to the project
653+ var newDocument = document . WithSyntaxRoot ( syntaxRoot ) ;
646654 project = newDocument . Project ;
647655 }
648656
0 commit comments