Skip to content

Commit bf685fc

Browse files
committed
缩小AbstractSubCommandGroup的feature范围
1 parent 3f812d6 commit bf685fc

File tree

8 files changed

+69
-82
lines changed

8 files changed

+69
-82
lines changed

mirai-console/backend/mirai-console/compatibility-validation/jvm/api/jvm.api

+8-8
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,14 @@ public abstract class net/mamoe/mirai/console/command/CompositeCommand : net/mam
387387
public fun getUsage ()Ljava/lang/String;
388388
}
389389

390+
protected abstract interface annotation class net/mamoe/mirai/console/command/CompositeCommand$Description : java/lang/annotation/Annotation {
391+
public abstract fun value ()Ljava/lang/String;
392+
}
393+
394+
protected abstract interface annotation class net/mamoe/mirai/console/command/CompositeCommand$SubCommand : java/lang/annotation/Annotation {
395+
public abstract fun value ()[Ljava/lang/String;
396+
}
397+
390398
public final class net/mamoe/mirai/console/command/ConsoleCommandOwner : net/mamoe/mirai/console/command/CommandOwner {
391399
public static final field INSTANCE Lnet/mamoe/mirai/console/command/ConsoleCommandOwner;
392400
public fun getParentPermission ()Lnet/mamoe/mirai/console/permission/Permission;
@@ -609,17 +617,9 @@ public abstract interface class net/mamoe/mirai/console/command/SubCommandGroup
609617
public abstract fun getOverloads ()Ljava/util/List;
610618
}
611619

612-
public abstract interface annotation class net/mamoe/mirai/console/command/SubCommandGroup$Description : java/lang/annotation/Annotation {
613-
public abstract fun value ()Ljava/lang/String;
614-
}
615-
616620
public abstract interface annotation class net/mamoe/mirai/console/command/SubCommandGroup$FlattenSubCommands : java/lang/annotation/Annotation {
617621
}
618622

619-
public abstract interface annotation class net/mamoe/mirai/console/command/SubCommandGroup$SubCommand : java/lang/annotation/Annotation {
620-
public abstract fun value ()[Ljava/lang/String;
621-
}
622-
623623
public abstract interface class net/mamoe/mirai/console/command/SystemCommandSender : net/mamoe/mirai/console/command/CommandSender {
624624
public abstract fun isAnsiSupported ()Z
625625
}

mirai-console/backend/mirai-console/src/command/BuiltInCommands.kt

-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ import net.mamoe.mirai.console.MiraiConsoleImplementation
2323
import net.mamoe.mirai.console.MiraiConsoleImplementation.ConsoleDataScope.Companion.get
2424
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.allRegisteredCommands
2525
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
26-
import net.mamoe.mirai.console.command.SubCommandGroup.Description
27-
import net.mamoe.mirai.console.command.SubCommandGroup.Name
28-
import net.mamoe.mirai.console.command.SubCommandGroup.SubCommand
2926
import net.mamoe.mirai.console.command.descriptor.CommandArgumentParserException
3027
import net.mamoe.mirai.console.command.descriptor.CommandValueArgumentParser.Companion.map
3128
import net.mamoe.mirai.console.command.descriptor.PermissionIdValueArgumentParser

mirai-console/backend/mirai-console/src/command/CompositeCommand.kt

+5-9
Original file line numberDiff line numberDiff line change
@@ -116,30 +116,26 @@ public abstract class CompositeCommand(
116116
*/ // open since 2.12
117117
public override val context: CommandArgumentContext = CommandArgumentContext.Builtins + overrideContext
118118

119-
120-
/* *//**
119+
/**
121120
* 标记一个函数为子指令, 当 [value] 为空时使用函数名.
122121
* @param value 子指令名
123-
*//*
122+
*/
124123
@Retention(RUNTIME)
125124
@Target(FUNCTION)
126-
@Deprecated(level = HIDDEN, message = "use SubCommandGroup.SubCommand")
127125
protected annotation class SubCommand(
128126
@ResolveContext(COMMAND_NAME) vararg val value: String = [],
129127
)
130128

131-
*//** 指令描述 *//*
129+
/** 指令描述 */
132130
@Retention(RUNTIME)
133131
@Target(FUNCTION)
134-
@Deprecated(level = HIDDEN, message = "use SubCommandGroup.Description")
135132
protected annotation class Description(val value: String)
136133

137-
*//** 参数名, 将参与构成 [usage] *//*
134+
/** 参数名, 将参与构成 [usage] */
138135
@ConsoleExperimentalApi("Classname might change")
139136
@Retention(RUNTIME)
140137
@Target(AnnotationTarget.VALUE_PARAMETER)
141-
@Deprecated(level = HIDDEN, message = "use SubCommandGroup.Name")
142-
protected annotation class Name(val value: String)*/
138+
protected annotation class Name(val value: String)
143139
}
144140

145141

mirai-console/backend/mirai-console/src/command/SubCommandGroup.kt

-22
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,4 @@ public interface SubCommandGroup {
2121
public annotation class FlattenSubCommands(
2222
)
2323

24-
/**
25-
* 1. 标记一个函数为子指令, 当 [value] 为空时使用函数名.
26-
* 2. 标记一个属性为子指令集合,且使用sub策略
27-
* @param value 子指令名
28-
*/
29-
@Retention(AnnotationRetention.RUNTIME)
30-
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY)
31-
public annotation class SubCommand(
32-
@ResolveContext(ResolveContext.Kind.COMMAND_NAME) vararg val value: String = [],
33-
)
34-
35-
/** 指令描述 */
36-
@Retention(AnnotationRetention.RUNTIME)
37-
@Target(AnnotationTarget.FUNCTION)
38-
public annotation class Description(val value: String)
39-
40-
/** 参数名, 由具体Command决定用途 */
41-
@ConsoleExperimentalApi("Classname might change")
42-
@Retention(AnnotationRetention.RUNTIME)
43-
@Target(AnnotationTarget.VALUE_PARAMETER)
44-
public annotation class Name(val value: String)
45-
4624
}

mirai-console/backend/mirai-console/src/internal/command/CommandReflector.kt

+30-14
Original file line numberDiff line numberDiff line change
@@ -68,28 +68,28 @@ internal fun Any.flattenCommandComponents(): MessageChain = buildMessageChain {
6868
internal object CompositeCommandSubCommandAnnotationResolver :
6969
SubCommandAnnotationResolver<Command> {
7070
override fun isDeclaredSubCommand(ownerCommand: Command, function: KFunction<*>) =
71-
function.hasAnnotation<SubCommandGroup.SubCommand>()
71+
function.hasAnnotation<CompositeCommand.SubCommand>()
7272

7373
override fun getDeclaredSubCommandNames(ownerCommand: Command, function: KFunction<*>): Array<out String> {
74-
val annotated = function.findAnnotation<SubCommandGroup.SubCommand>()!!.value
74+
val annotated = function.findAnnotation<CompositeCommand.SubCommand>()!!.value
7575
return if (annotated.isEmpty()) arrayOf(function.name)
7676
else annotated
7777
}
7878

7979
override fun getAnnotatedName(ownerCommand: Command, parameter: KParameter): String? =
80-
parameter.findAnnotation<SubCommandGroup.Name>()?.value
80+
parameter.findAnnotation<CompositeCommand.Name>()?.value
8181

8282
override fun getDescription(ownerCommand: Command, function: KFunction<*>): String? =
83-
function.findAnnotation<SubCommandGroup.Description>()?.value
83+
function.findAnnotation<CompositeCommand.Description>()?.value
8484

8585
override fun isCombinedSubCommands(command: Command, kProperty: KProperty<*>): Boolean =
86-
kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>() || kProperty.hasAnnotation<SubCommandGroup.SubCommand>()
86+
kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>() || kProperty.hasAnnotation<CompositeCommand.SubCommand>()
8787

8888
override fun getCombinedAdditionNames(command: Command, kProperty: KProperty<*>): Array<out String> {
8989
return if (kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>()) {
9090
emptyArray()
9191
} else {
92-
val annotated = kProperty.findAnnotation<SubCommandGroup.SubCommand>()!!.value
92+
val annotated = kProperty.findAnnotation<CompositeCommand.SubCommand>()!!.value
9393
if (annotated.isEmpty()) arrayOf(kProperty.name)
9494
else annotated
9595
}
@@ -100,28 +100,26 @@ internal object CompositeCommandSubCommandAnnotationResolver :
100100
internal object GroupedCommandSubCommandAnnotationResolver :
101101
SubCommandAnnotationResolver<Any> {
102102
override fun isDeclaredSubCommand(ownerCommand: Any, function: KFunction<*>) =
103-
function.hasAnnotation<SubCommandGroup.SubCommand>()
103+
function.hasAnnotation<CompositeCommand.SubCommand>()
104104

105105
override fun getDeclaredSubCommandNames(ownerCommand: Any, function: KFunction<*>): Array<out String> {
106-
val annotated = function.findAnnotation<SubCommandGroup.SubCommand>()!!.value
106+
val annotated = function.findAnnotation<CompositeCommand.SubCommand>()!!.value
107107
return if (annotated.isEmpty()) arrayOf(function.name)
108108
else annotated
109109
}
110110

111-
override fun getAnnotatedName(ownerCommand: Any, parameter: KParameter): String? =
112-
parameter.findAnnotation<SubCommandGroup.Name>()?.value
111+
override fun getAnnotatedName(ownerCommand: Any, parameter: KParameter): String? = null
113112

114-
override fun getDescription(ownerCommand: Any, function: KFunction<*>): String? =
115-
function.findAnnotation<SubCommandGroup.Description>()?.value
113+
override fun getDescription(ownerCommand: Any, function: KFunction<*>): String? = null
116114

117115
override fun isCombinedSubCommands(command: Any, kProperty: KProperty<*>): Boolean =
118-
kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>() || kProperty.hasAnnotation<SubCommandGroup.SubCommand>()
116+
kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>() || kProperty.hasAnnotation<CompositeCommand.SubCommand>()
119117

120118
override fun getCombinedAdditionNames(command: Any, kProperty: KProperty<*>): Array<out String> {
121119
return if (kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>()) {
122120
emptyArray()
123121
} else {
124-
val annotated = kProperty.findAnnotation<SubCommandGroup.SubCommand>()!!.value
122+
val annotated = kProperty.findAnnotation<CompositeCommand.SubCommand>()!!.value
125123
if (annotated.isEmpty()) arrayOf(kProperty.name)
126124
else annotated
127125
}
@@ -152,11 +150,29 @@ internal object SimpleCommandSubCommandAnnotationResolver :
152150
}
153151

154152
internal interface SubCommandAnnotationResolver<T> {
153+
/**
154+
* 判断ownerCommand中的一个function是否能成为SubCommand
155+
*/
155156
fun isDeclaredSubCommand(ownerCommand: T, function: KFunction<*>): Boolean
157+
/**
158+
* 得出ownerCommand中的一个function成为SubCommand时的名字列表
159+
*/
156160
fun getDeclaredSubCommandNames(ownerCommand: T, function: KFunction<*>): Array<out String>
161+
/**
162+
* 得出ownerCommand中的一个function成为SubCommand时其参数表中的参数的描述
163+
*/
157164
fun getAnnotatedName(ownerCommand: T, parameter: KParameter): String?
165+
/**
166+
* 得出ownerCommand中的一个function成为SubCommand时的描述
167+
*/
158168
fun getDescription(ownerCommand: T, function: KFunction<*>): String?
169+
/**
170+
* 判断ownerCommand中的一个kProperty是否能成为SubCommand
171+
*/
159172
fun isCombinedSubCommands(command: T, kProperty: KProperty<*>): Boolean
173+
/**
174+
* 得出ownerCommand中的一个kProperty成为SubCommand时的指令路径的增加部分
175+
*/
160176
fun getCombinedAdditionNames(command: T, kProperty: KProperty<*>): Array<out String>
161177
}
162178

mirai-console/backend/mirai-console/test/command/CommandContextTest.kt

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
1818
import net.mamoe.mirai.console.command.java.JCompositeCommand
1919
import net.mamoe.mirai.console.command.java.JRawCommand
2020
import net.mamoe.mirai.console.command.java.JSimpleCommand
21-
import net.mamoe.mirai.console.command.SubCommandGroup.SubCommand
2221
import net.mamoe.mirai.console.internal.data.classifierAsKClass
2322
import net.mamoe.mirai.message.data.*
2423
import net.mamoe.mirai.utils.safeCast

mirai-console/backend/mirai-console/test/command/CommandValueArgumentContextTest.kt

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
1919
import net.mamoe.mirai.console.command.descriptor.buildCommandArgumentContext
2020
import net.mamoe.mirai.console.command.java.JCompositeCommand
2121
import net.mamoe.mirai.console.command.java.JSimpleCommand
22-
import net.mamoe.mirai.console.command.SubCommandGroup.SubCommand
2322
import net.mamoe.mirai.console.internal.data.classifierAsKClass
2423
import net.mamoe.mirai.message.data.Image
2524
import net.mamoe.mirai.message.data.MessageContent

mirai-console/backend/mirai-console/test/command/InstanceTestCommand.kt

+26-24
Original file line numberDiff line numberDiff line change
@@ -35,42 +35,41 @@ import java.time.temporal.TemporalAccessor
3535
import kotlin.reflect.KClass
3636
import kotlin.test.*
3737

38-
import net.mamoe.mirai.console.command.SubCommandGroup.SubCommand
3938
import net.mamoe.mirai.console.command.SubCommandGroup.FlattenSubCommands
4039

4140
class MyUnifiedCommand : CompositeCommand(
4241
owner, "testMyUnifiedCommand", "tsMUC"
4342
) {
44-
// 插件一个模块的部分功能
45-
class ModuleAPart1 : AbstractSubCommandGroup() {
43+
class ModuleAPartB : AbstractSubCommandGroup() {
44+
@FlattenSubCommands
45+
val partC = ModuleAPartC()
46+
4647
@SubCommand
47-
fun function1(arg0: Int) {
48+
fun functionB0(arg0: Int) {
49+
Testing.ok(arg0)
50+
}
51+
@SubCommand("customNameB1")
52+
fun functionB1(arg0: Int) {
4853
Testing.ok(arg0)
4954
}
5055
}
5156

52-
// 插件一个模块的另一部分功能
53-
class ModuleAPart2 : AbstractSubCommandGroup() {
57+
class ModuleAPartC : AbstractSubCommandGroup() {
5458
@SubCommand
55-
fun function2(arg0: Int) {
59+
fun functionC0(arg0: Int) {
60+
Testing.ok(arg0)
61+
}
62+
@SubCommand("customNameC1")
63+
fun functionC1(arg0: Int) {
5664
Testing.ok(arg0)
5765
}
5866
}
5967

60-
class ModuleACommandGroup: AbstractSubCommandGroup() {
61-
@SubCommand // 与在函数上标注这个注解类似, 它会带 `part1` 这个名称前缀来注册指令. 需要执行 /base part1 function1
62-
val part1 = ModuleAPart1()
63-
@SubCommand("part1NewName") // 也可以使用 SubCommand 的参数来覆盖名称 /base part1NewName function1
64-
val part1b = ModuleAPart1()
65-
@FlattenSubCommands // 新增, 不带前缀注册指令, 执行 /base function2
66-
val part2 = ModuleAPart2()
67-
}
68-
69-
@FlattenSubCommands
70-
val moduleA = ModuleACommandGroup()
68+
@FlattenSubCommands // 新增若干, 不带前缀注册指令, 执行 `/base function10` `/base functionCustomName11`
69+
val partB = ModuleAPartB()
7170

7271
@SubCommand
73-
fun about(arg0: Int) {
72+
fun functionA0(arg0: Int) {
7473
Testing.ok(arg0)
7574
}
7675
}
@@ -542,19 +541,22 @@ internal class InstanceTestCommand : AbstractConsoleInstanceTest() {
542541
}
543542

544543
@Test
545-
fun `container composite command executing`() = runBlocking {
544+
fun `unified composite command executing`() = runBlocking {
546545
unifiedCompositeCommand.withRegistration {
547546
assertEquals(0, withTesting {
548-
assertSuccess(unifiedCompositeCommand.execute(sender, "part1 function1 0"))
547+
assertSuccess(unifiedCompositeCommand.execute(sender, "functionA0 0"))
548+
})
549+
assertEquals(0, withTesting {
550+
assertSuccess(unifiedCompositeCommand.execute(sender, "functionB0 0"))
549551
})
550552
assertEquals(0, withTesting {
551-
assertSuccess(unifiedCompositeCommand.execute(sender, "part1NewName function1 0"))
553+
assertSuccess(unifiedCompositeCommand.execute(sender, "customNameB1 0"))
552554
})
553555
assertEquals(0, withTesting {
554-
assertSuccess(unifiedCompositeCommand.execute(sender, "function2 0"))
556+
assertSuccess(unifiedCompositeCommand.execute(sender, "functionC0 0"))
555557
})
556558
assertEquals(0, withTesting {
557-
assertSuccess(unifiedCompositeCommand.execute(sender, "about 0"))
559+
assertSuccess(unifiedCompositeCommand.execute(sender, "customNameC1 0"))
558560
})
559561
}
560562
}

0 commit comments

Comments
 (0)