66namespace StructId ;
77
88/// <summary>
9- /// Represents a template for the value type of struct ids.
9+ /// Represents a templatized value type of a struct ids.
1010/// </summary>
11- /// <param name="TValue">The type of value the struct id holds, such as Guid or string.</param>
11+ /// <param name="TValue">The type of value the a struct id holds, such as Guid or string.</param>
1212/// <param name="Template">The template to apply to it.</param>
13- record TValueTemplate ( INamedTypeSymbol TValue , TValueTemplateInfo Template )
13+ record TemplatizedTValue ( INamedTypeSymbol TValue , TValueTemplate Template )
1414{
1515 SyntaxNode ? applied ;
1616
@@ -26,10 +26,26 @@ record TValueTemplate(INamedTypeSymbol TValue, TValueTemplateInfo Template)
2626 public string Render ( ) => Declaration . ToFullString ( ) ;
2727}
2828
29- record TValueTemplateInfo ( INamedTypeSymbol TTemplate , KnownTypes KnownTypes )
29+ /// <summary>
30+ /// Represents a generic file-local template that applies to TValues that match the template
31+ /// constraints.
32+ /// </summary>
33+ /// <param name="TTemplate">The declared symbol of the template in the compilation.</param>
34+ /// <param name="KnownTypes">Useful known types for use when applying the template.</param>
35+ record TValueTemplate ( INamedTypeSymbol TTemplate , KnownTypes KnownTypes )
3036{
37+ /// <summary>
38+ /// Syntax root of the file declaring the template.
39+ /// </summary>
3140 public SyntaxNode Syntax { get ; } = TTemplate . DeclaringSyntaxReferences [ 0 ] . GetSyntax ( ) . SyntaxTree . GetRoot ( ) ;
3241
42+ /// <summary>
43+ /// Whether the template should not be applied to string value types.
44+ /// </summary>
45+ /// <remarks>
46+ /// Since strings implement also a bunch of interfaces, an easy way to exclude them
47+ /// from matching a struct value template that has a restriction on just
48+ /// </remarks>
3349 public bool NoString { get ; } = new NoStringSyntaxWalker ( ) . Accept (
3450 TTemplate . DeclaringSyntaxReferences [ 0 ] . GetSyntax ( ) . SyntaxTree . GetRoot ( ) ) ;
3551
@@ -66,9 +82,12 @@ public bool AppliesTo(INamedTypeSymbol valueType)
6682 }
6783}
6884
69- static class TValueTemplateExtensions
85+ static class TemplatizedTValueExtensions
7086{
71- public static IncrementalValuesProvider < TValueTemplate > SelectTemplatizedValues ( this IncrementalGeneratorInitializationContext context )
87+ /// <summary>
88+ /// Gets all instantiations of TValue templates that apply to the struct ids in the compilation.
89+ /// </summary>
90+ public static IncrementalValuesProvider < TemplatizedTValue > SelectTemplatizedValues ( this IncrementalGeneratorInitializationContext context )
7291 {
7392 var structIdNamespace = context . AnalyzerConfigOptionsProvider . GetStructIdNamespace ( ) ;
7493
@@ -87,7 +106,7 @@ public static IncrementalValuesProvider<TValueTemplate> SelectTemplatizedValues(
87106 r => r . GetSyntax ( ) is TypeDeclarationSyntax declaration && x . GetAttributes ( ) . Any (
88107 a => a . IsValueTemplate ( ) ) ) )
89108 . Combine ( known )
90- . Select ( ( x , cancellation ) => new TValueTemplateInfo ( x . Left , x . Right ) )
109+ . Select ( ( x , cancellation ) => new TValueTemplate ( x . Left , x . Right ) )
91110 . Collect ( ) ;
92111
93112 var values = context . CompilationProvider
@@ -104,20 +123,9 @@ public static IncrementalValuesProvider<TValueTemplate> SelectTemplatizedValues(
104123 var tvalue = ( INamedTypeSymbol ) structId . TypeArguments [ 0 ] ;
105124 return templates
106125 . Where ( template => template . AppliesTo ( tvalue ) )
107- . Select ( template => new TValueTemplate ( tvalue , template ) ) ;
126+ . Select ( template => new TemplatizedTValue ( tvalue , template ) ) ;
108127 } ) ;
109128
110129 return values ;
111130 }
112-
113- //void GenerateCode(SourceProductionContext context, TIdTemplate source)
114- //{
115- // var templateFile = Path.GetFileNameWithoutExtension(source.Template.Syntax.SyntaxTree.FilePath);
116- // var hintName = $"{source.TValue.ToFileName()}/{templateFile}.cs";
117-
118- // var applied = source.Template.Syntax.Apply(source.TValue);
119- // var output = applied.ToFullString();
120-
121- // context.AddSource(hintName, SourceText.From(output, Encoding.UTF8));
122- //}
123131}
0 commit comments