Skip to content

Commit

Permalink
interface SubCommandProvider方案
Browse files Browse the repository at this point in the history
  • Loading branch information
hundun000 committed Jul 3, 2022
1 parent 877793d commit 20a8b8a
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ public abstract class net/mamoe/mirai/console/command/AbstractCommand : net/mamo
public fun getUsage ()Ljava/lang/String;
}

protected abstract interface annotation class net/mamoe/mirai/console/command/AbstractCommand$AsSubCommandProvider : java/lang/annotation/Annotation {
}

public abstract class net/mamoe/mirai/console/command/AbstractCommandSender : kotlinx/coroutines/CoroutineScope, net/mamoe/mirai/console/command/CommandSender {
public abstract fun getBot ()Lnet/mamoe/mirai/Bot;
public abstract fun getSubject ()Lnet/mamoe/mirai/contact/Contact;
Expand Down Expand Up @@ -91,6 +88,24 @@ public abstract class net/mamoe/mirai/console/command/AbstractPluginCustomComman
protected abstract fun sendMessageImpl (Ljava/lang/String;)V
}

public abstract class net/mamoe/mirai/console/command/AbstractSubCommandProvider : net/mamoe/mirai/console/command/SubCommandProvider, net/mamoe/mirai/console/command/descriptor/CommandArgumentContextAware {
public fun <init> (Lnet/mamoe/mirai/console/command/CommandOwner;Lnet/mamoe/mirai/console/command/descriptor/CommandArgumentContext;)V
public synthetic fun <init> (Lnet/mamoe/mirai/console/command/CommandOwner;Lnet/mamoe/mirai/console/command/descriptor/CommandArgumentContext;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun getContext ()Lnet/mamoe/mirai/console/command/descriptor/CommandArgumentContext;
public final fun getProvideOverloads ()Ljava/util/List;
}

protected abstract interface annotation class net/mamoe/mirai/console/command/AbstractSubCommandProvider$AnotherCombinedCommand : java/lang/annotation/Annotation {
}

protected abstract interface annotation class net/mamoe/mirai/console/command/AbstractSubCommandProvider$AnotherDescription : java/lang/annotation/Annotation {
public abstract fun value ()Ljava/lang/String;
}

protected abstract interface annotation class net/mamoe/mirai/console/command/AbstractSubCommandProvider$AnotherSubCommand : java/lang/annotation/Annotation {
public abstract fun value ()[Ljava/lang/String;
}

public abstract class net/mamoe/mirai/console/command/AbstractUserCommandSender : net/mamoe/mirai/console/command/AbstractCommandSender, net/mamoe/mirai/console/command/UserCommandSender {
public fun getBot ()Lnet/mamoe/mirai/Bot;
public final fun getName ()Ljava/lang/String;
Expand Down Expand Up @@ -384,15 +399,17 @@ public abstract interface class net/mamoe/mirai/console/command/CommandSenderOnM
public abstract fun getFromEvent ()Lnet/mamoe/mirai/event/events/MessageEvent;
}

public abstract class net/mamoe/mirai/console/command/CompositeCommand : net/mamoe/mirai/console/command/AbstractCommand, net/mamoe/mirai/console/command/Command, net/mamoe/mirai/console/command/SubCommandProvider, net/mamoe/mirai/console/command/descriptor/CommandArgumentContextAware {
public abstract class net/mamoe/mirai/console/command/CompositeCommand : net/mamoe/mirai/console/command/AbstractCommand, net/mamoe/mirai/console/command/Command, net/mamoe/mirai/console/command/descriptor/CommandArgumentContextAware {
public fun <init> (Lnet/mamoe/mirai/console/command/CommandOwner;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;Lnet/mamoe/mirai/console/permission/Permission;Lnet/mamoe/mirai/console/command/descriptor/CommandArgumentContext;)V
public synthetic fun <init> (Lnet/mamoe/mirai/console/command/CommandOwner;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;Lnet/mamoe/mirai/console/permission/Permission;Lnet/mamoe/mirai/console/command/descriptor/CommandArgumentContext;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun getContext ()Lnet/mamoe/mirai/console/command/descriptor/CommandArgumentContext;
public final fun getOverloads ()Ljava/util/List;
public final fun getProvideOverloads ()Ljava/util/List;
public fun getUsage ()Ljava/lang/String;
}

protected abstract interface annotation class net/mamoe/mirai/console/command/CompositeCommand$CombinedCommand : java/lang/annotation/Annotation {
}

protected abstract interface annotation class net/mamoe/mirai/console/command/CompositeCommand$Description : java/lang/annotation/Annotation {
public abstract fun value ()Ljava/lang/String;
}
Expand Down Expand Up @@ -512,15 +529,6 @@ public final class net/mamoe/mirai/console/command/GroupTempCommandSenderOnMessa
public synthetic fun getFromEvent ()Lnet/mamoe/mirai/event/events/MessageEvent;
}

public abstract class net/mamoe/mirai/console/command/GroupedCommand : net/mamoe/mirai/console/command/AbstractCommand, net/mamoe/mirai/console/command/Command, net/mamoe/mirai/console/command/SubCommandProvider, net/mamoe/mirai/console/command/descriptor/CommandArgumentContextAware {
public fun <init> (Lnet/mamoe/mirai/console/command/CommandOwner;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;Lnet/mamoe/mirai/console/permission/Permission;Lnet/mamoe/mirai/console/command/descriptor/CommandArgumentContext;)V
public synthetic fun <init> (Lnet/mamoe/mirai/console/command/CommandOwner;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;Lnet/mamoe/mirai/console/permission/Permission;Lnet/mamoe/mirai/console/command/descriptor/CommandArgumentContext;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun getContext ()Lnet/mamoe/mirai/console/command/descriptor/CommandArgumentContext;
public final fun getOverloads ()Ljava/util/List;
public final fun getProvideOverloads ()Ljava/util/List;
public fun getUsage ()Ljava/lang/String;
}

public class net/mamoe/mirai/console/command/IllegalCommandArgumentException : java/lang/IllegalArgumentException {
public fun <init> (Ljava/lang/String;)V
public fun <init> (Ljava/lang/String;Ljava/lang/Throwable;)V
Expand Down Expand Up @@ -1112,6 +1120,12 @@ public final class net/mamoe/mirai/console/command/descriptor/StringValueArgumen
public fun parse (Ljava/lang/String;Lnet/mamoe/mirai/console/command/CommandSender;)Ljava/lang/String;
}

public class net/mamoe/mirai/console/command/descriptor/SubcommandDeclarationClashException : net/mamoe/mirai/console/command/descriptor/CommandDeclarationException {
public fun <init> (Ljava/lang/Object;Ljava/util/List;)V
public final fun getOwner ()Ljava/lang/Object;
public final fun getSignatures ()Ljava/util/List;
}

public abstract interface class net/mamoe/mirai/console/command/descriptor/TypeVariant {
public static final field Companion Lnet/mamoe/mirai/console/command/descriptor/TypeVariant$Companion;
public abstract fun getOutType ()Lkotlin/reflect/KType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ public abstract class AbstractCommand
secondaryNames.forEach(Command.Companion::checkCommandName)
}

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

public override val usage: String get() = description
public override val permission: Permission by lazy { findOrCreateCommandPermission(parentPermission) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ package net.mamoe.mirai.console.command
import net.mamoe.mirai.console.command.descriptor.*
import net.mamoe.mirai.console.command.java.JCompositeCommand
import net.mamoe.mirai.console.compiler.common.ResolveContext
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.COMMAND_NAME
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.internal.command.CommandReflector
import net.mamoe.mirai.console.internal.command.CompositeCommandSubCommandAnnotationResolver
import net.mamoe.mirai.console.internal.command.GroupedCommandSubCommandAnnotationResolver
import net.mamoe.mirai.console.permission.Permission
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.PROPERTY


/**
* 复合指令. 指令注册时候会通过反射构造指令解析器.
*
Expand Down Expand Up @@ -86,38 +86,48 @@ import kotlin.annotation.AnnotationTarget.PROPERTY
*
* @see buildCommandArgumentContext
*/
public abstract class GroupedCommand(
public abstract class AbstractSubCommandProvider(
@ResolveContext(RESTRICTED_CONSOLE_COMMAND_OWNER) owner: CommandOwner,
@ResolveContext(COMMAND_NAME) primaryName: String,
@ResolveContext(COMMAND_NAME) vararg secondaryNames: String,
description: String = "no description available",
parentPermission: Permission = owner.parentPermission,
overrideContext: CommandArgumentContext = EmptyCommandArgumentContext,
) : Command, AbstractCommand(owner, primaryName, secondaryNames = secondaryNames, description, parentPermission),
CommandArgumentContextAware, SubCommandProvider {
) : CommandArgumentContextAware, SubCommandProvider {

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

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

@ExperimentalCommandDescriptors
public final override val provideOverloads: List<CommandSignatureFromKFunction> by lazy {
// TODO 再加上额外的filter/validate?
overloads
}
/**
* 标记一个属性为子指令集合
*/
@Retention(RUNTIME)
@Target(PROPERTY)
protected annotation class AnotherCombinedCommand(
)

/**
* 自动根据带有 [SubCommand] 注解的函数签名生成 [usage]. 也可以被覆盖.
* 标记一个函数为子指令, 当 [value] 为空时使用函数名.
* @param value 子指令名
*/
public override val usage: String by lazy {
@OptIn(ExperimentalCommandDescriptors::class)
reflector.generateUsage(overloads)
}
@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)

/** 参数名, 将参与构成 [usage] */
@ConsoleExperimentalApi("Classname might change")
@Retention(RUNTIME)
@Target(AnnotationTarget.VALUE_PARAMETER)
protected annotation class AnotherName(val value: String)

/**
* 智能参数解析环境
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.PROPERTY


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

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

Expand All @@ -104,12 +103,6 @@ public abstract class CompositeCommand(
}
}

@ExperimentalCommandDescriptors
public final override val provideOverloads: List<CommandSignatureFromKFunction> by lazy {
// TODO 再加上额外的filter/validate?
overloads
}

/**
* 自动根据带有 [SubCommand] 注解的函数签名生成 [usage]. 也可以被覆盖.
*/
Expand All @@ -123,6 +116,14 @@ 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 子指令名
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import net.mamoe.mirai.console.command.descriptor.CommandSignatureFromKFunction
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.util.ConsoleExperimentalApi

interface SubCommandProvider {
public interface SubCommandProvider {

/**
* 被聚合时提供的子指令
*/
@ConsoleExperimentalApi("Property name is experimental")
@ExperimentalCommandDescriptors
public val provideOverloads: List<@JvmWildcard CommandSignatureFromKFunction>

}
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

0 comments on commit 20a8b8a

Please sign in to comment.