//
// PacketClassReceiver.cs
//
// Copyright (c) František Boháček. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace NosSmooth.PacketSerializersGenerator
{
///
/// Syntax receiver of classes with generate attribute.
///
public class PacketClassReceiver : ISyntaxReceiver
{
private readonly List _packetClasses;
///
/// Gets the name of the attribute that indicates the packet should have a serializer generated.
///
public static string AttributeFullName => "GenerateSerializer";
///
/// Initializes a new instance of the class.
///
public PacketClassReceiver()
{
_packetClasses = new List();
}
///
/// Gets the classes that should have serializers generated.
///
public IReadOnlyList PacketClasses => _packetClasses;
///
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
if (syntaxNode.IsKind(SyntaxKind.RecordDeclaration) && syntaxNode is RecordDeclarationSyntax classDeclarationSyntax)
{
if (classDeclarationSyntax.AttributeLists.Any(x => x.Attributes.Any(x => x.Name.NormalizeWhitespace().ToFullString() == AttributeFullName)))
{
_packetClasses.Add(classDeclarationSyntax);
}
}
}
}
}