diff --git a/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs b/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs index 7917db5f..2fa8bbe2 100644 --- a/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs +++ b/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using System.Text; using CommunityToolkit.Mvvm.SourceGenerators.ComponentModel.Models; using CommunityToolkit.Mvvm.SourceGenerators.Extensions; using CommunityToolkit.Mvvm.SourceGenerators.Models; @@ -93,7 +94,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) // Insert all members into the same partial type declaration CompilationUnitSyntax compilationUnit = item.Hierarchy.GetCompilationUnit(memberDeclarations); - context.AddSource(item.Hierarchy.FilenameHint, compilationUnit.ToFullString()); + context.AddSource(item.Hierarchy.FilenameHint, compilationUnit.GetText(Encoding.UTF8)); }); // Gather all property changing names @@ -111,7 +112,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) if (compilationUnit is not null) { - context.AddSource("__KnownINotifyPropertyChangingArgs", compilationUnit.ToFullString()); + context.AddSource("__KnownINotifyPropertyChangingArgs", compilationUnit.GetText(Encoding.UTF8)); } }); @@ -130,7 +131,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) if (compilationUnit is not null) { - context.AddSource("__KnownINotifyPropertyChangedArgs", compilationUnit.ToFullString()); + context.AddSource("__KnownINotifyPropertyChangedArgs", compilationUnit.GetText(Encoding.UTF8)); } }); } diff --git a/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableRecipientGenerator.cs b/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableRecipientGenerator.cs index b18c934e..e16a161d 100644 --- a/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableRecipientGenerator.cs +++ b/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableRecipientGenerator.cs @@ -117,19 +117,18 @@ protected override ImmutableArray FilterDeclaredMembers // If the target type has no constructors, generate constructors as well if (!info.HasExplicitConstructors) { - foreach (ConstructorDeclarationSyntax ctor in memberDeclarations.OfType()) + foreach (ConstructorDeclarationSyntax originalConstructor in memberDeclarations.OfType()) { - string text = ctor.NormalizeWhitespace().ToFullString(); - string replaced = text.Replace("ObservableRecipient", info.TypeName); + ConstructorDeclarationSyntax modifiedConstructor = originalConstructor.WithIdentifier(Identifier(info.TypeName)); // Adjust the visibility of the constructors based on whether the target type is abstract. // If that is not the case, the constructors have to be declared as public and not protected. if (!info.IsAbstract) { - replaced = replaced.Replace("protected", "public"); + modifiedConstructor = modifiedConstructor.WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword))); } - builder.Add((ConstructorDeclarationSyntax)ParseMemberDeclaration(replaced)!); + builder.Add(modifiedConstructor); } } diff --git a/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableValidatorValidateAllPropertiesGenerator.cs b/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableValidatorValidateAllPropertiesGenerator.cs index 3a11f2f8..6273285e 100644 --- a/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableValidatorValidateAllPropertiesGenerator.cs +++ b/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableValidatorValidateAllPropertiesGenerator.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Linq; +using System.Text; using CommunityToolkit.Mvvm.SourceGenerators.Extensions; using CommunityToolkit.Mvvm.SourceGenerators.Input.Models; using Microsoft.CodeAnalysis; @@ -58,7 +59,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) { CompilationUnitSyntax compilationUnit = Execute.GetSyntax(item); - context.AddSource("__ObservableValidatorExtensions", compilationUnit.ToFullString()); + context.AddSource("__ObservableValidatorExtensions", compilationUnit.GetText(Encoding.UTF8)); }); // Generate the class with all validation methods @@ -66,7 +67,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) { CompilationUnitSyntax compilationUnit = Execute.GetSyntax(item); - context.AddSource(item.FilenameHint, compilationUnit.ToFullString()); + context.AddSource(item.FilenameHint, compilationUnit.GetText(Encoding.UTF8)); }); } } diff --git a/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/TransitiveMembersGenerator.cs b/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/TransitiveMembersGenerator.cs index b4029685..17316ab3 100644 --- a/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/TransitiveMembersGenerator.cs +++ b/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/TransitiveMembersGenerator.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using System.Text; using CommunityToolkit.Mvvm.SourceGenerators.Extensions; using CommunityToolkit.Mvvm.SourceGenerators.Models; using Microsoft.CodeAnalysis; @@ -121,7 +122,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) ImmutableArray filteredMemberDeclarations = FilterDeclaredMembers(item.Info, sourceMemberDeclarations); CompilationUnitSyntax compilationUnit = item.Hierarchy.GetCompilationUnit(filteredMemberDeclarations, this.classDeclaration.BaseList); - context.AddSource(item.Hierarchy.FilenameHint, compilationUnit.ToFullString()); + context.AddSource(item.Hierarchy.FilenameHint, compilationUnit.GetText(Encoding.UTF8)); }); } diff --git a/CommunityToolkit.Mvvm.SourceGenerators/Input/ICommandGenerator.cs b/CommunityToolkit.Mvvm.SourceGenerators/Input/ICommandGenerator.cs index dde7b56e..0cc82aa9 100644 --- a/CommunityToolkit.Mvvm.SourceGenerators/Input/ICommandGenerator.cs +++ b/CommunityToolkit.Mvvm.SourceGenerators/Input/ICommandGenerator.cs @@ -4,6 +4,7 @@ using System.Collections.Immutable; using System.Linq; +using System.Text; using CommunityToolkit.Mvvm.SourceGenerators.Extensions; using CommunityToolkit.Mvvm.SourceGenerators.Input.Models; using CommunityToolkit.Mvvm.SourceGenerators.Models; @@ -68,7 +69,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) ImmutableArray memberDeclarations = Execute.GetSyntax(item.Info); CompilationUnitSyntax compilationUnit = item.Hierarchy.GetCompilationUnit(memberDeclarations); - context.AddSource($"{item.Hierarchy.FilenameHint}.{item.Info.MethodName}", compilationUnit.ToFullString()); + context.AddSource($"{item.Hierarchy.FilenameHint}.{item.Info.MethodName}", compilationUnit.GetText(Encoding.UTF8)); }); } } diff --git a/CommunityToolkit.Mvvm.SourceGenerators/Messaging/IMessengerRegisterAllGenerator.cs b/CommunityToolkit.Mvvm.SourceGenerators/Messaging/IMessengerRegisterAllGenerator.cs index 8cb0b29c..b3029c2d 100644 --- a/CommunityToolkit.Mvvm.SourceGenerators/Messaging/IMessengerRegisterAllGenerator.cs +++ b/CommunityToolkit.Mvvm.SourceGenerators/Messaging/IMessengerRegisterAllGenerator.cs @@ -4,6 +4,7 @@ using System.Collections.Immutable; using System.Linq; +using System.Text; using CommunityToolkit.Mvvm.SourceGenerators.Extensions; using CommunityToolkit.Mvvm.SourceGenerators.Input.Models; using Microsoft.CodeAnalysis; @@ -66,7 +67,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) { CompilationUnitSyntax compilationUnit = Execute.GetSyntax(item); - context.AddSource("__IMessengerExtensions", compilationUnit.ToFullString()); + context.AddSource("__IMessengerExtensions", compilationUnit.GetText(Encoding.UTF8)); }); // Generate the class with all registration methods @@ -74,7 +75,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) { CompilationUnitSyntax compilationUnit = Execute.GetSyntax(item); - context.AddSource(item.FilenameHint, compilationUnit.ToFullString()); + context.AddSource(item.FilenameHint, compilationUnit.GetText(Encoding.UTF8)); }); } }