diff --git a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/MessageScopeCodegen.kt b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/MessageScopeCodegen.kt index 0f5dd7839..0f4cf60b2 100644 --- a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/MessageScopeCodegen.kt +++ b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/MessageScopeCodegen.kt @@ -74,7 +74,7 @@ internal object MessageScopeCodegen { fun main(args: Array) = super.startIndependently() override val defaultInvokeArgs: List = listOf(KtString) // invoke once - @Suppress("RedundantVisibilityModifier", "ClassName", "KDocUnresolvedReference") + @Suppress("RedundantVisibilityModifier", "ClassName", "KDocUnresolvedReference", "unused") override fun StringBuilder.apply(ktType: KtType) { for (candidate in TypeCandidatesForMessageScope) { appendKCode( @@ -109,7 +109,7 @@ internal object MessageScopeCodegen { appendLine() } - for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).distinctArrangements()) { + for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).arrangements()) { appendKCode( """ public fun ${a}.scopeWith(other: ${b}): MessageScope { @@ -120,7 +120,7 @@ internal object MessageScopeCodegen { appendLine() } - for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).distinctArrangements()) { + for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).arrangements()) { appendKCode( """ public fun ${a}?.scopeWithNotNull(other: ${b}?): MessageScope { @@ -163,7 +163,10 @@ internal object MessageScopeCodegen { for (a in (TypeCandidatesForMessageScope + KtMessageScope)) { appendKCode( """ - @Deprecated("Senseless scopeWith. Use asMessageScope.", ReplaceWith("this.asMessageScope()", "net.mamoe.mirai.console.util.asMessageScope")) + @Deprecated( + "Senseless scopeWith. Use asMessageScope.", + ReplaceWith("this.asMessageScope()", "net.mamoe.mirai.console.util.asMessageScope") + ) public inline fun ${a}.scopeWith(): MessageScope = asMessageScope() """ ) @@ -175,7 +178,11 @@ internal object MessageScopeCodegen { """ @Deprecated( "Senseless scopeWith. Use .asMessageScope().invoke.", - ReplaceWith("this.asMessageScope()(action)", "net.mamoe.mirai.console.util.asMessageScope", "net.mamoe.mirai.console.util.invoke") + ReplaceWith( + "this.asMessageScope()(action)", + "net.mamoe.mirai.console.util.asMessageScope", + "net.mamoe.mirai.console.util.invoke" + ) ) public inline fun ${a}.scopeWith(action: MessageScope.() -> R): R = asMessageScope()(action) """ diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/MessageScope.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/MessageScope.kt index 57d2fab0c..5996ec461 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/MessageScope.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/MessageScope.kt @@ -155,6 +155,10 @@ public fun Contact.scopeWith(other: MessageScope): MessageScope { return CombinedScope(asMessageScope(), other.asMessageScope()) } +public fun CommandSender.scopeWith(other: Contact): MessageScope { + return CombinedScope(asMessageScope(), other.asMessageScope()) +} + public fun CommandSender.scopeWith(other: CommandSender): MessageScope { return CombinedScope(asMessageScope(), other.asMessageScope()) } @@ -163,6 +167,14 @@ public fun CommandSender.scopeWith(other: MessageScope): MessageScope { return CombinedScope(asMessageScope(), other.asMessageScope()) } +public fun MessageScope.scopeWith(other: Contact): MessageScope { + return CombinedScope(asMessageScope(), other.asMessageScope()) +} + +public fun MessageScope.scopeWith(other: CommandSender): MessageScope { + return CombinedScope(asMessageScope(), other.asMessageScope()) +} + public fun MessageScope.scopeWith(other: MessageScope): MessageScope { return CombinedScope(asMessageScope(), other.asMessageScope()) } @@ -200,6 +212,17 @@ public fun Contact?.scopeWithNotNull(other: MessageScope?): MessageScope { } } +public fun CommandSender?.scopeWithNotNull(other: Contact?): MessageScope { + @Suppress("DuplicatedCode") + return when { + this == null && other == null -> NoopMessageScope + this == null && other != null -> other.asMessageScope() + this != null && other == null -> this.asMessageScope() + this != null && other != null -> CombinedScope(asMessageScope(), other.asMessageScope()) + else -> null!! + } +} + public fun CommandSender?.scopeWithNotNull(other: CommandSender?): MessageScope { @Suppress("DuplicatedCode") return when { @@ -222,6 +245,28 @@ public fun CommandSender?.scopeWithNotNull(other: MessageScope?): MessageScope { } } +public fun MessageScope?.scopeWithNotNull(other: Contact?): MessageScope { + @Suppress("DuplicatedCode") + return when { + this == null && other == null -> NoopMessageScope + this == null && other != null -> other.asMessageScope() + this != null && other == null -> this.asMessageScope() + this != null && other != null -> CombinedScope(asMessageScope(), other.asMessageScope()) + else -> null!! + } +} + +public fun MessageScope?.scopeWithNotNull(other: CommandSender?): MessageScope { + @Suppress("DuplicatedCode") + return when { + this == null && other == null -> NoopMessageScope + this == null && other != null -> other.asMessageScope() + this != null && other == null -> this.asMessageScope() + this != null && other != null -> CombinedScope(asMessageScope(), other.asMessageScope()) + else -> null!! + } +} + public fun MessageScope?.scopeWithNotNull(other: MessageScope?): MessageScope { @Suppress("DuplicatedCode") return when {