77using System . Xml ;
88
99using Microsoft . CodeAnalysis ;
10+ using Microsoft . CodeAnalysis . CSharp ;
1011using Microsoft . CodeAnalysis . Text ;
1112
1213using Microsoft . Maui . Controls . Xaml ;
@@ -32,6 +33,10 @@ public void Initialize(IncrementalGeneratorInitializationContext initContext)
3233 . WithComparer ( new CompilationReferencesComparer ( ) )
3334 . WithTrackingName ( TrackingNames . ReferenceCompilationProvider ) ;
3435
36+ var referenceTypeCacheProvider = referenceCompilationProvider
37+ . Select ( GetTypeCache )
38+ . WithTrackingName ( TrackingNames . ReferenceTypeCacheProvider ) ;
39+
3540 var xmlnsDefinitionsProvider = referenceCompilationProvider
3641 . Select ( GetAssemblyAttributes )
3742 . WithTrackingName ( TrackingNames . XmlnsDefinitionsProvider ) ;
@@ -57,27 +62,27 @@ public void Initialize(IncrementalGeneratorInitializationContext initContext)
5762 . Where ( static p => p ? . Kind == "Css" )
5863 . WithTrackingName ( TrackingNames . CssProjectItemProvider ) ;
5964
60- var referenceTypeCacheProvider = referenceCompilationProvider
61- . Select ( GetTypeCache )
62- . WithTrackingName ( TrackingNames . ReferenceTypeCacheProvider ) ;
63-
6465 var xamlSourceProviderForCB = xamlProjectItemProviderForCB
6566 . Combine ( xmlnsDefinitionsProvider , referenceTypeCacheProvider , referenceCompilationProvider )
67+ . Select ( GetSource )
6668 . WithTrackingName ( TrackingNames . XamlSourceProviderForCB ) ;
6769
6870 var compilationWithCodeBehindProvider = xamlSourceProviderForCB
69- . Select ( GetSyntaxTree )
7071 . Collect ( )
7172 . Combine ( referenceCompilationProvider )
7273 . Select ( static ( t , ct ) =>
7374 {
7475 var compilation = t . Right ;
75- foreach ( var tree in t . Left )
76+ var options = compilation . SyntaxTrees . FirstOrDefault ( ) ? . Options as CSharpParseOptions ;
77+ foreach ( var ( source , xamlItem , diagnostics ) in t . Left )
7678 {
7779 ct . ThrowIfCancellationRequested ( ) ;
7880
79- if ( tree is not null )
81+ if ( source is not null )
82+ {
83+ var tree = CSharpSyntaxTree . ParseText ( source , options : options , cancellationToken : ct ) ;
8084 compilation = compilation . AddSyntaxTrees ( tree ) ;
85+ }
8186 }
8287 return compilation ;
8388 } )
@@ -95,12 +100,15 @@ public void Initialize(IncrementalGeneratorInitializationContext initContext)
95100 // Register the XAML pipeline for CodeBehind
96101 initContext . RegisterSourceOutput ( xamlSourceProviderForCB , static ( sourceProductionContext , provider ) =>
97102 {
98- var ( xamlItem , xmlnsCache , typeCache , compilation ) = provider ;
103+ var ( source , xamlItem , diagnostics ) = provider ;
99104
100105 try
101106 {
102- var code = CodeBehindCodeWriter . GenerateXamlCodeBehind ( xamlItem , compilation , sourceProductionContext . ReportDiagnostic , sourceProductionContext . CancellationToken , xmlnsCache , typeCache ) ;
103- sourceProductionContext . AddSource ( GetHintName ( xamlItem ? . ProjectItem , "sg" ) , code ) ;
107+ if ( diagnostics != null )
108+ foreach ( var diag in diagnostics )
109+ sourceProductionContext . ReportDiagnostic ( diag ) ;
110+ if ( source != null )
111+ sourceProductionContext . AddSource ( GetHintName ( xamlItem ? . ProjectItem , "sg" ) , source ) ;
104112 }
105113 catch ( Exception e )
106114 {
0 commit comments