From 04b9e15e1183787b861a6b481b07b1140e1d32ad Mon Sep 17 00:00:00 2001 From: Velvet Toroyashi <42438262+VelvetToroyashi@users.noreply.github.com> Date: Thu, 27 Mar 2025 16:49:41 -0400 Subject: [PATCH 1/2] fix: Remove problematic code --- Remora.Commands/Trees/CommandTreeBuilder.cs | 56 --------------------- 1 file changed, 56 deletions(-) diff --git a/Remora.Commands/Trees/CommandTreeBuilder.cs b/Remora.Commands/Trees/CommandTreeBuilder.cs index 6c98978..66859d4 100644 --- a/Remora.Commands/Trees/CommandTreeBuilder.cs +++ b/Remora.Commands/Trees/CommandTreeBuilder.cs @@ -281,15 +281,6 @@ private IEnumerable ToChildNodes(IEnumerable moduleTypes, IPar var attributes = group.SelectMany(t => t.GetCustomAttributes(true).Cast().Where(att => att is not ConditionAttribute)).ToArray(); var conditions = group.SelectMany(t => t.GetCustomAttributes()).ToArray(); - if (group.First().DeclaringType is { } parentType && !parentType.TryGetGroupName(out _)) - { - // If the group is being hoisted, take the attributes of the parent type(s). - ExtractExtraAttributes(parentType, out var extraAttributes, out var extraConditions); - - attributes = extraAttributes.Concat(attributes).ToArray(); - conditions = extraConditions.Concat(conditions).ToArray(); - } - var groupNode = new GroupNode(group.ToArray(), groupChildren, parent, group.Key, groupAliases, attributes, conditions, description); foreach (var groupType in group) @@ -323,43 +314,6 @@ private IEnumerable ToChildNodes(IEnumerable moduleTypes, IPar } } - /// - /// Extracts attributes and conditions from the given type and its parent types. - /// - /// The type to begin extracting attributes from. - /// The extracted attributes, in descending order. - /// The extracted conditions, in descending order. - private static void ExtractExtraAttributes(Type parentType, out IEnumerable attributes, out IEnumerable conditions) - { - var parentGroupType = parentType; - - var extraAttributes = new List(); - var extraConditions = new List(); - - attributes = extraAttributes; - conditions = extraConditions; - - do - { - if (parentGroupType.TryGetGroupName(out _)) - { - break; - } - - parentGroupType.GetAttributesAndConditions(out var parentAttributes, out var parentConditions); - - extraAttributes.AddRange(parentAttributes.Reverse()); - extraConditions.AddRange(parentConditions.Reverse()); - } - while ((parentGroupType = parentGroupType!.DeclaringType) is not null); - - // These are inserted in reverse order as we traverse up the - // inheritance tree, so re-reversing the list gives us all attributes - // in the correct order, *decescending* down the tree, effectively. - extraAttributes.Reverse(); - extraConditions.Reverse(); - } - /// /// Parses a set of command nodes from the given type. /// @@ -369,7 +323,6 @@ private static void ExtractExtraAttributes(Type parentType, out IEnumerable GetModuleCommands(Type moduleType, IParentNode parent) { var methods = moduleType.GetMethods(); - var isInUnnamedGroup = !moduleType.TryGetGroupName(out var gn) || gn == string.Empty; foreach (var method in methods) { @@ -390,15 +343,6 @@ private IEnumerable GetModuleCommands(Type moduleType, IParentNode method.GetAttributesAndConditions(out var attributes, out var conditions); - if (isInUnnamedGroup) - { - // If the group is being hoisted, take the attributes of the parent type(s). - ExtractExtraAttributes(moduleType, out var extraAttributes, out var extraConditions); - - attributes = extraAttributes.Concat(attributes).ToArray(); - conditions = extraConditions.Concat(conditions).ToArray(); - } - yield return new CommandNode ( parent, From 96d2ad407df207f366203b2739948777590020c8 Mon Sep 17 00:00:00 2001 From: Velvet Toroyashi <42438262+VelvetToroyashi@users.noreply.github.com> Date: Thu, 27 Mar 2025 17:05:23 -0400 Subject: [PATCH 2/2] fix: Don't reverse order, shrimple as that --- Remora.Commands/Trees/CommandTreeBuilder.cs | 50 +++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/Remora.Commands/Trees/CommandTreeBuilder.cs b/Remora.Commands/Trees/CommandTreeBuilder.cs index 66859d4..2ec11c3 100644 --- a/Remora.Commands/Trees/CommandTreeBuilder.cs +++ b/Remora.Commands/Trees/CommandTreeBuilder.cs @@ -281,6 +281,15 @@ private IEnumerable ToChildNodes(IEnumerable moduleTypes, IPar var attributes = group.SelectMany(t => t.GetCustomAttributes(true).Cast().Where(att => att is not ConditionAttribute)).ToArray(); var conditions = group.SelectMany(t => t.GetCustomAttributes()).ToArray(); + if (group.First().DeclaringType is { } parentType && !parentType.TryGetGroupName(out _)) + { + // If the group is being hoisted, take the attributes of the parent type(s). + ExtractExtraAttributes(parentType, out var extraAttributes, out var extraConditions); + + attributes = [..attributes, ..extraAttributes]; + conditions = [..conditions, ..extraConditions]; + } + var groupNode = new GroupNode(group.ToArray(), groupChildren, parent, group.Key, groupAliases, attributes, conditions, description); foreach (var groupType in group) @@ -314,6 +323,37 @@ private IEnumerable ToChildNodes(IEnumerable moduleTypes, IPar } } + /// + /// Extracts attributes and conditions from the given type and its parent types. + /// + /// The type to begin extracting attributes from. + /// The extracted attributes, in descending order. + /// The extracted conditions, in descending order. + private static void ExtractExtraAttributes(Type parentType, out IEnumerable attributes, out IEnumerable conditions) + { + var parentGroupType = parentType; + + var extraAttributes = new List(); + var extraConditions = new List(); + + attributes = extraAttributes; + conditions = extraConditions; + + do + { + if (parentGroupType.TryGetGroupName(out _)) + { + break; + } + + parentGroupType.GetAttributesAndConditions(out var parentAttributes, out var parentConditions); + + extraAttributes.AddRange(parentAttributes); + extraConditions.AddRange(parentConditions); + } + while ((parentGroupType = parentGroupType!.DeclaringType) is not null); + } + /// /// Parses a set of command nodes from the given type. /// @@ -323,6 +363,7 @@ private IEnumerable ToChildNodes(IEnumerable moduleTypes, IPar private IEnumerable GetModuleCommands(Type moduleType, IParentNode parent) { var methods = moduleType.GetMethods(); + var isInUnnamedGroup = !moduleType.TryGetGroupName(out var gn) || gn == string.Empty; foreach (var method in methods) { @@ -343,6 +384,15 @@ private IEnumerable GetModuleCommands(Type moduleType, IParentNode method.GetAttributesAndConditions(out var attributes, out var conditions); + if (isInUnnamedGroup) + { + // If the group is being hoisted, take the attributes of the parent type(s). + ExtractExtraAttributes(moduleType, out var extraAttributes, out var extraConditions); + + attributes = [..attributes, ..extraAttributes]; + conditions = [..conditions, ..extraConditions]; + } + yield return new CommandNode ( parent,