-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathMessageScopeCodegen.kt
203 lines (186 loc) · 8.13 KB
/
MessageScopeCodegen.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.console.codegen
import net.mamoe.mirai.utils.capitalize
internal val TypeCandidatesForMessageScope = arrayOf(
KtType("Contact"),
KtType("CommandSender"),
)
internal val KtMessageScope = KtType("MessageScope")
internal fun <A> Array<A>.arrangements(): List<Pair<A, A>> {
val result = mutableListOf<Pair<A, A>>()
for (a in this) {
for (b in this) {
result.add(a to b)
}
}
return result
}
internal object MessageScopeCodegen {
object IterableMessageScopeBuildersCodegen : RegionCodegen("MessageScope.kt"), DefaultInvoke {
@JvmStatic
fun main(args: Array<String>) = super.startIndependently()
override val defaultInvokeArgs: List<KtType> = listOf(KtString) // invoke once
@Suppress(
"RedundantVisibilityModifier", "ClassName", "KDocUnresolvedReference", "RedundantSuspendModifier",
"SpellCheckingInspection"
)
override fun StringBuilder.apply(ktType: KtType) {
for (collectionName in arrayOf("Iterable", "Sequence", "Array")) {
for (candidate in (TypeCandidatesForMessageScope + KtMessageScope)) {
appendKCode(
"""
@JvmName("toMessageScope${candidate.standardName.capitalize()}${collectionName.capitalize()}")
public fun $collectionName<$candidate?>.toMessageScope(): MessageScope {
return this.fold(this.firstOrNull().asMessageScopeOrNoop()) { acc, messageScope -> CombinedScope(acc, messageScope.asMessageScopeOrNoop()) }
}
"""
)
appendLine()
}
}
for (candidate in (TypeCandidatesForMessageScope + KtMessageScope)) {
appendKCode(
"""
@JvmSynthetic
@JvmName("toMessageScope${candidate.standardName.capitalize()}Flow")
public suspend fun Flow<$candidate>.toMessageScope(): MessageScope { // Flow<Any?>.firstOrNull isn't yet supported
return this.fold(this.firstOrNull().asMessageScopeOrNoop()) { acc, messageScope -> CombinedScope(acc, messageScope.asMessageScope()) }
}
"""
)
appendLine()
}
}
}
object MessageScopeBuildersCodegen : RegionCodegen("MessageScope.kt"), DefaultInvoke {
@JvmStatic
fun main(args: Array<String>) = super.startIndependently()
override val defaultInvokeArgs: List<KtType> = listOf(KtString) // invoke once
@Suppress("RedundantVisibilityModifier", "ClassName", "KDocUnresolvedReference", "unused")
override fun StringBuilder.apply(ktType: KtType) {
for (candidate in TypeCandidatesForMessageScope) {
appendKCode(
"""
public fun ${candidate}.asMessageScope(): MessageScope = createScopeDelegate(this)
"""
)
appendLine()
}
//
// for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).arrangements()) {
// appendKCode(
// """
// @LowPriorityInOverloadResolution
// public fun ${a}.scopeWith(vararg others: ${b}): MessageScope {
// return others.fold(this.asMessageScope()) { acc, other -> CombinedScope(acc, other.asMessageScope()) }
// }
// """
// )
// appendLine()
// }
for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).arrangements()) {
appendKCode(
"""
@LowPriorityInOverloadResolution
public fun ${a}?.scopeWith(vararg others: ${b}?): MessageScope {
return others.fold(this.asMessageScopeOrNoop()) { acc, other -> acc.scopeWith(other?.asMessageScope()) }
}
"""
)
appendLine()
}
//
// for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).arrangements()) {
// appendKCode(
// """
// public fun ${a}.scopeWith(other: ${b}): MessageScope {
// return CombinedScope(asMessageScope(), other.asMessageScope())
// }
// """
// )
// appendLine()
// }
for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).arrangements()) {
appendKCode(
"""
public fun ${a}?.scopeWith(other: ${b}?): 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!!
}
}
"""
)
appendLine()
}
//
// for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).arrangements()) {
// appendKCode(
// """
// public inline fun <R> ${a}.scopeWith(vararg others: ${b}, action: MessageScope.() -> R): R {
// return scopeWith(*others).invoke(action)
// }
// """
// )
// appendLine()
// }
for ((a, b) in (TypeCandidatesForMessageScope + KtMessageScope).arrangements()) {
appendKCode(
"""
public inline fun <R> ${a}?.scopeWith(vararg others: ${b}?, action: MessageScope.() -> R): R {
return scopeWith(*others).invoke(action)
}
"""
)
appendLine()
}
for (a in (TypeCandidatesForMessageScope + KtMessageScope)) {
appendKCode(
"""
@Deprecated(
"Senseless scopeWith. Use asMessageScope.",
ReplaceWith("this.asMessageScope()", "net.mamoe.mirai.console.util.asMessageScope")
) // diagnostic deprecation
public inline fun ${a}.scopeWith(): MessageScope = asMessageScope()
"""
)
appendLine()
}
for (a in (TypeCandidatesForMessageScope + KtMessageScope)) {
appendKCode(
"""
@Deprecated(
"Senseless scopeWith. Use .asMessageScope().invoke.",
ReplaceWith(
"this.asMessageScope()(action)",
"net.mamoe.mirai.console.util.asMessageScope",
"net.mamoe.mirai.console.util.invoke",
)
) // diagnostic deprecation
public inline fun <R> ${a}.scopeWith(action: MessageScope.() -> R): R = asMessageScope()(action)
"""
)
appendLine()
}
}
}
/**
* 运行本 object 中所有嵌套 object Codegen
*/
@OptIn(ExperimentalStdlibApi::class)
@JvmStatic
fun main(args: Array<String>) {
runCodegenInObject(this::class)
}
}