Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 支持CompositeCommand间组合 #2125

Closed
wants to merge 12 commits into from

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/

package net.mamoe.mirai.console.command

import net.mamoe.mirai.console.command.descriptor.*
import net.mamoe.mirai.console.internal.command.GroupedCommandSubCommandAnnotationResolver
import net.mamoe.mirai.console.internal.command.SubCommandReflector

public abstract class AbstractSubCommandGroup(
overrideContext: CommandArgumentContext = EmptyCommandArgumentContext,
) : CommandArgumentContextAware, SubCommandGroup {

private val reflector by lazy { SubCommandReflector(this, GroupedCommandSubCommandAnnotationResolver) }

@ExperimentalCommandDescriptors
public final override val overloads: List<CommandSignatureFromKFunction> by lazy {
reflector.findSubCommands().also {
reflector.validate(it)
}
}

/**
* 智能参数解析环境
*/ // open since 2.12
public override val context: CommandArgumentContext = CommandArgumentContext.Builtins + overrideContext

}


Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import net.mamoe.mirai.console.internal.command.CommandReflector
import net.mamoe.mirai.console.internal.command.CompositeCommandSubCommandAnnotationResolver
import net.mamoe.mirai.console.permission.Permission
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import kotlin.DeprecationLevel.*
import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.annotation.AnnotationTarget.FUNCTION


/**
* 复合指令. 指令注册时候会通过反射构造指令解析器.
*
Expand Down Expand Up @@ -92,7 +92,7 @@ public abstract class CompositeCommand(
parentPermission: Permission = owner.parentPermission,
overrideContext: CommandArgumentContext = EmptyCommandArgumentContext,
) : Command, AbstractCommand(owner, primaryName, secondaryNames = secondaryNames, description, parentPermission),
CommandArgumentContextAware {
CommandArgumentContextAware, SubCommandGroup {

private val reflector by lazy { CommandReflector(this, CompositeCommandSubCommandAnnotationResolver) }

Expand All @@ -116,26 +116,30 @@ 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)
protected annotation class Name(val value: String)
@Deprecated(level = HIDDEN, message = "use SubCommandGroup.Name")
protected annotation class Name(val value: String)*/
}


46 changes: 46 additions & 0 deletions mirai-console/backend/mirai-console/src/command/SubCommandGroup.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.mamoe.mirai.console.command

import net.mamoe.mirai.console.command.descriptor.CommandSignatureFromKFunction
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.compiler.common.ResolveContext
import net.mamoe.mirai.console.util.ConsoleExperimentalApi

public interface SubCommandGroup {

/**
* 被聚合时提供的子指令
*/
@ExperimentalCommandDescriptors
public val overloads: List<@JvmWildcard CommandSignatureFromKFunction>

/**
* 标记一个属性为子指令集合,且使用flat策略
*/
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.PROPERTY)
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)

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public open class CommandDeclarationClashException(
public val signatures: List<CommandSignature>,
) : CommandDeclarationException("Declaration clash for command '${command.primaryName}': \n${signatures.joinToString("\n")}")

@ExperimentalCommandDescriptors
public open class SubcommandDeclarationClashException(
public val owner: Any,
public val signatures: List<CommandSignature>,
) : CommandDeclarationException("Declaration clash for owner '${owner::class.qualifiedName}': \n${signatures.joinToString("\n")}")


public open class CommandDeclarationException : RuntimeException {
public constructor() : super()
public constructor(message: String?) : super(message)
Expand Down
Loading
Loading