|
| 1 | +/* |
| 2 | + * Minecraft Dev for IntelliJ |
| 3 | + * |
| 4 | + * https://minecraftdev.org |
| 5 | + * |
| 6 | + * Copyright (c) 2022 minecraft-dev |
| 7 | + * |
| 8 | + * MIT License |
| 9 | + */ |
| 10 | + |
| 11 | +package com.demonwav.mcdev.platform.mcp.actions |
| 12 | + |
| 13 | +import com.demonwav.mcdev.platform.mcp.srg.McpSrgMap |
| 14 | +import com.demonwav.mcdev.util.ActionData |
| 15 | +import com.intellij.openapi.actionSystem.AnActionEvent |
| 16 | +import com.intellij.openapi.editor.Editor |
| 17 | +import com.intellij.psi.PsiClass |
| 18 | +import com.intellij.psi.PsiElement |
| 19 | +import com.intellij.psi.PsiField |
| 20 | +import com.intellij.psi.PsiMethod |
| 21 | +import java.awt.Toolkit |
| 22 | +import java.awt.datatransfer.StringSelection |
| 23 | +import kotlinx.serialization.json.JsonObject |
| 24 | +import kotlinx.serialization.json.JsonPrimitive |
| 25 | + |
| 26 | +class CopyCoremodTargetAction : SrgActionBase() { |
| 27 | + override fun withSrgTarget(parent: PsiElement, srgMap: McpSrgMap, e: AnActionEvent, data: ActionData) { |
| 28 | + when (parent) { |
| 29 | + is PsiField -> { |
| 30 | + val containing = parent.containingClass ?: return showBalloon("No SRG name found", e) |
| 31 | + val classSrg = srgMap.getSrgClass(containing) ?: return showBalloon("No SRG name found", e) |
| 32 | + val srg = srgMap.getSrgField(parent) ?: return showBalloon("No SRG name found", e) |
| 33 | + copyToClipboard( |
| 34 | + data.editor, |
| 35 | + data.element, |
| 36 | + Pair("target", "FIELD"), |
| 37 | + Pair("class", classSrg), |
| 38 | + Pair("fieldName", srg.name) |
| 39 | + ) |
| 40 | + } |
| 41 | + is PsiMethod -> { |
| 42 | + val containing = parent.containingClass ?: return showBalloon("No SRG name found", e) |
| 43 | + val classSrg = srgMap.getSrgClass(containing) ?: return showBalloon("No SRG name found", e) |
| 44 | + val srg = srgMap.getSrgMethod(parent) ?: return showBalloon("No SRG name found", e) |
| 45 | + val srgDescriptor = srg.descriptor ?: return showBalloon("No SRG name found", e) |
| 46 | + copyToClipboard( |
| 47 | + data.editor, |
| 48 | + data.element, |
| 49 | + Pair("target", "METHOD"), |
| 50 | + Pair("class", classSrg), |
| 51 | + Pair("methodName", srg.name), |
| 52 | + Pair("methodDesc", srgDescriptor) |
| 53 | + ) |
| 54 | + } |
| 55 | + is PsiClass -> { |
| 56 | + val classSrg = srgMap.getSrgClass(parent) ?: return showBalloon("No SRG name found", e) |
| 57 | + copyToClipboard( |
| 58 | + data.editor, |
| 59 | + data.element, |
| 60 | + Pair("target", "CLASS"), |
| 61 | + Pair("name", classSrg), |
| 62 | + ) |
| 63 | + } |
| 64 | + else -> showBalloon("Not a valid element", e) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private fun copyToClipboard(editor: Editor, element: PsiElement, vararg keys: Pair<String, String>) { |
| 69 | + val text = JsonObject(keys.toMap().mapValues { JsonPrimitive(it.value) }).toString() |
| 70 | + val stringSelection = StringSelection(text) |
| 71 | + val clpbrd = Toolkit.getDefaultToolkit().systemClipboard |
| 72 | + clpbrd.setContents(stringSelection, null) |
| 73 | + showSuccessBalloon(editor, element, "Copied Coremod Target Reference") |
| 74 | + } |
| 75 | +} |
0 commit comments