diff --git a/Aspid.MVVM.Unity.Generators/Aspid.MVVM.Unity.Generators/Generators/ContextMenu/AddComponentContextMenuGenerator.cs b/Aspid.MVVM.Unity.Generators/Aspid.MVVM.Unity.Generators/Generators/ContextMenu/AddBinderContextMenuGenerator.cs similarity index 70% rename from Aspid.MVVM.Unity.Generators/Aspid.MVVM.Unity.Generators/Generators/ContextMenu/AddComponentContextMenuGenerator.cs rename to Aspid.MVVM.Unity.Generators/Aspid.MVVM.Unity.Generators/Generators/ContextMenu/AddBinderContextMenuGenerator.cs index 6f084c2..85143b6 100644 --- a/Aspid.MVVM.Unity.Generators/Aspid.MVVM.Unity.Generators/Generators/ContextMenu/AddComponentContextMenuGenerator.cs +++ b/Aspid.MVVM.Unity.Generators/Aspid.MVVM.Unity.Generators/Generators/ContextMenu/AddBinderContextMenuGenerator.cs @@ -11,11 +11,11 @@ namespace Aspid.MVVM.Unity.Generators.ContextMenu; [Generator(LanguageNames.CSharp)] -public sealed class AddComponentContextMenuGenerator : IIncrementalGenerator +public sealed class AddBinderContextMenuGenerator : IIncrementalGenerator { public void Initialize(IncrementalGeneratorInitializationContext context) { - var provider = context.SyntaxProvider.ForAttributeWithMetadataName(AddComponentContextMenuAttribute.FullName, SyntacticPredicate, Find) + var provider = context.SyntaxProvider.ForAttributeWithMetadataName(AddBinderContextMenuAttribute.FullName, SyntacticPredicate, Find) .Where(foundForSourceGenerator => foundForSourceGenerator.IsNeed) .Select((foundForSourceGenerator, _) => foundForSourceGenerator.Container); @@ -32,25 +32,42 @@ private static FoundForGenerator Find(GeneratorAttributeSyntaxC var declaration = (ClassDeclarationSyntax)context.TargetNode; if (context.TargetSymbol is not INamedTypeSymbol symbol) return default; - if (symbol.HasAnyAttribute(out var attribute, AddComponentContextMenuAttribute)) + if (symbol.HasAnyAttribute(out var attribute, AddBinderContextMenuAttribute)) { if (attribute!.ConstructorArguments[0].Value is not ITypeSymbol type) return default; - - string? path = null; - if (attribute.ConstructorArguments.Length > 1) - path = attribute.ConstructorArguments[1].Value?.ToString(); - var arguments = attribute.NamedArguments; - var priority = (int)(arguments.FirstOrDefault(pair => pair.Key == "Priority").Value.Value ?? 100001); + var path = arguments.FirstOrDefault(pair => pair.Key == "Path").Value.Value as string; + var suPath = arguments.FirstOrDefault(pair => pair.Key == "SubPath").Value.Value as string; + suPath = suPath is not null ? $"{suPath}/" : string.Empty; + + if (path is null) + { + if (symbol.HasAnyAttribute(out var addComponentMenuAttribute, "UnityEngine.AddComponentMenu")) + { + path = addComponentMenuAttribute!.ConstructorArguments[0].Value as string; + + if (path is not null) + path = $"Add {type.Name} Binder" + path.Substring(path.LastIndexOf("/", StringComparison.Ordinal)); + } - return new FoundForGenerator(new ContextMenuData(declaration, symbol, symbol.Name, type.Name, path, priority)); + path ??= $"Add {type.Name} Binder/{symbol.Name}"; + } + + if (!string.IsNullOrWhiteSpace(path) && !string.IsNullOrWhiteSpace(suPath)) + { + var index = path.IndexOf('/'); + if (index is -1) index = path.Length - 2; + + index++; + if (index >= 0 && index < path.Length) + path = path.Insert(path.IndexOf('/') + 1, suPath); + } + + return new FoundForGenerator(new ContextMenuData(declaration, symbol, symbol.Name, type.Name, path, 100001)); } return default; - - string? GetName(string? path) => - path?.Substring(path.LastIndexOf("/", StringComparison.Ordinal) + 1); } private static void GenerateCode(SourceProductionContext context, ContextMenuData data) diff --git a/Aspid.MVVM.Unity.Generators/Aspid.MVVM.Unity.Generators/Generators/Descriptions/Classes.Aspid.cs b/Aspid.MVVM.Unity.Generators/Aspid.MVVM.Unity.Generators/Generators/Descriptions/Classes.Aspid.cs index 95040bc..66a6149 100644 --- a/Aspid.MVVM.Unity.Generators/Aspid.MVVM.Unity.Generators/Generators/Descriptions/Classes.Aspid.cs +++ b/Aspid.MVVM.Unity.Generators/Aspid.MVVM.Unity.Generators/Generators/Descriptions/Classes.Aspid.cs @@ -5,6 +5,6 @@ namespace Aspid.MVVM.Generators.Descriptions; // ReSharper disable InconsistentNaming public static partial class Classes { - public static readonly AttributeText AddComponentContextMenuAttribute = - new ("AddComponentContextMenu", Namespaces.Aspid_MVVM); + public static readonly AttributeText AddBinderContextMenuAttribute = + new ("AddBinderContextMenu", Namespaces.Aspid_MVVM); } \ No newline at end of file diff --git a/Directory.Build.targets b/Directory.Build.targets new file mode 100644 index 0000000..6090a43 --- /dev/null +++ b/Directory.Build.targets @@ -0,0 +1,11 @@ + + + + + <_UnityDestination>$(MSBuildThisFileDirectory)../Aspid.MVVM/Assets/Aspid/MVVM/Unity/ + + + + + +