|
| 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.platform.mcp.actions |
| 22 | + |
| 23 | +import com.demonwav.mcdev.platform.mcp.McpModuleType |
| 24 | +import com.demonwav.mcdev.platform.mcp.actions.SrgActionBase.Companion.showBalloon |
| 25 | +import com.demonwav.mcdev.platform.mcp.actions.SrgActionBase.Companion.showSuccessBalloon |
| 26 | +import com.demonwav.mcdev.platform.mixin.handlers.ShadowHandler |
| 27 | +import com.demonwav.mcdev.platform.neoforge.NeoForgeModuleType |
| 28 | +import com.demonwav.mcdev.util.MinecraftVersions |
| 29 | +import com.demonwav.mcdev.util.SemanticVersion |
| 30 | +import com.demonwav.mcdev.util.descriptor |
| 31 | +import com.demonwav.mcdev.util.getDataFromActionEvent |
| 32 | +import com.intellij.openapi.actionSystem.ActionUpdateThread |
| 33 | +import com.intellij.openapi.actionSystem.AnAction |
| 34 | +import com.intellij.openapi.actionSystem.AnActionEvent |
| 35 | +import com.intellij.openapi.editor.Editor |
| 36 | +import com.intellij.psi.PsiClass |
| 37 | +import com.intellij.psi.PsiElement |
| 38 | +import com.intellij.psi.PsiField |
| 39 | +import com.intellij.psi.PsiMember |
| 40 | +import com.intellij.psi.PsiMethod |
| 41 | +import com.intellij.psi.PsiReference |
| 42 | +import java.awt.Toolkit |
| 43 | +import java.awt.datatransfer.StringSelection |
| 44 | + |
| 45 | +class CopyNeoForgeAtAction : AnAction() { |
| 46 | + |
| 47 | + override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT |
| 48 | + |
| 49 | + override fun update(e: AnActionEvent) { |
| 50 | + e.presentation.isEnabledAndVisible = isAvailable(e) |
| 51 | + } |
| 52 | + |
| 53 | + private fun isAvailable(e: AnActionEvent): Boolean { |
| 54 | + val data = getDataFromActionEvent(e) ?: return false |
| 55 | + if (!data.instance.isOfType(NeoForgeModuleType)) { |
| 56 | + return false |
| 57 | + } |
| 58 | + |
| 59 | + val mcpModule = data.instance.getModuleOfType(McpModuleType) ?: return false |
| 60 | + val mcVersion = mcpModule.getSettings().minecraftVersion?.let(SemanticVersion::tryParse) ?: return false |
| 61 | + return mcVersion >= MinecraftVersions.MC1_20_2 |
| 62 | + } |
| 63 | + |
| 64 | + override fun actionPerformed(e: AnActionEvent) { |
| 65 | + val data = getDataFromActionEvent(e) ?: return |
| 66 | + |
| 67 | + var parent = data.element.parent |
| 68 | + if (parent is PsiMember) { |
| 69 | + val shadowTarget = ShadowHandler.getInstance()?.findFirstShadowTargetForReference(parent)?.element |
| 70 | + if (shadowTarget != null) { |
| 71 | + parent = shadowTarget |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if (parent is PsiReference) { |
| 76 | + parent = parent.resolve() ?: return showBalloon("Not a valid element", e) |
| 77 | + } |
| 78 | + |
| 79 | + when (parent) { |
| 80 | + is PsiClass -> { |
| 81 | + val fqn = parent.qualifiedName ?: return showBalloon("Could not find class FQN", e) |
| 82 | + copyToClipboard(data.editor, data.element, fqn) |
| 83 | + } |
| 84 | + is PsiField -> { |
| 85 | + val classFqn = parent.containingClass?.qualifiedName |
| 86 | + ?: return showBalloon("Could not find class FQN", e) |
| 87 | + copyToClipboard(data.editor, data.element, "$classFqn ${parent.name}") |
| 88 | + } |
| 89 | + is PsiMethod -> { |
| 90 | + val classFqn = parent.containingClass?.qualifiedName |
| 91 | + ?: return showBalloon("Could not find class FQN", e) |
| 92 | + val methodDescriptor = parent.descriptor |
| 93 | + ?: return showBalloon("Could not compute method descriptor", e) |
| 94 | + copyToClipboard(data.editor, data.element, "$classFqn ${parent.name}$methodDescriptor") |
| 95 | + } |
| 96 | + else -> showBalloon("Not a valid element", e) |
| 97 | + } |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + private fun copyToClipboard(editor: Editor, element: PsiElement, text: String) { |
| 102 | + val stringSelection = StringSelection(text) |
| 103 | + val clpbrd = Toolkit.getDefaultToolkit().systemClipboard |
| 104 | + clpbrd.setContents(stringSelection, null) |
| 105 | + showSuccessBalloon(editor, element, "Copied $text") |
| 106 | + } |
| 107 | +} |
0 commit comments