1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-04-24 20:43:33 +08:00

缩小AbstractSubCommandGroup的feature范围

This commit is contained in:
hundun 2024-08-20 16:12:29 +08:00
parent 3f812d6613
commit bf685fc7e6
8 changed files with 69 additions and 82 deletions

View File

@ -387,6 +387,14 @@ public abstract class net/mamoe/mirai/console/command/CompositeCommand : net/mam
public fun getUsage ()Ljava/lang/String;
}
protected abstract interface annotation class net/mamoe/mirai/console/command/CompositeCommand$Description : java/lang/annotation/Annotation {
public abstract fun value ()Ljava/lang/String;
}
protected abstract interface annotation class net/mamoe/mirai/console/command/CompositeCommand$SubCommand : java/lang/annotation/Annotation {
public abstract fun value ()[Ljava/lang/String;
}
public final class net/mamoe/mirai/console/command/ConsoleCommandOwner : net/mamoe/mirai/console/command/CommandOwner {
public static final field INSTANCE Lnet/mamoe/mirai/console/command/ConsoleCommandOwner;
public fun getParentPermission ()Lnet/mamoe/mirai/console/permission/Permission;
@ -609,17 +617,9 @@ public abstract interface class net/mamoe/mirai/console/command/SubCommandGroup
public abstract fun getOverloads ()Ljava/util/List;
}
public abstract interface annotation class net/mamoe/mirai/console/command/SubCommandGroup$Description : java/lang/annotation/Annotation {
public abstract fun value ()Ljava/lang/String;
}
public abstract interface annotation class net/mamoe/mirai/console/command/SubCommandGroup$FlattenSubCommands : java/lang/annotation/Annotation {
}
public abstract interface annotation class net/mamoe/mirai/console/command/SubCommandGroup$SubCommand : java/lang/annotation/Annotation {
public abstract fun value ()[Ljava/lang/String;
}
public abstract interface class net/mamoe/mirai/console/command/SystemCommandSender : net/mamoe/mirai/console/command/CommandSender {
public abstract fun isAnsiSupported ()Z
}

View File

@ -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

View File

@ -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)
}

View File

@ -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)
}

View File

@ -68,28 +68,28 @@ internal fun Any.flattenCommandComponents(): MessageChain = buildMessageChain {
internal object CompositeCommandSubCommandAnnotationResolver :
SubCommandAnnotationResolver<Command> {
override fun isDeclaredSubCommand(ownerCommand: Command, function: KFunction<*>) =
function.hasAnnotation<SubCommandGroup.SubCommand>()
function.hasAnnotation<CompositeCommand.SubCommand>()
override fun getDeclaredSubCommandNames(ownerCommand: Command, function: KFunction<*>): Array<out String> {
val annotated = function.findAnnotation<SubCommandGroup.SubCommand>()!!.value
val annotated = function.findAnnotation<CompositeCommand.SubCommand>()!!.value
return if (annotated.isEmpty()) arrayOf(function.name)
else annotated
}
override fun getAnnotatedName(ownerCommand: Command, parameter: KParameter): String? =
parameter.findAnnotation<SubCommandGroup.Name>()?.value
parameter.findAnnotation<CompositeCommand.Name>()?.value
override fun getDescription(ownerCommand: Command, function: KFunction<*>): String? =
function.findAnnotation<SubCommandGroup.Description>()?.value
function.findAnnotation<CompositeCommand.Description>()?.value
override fun isCombinedSubCommands(command: Command, kProperty: KProperty<*>): Boolean =
kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>() || kProperty.hasAnnotation<SubCommandGroup.SubCommand>()
kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>() || kProperty.hasAnnotation<CompositeCommand.SubCommand>()
override fun getCombinedAdditionNames(command: Command, kProperty: KProperty<*>): Array<out String> {
return if (kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>()) {
emptyArray()
} else {
val annotated = kProperty.findAnnotation<SubCommandGroup.SubCommand>()!!.value
val annotated = kProperty.findAnnotation<CompositeCommand.SubCommand>()!!.value
if (annotated.isEmpty()) arrayOf(kProperty.name)
else annotated
}
@ -100,28 +100,26 @@ internal object CompositeCommandSubCommandAnnotationResolver :
internal object GroupedCommandSubCommandAnnotationResolver :
SubCommandAnnotationResolver<Any> {
override fun isDeclaredSubCommand(ownerCommand: Any, function: KFunction<*>) =
function.hasAnnotation<SubCommandGroup.SubCommand>()
function.hasAnnotation<CompositeCommand.SubCommand>()
override fun getDeclaredSubCommandNames(ownerCommand: Any, function: KFunction<*>): Array<out String> {
val annotated = function.findAnnotation<SubCommandGroup.SubCommand>()!!.value
val annotated = function.findAnnotation<CompositeCommand.SubCommand>()!!.value
return if (annotated.isEmpty()) arrayOf(function.name)
else annotated
}
override fun getAnnotatedName(ownerCommand: Any, parameter: KParameter): String? =
parameter.findAnnotation<SubCommandGroup.Name>()?.value
override fun getAnnotatedName(ownerCommand: Any, parameter: KParameter): String? = null
override fun getDescription(ownerCommand: Any, function: KFunction<*>): String? =
function.findAnnotation<SubCommandGroup.Description>()?.value
override fun getDescription(ownerCommand: Any, function: KFunction<*>): String? = null
override fun isCombinedSubCommands(command: Any, kProperty: KProperty<*>): Boolean =
kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>() || kProperty.hasAnnotation<SubCommandGroup.SubCommand>()
kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>() || kProperty.hasAnnotation<CompositeCommand.SubCommand>()
override fun getCombinedAdditionNames(command: Any, kProperty: KProperty<*>): Array<out String> {
return if (kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>()) {
emptyArray()
} else {
val annotated = kProperty.findAnnotation<SubCommandGroup.SubCommand>()!!.value
val annotated = kProperty.findAnnotation<CompositeCommand.SubCommand>()!!.value
if (annotated.isEmpty()) arrayOf(kProperty.name)
else annotated
}
@ -152,11 +150,29 @@ internal object SimpleCommandSubCommandAnnotationResolver :
}
internal interface SubCommandAnnotationResolver<T> {
/**
* 判断ownerCommand中的一个function是否能成为SubCommand
*/
fun isDeclaredSubCommand(ownerCommand: T, function: KFunction<*>): Boolean
/**
* 得出ownerCommand中的一个function成为SubCommand时的名字列表
*/
fun getDeclaredSubCommandNames(ownerCommand: T, function: KFunction<*>): Array<out String>
/**
* 得出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<out String>
}

View File

@ -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

View File

@ -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

View File

@ -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, "part1NewName function1 0"))
assertSuccess(unifiedCompositeCommand.execute(sender, "functionB0 0"))
})
assertEquals(0, withTesting {
assertSuccess(unifiedCompositeCommand.execute(sender, "function2 0"))
assertSuccess(unifiedCompositeCommand.execute(sender, "customNameB1 0"))
})
assertEquals(0, withTesting {
assertSuccess(unifiedCompositeCommand.execute(sender, "about 0"))
assertSuccess(unifiedCompositeCommand.execute(sender, "functionC0 0"))
})
assertEquals(0, withTesting {
assertSuccess(unifiedCompositeCommand.execute(sender, "customNameC1 0"))
})
}
}