diff --git a/tools/intellij-plugin/run/projects/test-project/build.gradle.kts b/tools/intellij-plugin/run/projects/test-project/build.gradle.kts index 6b4e39278..e9197fb74 100644 --- a/tools/intellij-plugin/run/projects/test-project/build.gradle.kts +++ b/tools/intellij-plugin/run/projects/test-project/build.gradle.kts @@ -1,7 +1,7 @@ plugins { kotlin("jvm") version "1.4.20" kotlin("plugin.serialization") version "1.4.20" - id("net.mamoe.mirai-console") version "1.1.0-dev-33" + id("net.mamoe.mirai-console") version "1.1.0-dev-36" } group = "org.example" diff --git a/tools/intellij-plugin/run/projects/test-project/src/main/kotlin/org/example/myplugin/MySimpleCommand.kt b/tools/intellij-plugin/run/projects/test-project/src/main/kotlin/org/example/myplugin/MySimpleCommand.kt index 7ebe7b6a5..ca59a514b 100644 --- a/tools/intellij-plugin/run/projects/test-project/src/main/kotlin/org/example/myplugin/MySimpleCommand.kt +++ b/tools/intellij-plugin/run/projects/test-project/src/main/kotlin/org/example/myplugin/MySimpleCommand.kt @@ -28,7 +28,7 @@ object DataTest : AutoSavePluginConfig("data") { } object DataTest1 : ReadOnlyPluginConfig("data") { - var pp by value() + val pp by value() } @Serializable diff --git a/tools/intellij-plugin/src/QuickFixRegistrar.kt b/tools/intellij-plugin/src/QuickFixRegistrar.kt index e860ec0a0..334195a3d 100644 --- a/tools/intellij-plugin/src/QuickFixRegistrar.kt +++ b/tools/intellij-plugin/src/QuickFixRegistrar.kt @@ -12,6 +12,7 @@ package net.mamoe.mirai.console.intellij import com.intellij.codeInsight.intention.IntentionAction import net.mamoe.mirai.console.compiler.common.diagnostics.MiraiConsoleErrors import net.mamoe.mirai.console.intellij.diagnostics.fix.AddSerializerFix +import net.mamoe.mirai.console.intellij.diagnostics.fix.ConvertToValFix import net.mamoe.mirai.console.intellij.diagnostics.fix.ProvideDefaultValueFix import org.jetbrains.kotlin.diagnostics.DiagnosticFactory import org.jetbrains.kotlin.idea.quickfix.KotlinIntentionActionsFactory @@ -31,5 +32,6 @@ class QuickFixRegistrar : QuickFixContributor { MiraiConsoleErrors.UNSERIALIZABLE_TYPE.registerFactory(AddSerializerFix) MiraiConsoleErrors.NOT_CONSTRUCTABLE_TYPE.registerFactory(ProvideDefaultValueFix) + MiraiConsoleErrors.READ_ONLY_VALUE_CANNOT_BE_VAR.registerFactory(ConvertToValFix) } } diff --git a/tools/intellij-plugin/src/diagnostics/fix/ConvertToValFix.kt b/tools/intellij-plugin/src/diagnostics/fix/ConvertToValFix.kt new file mode 100644 index 000000000..70a493678 --- /dev/null +++ b/tools/intellij-plugin/src/diagnostics/fix/ConvertToValFix.kt @@ -0,0 +1,52 @@ +/* + * Copyright 2019-2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.console.intellij.diagnostics.fix + +import com.intellij.codeInsight.intention.IntentionAction +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiFile +import net.mamoe.mirai.console.compiler.common.castOrNull +import net.mamoe.mirai.console.compiler.common.diagnostics.MiraiConsoleErrors +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.diagnostics.SimpleDiagnostic +import org.jetbrains.kotlin.idea.inspections.KotlinUniversalQuickFix +import org.jetbrains.kotlin.idea.quickfix.KotlinCrossLanguageQuickFixAction +import org.jetbrains.kotlin.idea.quickfix.KotlinSingleIntentionActionFactory +import org.jetbrains.kotlin.idea.util.application.executeWriteCommand +import org.jetbrains.kotlin.psi.KtPsiFactory + +/** + * @see MiraiConsoleErrors.READ_ONLY_VALUE_CANNOT_BE_VAR + */ +class ConvertToValFix( + element: PsiElement, +) : KotlinCrossLanguageQuickFixAction(element), KotlinUniversalQuickFix { + + override fun getFamilyName(): String = "Mirai Console" + override fun getText(): String = "转换为 val" + + override fun invokeImpl(project: Project, editor: Editor?, file: PsiFile) { + val element = element ?: return + project.executeWriteCommand(name) { + element.replace(KtPsiFactory(project).createValKeyword()) + } + } + + companion object : KotlinSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): IntentionAction? { + val diagnostic1 = diagnostic.castOrNull>() ?: return null + return ConvertToValFix(diagnostic1.psiElement) + } + + override fun isApplicableForCodeFragment(): Boolean = false + } +}