@@ -15,6 +15,10 @@ namespace Silk.NET.SilkTouch.Mods.LocationTransformation;
1515/// <param name="includeCandidateLocations">Should candidate references or implicit references be renamed?</param>
1616public class IdentifierRenamingTransformer ( IEnumerable < ( ISymbol Symbol , string NewName ) > newNamesBySymbol , bool includeDeclarations = true , bool includeCandidateLocations = false ) : LocationTransformer
1717{
18+ // Identifiers can also be referenced within XML doc, which are trivia nodes.
19+ /// <inheritdoc />
20+ public override bool VisitIntoStructuredTrivia => true ;
21+
1822 private LocationTransformerContext _context ;
1923 private Dictionary < ISymbol , string > _newNameLookup = newNamesBySymbol . Select ( t => new KeyValuePair < ISymbol , string > ( t . Symbol , t . NewName ) ) . ToDictionary ( SymbolEqualityComparer . Default ) ;
2024
@@ -36,71 +40,116 @@ public class IdentifierRenamingTransformer(IEnumerable<(ISymbol Symbol, string N
3640 return current ;
3741 }
3842
39- private SyntaxToken GetNewName ( ) => Identifier ( _newNameLookup [ _context . Symbol ] ) ;
43+ private SyntaxToken GetNewName ( string currentName )
44+ {
45+ var symbolName = _context . Symbol switch
46+ {
47+ // Constructor/destructor symbols have a name of .ctor/.dtor, which isn't what we want
48+ IMethodSymbol { MethodKind : MethodKind . Constructor or MethodKind . Destructor } methodSymbol => methodSymbol . ContainingType . Name ,
49+ _ => _context . Symbol . Name ,
50+ } ;
51+
52+ if ( currentName != symbolName )
53+ {
54+ return Identifier ( currentName ) ;
55+ }
56+
57+ return Identifier ( _newNameLookup [ _context . Symbol ] ) ;
58+ }
4059
4160 /// <inheritdoc />
4261 public override SyntaxNode ? VisitIdentifierName ( IdentifierNameSyntax node )
43- => IdentifierName ( GetNewName ( ) ) ;
62+ => IdentifierName ( GetNewName ( node . Identifier . ValueText ) )
63+ . WithLeadingTrivia ( node . GetLeadingTrivia ( ) )
64+ . WithTrailingTrivia ( node . GetTrailingTrivia ( ) ) ;
4465
4566 // ----- Types -----
4667
4768 /// <inheritdoc />
4869 public override SyntaxNode ? VisitClassDeclaration ( ClassDeclarationSyntax node )
49- => node . WithIdentifier ( GetNewName ( ) ) ;
70+ => node . WithIdentifier ( GetNewName ( node . Identifier . ValueText ) )
71+ . WithLeadingTrivia ( node . GetLeadingTrivia ( ) . Select ( VisitTrivia ) )
72+ . WithTrailingTrivia ( node . GetTrailingTrivia ( ) ) ;
5073
5174 /// <inheritdoc />
5275 public override SyntaxNode ? VisitStructDeclaration ( StructDeclarationSyntax node )
53- => node . WithIdentifier ( GetNewName ( ) ) ;
76+ => node . WithIdentifier ( GetNewName ( node . Identifier . ValueText ) )
77+ . WithLeadingTrivia ( node . GetLeadingTrivia ( ) . Select ( VisitTrivia ) )
78+ . WithTrailingTrivia ( node . GetTrailingTrivia ( ) ) ;
5479
5580 /// <inheritdoc />
5681 public override SyntaxNode ? VisitInterfaceDeclaration ( InterfaceDeclarationSyntax node )
57- => node . WithIdentifier ( GetNewName ( ) ) ;
82+ => node . WithIdentifier ( GetNewName ( node . Identifier . ValueText ) )
83+ . WithLeadingTrivia ( node . GetLeadingTrivia ( ) . Select ( VisitTrivia ) )
84+ . WithTrailingTrivia ( node . GetTrailingTrivia ( ) ) ;
5885
5986 /// <inheritdoc />
6087 public override SyntaxNode ? VisitRecordDeclaration ( RecordDeclarationSyntax node )
61- => node . WithIdentifier ( GetNewName ( ) ) ;
88+ => node . WithIdentifier ( GetNewName ( node . Identifier . ValueText ) )
89+ . WithLeadingTrivia ( node . GetLeadingTrivia ( ) . Select ( VisitTrivia ) )
90+ . WithTrailingTrivia ( node . GetTrailingTrivia ( ) ) ;
6291
6392 /// <inheritdoc />
6493 public override SyntaxNode ? VisitDelegateDeclaration ( DelegateDeclarationSyntax node )
65- => node . WithIdentifier ( GetNewName ( ) ) ;
94+ => node . WithIdentifier ( GetNewName ( node . Identifier . ValueText ) )
95+ . WithLeadingTrivia ( node . GetLeadingTrivia ( ) . Select ( VisitTrivia ) )
96+ . WithTrailingTrivia ( node . GetTrailingTrivia ( ) ) ;
6697
6798 /// <inheritdoc />
6899 public override SyntaxNode ? VisitEnumDeclaration ( EnumDeclarationSyntax node )
69- => node . WithIdentifier ( GetNewName ( ) ) ;
100+ => node . WithIdentifier ( GetNewName ( node . Identifier . ValueText ) )
101+ . WithLeadingTrivia ( node . GetLeadingTrivia ( ) . Select ( VisitTrivia ) )
102+ . WithTrailingTrivia ( node . GetTrailingTrivia ( ) ) ;
70103
71104 // ----- Members -----
72105
73106 /// <inheritdoc />
74107 public override SyntaxNode ? VisitEnumMemberDeclaration ( EnumMemberDeclarationSyntax node )
75- => node . WithIdentifier ( GetNewName ( ) ) ;
108+ => node . WithIdentifier ( GetNewName ( node . Identifier . ValueText ) )
109+ . WithLeadingTrivia ( node . GetLeadingTrivia ( ) . Select ( VisitTrivia ) )
110+ . WithTrailingTrivia ( node . GetTrailingTrivia ( ) ) ;
76111
77112 /// <inheritdoc />
78113 public override SyntaxNode ? VisitEventDeclaration ( EventDeclarationSyntax node )
79- => node . WithIdentifier ( GetNewName ( ) ) ;
114+ => node . WithIdentifier ( GetNewName ( node . Identifier . ValueText ) )
115+ . WithLeadingTrivia ( node . GetLeadingTrivia ( ) . Select ( VisitTrivia ) )
116+ . WithTrailingTrivia ( node . GetTrailingTrivia ( ) ) ;
80117
81118 /// <inheritdoc />
82119 public override SyntaxNode ? VisitMethodDeclaration ( MethodDeclarationSyntax node )
83- => node . WithIdentifier ( GetNewName ( ) ) ;
120+ => node . WithIdentifier ( GetNewName ( node . Identifier . ValueText ) )
121+ . WithLeadingTrivia ( node . GetLeadingTrivia ( ) . Select ( VisitTrivia ) )
122+ . WithTrailingTrivia ( node . GetTrailingTrivia ( ) ) ;
84123
85124 /// <inheritdoc />
86125 public override SyntaxNode ? VisitPropertyDeclaration ( PropertyDeclarationSyntax node )
87- => node . WithIdentifier ( GetNewName ( ) ) ;
126+ => node . WithIdentifier ( GetNewName ( node . Identifier . ValueText ) )
127+ . WithLeadingTrivia ( node . GetLeadingTrivia ( ) . Select ( VisitTrivia ) )
128+ . WithTrailingTrivia ( node . GetTrailingTrivia ( ) ) ;
88129
89130 /// <inheritdoc />
90131 public override SyntaxNode ? VisitConstructorDeclaration ( ConstructorDeclarationSyntax node )
91- => node . WithIdentifier ( GetNewName ( ) ) ;
132+ => node . WithIdentifier ( GetNewName ( node . Identifier . ValueText ) )
133+ . WithLeadingTrivia ( node . GetLeadingTrivia ( ) . Select ( VisitTrivia ) )
134+ . WithTrailingTrivia ( node . GetTrailingTrivia ( ) ) ;
92135
93136 /// <inheritdoc />
94137 public override SyntaxNode ? VisitDestructorDeclaration ( DestructorDeclarationSyntax node )
95- => node . WithIdentifier ( GetNewName ( ) ) ;
138+ => node . WithIdentifier ( GetNewName ( node . Identifier . ValueText ) )
139+ . WithLeadingTrivia ( node . GetLeadingTrivia ( ) . Select ( VisitTrivia ) )
140+ . WithTrailingTrivia ( node . GetTrailingTrivia ( ) ) ;
96141
97142 // ----- Other -----
98143
99144 /// <inheritdoc />
100145 public override SyntaxNode ? VisitVariableDeclarator ( VariableDeclaratorSyntax node )
101- => node . WithIdentifier ( GetNewName ( ) ) ;
146+ => node . WithIdentifier ( GetNewName ( node . Identifier . ValueText ) )
147+ . WithLeadingTrivia ( node . GetLeadingTrivia ( ) )
148+ . WithTrailingTrivia ( node . GetTrailingTrivia ( ) ) ;
102149
103150 /// <inheritdoc />
104151 public override SyntaxNode ? VisitTypeParameter ( TypeParameterSyntax node )
105- => node . WithIdentifier ( GetNewName ( ) ) ;
152+ => node . WithIdentifier ( GetNewName ( node . Identifier . ValueText ) )
153+ . WithLeadingTrivia ( node . GetLeadingTrivia ( ) )
154+ . WithTrailingTrivia ( node . GetTrailingTrivia ( ) ) ;
106155}
0 commit comments