|
| 1 | +/* |
| 2 | + * Minecraft Development for IntelliJ |
| 3 | + * |
| 4 | + * https://mcdev.io/ |
| 5 | + * |
| 6 | + * Copyright (C) 2024 minecraft-dev |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Lesser General Public License as published |
| 10 | + * by the Free Software Foundation, version 3.0 only. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.demonwav.mcdev.insight.generation |
| 22 | + |
| 23 | +import com.demonwav.mcdev.util.addImplements |
| 24 | +import com.intellij.core.CoreJavaCodeStyleManager |
| 25 | +import com.intellij.lang.LanguageExtension |
| 26 | +import com.intellij.lang.LanguageExtensionPoint |
| 27 | +import com.intellij.openapi.editor.RangeMarker |
| 28 | +import com.intellij.openapi.extensions.ExtensionPointName |
| 29 | +import com.intellij.openapi.project.Project |
| 30 | +import com.intellij.psi.PsiClass |
| 31 | +import com.intellij.psi.PsiDocumentManager |
| 32 | +import com.intellij.psi.PsiElement |
| 33 | +import com.intellij.psi.PsiFile |
| 34 | +import com.intellij.psi.codeStyle.CodeStyleManager |
| 35 | +import com.intellij.psi.util.parentOfType |
| 36 | +import org.jetbrains.kotlin.idea.core.ShortenReferences |
| 37 | +import org.jetbrains.kotlin.psi.KtClassOrObject |
| 38 | +import org.jetbrains.kotlin.psi.KtFile |
| 39 | +import org.jetbrains.kotlin.psi.KtPsiFactory |
| 40 | + |
| 41 | +interface EventGenHelper { |
| 42 | + |
| 43 | + fun addImplements(context: PsiElement, fqn: String) |
| 44 | + |
| 45 | + fun reformatAndShortenRefs(file: PsiFile, startOffset: Int, endOffset: Int) |
| 46 | + |
| 47 | + companion object { |
| 48 | + |
| 49 | + val EP_NAME = ExtensionPointName.create<LanguageExtensionPoint<EventGenHelper>>( |
| 50 | + "com.demonwav.minecraft-dev.eventGenHelper" |
| 51 | + ) |
| 52 | + val COLLECTOR = LanguageExtension<EventGenHelper>(EP_NAME, JvmEventGenHelper()) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +open class JvmEventGenHelper : EventGenHelper { |
| 57 | + |
| 58 | + override fun addImplements(context: PsiElement, fqn: String) {} |
| 59 | + |
| 60 | + override fun reformatAndShortenRefs(file: PsiFile, startOffset: Int, endOffset: Int) { |
| 61 | + val project = file.project |
| 62 | + |
| 63 | + val marker = doReformat(project, file, startOffset, endOffset) ?: return |
| 64 | + |
| 65 | + CoreJavaCodeStyleManager.getInstance(project).shortenClassReferences(file, marker.startOffset, marker.endOffset) |
| 66 | + } |
| 67 | + |
| 68 | + companion object { |
| 69 | + |
| 70 | + fun doReformat(project: Project, file: PsiFile, startOffset: Int, endOffset: Int): RangeMarker? { |
| 71 | + val documentManager = PsiDocumentManager.getInstance(project) |
| 72 | + val document = documentManager.getDocument(file) ?: return null |
| 73 | + |
| 74 | + val marker = document.createRangeMarker(startOffset, endOffset).apply { |
| 75 | + isGreedyToLeft = true |
| 76 | + isGreedyToRight = true |
| 77 | + } |
| 78 | + |
| 79 | + CodeStyleManager.getInstance(project).reformatText(file, startOffset, endOffset) |
| 80 | + documentManager.commitDocument(document) |
| 81 | + |
| 82 | + return marker |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +class JavaEventGenHelper : JvmEventGenHelper() { |
| 88 | + |
| 89 | + override fun addImplements(context: PsiElement, fqn: String) { |
| 90 | + val psiClass = context.parentOfType<PsiClass>(true) ?: return |
| 91 | + psiClass.addImplements(fqn) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +class KotlinEventGenHelper : EventGenHelper { |
| 96 | + |
| 97 | + private fun hasSuperType(ktClass: KtClassOrObject, fqn: String): Boolean { |
| 98 | + val names = setOf(fqn, fqn.substringAfterLast('.')) |
| 99 | + return ktClass.superTypeListEntries.any { it.text in names } |
| 100 | + } |
| 101 | + |
| 102 | + override fun addImplements(context: PsiElement, fqn: String) { |
| 103 | + val ktClass = context.parentOfType<KtClassOrObject>(true) ?: return |
| 104 | + if (hasSuperType(ktClass, fqn)) { |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + val factory = KtPsiFactory.contextual(context) |
| 109 | + val entry = factory.createSuperTypeEntry(fqn) |
| 110 | + val insertedEntry = ktClass.addSuperTypeListEntry(entry) |
| 111 | + ShortenReferences.DEFAULT.process(insertedEntry) |
| 112 | + } |
| 113 | + |
| 114 | + override fun reformatAndShortenRefs(file: PsiFile, startOffset: Int, endOffset: Int) { |
| 115 | + file as? KtFile ?: return |
| 116 | + val project = file.project |
| 117 | + |
| 118 | + val marker = JvmEventGenHelper.doReformat(project, file, startOffset, endOffset) ?: return |
| 119 | + |
| 120 | + ShortenReferences.DEFAULT.process(file, marker.startOffset, marker.endOffset) |
| 121 | + } |
| 122 | +} |
0 commit comments