From ceb689066eda7045c17691aeced4e14f70714c7a Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 19 Sep 2020 13:48:23 +0800 Subject: [PATCH] Introduce RestrictedScope for further resolution for IDE plugin --- .../console/compiler/common/ResolveContext.kt | 19 ++++++------- .../compiler/common/RestrictedScope.kt | 28 +++++++++++++++++++ .../mamoe/mirai/console/data/PluginData.kt | 4 +-- .../mirai/console/plugin/jvm/JvmPlugin.kt | 4 +++ .../diagnostics/MiraiConsoleErrors.java | 3 ++ .../MiraiConsoleErrorsRendering.kt | 14 ++++++++++ 6 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/compiler/common/RestrictedScope.kt diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/compiler/common/ResolveContext.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/compiler/common/ResolveContext.kt index 8a3702b69..9dff987e2 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/compiler/common/ResolveContext.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/compiler/common/ResolveContext.kt @@ -36,19 +36,16 @@ public annotation class ResolveContext( // ConstantKind /////////////////////////////////////////////////////////////////////////// - PLUGIN_ID, - PLUGIN_NAME, - PLUGIN_VERSION, + PLUGIN_ID, // ILLEGAL_PLUGIN_DESCRIPTION + PLUGIN_NAME, // ILLEGAL_PLUGIN_DESCRIPTION + PLUGIN_VERSION, // ILLEGAL_PLUGIN_DESCRIPTION - COMMAND_NAME, + COMMAND_NAME, // ILLEGAL_COMMAND_NAME - PERMISSION_NAMESPACE, - PERMISSION_NAME, - PERMISSION_ID, // for parseFromString + PERMISSION_NAMESPACE, // ILLEGAL_COMMAND_NAMESPACE + PERMISSION_NAME, // ILLEGAL_COMMAND_NAME + PERMISSION_ID, // ILLEGAL_COMMAND_ID - /** - * Custom serializers allowed - */ - RESTRICTED_NO_ARG_CONSTRUCTOR, + RESTRICTED_NO_ARG_CONSTRUCTOR, // NOT_CONSTRUCTABLE_TYPE } } \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/compiler/common/RestrictedScope.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/compiler/common/RestrictedScope.kt new file mode 100644 index 000000000..640016cc7 --- /dev/null +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/compiler/common/RestrictedScope.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2020 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.compiler.common + +import net.mamoe.mirai.console.util.ConsoleExperimentalApi +import kotlin.annotation.AnnotationTarget.FUNCTION + +/** + * 标记一个函数, 在其函数体内限制特定一些函数的使用. + */ +@ConsoleExperimentalApi +@Target(FUNCTION) +@Retention(AnnotationRetention.BINARY) +public annotation class RestrictedScope( + vararg val kinds: Kind, +) { + public enum class Kind { + PERMISSION_REGISTER, // ILLEGAL_PERMISSION_REGISTER_USE + COMMAND_REGISTER, // ILLEGAL_COMMAND_REGISTER_USE + } +} \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginData.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginData.kt index c8c01736d..174170e1b 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginData.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginData.kt @@ -217,8 +217,8 @@ public inline fun PluginData.value( */ @ResolveContext(RESTRICTED_NO_ARG_CONSTRUCTOR) @LowPriorityInOverloadResolution -public inline fun PluginData.value(apply: T.() -> Unit = {}): SerializerAwareValue = - valueImpl(typeOf0(), T::class).also { it.value.apply() } +public inline fun <@ResolveContext(RESTRICTED_NO_ARG_CONSTRUCTOR) reified T> + PluginData.value(apply: T.() -> Unit = {}): SerializerAwareValue = valueImpl(typeOf0(), T::class).also { it.value.apply() } @Suppress("UNCHECKED_CAST") @PublishedApi diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPlugin.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPlugin.kt index c76c3ee6a..d3aa7e7cb 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPlugin.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPlugin.kt @@ -18,6 +18,9 @@ package net.mamoe.mirai.console.plugin.jvm import kotlinx.coroutines.CoroutineScope +import net.mamoe.mirai.console.compiler.common.RestrictedScope +import net.mamoe.mirai.console.compiler.common.RestrictedScope.Kind.COMMAND_REGISTER +import net.mamoe.mirai.console.compiler.common.RestrictedScope.Kind.PERMISSION_REGISTER import net.mamoe.mirai.console.extension.PluginComponentStorage import net.mamoe.mirai.console.permission.PermissionIdNamespace import net.mamoe.mirai.console.plugin.Plugin @@ -59,6 +62,7 @@ public interface JvmPlugin : Plugin, CoroutineScope, * * @receiver 组件容器 */ + @RestrictedScope(COMMAND_REGISTER, PERMISSION_REGISTER) public fun PluginComponentStorage.onLoad() {} /** diff --git a/tools/compiler-common/src/main/java/net/mamoe/mirai/console/compiler/common/diagnostics/MiraiConsoleErrors.java b/tools/compiler-common/src/main/java/net/mamoe/mirai/console/compiler/common/diagnostics/MiraiConsoleErrors.java index e4e2e21da..a8181fb84 100644 --- a/tools/compiler-common/src/main/java/net/mamoe/mirai/console/compiler/common/diagnostics/MiraiConsoleErrors.java +++ b/tools/compiler-common/src/main/java/net/mamoe/mirai/console/compiler/common/diagnostics/MiraiConsoleErrors.java @@ -14,6 +14,7 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor; import org.jetbrains.kotlin.diagnostics.DiagnosticFactory1; import org.jetbrains.kotlin.diagnostics.DiagnosticFactory2; import org.jetbrains.kotlin.diagnostics.Errors; +import org.jetbrains.kotlin.psi.KtNamedDeclaration; import static org.jetbrains.kotlin.diagnostics.Severity.ERROR; @@ -25,6 +26,8 @@ public interface MiraiConsoleErrors { DiagnosticFactory2 ILLEGAL_PERMISSION_NAME = DiagnosticFactory2.create(ERROR); DiagnosticFactory2 ILLEGAL_PERMISSION_ID = DiagnosticFactory2.create(ERROR); DiagnosticFactory2 ILLEGAL_PERMISSION_NAMESPACE = DiagnosticFactory2.create(ERROR); + DiagnosticFactory2 ILLEGAL_COMMAND_REGISTER_USE = DiagnosticFactory2.create(ERROR); + DiagnosticFactory2 ILLEGAL_PERMISSION_REGISTER_USE = DiagnosticFactory2.create(ERROR); @Deprecated Object _init = new Object() { diff --git a/tools/compiler-common/src/main/kotlin/net/mamoe/mirai/console/compiler/common/diagnostics/MiraiConsoleErrorsRendering.kt b/tools/compiler-common/src/main/kotlin/net/mamoe/mirai/console/compiler/common/diagnostics/MiraiConsoleErrorsRendering.kt index e3e7b7b6a..ca79ec5b3 100644 --- a/tools/compiler-common/src/main/kotlin/net/mamoe/mirai/console/compiler/common/diagnostics/MiraiConsoleErrorsRendering.kt +++ b/tools/compiler-common/src/main/kotlin/net/mamoe/mirai/console/compiler/common/diagnostics/MiraiConsoleErrorsRendering.kt @@ -61,6 +61,20 @@ object MiraiConsoleErrorsRendering : DefaultErrorMessages.Extension { Renderers.STRING, Renderers.STRING, ) + + put( + ILLEGAL_COMMAND_REGISTER_USE, + "''{0}'' 无法使用在 ''{1}'' 环境下.", + Renderers.DECLARATION_NAME, + Renderers.STRING + ) + + put( + ILLEGAL_PERMISSION_REGISTER_USE, + "''{0}'' 无法使用在 ''{1}'' 环境下.", + Renderers.DECLARATION_NAME, + Renderers.STRING + ) } override fun getMap() = MAP