Skip to content

Commit

Permalink
update about SubCommandGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
hundun000 committed Aug 20, 2024
1 parent 7ace541 commit 8b30851
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,8 @@
package net.mamoe.mirai.console.command

import net.mamoe.mirai.console.command.descriptor.*
import net.mamoe.mirai.console.compiler.common.ResolveContext
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.RESTRICTED_CONSOLE_COMMAND_OWNER
import net.mamoe.mirai.console.internal.command.GroupedCommandSubCommandAnnotationResolver
import net.mamoe.mirai.console.internal.command.SubCommandReflector
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.COMMAND_NAME
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.PROPERTY

public abstract class AbstractSubCommandGroup(
overrideContext: CommandArgumentContext = EmptyCommandArgumentContext,
Expand All @@ -33,35 +26,6 @@ public abstract class AbstractSubCommandGroup(
}
}

/**
* 标记一个属性为子指令集合
*/
@Retention(RUNTIME)
@Target(PROPERTY)
protected annotation class AnotherCombinedCommand(
)

/**
* 标记一个函数为子指令, 当 [value] 为空时使用函数名.
* @param value 子指令名
*/
@Retention(RUNTIME)
@Target(FUNCTION)
protected annotation class AnotherSubCommand(
@ResolveContext(COMMAND_NAME) vararg val value: String = [],
)

/** 指令描述 */
@Retention(RUNTIME)
@Target(FUNCTION)
protected annotation class AnotherDescription(val value: String)

/** 参数名, 由具体Command决定用途 */
@ConsoleExperimentalApi("Classname might change")
@Retention(RUNTIME)
@Target(AnnotationTarget.VALUE_PARAMETER)
protected annotation class AnotherName(val value: String)

/**
* 智能参数解析环境
*/ // open since 2.12
Expand Down
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
24 changes: 10 additions & 14 deletions mirai-console/backend/mirai-console/src/command/CompositeCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ 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
import kotlin.annotation.AnnotationTarget.PROPERTY

/**
* 复合指令. 指令注册时候会通过反射构造指令解析器.
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,34 +116,30 @@ public abstract class CompositeCommand(
*/ // open since 2.12
public override val context: CommandArgumentContext = CommandArgumentContext.Builtins + overrideContext

/**
* 标记一个属性为子指令集合
*/
@Retention(RUNTIME)
@Target(PROPERTY)
protected annotation class CombinedCommand(
)

/**
/* *//**
* 标记一个函数为子指令, 当 [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)*/
}


31 changes: 31 additions & 0 deletions mirai-console/backend/mirai-console/src/command/SubCommandGroup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 {
Expand All @@ -12,4 +13,34 @@ 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)

}
Loading

0 comments on commit 8b30851

Please sign in to comment.