-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
64 changed files
with
1,976 additions
and
408 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Minecraft Development for IntelliJ | ||
* | ||
* https://mcdev.io/ | ||
* | ||
* Copyright (C) 2024 minecraft-dev | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published | ||
* by the Free Software Foundation, version 3.0 only. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.demonwav.mcdev.insight.generation | ||
|
||
import com.demonwav.mcdev.util.addImplements | ||
import com.intellij.core.CoreJavaCodeStyleManager | ||
import com.intellij.lang.LanguageExtension | ||
import com.intellij.lang.LanguageExtensionPoint | ||
import com.intellij.openapi.editor.RangeMarker | ||
import com.intellij.openapi.extensions.ExtensionPointName | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiClass | ||
import com.intellij.psi.PsiDocumentManager | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiFile | ||
import com.intellij.psi.codeStyle.CodeStyleManager | ||
import com.intellij.psi.util.parentOfType | ||
import org.jetbrains.kotlin.idea.core.ShortenReferences | ||
import org.jetbrains.kotlin.psi.KtClassOrObject | ||
import org.jetbrains.kotlin.psi.KtFile | ||
import org.jetbrains.kotlin.psi.KtPsiFactory | ||
|
||
interface EventGenHelper { | ||
|
||
fun addImplements(context: PsiElement, fqn: String) | ||
|
||
fun reformatAndShortenRefs(file: PsiFile, startOffset: Int, endOffset: Int) | ||
|
||
companion object { | ||
|
||
val EP_NAME = ExtensionPointName.create<LanguageExtensionPoint<EventGenHelper>>( | ||
"com.demonwav.minecraft-dev.eventGenHelper" | ||
) | ||
val COLLECTOR = LanguageExtension<EventGenHelper>(EP_NAME, JvmEventGenHelper()) | ||
} | ||
} | ||
|
||
open class JvmEventGenHelper : EventGenHelper { | ||
|
||
override fun addImplements(context: PsiElement, fqn: String) {} | ||
|
||
override fun reformatAndShortenRefs(file: PsiFile, startOffset: Int, endOffset: Int) { | ||
val project = file.project | ||
|
||
val marker = doReformat(project, file, startOffset, endOffset) ?: return | ||
|
||
CoreJavaCodeStyleManager.getInstance(project).shortenClassReferences(file, marker.startOffset, marker.endOffset) | ||
} | ||
|
||
companion object { | ||
|
||
fun doReformat(project: Project, file: PsiFile, startOffset: Int, endOffset: Int): RangeMarker? { | ||
val documentManager = PsiDocumentManager.getInstance(project) | ||
val document = documentManager.getDocument(file) ?: return null | ||
|
||
val marker = document.createRangeMarker(startOffset, endOffset).apply { | ||
isGreedyToLeft = true | ||
isGreedyToRight = true | ||
} | ||
|
||
CodeStyleManager.getInstance(project).reformatText(file, startOffset, endOffset) | ||
documentManager.commitDocument(document) | ||
|
||
return marker | ||
} | ||
} | ||
} | ||
|
||
class JavaEventGenHelper : JvmEventGenHelper() { | ||
|
||
override fun addImplements(context: PsiElement, fqn: String) { | ||
val psiClass = context.parentOfType<PsiClass>(true) ?: return | ||
psiClass.addImplements(fqn) | ||
} | ||
} | ||
|
||
class KotlinEventGenHelper : EventGenHelper { | ||
|
||
private fun hasSuperType(ktClass: KtClassOrObject, fqn: String): Boolean { | ||
val names = setOf(fqn, fqn.substringAfterLast('.')) | ||
return ktClass.superTypeListEntries.any { it.text in names } | ||
} | ||
|
||
override fun addImplements(context: PsiElement, fqn: String) { | ||
val ktClass = context.parentOfType<KtClassOrObject>(true) ?: return | ||
if (hasSuperType(ktClass, fqn)) { | ||
return | ||
} | ||
|
||
val factory = KtPsiFactory.contextual(context) | ||
val entry = factory.createSuperTypeEntry(fqn) | ||
val insertedEntry = ktClass.addSuperTypeListEntry(entry) | ||
ShortenReferences.DEFAULT.process(insertedEntry) | ||
} | ||
|
||
override fun reformatAndShortenRefs(file: PsiFile, startOffset: Int, endOffset: Int) { | ||
file as? KtFile ?: return | ||
val project = file.project | ||
|
||
val marker = JvmEventGenHelper.doReformat(project, file, startOffset, endOffset) ?: return | ||
|
||
ShortenReferences.DEFAULT.process(file, marker.startOffset, marker.endOffset) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/kotlin/insight/generation/EventListenerGenerationSupport.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Minecraft Development for IntelliJ | ||
* | ||
* https://mcdev.io/ | ||
* | ||
* Copyright (C) 2024 minecraft-dev | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published | ||
* by the Free Software Foundation, version 3.0 only. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.demonwav.mcdev.insight.generation | ||
|
||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.psi.PsiClass | ||
import com.intellij.psi.PsiElement | ||
|
||
interface EventListenerGenerationSupport { | ||
|
||
fun canGenerate(context: PsiElement, editor: Editor): Boolean | ||
|
||
fun generateEventListener( | ||
context: PsiElement, | ||
listenerName: String, | ||
eventClass: PsiClass, | ||
data: GenerationData?, | ||
editor: Editor | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.