From 46ff28050f380dad3fa44376171519b3541b4da5 Mon Sep 17 00:00:00 2001 From: Him188 Date: Mon, 16 Nov 2020 15:30:57 +0800 Subject: [PATCH] Fix CommandExecuteResult --- .../src/command/CommandExecuteResult.kt | 13 +++--- .../resolve/BuiltInCommandCallResolver.kt | 2 +- .../command/resolve/CommandCallResolver.kt | 5 +- .../internal/command/CommandManagerImpl.kt | 4 +- .../src/internal/command/CommandReflector.kt | 46 ++++++++++--------- 5 files changed, 36 insertions(+), 34 deletions(-) diff --git a/backend/mirai-console/src/command/CommandExecuteResult.kt b/backend/mirai-console/src/command/CommandExecuteResult.kt index 69617aede..c1961445c 100644 --- a/backend/mirai-console/src/command/CommandExecuteResult.kt +++ b/backend/mirai-console/src/command/CommandExecuteResult.kt @@ -83,16 +83,16 @@ public sealed class CommandExecuteResult { ) : Failure() /** 没有匹配的指令 */ - public class UnresolvedCommand : Failure() { + public class UnresolvedCommand( + /** 解析的 [CommandCall] (如果匹配到) */ + public override val call: CommandCall?, + ) : Failure() { /** 指令执行时发生的错误, 总是 `null` */ public override val exception: Nothing? get() = null /** 尝试执行的指令, 总是 `null` */ public override val command: Nothing? get() = null - /** 解析的 [CommandCall] (如果匹配到) */ - public override val call: CommandCall? get() = null - /** 解析的 [ResolvedCommandCall] (如果匹配到) */ public override val resolvedCall: ResolvedCommandCall? get() = null } @@ -114,6 +114,8 @@ public sealed class CommandExecuteResult { public class UnmatchedSignature( /** 尝试执行的指令 */ public override val command: Command, + /** 解析的 [CommandCall] (如果匹配到) */ + public override val call: CommandCall, /** 尝试执行的指令 */ @ExperimentalCommandDescriptors @ConsoleExperimentalApi @@ -122,9 +124,6 @@ public sealed class CommandExecuteResult { /** 指令执行时发生的错误, 总是 `null` */ public override val exception: Nothing? get() = null - /** 解析的 [CommandCall] (如果匹配到) */ - public override val call: CommandCall? get() = null - /** 解析的 [ResolvedCommandCall] (如果匹配到) */ public override val resolvedCall: ResolvedCommandCall? get() = null } diff --git a/backend/mirai-console/src/command/resolve/BuiltInCommandCallResolver.kt b/backend/mirai-console/src/command/resolve/BuiltInCommandCallResolver.kt index e591c7012..95ab24b12 100644 --- a/backend/mirai-console/src/command/resolve/BuiltInCommandCallResolver.kt +++ b/backend/mirai-console/src/command/resolve/BuiltInCommandCallResolver.kt @@ -79,7 +79,7 @@ public object BuiltInCommandCallResolver : CommandCallResolver { val failureReasons = unmatchedCommandSignatures.toMutableList() val rA = FailureReason.ResolutionAmbiguity(resolutionAmbiguities) failureReasons.addAll(resolutionAmbiguities.map { UnmatchedCommandSignature(it, rA) }) - return CommandExecuteResult.UnmatchedSignature(command, unmatchedCommandSignatures) + return CommandExecuteResult.UnmatchedSignature(command, call, unmatchedCommandSignatures) } } diff --git a/backend/mirai-console/src/command/resolve/CommandCallResolver.kt b/backend/mirai-console/src/command/resolve/CommandCallResolver.kt index 72209e2d0..d592654be 100644 --- a/backend/mirai-console/src/command/resolve/CommandCallResolver.kt +++ b/backend/mirai-console/src/command/resolve/CommandCallResolver.kt @@ -33,16 +33,15 @@ public class CommandResolveResult private constructor( get() = value.safeCast() public inline fun fold( - onSuccess: (ResolvedCommandCall) -> R, + onSuccess: (ResolvedCommandCall?) -> R, onFailure: (CommandExecuteResult.Failure) -> R, ): R { contract { callsInPlace(onSuccess, InvocationKind.AT_MOST_ONCE) callsInPlace(onFailure, InvocationKind.AT_MOST_ONCE) } - call?.let(onSuccess)?.let { return it } failure?.let(onFailure)?.let { return it } - throw kotlin.AssertionError() + return call.let(onSuccess) } public constructor(call: ResolvedCommandCall?) : this(call as Any?) diff --git a/backend/mirai-console/src/internal/command/CommandManagerImpl.kt b/backend/mirai-console/src/internal/command/CommandManagerImpl.kt index bc5120219..4818c1c1e 100644 --- a/backend/mirai-console/src/internal/command/CommandManagerImpl.kt +++ b/backend/mirai-console/src/internal/command/CommandManagerImpl.kt @@ -177,11 +177,11 @@ internal suspend fun executeCommandImpl( caller: CommandSender, checkPermission: Boolean, ): CommandExecuteResult { - val call = message.asMessageChain().parseCommandCall(caller) ?: return CommandExecuteResult.UnresolvedCommand() + val call = message.asMessageChain().parseCommandCall(caller) ?: return CommandExecuteResult.UnresolvedCommand(null) val resolved = call.resolve().fold( onSuccess = { it }, onFailure = { return it } - ) + ) ?: return CommandExecuteResult.UnresolvedCommand(call) val command = resolved.callee diff --git a/backend/mirai-console/src/internal/command/CommandReflector.kt b/backend/mirai-console/src/internal/command/CommandReflector.kt index 867c50ec3..842368e8f 100644 --- a/backend/mirai-console/src/internal/command/CommandReflector.kt +++ b/backend/mirai-console/src/internal/command/CommandReflector.kt @@ -144,30 +144,34 @@ internal class CommandReflector( } fun generateUsage(overloads: Iterable): String { - return overloads.joinToString("\n") { subcommand -> - buildString { - if (command.prefixOptional) { - append("(") - append(CommandManager.commandPrefix) - append(")") - } else { - append(CommandManager.commandPrefix) - } - //if (command is CompositeCommand) { - append(command.primaryName) - append(" ") - //} - append(subcommand.valueParameters.joinToString(" ") { it.render() }) - annotationResolver.getDescription(command, subcommand.originFunction)?.let { description -> - append(" # ") - append(description) + return generateUsage(command, annotationResolver, overloads) + } + + companion object { + fun generateUsage(command: Command, annotationResolver: SubCommandAnnotationResolver?, overloads: Iterable): String { + return overloads.joinToString("\n") { subcommand -> + buildString { + if (command.prefixOptional) { + append("(") + append(CommandManager.commandPrefix) + append(")") + } else { + append(CommandManager.commandPrefix) + } + //if (command is CompositeCommand) { + append(command.primaryName) + append(" ") + //} + append(subcommand.valueParameters.joinToString(" ") { it.render() }) + if (annotationResolver != null && subcommand is CommandSignatureFromKFunction) { + annotationResolver.getDescription(command, subcommand.originFunction)?.let { description -> + append(" # ") + append(description) + } + } } } } - } - - - companion object { private fun AbstractCommandValueParameter.render(): String { return when (this) {