Provide ConvertToValFix for READ_ONLY_VALUE_CANNOT_BE_VAR

This commit is contained in:
Him188 2020-12-01 10:14:12 +08:00
parent d532a9ffa1
commit f802415c7a
4 changed files with 56 additions and 2 deletions

View File

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

View File

@ -28,7 +28,7 @@ object DataTest : AutoSavePluginConfig("data") {
}
object DataTest1 : ReadOnlyPluginConfig("data") {
var pp by value<String>()
val pp by value<String>()
}
@Serializable

View File

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

View File

@ -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<PsiElement>(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<SimpleDiagnostic<PsiElement>>() ?: return null
return ConvertToValFix(diagnostic1.psiElement)
}
override fun isApplicableForCodeFragment(): Boolean = false
}
}