Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove temporary strings in source generators formatting #281

Merged
merged 2 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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));
}
});

Expand All @@ -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));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,18 @@ protected override ImmutableArray<MemberDeclarationSyntax> FilterDeclaredMembers
// If the target type has no constructors, generate constructors as well
if (!info.HasExplicitConstructors)
{
foreach (ConstructorDeclarationSyntax ctor in memberDeclarations.OfType<ConstructorDeclarationSyntax>())
foreach (ConstructorDeclarationSyntax originalConstructor in memberDeclarations.OfType<ConstructorDeclarationSyntax>())
{
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)));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the constructor has other modifiers as well, like unsafe? This would remove all of them. Or is this constructor not from user code but generated by the generator and it's guaranteed that it won't have other modifiers?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If preserving other modifiers is needed, try something like this:

modifiedConstructor = modifiedConstructor.WithModifiers(
    modifiedConstructor.Modifiers.Replace(
        modifiedConstructor.Modifiers.First(token => token.Kind() is SyntaxKind.ProtectedKeyword),
        SyntaxFactory.Token(SyntaxKind.PublicKeyword)));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The syntax tree is from the ObservableRecipient.cs embedded resource file, so yeah we can assume that protected is the only modifier that could possibly be there. These trees are not provided by code parsed from consumers 🙂

}

builder.Add((ConstructorDeclarationSyntax)ParseMemberDeclaration(replaced)!);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh it was being reparsed just to replace a token... I'm glad this is fixed 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, looking back at this it was indeed quite bad ahah

builder.Add(modifiedConstructor);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,15 +59,15 @@ 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
context.RegisterImplementationSourceOutput(validationInfo, static (context, item) =>
{
CompilationUnitSyntax compilationUnit = Execute.GetSyntax(item);

context.AddSource(item.FilenameHint, compilationUnit.ToFullString());
context.AddSource(item.FilenameHint, compilationUnit.GetText(Encoding.UTF8));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -121,7 +122,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
ImmutableArray<MemberDeclarationSyntax> 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));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,7 +69,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
ImmutableArray<MemberDeclarationSyntax> 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));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,15 +67,15 @@ 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
context.RegisterImplementationSourceOutput(recipientInfo, static (context, item) =>
{
CompilationUnitSyntax compilationUnit = Execute.GetSyntax(item);

context.AddSource(item.FilenameHint, compilationUnit.ToFullString());
context.AddSource(item.FilenameHint, compilationUnit.GetText(Encoding.UTF8));
});
}
}