File tree Expand file tree Collapse file tree 7 files changed +43
-14
lines changed Expand file tree Collapse file tree 7 files changed +43
-14
lines changed Original file line number Diff line number Diff line change 55
55
<Compile Include =" $(MSBuildThisFileDirectory)Extensions\IncrementalGeneratorInitializationContextExtensions.cs" />
56
56
<Compile Include =" $(MSBuildThisFileDirectory)Extensions\IncrementalValuesProviderExtensions.cs" />
57
57
<Compile Include =" $(MSBuildThisFileDirectory)Extensions\ISymbolExtensions.cs" />
58
+ <Compile Include =" $(MSBuildThisFileDirectory)Extensions\SourceProductionContextExtensions.cs" />
58
59
<Compile Include =" $(MSBuildThisFileDirectory)Extensions\ITypeSymbolExtensions.cs" />
59
60
<Compile Include =" $(MSBuildThisFileDirectory)Extensions\MemberDeclarationSyntaxExtensions.cs" />
60
61
<Compile Include =" $(MSBuildThisFileDirectory)Extensions\SyntaxNodeExtensions.cs" />
Original file line number Diff line number Diff line change 4
4
5
5
using System . Collections . Immutable ;
6
6
using System . Linq ;
7
- using System . Text ;
8
7
using CommunityToolkit . Mvvm . SourceGenerators . ComponentModel . Models ;
9
8
using CommunityToolkit . Mvvm . SourceGenerators . Extensions ;
10
9
using CommunityToolkit . Mvvm . SourceGenerators . Helpers ;
@@ -75,7 +74,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
75
74
// Insert all members into the same partial type declaration
76
75
CompilationUnitSyntax compilationUnit = item . Hierarchy . GetCompilationUnit ( memberDeclarations ) ;
77
76
78
- context . AddSource ( $ "{ item . Hierarchy . FilenameHint } .g.cs", compilationUnit . GetText ( Encoding . UTF8 ) ) ;
77
+ context . AddSource ( $ "{ item . Hierarchy . FilenameHint } .g.cs", compilationUnit ) ;
79
78
} ) ;
80
79
81
80
// Gather all property changing names
@@ -92,7 +91,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
92
91
93
92
if ( compilationUnit is not null )
94
93
{
95
- context . AddSource ( "__KnownINotifyPropertyChangingArgs.g.cs" , compilationUnit . GetText ( Encoding . UTF8 ) ) ;
94
+ context . AddSource ( "__KnownINotifyPropertyChangingArgs.g.cs" , compilationUnit ) ;
96
95
}
97
96
} ) ;
98
97
@@ -110,7 +109,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
110
109
111
110
if ( compilationUnit is not null )
112
111
{
113
- context . AddSource ( "__KnownINotifyPropertyChangedArgs.g.cs" , compilationUnit . GetText ( Encoding . UTF8 ) ) ;
112
+ context . AddSource ( "__KnownINotifyPropertyChangedArgs.g.cs" , compilationUnit ) ;
114
113
}
115
114
} ) ;
116
115
}
Original file line number Diff line number Diff line change 3
3
// See the LICENSE file in the project root for more information.
4
4
5
5
using System . Linq ;
6
- using System . Text ;
7
6
using CommunityToolkit . Mvvm . SourceGenerators . ComponentModel . Models ;
8
7
using CommunityToolkit . Mvvm . SourceGenerators . Extensions ;
9
8
using Microsoft . CodeAnalysis ;
@@ -79,15 +78,15 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
79
78
{
80
79
CompilationUnitSyntax compilationUnit = Execute . GetSyntax ( item ) ;
81
80
82
- context . AddSource ( "__ObservableValidatorExtensions.g.cs" , compilationUnit . GetText ( Encoding . UTF8 ) ) ;
81
+ context . AddSource ( "__ObservableValidatorExtensions.g.cs" , compilationUnit ) ;
83
82
} ) ;
84
83
85
84
// Generate the class with all validation methods
86
85
context . RegisterImplementationSourceOutput ( validationInfo , static ( context , item ) =>
87
86
{
88
87
CompilationUnitSyntax compilationUnit = Execute . GetSyntax ( item ) ;
89
88
90
- context . AddSource ( $ "{ item . FilenameHint } .g.cs", compilationUnit . GetText ( Encoding . UTF8 ) ) ;
89
+ context . AddSource ( $ "{ item . FilenameHint } .g.cs", compilationUnit ) ;
91
90
} ) ;
92
91
}
93
92
}
Original file line number Diff line number Diff line change 5
5
using System ;
6
6
using System . Collections . Immutable ;
7
7
using System . Linq ;
8
- using System . Text ;
9
8
using CommunityToolkit . Mvvm . SourceGenerators . Extensions ;
10
9
using CommunityToolkit . Mvvm . SourceGenerators . Models ;
11
10
using Microsoft . CodeAnalysis ;
@@ -108,7 +107,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
108
107
ImmutableArray < MemberDeclarationSyntax > updatedMemberDeclarations = Execute . AdjustMemberDeclarationNullabilityAnnotations ( filteredMemberDeclarations , item . MetadataInfo . IsNullabilitySupported ) ;
109
108
CompilationUnitSyntax compilationUnit = item . Hierarchy . GetCompilationUnit ( updatedMemberDeclarations , this . classDeclaration . BaseList ) ;
110
109
111
- context . AddSource ( $ "{ item . Hierarchy . FilenameHint } .g.cs", compilationUnit . GetText ( Encoding . UTF8 ) ) ;
110
+ context . AddSource ( $ "{ item . Hierarchy . FilenameHint } .g.cs", compilationUnit ) ;
112
111
} ) ;
113
112
}
114
113
Original file line number Diff line number Diff line change
1
+ // Licensed to the .NET Foundation under one or more agreements.
2
+ // The .NET Foundation licenses this file to you under the MIT license.
3
+ // See the LICENSE file in the project root for more information.
4
+
5
+ using System . Text ;
6
+ using Microsoft . CodeAnalysis ;
7
+ using Microsoft . CodeAnalysis . CSharp . Syntax ;
8
+
9
+ namespace CommunityToolkit . Mvvm . SourceGenerators . Extensions ;
10
+
11
+ /// <summary>
12
+ /// Extension methods for the <see cref="SourceProductionContext"/> type.
13
+ /// </summary>
14
+ internal static class SourceProductionContextExtensions
15
+ {
16
+ /// <summary>
17
+ /// Adds a new source file to a target <see cref="SourceProductionContext"/> instance.
18
+ /// </summary>
19
+ /// <param name="context">The input <see cref="SourceProductionContext"/> instance to use.</param>
20
+ /// <param name="name">The name of the source file to add.</param>
21
+ /// <param name="compilationUnit">The <see cref="CompilationUnitSyntax"/> instance representing the syntax tree to add.</param>
22
+ public static void AddSource ( this SourceProductionContext context , string name , CompilationUnitSyntax compilationUnit )
23
+ {
24
+ #if ! ROSLYN_4_3_1_OR_GREATER
25
+ // We're fine with the extra allocation in the few cases where adjusting the filename is necessary.
26
+ // This will only ever be done when code generation is executed again anyway, which is a slow path.
27
+ name = name . Replace ( '+' , '_' ) . Replace ( '`' , '_' ) ;
28
+ #endif
29
+
30
+ // Add the UTF8 text for the input compilation unit
31
+ context . AddSource ( name , compilationUnit . GetText ( Encoding . UTF8 ) ) ;
32
+ }
33
+ }
Original file line number Diff line number Diff line change 4
4
5
5
using System . Collections . Immutable ;
6
6
using System . Linq ;
7
- using System . Text ;
8
7
using CommunityToolkit . Mvvm . SourceGenerators . Extensions ;
9
8
using CommunityToolkit . Mvvm . SourceGenerators . Input . Models ;
10
9
using CommunityToolkit . Mvvm . SourceGenerators . Models ;
@@ -61,7 +60,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
61
60
ImmutableArray < MemberDeclarationSyntax > memberDeclarations = Execute . GetSyntax ( item . Info . Value ) ;
62
61
CompilationUnitSyntax compilationUnit = item . Hierarchy . GetCompilationUnit ( memberDeclarations ) ;
63
62
64
- context . AddSource ( $ "{ item . Hierarchy . FilenameHint } .{ item . Info . Value . MethodName } .g.cs", compilationUnit . GetText ( Encoding . UTF8 ) ) ;
63
+ context . AddSource ( $ "{ item . Hierarchy . FilenameHint } .{ item . Info . Value . MethodName } .g.cs", compilationUnit ) ;
65
64
} ) ;
66
65
}
67
66
}
Original file line number Diff line number Diff line change 4
4
5
5
using System . Collections . Immutable ;
6
6
using System . Linq ;
7
- using System . Text ;
8
7
using CommunityToolkit . Mvvm . SourceGenerators . Extensions ;
9
8
using CommunityToolkit . Mvvm . SourceGenerators . Messaging . Models ;
10
9
using Microsoft . CodeAnalysis ;
@@ -84,15 +83,15 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
84
83
{
85
84
CompilationUnitSyntax compilationUnit = Execute . GetSyntax ( item ) ;
86
85
87
- context . AddSource ( "__IMessengerExtensions.g.cs" , compilationUnit . GetText ( Encoding . UTF8 ) ) ;
86
+ context . AddSource ( "__IMessengerExtensions.g.cs" , compilationUnit ) ;
88
87
} ) ;
89
88
90
89
// Generate the class with all registration methods
91
90
context . RegisterImplementationSourceOutput ( recipientInfo , static ( context , item ) =>
92
91
{
93
92
CompilationUnitSyntax compilationUnit = Execute . GetSyntax ( item ) ;
94
93
95
- context . AddSource ( $ "{ item . FilenameHint } .g.cs", compilationUnit . GetText ( Encoding . UTF8 ) ) ;
94
+ context . AddSource ( $ "{ item . FilenameHint } .g.cs", compilationUnit ) ;
96
95
} ) ;
97
96
}
98
97
}
You can’t perform that action at this time.
0 commit comments