// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.Collections.Generic; using System.Linq; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; namespace CommunityToolkit.Mvvm.SourceGenerators; /// public sealed partial class ICommandGenerator { /// /// An that selects candidate nodes to process. /// private sealed class SyntaxReceiver : ISyntaxContextReceiver { /// /// The list of info gathered during exploration. /// private readonly List gatheredInfo = new(); /// /// Gets the collection of gathered info to process. /// public IReadOnlyCollection GatheredInfo => this.gatheredInfo; /// public void OnVisitSyntaxNode(GeneratorSyntaxContext context) { if (context.Node is MethodDeclarationSyntax methodDeclaration && context.SemanticModel.GetDeclaredSymbol(methodDeclaration) is IMethodSymbol methodSymbol && context.SemanticModel.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.Input.ICommandAttribute") is INamedTypeSymbol iCommandSymbol && methodSymbol.GetAttributes().FirstOrDefault(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, iCommandSymbol)) is AttributeData attributeData) { this.gatheredInfo.Add(new Item(methodDeclaration.GetLeadingTrivia(), methodSymbol, attributeData)); } } /// /// A model for a group of item representing a discovered type to process. /// /// The leading trivia for the field declaration. /// The instance for the target method. /// The instance the method was annotated with. public sealed record Item(SyntaxTriviaList LeadingTrivia, IMethodSymbol MethodSymbol, AttributeData AttributeData); } }