From e4fcda548f7782f95acdbcfa0c4647b8b001396f Mon Sep 17 00:00:00 2001 From: hundun Date: Tue, 20 Aug 2024 16:09:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=A9=E5=B0=8FAbstractSubCommandGroup?= =?UTF-8?q?=E7=9A=84feature=E8=8C=83=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/command/BuiltInCommands.kt | 3 -- .../src/command/CompositeCommand.kt | 14 ++---- .../src/command/SubCommandGroup.kt | 22 -------- .../src/internal/command/CommandReflector.kt | 44 ++++++++++------ .../test/command/CommandContextTest.kt | 1 - .../CommandValueArgumentContextTest.kt | 1 - .../test/command/InstanceTestCommand.kt | 50 ++++++++++--------- 7 files changed, 61 insertions(+), 74 deletions(-) diff --git a/mirai-console/backend/mirai-console/src/command/BuiltInCommands.kt b/mirai-console/backend/mirai-console/src/command/BuiltInCommands.kt index 581186fc42..c2a27bb9d8 100644 --- a/mirai-console/backend/mirai-console/src/command/BuiltInCommands.kt +++ b/mirai-console/backend/mirai-console/src/command/BuiltInCommands.kt @@ -23,9 +23,6 @@ import net.mamoe.mirai.console.MiraiConsoleImplementation import net.mamoe.mirai.console.MiraiConsoleImplementation.ConsoleDataScope.Companion.get import net.mamoe.mirai.console.command.CommandManager.INSTANCE.allRegisteredCommands import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register -import net.mamoe.mirai.console.command.SubCommandGroup.Description -import net.mamoe.mirai.console.command.SubCommandGroup.Name -import net.mamoe.mirai.console.command.SubCommandGroup.SubCommand import net.mamoe.mirai.console.command.descriptor.CommandArgumentParserException import net.mamoe.mirai.console.command.descriptor.CommandValueArgumentParser.Companion.map import net.mamoe.mirai.console.command.descriptor.PermissionIdValueArgumentParser diff --git a/mirai-console/backend/mirai-console/src/command/CompositeCommand.kt b/mirai-console/backend/mirai-console/src/command/CompositeCommand.kt index d619a17adf..d713256d41 100644 --- a/mirai-console/backend/mirai-console/src/command/CompositeCommand.kt +++ b/mirai-console/backend/mirai-console/src/command/CompositeCommand.kt @@ -116,30 +116,26 @@ public abstract class CompositeCommand( */ // open since 2.12 public override val context: CommandArgumentContext = CommandArgumentContext.Builtins + overrideContext - -/* *//** + /** * 标记一个函数为子指令, 当 [value] 为空时使用函数名. * @param value 子指令名 - *//* + */ @Retention(RUNTIME) @Target(FUNCTION) - @Deprecated(level = HIDDEN, message = "use SubCommandGroup.SubCommand") protected annotation class SubCommand( @ResolveContext(COMMAND_NAME) vararg val value: String = [], ) - *//** 指令描述 *//* + /** 指令描述 */ @Retention(RUNTIME) @Target(FUNCTION) - @Deprecated(level = HIDDEN, message = "use SubCommandGroup.Description") protected annotation class Description(val value: String) - *//** 参数名, 将参与构成 [usage] *//* + /** 参数名, 将参与构成 [usage] */ @ConsoleExperimentalApi("Classname might change") @Retention(RUNTIME) @Target(AnnotationTarget.VALUE_PARAMETER) - @Deprecated(level = HIDDEN, message = "use SubCommandGroup.Name") - protected annotation class Name(val value: String)*/ + protected annotation class Name(val value: String) } diff --git a/mirai-console/backend/mirai-console/src/command/SubCommandGroup.kt b/mirai-console/backend/mirai-console/src/command/SubCommandGroup.kt index c84c754161..98a373df3f 100644 --- a/mirai-console/backend/mirai-console/src/command/SubCommandGroup.kt +++ b/mirai-console/backend/mirai-console/src/command/SubCommandGroup.kt @@ -21,26 +21,4 @@ public interface SubCommandGroup { public annotation class FlattenSubCommands( ) - /** - * 1. 标记一个函数为子指令, 当 [value] 为空时使用函数名. - * 2. 标记一个属性为子指令集合,且使用sub策略 - * @param value 子指令名 - */ - @Retention(AnnotationRetention.RUNTIME) - @Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY) - public annotation class SubCommand( - @ResolveContext(ResolveContext.Kind.COMMAND_NAME) vararg val value: String = [], - ) - - /** 指令描述 */ - @Retention(AnnotationRetention.RUNTIME) - @Target(AnnotationTarget.FUNCTION) - public annotation class Description(val value: String) - - /** 参数名, 由具体Command决定用途 */ - @ConsoleExperimentalApi("Classname might change") - @Retention(AnnotationRetention.RUNTIME) - @Target(AnnotationTarget.VALUE_PARAMETER) - public annotation class Name(val value: String) - } \ No newline at end of file diff --git a/mirai-console/backend/mirai-console/src/internal/command/CommandReflector.kt b/mirai-console/backend/mirai-console/src/internal/command/CommandReflector.kt index 06503d2598..98b135396f 100644 --- a/mirai-console/backend/mirai-console/src/internal/command/CommandReflector.kt +++ b/mirai-console/backend/mirai-console/src/internal/command/CommandReflector.kt @@ -68,28 +68,28 @@ internal fun Any.flattenCommandComponents(): MessageChain = buildMessageChain { internal object CompositeCommandSubCommandAnnotationResolver : SubCommandAnnotationResolver { override fun isDeclaredSubCommand(ownerCommand: Command, function: KFunction<*>) = - function.hasAnnotation() + function.hasAnnotation() override fun getDeclaredSubCommandNames(ownerCommand: Command, function: KFunction<*>): Array { - val annotated = function.findAnnotation()!!.value + val annotated = function.findAnnotation()!!.value return if (annotated.isEmpty()) arrayOf(function.name) else annotated } override fun getAnnotatedName(ownerCommand: Command, parameter: KParameter): String? = - parameter.findAnnotation()?.value + parameter.findAnnotation()?.value override fun getDescription(ownerCommand: Command, function: KFunction<*>): String? = - function.findAnnotation()?.value + function.findAnnotation()?.value override fun isCombinedSubCommands(command: Command, kProperty: KProperty<*>): Boolean = - kProperty.hasAnnotation() || kProperty.hasAnnotation() + kProperty.hasAnnotation() || kProperty.hasAnnotation() override fun getCombinedAdditionNames(command: Command, kProperty: KProperty<*>): Array { return if (kProperty.hasAnnotation()) { emptyArray() } else { - val annotated = kProperty.findAnnotation()!!.value + val annotated = kProperty.findAnnotation()!!.value if (annotated.isEmpty()) arrayOf(kProperty.name) else annotated } @@ -100,28 +100,26 @@ internal object CompositeCommandSubCommandAnnotationResolver : internal object GroupedCommandSubCommandAnnotationResolver : SubCommandAnnotationResolver { override fun isDeclaredSubCommand(ownerCommand: Any, function: KFunction<*>) = - function.hasAnnotation() + function.hasAnnotation() override fun getDeclaredSubCommandNames(ownerCommand: Any, function: KFunction<*>): Array { - val annotated = function.findAnnotation()!!.value + val annotated = function.findAnnotation()!!.value return if (annotated.isEmpty()) arrayOf(function.name) else annotated } - override fun getAnnotatedName(ownerCommand: Any, parameter: KParameter): String? = - parameter.findAnnotation()?.value + override fun getAnnotatedName(ownerCommand: Any, parameter: KParameter): String? = null - override fun getDescription(ownerCommand: Any, function: KFunction<*>): String? = - function.findAnnotation()?.value + override fun getDescription(ownerCommand: Any, function: KFunction<*>): String? = null override fun isCombinedSubCommands(command: Any, kProperty: KProperty<*>): Boolean = - kProperty.hasAnnotation() || kProperty.hasAnnotation() + kProperty.hasAnnotation() || kProperty.hasAnnotation() override fun getCombinedAdditionNames(command: Any, kProperty: KProperty<*>): Array { return if (kProperty.hasAnnotation()) { emptyArray() } else { - val annotated = kProperty.findAnnotation()!!.value + val annotated = kProperty.findAnnotation()!!.value if (annotated.isEmpty()) arrayOf(kProperty.name) else annotated } @@ -152,11 +150,29 @@ internal object SimpleCommandSubCommandAnnotationResolver : } internal interface SubCommandAnnotationResolver { + /** + * 判断ownerCommand中的一个function是否能成为SubCommand + */ fun isDeclaredSubCommand(ownerCommand: T, function: KFunction<*>): Boolean + /** + * 得出ownerCommand中的一个function成为SubCommand时的名字列表 + */ fun getDeclaredSubCommandNames(ownerCommand: T, function: KFunction<*>): Array + /** + * 得出ownerCommand中的一个function成为SubCommand时其参数表中的参数的描述 + */ fun getAnnotatedName(ownerCommand: T, parameter: KParameter): String? + /** + * 得出ownerCommand中的一个function成为SubCommand时的描述 + */ fun getDescription(ownerCommand: T, function: KFunction<*>): String? + /** + * 判断ownerCommand中的一个kProperty是否能成为SubCommand + */ fun isCombinedSubCommands(command: T, kProperty: KProperty<*>): Boolean + /** + * 得出ownerCommand中的一个kProperty成为SubCommand时的指令路径的增加部分 + */ fun getCombinedAdditionNames(command: T, kProperty: KProperty<*>): Array } diff --git a/mirai-console/backend/mirai-console/test/command/CommandContextTest.kt b/mirai-console/backend/mirai-console/test/command/CommandContextTest.kt index 761c1137d2..1a59e80b60 100644 --- a/mirai-console/backend/mirai-console/test/command/CommandContextTest.kt +++ b/mirai-console/backend/mirai-console/test/command/CommandContextTest.kt @@ -18,7 +18,6 @@ import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors import net.mamoe.mirai.console.command.java.JCompositeCommand import net.mamoe.mirai.console.command.java.JRawCommand import net.mamoe.mirai.console.command.java.JSimpleCommand -import net.mamoe.mirai.console.command.SubCommandGroup.SubCommand import net.mamoe.mirai.console.internal.data.classifierAsKClass import net.mamoe.mirai.message.data.* import net.mamoe.mirai.utils.safeCast diff --git a/mirai-console/backend/mirai-console/test/command/CommandValueArgumentContextTest.kt b/mirai-console/backend/mirai-console/test/command/CommandValueArgumentContextTest.kt index 3b163904db..c67ed82642 100644 --- a/mirai-console/backend/mirai-console/test/command/CommandValueArgumentContextTest.kt +++ b/mirai-console/backend/mirai-console/test/command/CommandValueArgumentContextTest.kt @@ -19,7 +19,6 @@ import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors import net.mamoe.mirai.console.command.descriptor.buildCommandArgumentContext import net.mamoe.mirai.console.command.java.JCompositeCommand import net.mamoe.mirai.console.command.java.JSimpleCommand -import net.mamoe.mirai.console.command.SubCommandGroup.SubCommand import net.mamoe.mirai.console.internal.data.classifierAsKClass import net.mamoe.mirai.message.data.Image import net.mamoe.mirai.message.data.MessageContent diff --git a/mirai-console/backend/mirai-console/test/command/InstanceTestCommand.kt b/mirai-console/backend/mirai-console/test/command/InstanceTestCommand.kt index 31d1f91e53..38932f1441 100644 --- a/mirai-console/backend/mirai-console/test/command/InstanceTestCommand.kt +++ b/mirai-console/backend/mirai-console/test/command/InstanceTestCommand.kt @@ -35,42 +35,41 @@ import java.time.temporal.TemporalAccessor import kotlin.reflect.KClass import kotlin.test.* -import net.mamoe.mirai.console.command.SubCommandGroup.SubCommand import net.mamoe.mirai.console.command.SubCommandGroup.FlattenSubCommands class MyUnifiedCommand : CompositeCommand( owner, "testMyUnifiedCommand", "tsMUC" ) { - // 插件一个模块的部分功能 - class ModuleAPart1 : AbstractSubCommandGroup() { + class ModuleAPartB : AbstractSubCommandGroup() { + @FlattenSubCommands + val partC = ModuleAPartC() + @SubCommand - fun function1(arg0: Int) { + fun functionB0(arg0: Int) { + Testing.ok(arg0) + } + @SubCommand("customNameB1") + fun functionB1(arg0: Int) { Testing.ok(arg0) } } - // 插件一个模块的另一部分功能 - class ModuleAPart2 : AbstractSubCommandGroup() { + class ModuleAPartC : AbstractSubCommandGroup() { @SubCommand - fun function2(arg0: Int) { + fun functionC0(arg0: Int) { + Testing.ok(arg0) + } + @SubCommand("customNameC1") + fun functionC1(arg0: Int) { Testing.ok(arg0) } } - class ModuleACommandGroup: AbstractSubCommandGroup() { - @SubCommand // 与在函数上标注这个注解类似, 它会带 `part1` 这个名称前缀来注册指令. 需要执行 /base part1 function1 - val part1 = ModuleAPart1() - @SubCommand("part1NewName") // 也可以使用 SubCommand 的参数来覆盖名称 /base part1NewName function1 - val part1b = ModuleAPart1() - @FlattenSubCommands // 新增, 不带前缀注册指令, 执行 /base function2 - val part2 = ModuleAPart2() - } - - @FlattenSubCommands - val moduleA = ModuleACommandGroup() + @FlattenSubCommands // 新增若干, 不带前缀注册指令, 执行 `/base function10` `/base functionCustomName11` + val partB = ModuleAPartB() @SubCommand - fun about(arg0: Int) { + fun functionA0(arg0: Int) { Testing.ok(arg0) } } @@ -542,19 +541,22 @@ internal class InstanceTestCommand : AbstractConsoleInstanceTest() { } @Test - fun `container composite command executing`() = runBlocking { + fun `unified composite command executing`() = runBlocking { unifiedCompositeCommand.withRegistration { assertEquals(0, withTesting { - assertSuccess(unifiedCompositeCommand.execute(sender, "part1 function1 0")) + assertSuccess(unifiedCompositeCommand.execute(sender, "functionA0 0")) + }) + assertEquals(0, withTesting { + assertSuccess(unifiedCompositeCommand.execute(sender, "functionB0 0")) }) assertEquals(0, withTesting { - assertSuccess(unifiedCompositeCommand.execute(sender, "part1NewName function1 0")) + assertSuccess(unifiedCompositeCommand.execute(sender, "customNameB1 0")) }) assertEquals(0, withTesting { - assertSuccess(unifiedCompositeCommand.execute(sender, "function2 0")) + assertSuccess(unifiedCompositeCommand.execute(sender, "functionC0 0")) }) assertEquals(0, withTesting { - assertSuccess(unifiedCompositeCommand.execute(sender, "about 0")) + assertSuccess(unifiedCompositeCommand.execute(sender, "customNameC1 0")) }) } }