Fix subCommandNames

This commit is contained in:
Him188 2020-10-30 11:23:55 +08:00
parent f7c8534b4e
commit 80e869e8c9

View File

@ -66,7 +66,7 @@ internal object SimpleCommandSubCommandAnnotationResolver :
function.hasAnnotation<SimpleCommand.Handler>()
override fun getSubCommandNames(ownerCommand: Command, function: KFunction<*>): Array<out String> =
ownerCommand.secondaryNames
emptyArray()
override fun getAnnotatedName(ownerCommand: Command, parameter: KParameter): String? =
parameter.findAnnotation<SimpleCommand.Name>()?.value
@ -228,10 +228,16 @@ internal class CommandReflector(
.onEach { it.checkExtensionReceiver() }
.onEach { it.checkModifiers() }
.onEach { it.checkNames() }
.map { function ->
.flatMap { function ->
val names = annotationResolver.getSubCommandNames(command, function)
if (names.isEmpty()) sequenceOf(createMapEntry(function, null))
else names.associateBy { function }.asSequence()
}
.map { (function, name) ->
val functionNameAsValueParameter =
annotationResolver.getSubCommandNames(command, function).mapIndexed { index, s -> createStringConstantParameter(index, s) }
name?.split(' ')?.mapIndexed { index, s -> createStringConstantParameter(index, s) }
.orEmpty()
val functionValueParameters =
function.valueParameters.associateBy { it.toUserDefinedCommandParameter() }
@ -274,6 +280,11 @@ internal class CommandReflector(
}.toList()
}
private fun <K, V> createMapEntry(key: K, value: V) = object : Map.Entry<K, V> {
override val key: K get() = key
override val value: V get() = value
}
private fun KParameter.toCommandReceiverParameter(): CommandReceiverParameter<out CommandSender> {
check(!this.isVararg) { "Receiver cannot be vararg." }
check(this.type.classifierAsKClass().isSubclassOf(CommandSender::class)) { "Receiver must be subclass of CommandSender" }