Skip to content

Commit aaeb59f

Browse files
authored
Merge pull request #216 from simple-robot/dev/fix-event-name
fix: 修复部分事件在命名模式下的错误结果 see #215
2 parents 8dea802 + a46cb60 commit aaeb59f

File tree

3 files changed

+73
-8
lines changed

3 files changed

+73
-8
lines changed

internal-processors/intents-processor/src/main/kotlin/qg/internal/processors/intents/EventIntentsAggregationProcessor.kt

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ private val IntentsClassName = ClassName(INTENTS_PACKAGE, INTENTS_CLASS_NAME)
5252

5353
private const val NO_WARP = "·"
5454

55+
private const val EVENT_NAME_BASED_MARKER_NAME = "EventNameBasedMarker"
56+
private const val EVENT_NAME_BASED_MARKER_PKG = "love.forte.simbot.qguild.internal"
57+
5558
/**
5659
* 寻找所有 `love.forte.simbot.qguild.event.EventIntents` 的子 `object` 类型,
5760
* 并生成它们 intents 的各种聚合到 `EventIntentsAggregation` 中。
@@ -99,7 +102,10 @@ internal class EventIntentsAggregationProcessor(
99102

100103
val fileBuilder = FileSpec.builder(OUTPUT_PACKAGE, AGGREGATION_FILE_NAME)
101104
fileBuilder.addType(aggregationBuilder.build())
102-
fileBuilder.addFileComment("本文件内容为自动生成,生成于 %L", OffsetDateTime.now(ZoneOffset.ofHours(8)).toString())
105+
fileBuilder.addFileComment(
106+
"本文件内容为自动生成,生成于 %L",
107+
OffsetDateTime.now(ZoneOffset.ofHours(8)).toString()
108+
)
103109

104110
val targetFile = fileBuilder.build()
105111

@@ -198,14 +204,40 @@ internal class EventIntentsAggregationProcessor(
198204
data class NamesToType(val names: Set<String>, val declaration: KSClassDeclaration)
199205

200206
val nameToTypes = list.map { declaration ->
201-
val baseName = declaration.simpleName.asString()
207+
val nameBasedAnnotation = declaration.annotations
208+
.firstOrNull {
209+
(it.annotationType.resolve().declaration as? KSClassDeclaration)?.let { annoDecl ->
210+
annoDecl.simpleName.asString() == EVENT_NAME_BASED_MARKER_NAME
211+
&& annoDecl.packageName.asString() == EVENT_NAME_BASED_MARKER_PKG
212+
} ?: false
213+
}
214+
215+
fun baseName(): String = declaration.simpleName.asString()
216+
217+
fun findFromAnnotation(name: String): String? =
218+
nameBasedAnnotation?.arguments?.firstOrNull {
219+
it.name?.asString() == name
220+
}?.value as? String?
221+
222+
val firstUpper = findFromAnnotation("firstUpper")
223+
?.takeUnless { it.isBlank() }
224+
?: baseName()
225+
val firstLower = findFromAnnotation("firstLower")
226+
?.takeUnless { it.isBlank() }
227+
?: baseName().replaceFirstChar(Char::lowercaseChar)
228+
val snackUpper = findFromAnnotation("snackUpper")
229+
?.takeUnless { it.isBlank() }
230+
?: baseName().toSnack(true)
231+
val snackLower = findFromAnnotation("snackLower")
232+
?.takeUnless { it.isBlank() }
233+
?: baseName().toSnack(false)
234+
235+
202236
val set = setOf(
203-
baseName,
204-
// 开头小写
205-
baseName.replaceFirstChar(Char::lowercaseChar),
206-
// 下划线的全大写与全小写
207-
baseName.toSnack(false),
208-
baseName.toSnack(true)
237+
firstUpper,
238+
firstLower,
239+
snackUpper,
240+
snackLower,
209241
)
210242

211243
NamesToType(set, declaration)

simbot-component-qq-guild-api/src/commonMain/kotlin/love/forte/simbot/qguild/event/EventIntents.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import love.forte.simbot.qguild.PrivateDomainOnly
2323
import love.forte.simbot.qguild.event.EventIntents.*
2424
import love.forte.simbot.qguild.event.EventIntents.Companion.READY_TYPE
2525
import love.forte.simbot.qguild.event.EventIntents.Companion.RESUMED_TYPE
26+
import love.forte.simbot.qguild.internal.EventNameBasedMarker
2627
import love.forte.simbot.qguild.model.User
2728
import kotlin.jvm.JvmInline
2829
import kotlin.jvm.JvmName
@@ -350,6 +351,10 @@ public sealed class EventIntents {
350351
* - GROUP_MSG_RECEIVE // 群管理员主动在机器人资料页操作开启通知
351352
* ```
352353
*/
354+
@EventNameBasedMarker(
355+
snackLower = "group_and_c2c_event",
356+
snackUpper = "GROUP_AND_C2C_EVENT"
357+
)
353358
public data object GroupAndC2CEvent : EventIntents() {
354359
public const val INTENTS_INDEX: Int = 25
355360
internal const val INTENTS: Int = 1 shl INTENTS_INDEX
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* This file is part of simbot-component-qq-guild.
5+
*
6+
* simbot-component-qq-guild is free software: you can redistribute it and/or modify it under the terms
7+
* of the GNU Lesser General Public License as published by the Free Software Foundation,
8+
* either version 3 of the License, or (at your option) any later version.
9+
*
10+
* simbot-component-qq-guild is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-qq-guild.
15+
* If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package love.forte.simbot.qguild.internal
19+
20+
@Suppress("unused")
21+
@Target(AnnotationTarget.CLASS)
22+
@Retention(AnnotationRetention.SOURCE)
23+
internal annotation class EventNameBasedMarker(
24+
val firstUpper: String = "",
25+
val firstLower: String = "",
26+
val snackUpper: String = "",
27+
val snackLower: String = "",
28+
)

0 commit comments

Comments
 (0)