Fix MessageScopeCodegen

This commit is contained in:
Him188 2020-08-30 19:12:53 +08:00
parent 6debc1d45b
commit 2dd808c0d7
2 changed files with 57 additions and 5 deletions

View File

@ -74,7 +74,7 @@ internal object MessageScopeCodegen {
fun main(args: Array<String>) = super.startIndependently()
override val defaultInvokeArgs: List<KtType> = 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 <R> ${a}.scopeWith(action: MessageScope.() -> R): R = asMessageScope()(action)
"""

View File

@ -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 {