Introduce RestrictedScope for further resolution for IDE plugin

This commit is contained in:
Him188 2020-09-19 13:48:23 +08:00
parent c8696b6ac5
commit ceb689066e
6 changed files with 59 additions and 13 deletions

View File

@ -36,19 +36,16 @@ public annotation class ResolveContext(
// ConstantKind // ConstantKind
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
PLUGIN_ID, PLUGIN_ID, // ILLEGAL_PLUGIN_DESCRIPTION
PLUGIN_NAME, PLUGIN_NAME, // ILLEGAL_PLUGIN_DESCRIPTION
PLUGIN_VERSION, PLUGIN_VERSION, // ILLEGAL_PLUGIN_DESCRIPTION
COMMAND_NAME, COMMAND_NAME, // ILLEGAL_COMMAND_NAME
PERMISSION_NAMESPACE, PERMISSION_NAMESPACE, // ILLEGAL_COMMAND_NAMESPACE
PERMISSION_NAME, PERMISSION_NAME, // ILLEGAL_COMMAND_NAME
PERMISSION_ID, // for parseFromString PERMISSION_ID, // ILLEGAL_COMMAND_ID
/** RESTRICTED_NO_ARG_CONSTRUCTOR, // NOT_CONSTRUCTABLE_TYPE
* Custom serializers allowed
*/
RESTRICTED_NO_ARG_CONSTRUCTOR,
} }
} }

View File

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

View File

@ -217,8 +217,8 @@ public inline fun <reified T> PluginData.value(
*/ */
@ResolveContext(RESTRICTED_NO_ARG_CONSTRUCTOR) @ResolveContext(RESTRICTED_NO_ARG_CONSTRUCTOR)
@LowPriorityInOverloadResolution @LowPriorityInOverloadResolution
public inline fun <reified T> PluginData.value(apply: T.() -> Unit = {}): SerializerAwareValue<T> = public inline fun <@ResolveContext(RESTRICTED_NO_ARG_CONSTRUCTOR) reified T>
valueImpl<T>(typeOf0<T>(), T::class).also { it.value.apply() } PluginData.value(apply: T.() -> Unit = {}): SerializerAwareValue<T> = valueImpl<T>(typeOf0<T>(), T::class).also { it.value.apply() }
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
@PublishedApi @PublishedApi

View File

@ -18,6 +18,9 @@
package net.mamoe.mirai.console.plugin.jvm package net.mamoe.mirai.console.plugin.jvm
import kotlinx.coroutines.CoroutineScope 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.extension.PluginComponentStorage
import net.mamoe.mirai.console.permission.PermissionIdNamespace import net.mamoe.mirai.console.permission.PermissionIdNamespace
import net.mamoe.mirai.console.plugin.Plugin import net.mamoe.mirai.console.plugin.Plugin
@ -59,6 +62,7 @@ public interface JvmPlugin : Plugin, CoroutineScope,
* *
* @receiver 组件容器 * @receiver 组件容器
*/ */
@RestrictedScope(COMMAND_REGISTER, PERMISSION_REGISTER)
public fun PluginComponentStorage.onLoad() {} public fun PluginComponentStorage.onLoad() {}
/** /**

View File

@ -14,6 +14,7 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor;
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory1; import org.jetbrains.kotlin.diagnostics.DiagnosticFactory1;
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory2; import org.jetbrains.kotlin.diagnostics.DiagnosticFactory2;
import org.jetbrains.kotlin.diagnostics.Errors; import org.jetbrains.kotlin.diagnostics.Errors;
import org.jetbrains.kotlin.psi.KtNamedDeclaration;
import static org.jetbrains.kotlin.diagnostics.Severity.ERROR; import static org.jetbrains.kotlin.diagnostics.Severity.ERROR;
@ -25,6 +26,8 @@ public interface MiraiConsoleErrors {
DiagnosticFactory2<PsiElement, String, String> ILLEGAL_PERMISSION_NAME = DiagnosticFactory2.create(ERROR); DiagnosticFactory2<PsiElement, String, String> ILLEGAL_PERMISSION_NAME = DiagnosticFactory2.create(ERROR);
DiagnosticFactory2<PsiElement, String, String> ILLEGAL_PERMISSION_ID = DiagnosticFactory2.create(ERROR); DiagnosticFactory2<PsiElement, String, String> ILLEGAL_PERMISSION_ID = DiagnosticFactory2.create(ERROR);
DiagnosticFactory2<PsiElement, String, String> ILLEGAL_PERMISSION_NAMESPACE = DiagnosticFactory2.create(ERROR); DiagnosticFactory2<PsiElement, String, String> ILLEGAL_PERMISSION_NAMESPACE = DiagnosticFactory2.create(ERROR);
DiagnosticFactory2<PsiElement, KtNamedDeclaration, String> ILLEGAL_COMMAND_REGISTER_USE = DiagnosticFactory2.create(ERROR);
DiagnosticFactory2<PsiElement, KtNamedDeclaration, String> ILLEGAL_PERMISSION_REGISTER_USE = DiagnosticFactory2.create(ERROR);
@Deprecated @Deprecated
Object _init = new Object() { Object _init = new Object() {

View File

@ -61,6 +61,20 @@ object MiraiConsoleErrorsRendering : DefaultErrorMessages.Extension {
Renderers.STRING, Renderers.STRING,
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 override fun getMap() = MAP