Make mirai-console compatible with IDEA 2021.1, fix #323

This commit is contained in:
Him188 2021-04-08 01:42:12 +08:00
parent 2d12d8bbce
commit a40e5731e5
4 changed files with 25 additions and 18 deletions

View File

@ -20,8 +20,8 @@ object Versions {
const val kotlinCompiler = "1.4.21"
const val kotlinStdlib = "1.4.21"
const val kotlinIntellijPlugin = "203-1.4.21-release-IJ5981.133" // keep to newest as kotlinCompiler
const val intellij = "2020.3" // don't update easily unless you want your disk space -= 500MB
const val kotlinIntellijPlugin = "211-1.4.32-release-IJ6693.72" // keep to newest as kotlinCompiler
const val intellij = "2021.1" // don't update easily unless you want your disk space -= 500MB
const val coroutines = "1.4.0"
@ -40,7 +40,7 @@ object Versions {
@Suppress("SpellCheckingInspection")
const val yamlkt = "0.7.5"
const val intellijGradlePlugin = "0.4.16"
const val intellijGradlePlugin = "0.7.2"
}
const val `kotlin-compiler` = "org.jetbrains.kotlin:kotlin-compiler:${Versions.kotlinCompiler}"

View File

@ -15,7 +15,6 @@ import com.intellij.openapi.module.Module
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.testFramework.writeChild
import net.mamoe.mirai.console.intellij.assets.FT
import net.mamoe.mirai.console.intellij.creator.MiraiProjectModel
import net.mamoe.mirai.console.intellij.creator.tasks.getTemplate
@ -24,6 +23,7 @@ import net.mamoe.mirai.console.intellij.creator.tasks.runWriteActionAndWait
import net.mamoe.mirai.console.intellij.creator.tasks.writeChild
import net.mamoe.mirai.console.intellij.creator.templateProperties
import org.jetbrains.kotlin.idea.core.util.toPsiFile
import java.nio.file.Path
sealed class ProjectCreator(
val module: Module,
@ -36,11 +36,11 @@ sealed class ProjectCreator(
model.checkValuesNotNull()
}
protected val filesChanged = mutableListOf<VirtualFile>()
protected val filesChanged = mutableListOf<Path>()
@Synchronized
protected fun addFileChanged(vf: VirtualFile) {
filesChanged.add(vf)
protected fun addFileChanged(path: Path) {
filesChanged.add(path)
}
protected fun getTemplate(name: String) = project.getTemplate(name, model.templateProperties)
@ -49,7 +49,7 @@ sealed class ProjectCreator(
indicator.text2 = "Reformatting files"
invokeAndWait {
for (file in filesChanged) {
val psi = file.toPsiFile(project) ?: continue
val psi = file.toFile().toPsiFile(project) ?: continue
ReformatCodeProcessor(psi, false).run()
}
}
@ -69,13 +69,14 @@ sealed class GradleProjectCreator(
runWriteActionAndWait {
VfsUtil.createDirectoryIfMissing(root, "src/main/${model.languageType.sourceSetDirName}")
VfsUtil.createDirectoryIfMissing(root, "src/main/resources")
filesChanged += root.writeChild(model.languageType.pluginMainClassFile(this))
filesChanged += root.writeChild("src/main/resources/META-INF/services/net.mamoe.mirai.console.plugin.jvm.JvmPlugin", model.mainClassQualifiedName)
filesChanged += root.writeChild("gradle.properties", getTemplate(FT.GradleProperties))
addFileChanged(root.writeChild(model.languageType.pluginMainClassFile(this)))
addFileChanged(root.writeChild("src/main/resources/META-INF/services/net.mamoe.mirai.console.plugin.jvm.JvmPlugin", model.mainClassQualifiedName))
addFileChanged(root.writeChild("gradle.properties", getTemplate(FT.GradleProperties)))
}
}
}
class GradleKotlinProjectCreator(
module: Module, root: VirtualFile, model: MiraiProjectModel,
) : GradleProjectCreator(
@ -84,8 +85,8 @@ class GradleKotlinProjectCreator(
override fun createProject(module: Module, root: VirtualFile, model: MiraiProjectModel) {
super.createProject(module, root, model)
runWriteActionAndWait {
filesChanged += root.writeChild("build.gradle.kts", getTemplate(FT.BuildGradleKts))
filesChanged += root.writeChild("settings.gradle.kts", getTemplate(FT.SettingsGradleKts))
addFileChanged(root.writeChild("build.gradle.kts", getTemplate(FT.BuildGradleKts)))
addFileChanged(root.writeChild("settings.gradle.kts", getTemplate(FT.SettingsGradleKts)))
}
}
}
@ -98,8 +99,8 @@ class GradleGroovyProjectCreator(
override fun createProject(module: Module, root: VirtualFile, model: MiraiProjectModel) {
super.createProject(module, root, model)
runWriteActionAndWait {
filesChanged += root.writeChild("build.gradle", getTemplate(FT.BuildGradle))
filesChanged += root.writeChild("settings.gradle", getTemplate(FT.SettingsGradle))
addFileChanged(root.writeChild("build.gradle", getTemplate(FT.BuildGradle)))
addFileChanged(root.writeChild("settings.gradle", getTemplate(FT.SettingsGradle)))
}
}
}

View File

@ -18,7 +18,7 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.testFramework.writeChild
import com.intellij.util.io.writeChild
import net.mamoe.mirai.console.intellij.creator.steps.NamedFile
import org.intellij.lang.annotations.Language
import java.nio.file.Path
@ -81,7 +81,13 @@ fun Project.getTemplate(
): String = getTemplate(templateName, properties.toMap())
fun VirtualFile.writeChild(namedFile: NamedFile): VirtualFile = this.writeChild(namedFile.path, namedFile.content)
internal fun VirtualFile.writeChild(pluginMainClassFile: NamedFile): Path {
return writeChild(pluginMainClassFile.path, pluginMainClassFile.content)
}
internal fun VirtualFile.writeChild(path: String, content: String): Path {
return toNioPath().writeChild(path, content)
}
@Language("RegExp")
const val CLASS_NAME_PATTERN = "[a-zA-Z]+[0-9a-zA-Z_]*" // self written

View File

@ -16,8 +16,8 @@ import com.intellij.openapi.project.rootManager
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.testFramework.writeChild
import net.mamoe.mirai.console.intellij.creator.tasks.readChildText
import net.mamoe.mirai.console.intellij.creator.tasks.writeChild
import org.jetbrains.kotlin.idea.core.isAndroidModule
import org.jetbrains.kotlin.idea.inspections.KotlinUniversalQuickFix
import org.jetbrains.kotlin.idea.quickfix.KotlinCrossLanguageQuickFixAction