From 21cb1259840470c3453e4a0a7f58ed1e11603c07 Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 23 Dec 2020 17:51:13 +0800 Subject: [PATCH] Move MessageKey from Message.kt to MessageKey.kt --- .../commonMain/kotlin/message/data/Message.kt | 48 ----------------- .../kotlin/message/data/MessageKey.kt | 51 +++++++++++++++++++ 2 files changed, 51 insertions(+), 48 deletions(-) diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/Message.kt b/mirai-core-api/src/commonMain/kotlin/message/data/Message.kt index ef40917be..dd12a902b 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/Message.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/Message.kt @@ -320,54 +320,6 @@ public interface ConstrainSingle : SingleMessage { public val key: MessageKey<*> } -/** - * 独立的 [MessageKey] 的实现. '独立' 即 `final`, 不支持多态类型. 适用于作为最顶层的 [MessageKey], 如 [MessageSource]. - * - * @see AbstractPolymorphicMessageKey - */ -@ExperimentalMessageKey -public abstract class AbstractMessageKey( - override val safeCast: (SingleMessage) -> M?, -) : MessageKey - -/** - * 多态 [MessageKey]. - * - * 示例: [HummerMessage] - * ``` - * MessageContent - * ↑ - * HummerMessage - * ↑ - * +------------+-------------+ - * | | | - * PokeMessage VipFace FlashImage - * - * ``` - * - * 当 [连接][Message.plus] 一个 [VipFace] 到一个 [MessageChain] 时, - * 由于 [VipFace] 最上层为 [MessageContent], 消息链中第一个 [MessageContent] 会被 (保留顺序地) 替换为 [VipFace], 其他所有 [MessageContent] 都会被删除. - * - * 如: - * ``` - * val source: MessageSource = ... - * - * val result = messageChainOf(PlainText("a"), PlainText("b"), source, AtAll) + VipFace.LiuLian - * // result 为 [VipFace.LiuLian, source] - * - * val result = source1 + source2 - * // result 为 [source2], 总是右侧替换左侧 - * ``` - */ -@ExperimentalMessageKey -public abstract class AbstractPolymorphicMessageKey( - baseKey: MessageKey, - safeCast: (SingleMessage) -> M?, -) : MessageKey, AbstractMessageKey(safeCast) { - internal val topmostKey: MessageKey<*> = - if (baseKey is AbstractPolymorphicMessageKey<*, *>) baseKey.topmostKey else baseKey -} - /** * 带内容的消息. * diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/MessageKey.kt b/mirai-core-api/src/commonMain/kotlin/message/data/MessageKey.kt index 97c1ff8d3..78cbc97f4 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/MessageKey.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/MessageKey.kt @@ -30,6 +30,57 @@ public interface MessageKey { public val safeCast: (SingleMessage) -> M? } + +/** + * 独立的 [MessageKey] 的实现. '独立' 即 `final`, 不支持多态类型. 适用于作为最顶层的 [MessageKey], 如 [MessageSource]. + * + * @see AbstractPolymorphicMessageKey + */ +@ExperimentalMessageKey +public abstract class AbstractMessageKey( + override val safeCast: (SingleMessage) -> M?, +) : MessageKey + + +/** + * 多态 [MessageKey]. + * + * 示例: [HummerMessage] + * ``` + * MessageContent + * ↑ + * HummerMessage + * ↑ + * +------------+-------------+ + * | | | + * PokeMessage VipFace FlashImage + * + * ``` + * + * 当 [连接][Message.plus] 一个 [VipFace] 到一个 [MessageChain] 时, + * 由于 [VipFace] 最上层为 [MessageContent], 消息链中第一个 [MessageContent] 会被 (保留顺序地) 替换为 [VipFace], 其他所有 [MessageContent] 都会被删除. + * + * 如: + * ``` + * val source: MessageSource = ... + * + * val result = messageChainOf(PlainText("a"), PlainText("b"), source, AtAll) + VipFace.LiuLian + * // result 为 [VipFace.LiuLian, source] + * + * val result = source1 + source2 + * // result 为 [source2], 总是右侧替换左侧 + * ``` + */ +@ExperimentalMessageKey +public abstract class AbstractPolymorphicMessageKey( + baseKey: MessageKey, + safeCast: (SingleMessage) -> M?, +) : MessageKey, AbstractMessageKey(safeCast) { + internal val topmostKey: MessageKey<*> = + if (baseKey is AbstractPolymorphicMessageKey<*, *>) baseKey.topmostKey else baseKey +} + + @ExperimentalMessageKey public fun MessageKey<*>.isInstance(message: SingleMessage): Boolean = this.safeCast(message) != null