Fix CommandExecuteResult

This commit is contained in:
Him188 2020-11-16 15:30:57 +08:00
parent fbe5ce0970
commit 46ff28050f
5 changed files with 36 additions and 34 deletions

View File

@ -83,16 +83,16 @@ public sealed class CommandExecuteResult {
) : Failure() ) : Failure()
/** 没有匹配的指令 */ /** 没有匹配的指令 */
public class UnresolvedCommand : Failure() { public class UnresolvedCommand(
/** 解析的 [CommandCall] (如果匹配到) */
public override val call: CommandCall?,
) : Failure() {
/** 指令执行时发生的错误, 总是 `null` */ /** 指令执行时发生的错误, 总是 `null` */
public override val exception: Nothing? get() = null public override val exception: Nothing? get() = null
/** 尝试执行的指令, 总是 `null` */ /** 尝试执行的指令, 总是 `null` */
public override val command: Nothing? get() = null public override val command: Nothing? get() = null
/** 解析的 [CommandCall] (如果匹配到) */
public override val call: CommandCall? get() = null
/** 解析的 [ResolvedCommandCall] (如果匹配到) */ /** 解析的 [ResolvedCommandCall] (如果匹配到) */
public override val resolvedCall: ResolvedCommandCall? get() = null public override val resolvedCall: ResolvedCommandCall? get() = null
} }
@ -114,6 +114,8 @@ public sealed class CommandExecuteResult {
public class UnmatchedSignature( public class UnmatchedSignature(
/** 尝试执行的指令 */ /** 尝试执行的指令 */
public override val command: Command, public override val command: Command,
/** 解析的 [CommandCall] (如果匹配到) */
public override val call: CommandCall,
/** 尝试执行的指令 */ /** 尝试执行的指令 */
@ExperimentalCommandDescriptors @ExperimentalCommandDescriptors
@ConsoleExperimentalApi @ConsoleExperimentalApi
@ -122,9 +124,6 @@ public sealed class CommandExecuteResult {
/** 指令执行时发生的错误, 总是 `null` */ /** 指令执行时发生的错误, 总是 `null` */
public override val exception: Nothing? get() = null public override val exception: Nothing? get() = null
/** 解析的 [CommandCall] (如果匹配到) */
public override val call: CommandCall? get() = null
/** 解析的 [ResolvedCommandCall] (如果匹配到) */ /** 解析的 [ResolvedCommandCall] (如果匹配到) */
public override val resolvedCall: ResolvedCommandCall? get() = null public override val resolvedCall: ResolvedCommandCall? get() = null
} }

View File

@ -79,7 +79,7 @@ public object BuiltInCommandCallResolver : CommandCallResolver {
val failureReasons = unmatchedCommandSignatures.toMutableList() val failureReasons = unmatchedCommandSignatures.toMutableList()
val rA = FailureReason.ResolutionAmbiguity(resolutionAmbiguities) val rA = FailureReason.ResolutionAmbiguity(resolutionAmbiguities)
failureReasons.addAll(resolutionAmbiguities.map { UnmatchedCommandSignature(it, rA) }) failureReasons.addAll(resolutionAmbiguities.map { UnmatchedCommandSignature(it, rA) })
return CommandExecuteResult.UnmatchedSignature(command, unmatchedCommandSignatures) return CommandExecuteResult.UnmatchedSignature(command, call, unmatchedCommandSignatures)
} }
} }

View File

@ -33,16 +33,15 @@ public class CommandResolveResult private constructor(
get() = value.safeCast() get() = value.safeCast()
public inline fun <R> fold( public inline fun <R> fold(
onSuccess: (ResolvedCommandCall) -> R, onSuccess: (ResolvedCommandCall?) -> R,
onFailure: (CommandExecuteResult.Failure) -> R, onFailure: (CommandExecuteResult.Failure) -> R,
): R { ): R {
contract { contract {
callsInPlace(onSuccess, InvocationKind.AT_MOST_ONCE) callsInPlace(onSuccess, InvocationKind.AT_MOST_ONCE)
callsInPlace(onFailure, InvocationKind.AT_MOST_ONCE) callsInPlace(onFailure, InvocationKind.AT_MOST_ONCE)
} }
call?.let(onSuccess)?.let { return it }
failure?.let(onFailure)?.let { return it } failure?.let(onFailure)?.let { return it }
throw kotlin.AssertionError() return call.let(onSuccess)
} }
public constructor(call: ResolvedCommandCall?) : this(call as Any?) public constructor(call: ResolvedCommandCall?) : this(call as Any?)

View File

@ -177,11 +177,11 @@ internal suspend fun executeCommandImpl(
caller: CommandSender, caller: CommandSender,
checkPermission: Boolean, checkPermission: Boolean,
): CommandExecuteResult { ): 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( val resolved = call.resolve().fold(
onSuccess = { it }, onSuccess = { it },
onFailure = { return it } onFailure = { return it }
) ) ?: return CommandExecuteResult.UnresolvedCommand(call)
val command = resolved.callee val command = resolved.callee

View File

@ -144,6 +144,11 @@ internal class CommandReflector(
} }
fun generateUsage(overloads: Iterable<CommandSignatureFromKFunction>): String { fun generateUsage(overloads: Iterable<CommandSignatureFromKFunction>): String {
return generateUsage(command, annotationResolver, overloads)
}
companion object {
fun generateUsage(command: Command, annotationResolver: SubCommandAnnotationResolver?, overloads: Iterable<CommandSignature>): String {
return overloads.joinToString("\n") { subcommand -> return overloads.joinToString("\n") { subcommand ->
buildString { buildString {
if (command.prefixOptional) { if (command.prefixOptional) {
@ -158,6 +163,7 @@ internal class CommandReflector(
append(" ") append(" ")
//} //}
append(subcommand.valueParameters.joinToString(" ") { it.render() }) append(subcommand.valueParameters.joinToString(" ") { it.render() })
if (annotationResolver != null && subcommand is CommandSignatureFromKFunction) {
annotationResolver.getDescription(command, subcommand.originFunction)?.let { description -> annotationResolver.getDescription(command, subcommand.originFunction)?.let { description ->
append(" # ") append(" # ")
append(description) append(description)
@ -165,9 +171,7 @@ internal class CommandReflector(
} }
} }
} }
}
companion object {
private fun <T> AbstractCommandValueParameter<T>.render(): String { private fun <T> AbstractCommandValueParameter<T>.render(): String {
return when (this) { return when (this) {