Minor code refactoring

This commit is contained in:
Sergio Pedri
2021-07-21 02:32:09 +02:00
parent a72a683ea2
commit d5cb56aec9
2 changed files with 4 additions and 22 deletions
@@ -107,20 +107,6 @@ namespace Microsoft.Toolkit.Mvvm.SourceGenerators
{
ClassDeclarationSyntax sourceDeclaration = sourceSyntaxTree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().First();
UsingDirectiveSyntax[] usingDirectives = sourceSyntaxTree.GetRoot().DescendantNodes().OfType<UsingDirectiveSyntax>().ToArray();
BaseListSyntax? baseListSyntax = BaseList(SeparatedList(
sourceDeclaration.BaseList?.Types
.OfType<SimpleBaseTypeSyntax>()
.Select(static t => t.Type)
.OfType<IdentifierNameSyntax>()
.Where(static t => t.Identifier.ValueText.StartsWith("I"))
.Select(static t => SimpleBaseType(t))
.ToArray()
?? Array.Empty<BaseTypeSyntax>()));
if (baseListSyntax.Types.Count == 0)
{
baseListSyntax = null;
}
// Create the class declaration for the user type. This will produce a tree as follows:
//
@@ -131,7 +117,7 @@ namespace Microsoft.Toolkit.Mvvm.SourceGenerators
var classDeclarationSyntax =
ClassDeclaration(classDeclaration.Identifier.Text)
.WithModifiers(classDeclaration.Modifiers)
.WithBaseList(baseListSyntax)
.WithBaseList(sourceDeclaration.BaseList)
.AddMembers(FilterDeclaredMembers(context, attributeData, classDeclaration, classDeclarationSymbol, sourceDeclaration).ToArray());
TypeDeclarationSyntax typeDeclarationSyntax = classDeclarationSyntax;
@@ -151,16 +137,12 @@ namespace Microsoft.Toolkit.Mvvm.SourceGenerators
// From this, we can finally generate the source code to output.
var namespaceName = classDeclarationSymbol.ContainingNamespace.ToDisplayString(new(typeQualificationStyle: NameAndContainingTypesAndNamespaces));
// Create the final compilation unit to generate (with using directives and the full type declaration)
var source =
CompilationUnit()
.AddMembers(NamespaceDeclaration(IdentifierName(namespaceName))
.AddMembers(typeDeclarationSyntax))
.AddUsings(usingDirectives.First().WithLeadingTrivia(TriviaList(
Comment("// Licensed to the .NET Foundation under one or more agreements."),
Comment("// The .NET Foundation licenses this file to you under the MIT license."),
Comment("// See the LICENSE file in the project root for more information."),
Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true)))))
.AddUsings(usingDirectives.Skip(1).ToArray())
.AddUsings(usingDirectives)
.NormalizeWhitespace()
.ToFullString();
@@ -18,7 +18,7 @@ namespace Microsoft.Toolkit.Mvvm.ComponentModel
/// A base class for observable objects that also acts as recipients for messages. This class is an extension of
/// <see cref="ObservableObject"/> which also provides built-in support to use the <see cref="IMessenger"/> type.
/// </summary>
public abstract class ObservableRecipient : ObservableObject
public abstract class ObservableRecipient
{
/// <summary>
/// Initializes a new instance of the <see cref="ObservableRecipient"/> class.