@@ -31,14 +31,14 @@ public static string Apply(string template, string structIdType, string valueTyp
3131
3232 public static string Apply ( string template , string valueType , bool normalizeWhitespace = false )
3333 {
34- var applied = ApplyImpl ( Parse ( template ) , valueType ) ;
34+ var applied = ApplyValueImpl ( Parse ( template ) , valueType ) ;
3535
3636 return normalizeWhitespace ?
3737 applied . NormalizeWhitespace ( ) . ToFullString ( ) . Trim ( ) :
3838 applied . ToFullString ( ) . Trim ( ) ;
3939 }
4040
41- public static SyntaxNode ApplyValue ( this SyntaxNode node , INamedTypeSymbol valueType ) => ApplyImpl ( node , valueType . ToFullName ( ) ) ;
41+ public static SyntaxNode ApplyValue ( this SyntaxNode node , INamedTypeSymbol valueType ) => ApplyValueImpl ( node , valueType . ToFullName ( ) ) ;
4242
4343 public static SyntaxNode Apply ( this SyntaxNode node , INamedTypeSymbol structId )
4444 {
@@ -59,7 +59,7 @@ public static SyntaxNode Apply(this SyntaxNode node, INamedTypeSymbol structId)
5959 return ApplyImpl ( root , structId . Name , tid , targetNamespace , corens ) ;
6060 }
6161
62- static SyntaxNode ApplyImpl ( this SyntaxNode node , string valueType )
62+ static SyntaxNode ApplyValueImpl ( this SyntaxNode node , string valueType )
6363 {
6464 var root = node . SyntaxTree . GetCompilationUnitRoot ( ) ;
6565 if ( root == null )
@@ -194,7 +194,7 @@ bool IsFileLocal(TypeDeclarationSyntax node) =>
194194 ! node . AttributeLists . Any ( list => list . Attributes . Any ( a => a . IsValueTemplate ( ) ) ) ;
195195 }
196196
197- class TemplateRewriter ( string tself , string tid ) : CSharpSyntaxRewriter
197+ class TemplateRewriter ( string tself , string tvalue ) : CSharpSyntaxRewriter
198198 {
199199 public override SyntaxNode ? VisitRecordDeclaration ( RecordDeclarationSyntax node )
200200 {
@@ -282,8 +282,20 @@ class TemplateRewriter(string tself, string tid) : CSharpSyntaxRewriter
282282 return IdentifierName ( tself )
283283 . WithLeadingTrivia ( node . Identifier . LeadingTrivia )
284284 . WithTrailingTrivia ( node . Identifier . TrailingTrivia ) ;
285- else if ( node . Identifier . Text == "TId" || node . Identifier . Text == "TValue" )
286- return IdentifierName ( tid )
285+
286+ if ( node . Identifier . Text . StartsWith ( "TSelf_" ) )
287+ return IdentifierName ( node . Identifier . Text . Replace ( "TSelf_" , tvalue . Replace ( '.' , '_' ) + "_" ) )
288+ . WithLeadingTrivia ( node . Identifier . LeadingTrivia )
289+ . WithTrailingTrivia ( node . Identifier . TrailingTrivia ) ;
290+
291+ // TODO: remove TId as it's legacy
292+ if ( node . Identifier . Text == "TId" || node . Identifier . Text == "TValue" )
293+ return IdentifierName ( tvalue )
294+ . WithLeadingTrivia ( node . Identifier . LeadingTrivia )
295+ . WithTrailingTrivia ( node . Identifier . TrailingTrivia ) ;
296+
297+ if ( node . Identifier . Text . StartsWith ( "TValue_" ) )
298+ return IdentifierName ( node . Identifier . Text . Replace ( "TValue_" , tvalue . Replace ( '.' , '_' ) + "_" ) )
287299 . WithLeadingTrivia ( node . Identifier . LeadingTrivia )
288300 . WithTrailingTrivia ( node . Identifier . TrailingTrivia ) ;
289301
@@ -297,8 +309,20 @@ public override SyntaxToken VisitToken(SyntaxToken token)
297309 return Identifier ( tself )
298310 . WithLeadingTrivia ( token . LeadingTrivia )
299311 . WithTrailingTrivia ( token . TrailingTrivia ) ;
300- else if ( token . IsKind ( SyntaxKind . IdentifierToken ) && ( token . Text == "TId" || token . Text == "TValue" ) )
301- return Identifier ( tid )
312+
313+ if ( token . IsKind ( SyntaxKind . IdentifierToken ) && token . Text . StartsWith ( "TSelf_" ) )
314+ return Identifier ( token . Text . Replace ( "TSelf_" , tvalue . Replace ( '.' , '_' ) + "_" ) )
315+ . WithLeadingTrivia ( token . LeadingTrivia )
316+ . WithTrailingTrivia ( token . TrailingTrivia ) ;
317+
318+ // TODO: remove TId as it's legacy
319+ if ( token . IsKind ( SyntaxKind . IdentifierToken ) && ( token . Text == "TId" || token . Text == "TValue" ) )
320+ return Identifier ( tvalue )
321+ . WithLeadingTrivia ( token . LeadingTrivia )
322+ . WithTrailingTrivia ( token . TrailingTrivia ) ;
323+
324+ if ( token . IsKind ( SyntaxKind . IdentifierToken ) && token . Text . StartsWith ( "TValue_" ) )
325+ return Identifier ( token . Text . Replace ( "TValue_" , tvalue . Replace ( '.' , '_' ) + "_" ) )
302326 . WithLeadingTrivia ( token . LeadingTrivia )
303327 . WithTrailingTrivia ( token . TrailingTrivia ) ;
304328
0 commit comments