Skip to content

Commit 5038fc5

Browse files
committed
Update SrgActionBase.kt
make work with neoforges runtime mappings (will most likely work in the future for mojangs non-obfuscated source post 1.21.11)
1 parent 1a1fc03 commit 5038fc5

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

src/main/kotlin/platform/mcp/actions/SrgActionBase.kt

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import com.demonwav.mcdev.platform.mcp.McpModuleType
2424
import com.demonwav.mcdev.platform.mcp.mappings.Mappings
2525
import com.demonwav.mcdev.platform.mixin.handlers.ShadowHandler
2626
import com.demonwav.mcdev.util.ActionData
27+
import com.demonwav.mcdev.util.descriptor
28+
import com.demonwav.mcdev.util.fullQualifiedName
2729
import com.demonwav.mcdev.util.getDataFromActionEvent
30+
import com.demonwav.mcdev.util.internalName
2831
import com.demonwav.mcdev.util.invokeLater
2932
import com.intellij.openapi.actionSystem.AnAction
3033
import com.intellij.openapi.actionSystem.AnActionEvent
@@ -33,14 +36,19 @@ import com.intellij.openapi.editor.VisualPosition
3336
import com.intellij.openapi.ui.popup.Balloon
3437
import com.intellij.openapi.ui.popup.JBPopupFactory
3538
import com.intellij.openapi.wm.WindowManager
39+
import com.intellij.psi.PsiClass
3640
import com.intellij.psi.PsiElement
41+
import com.intellij.psi.PsiField
3742
import com.intellij.psi.PsiIdentifier
3843
import com.intellij.psi.PsiMember
44+
import com.intellij.psi.PsiMethod
3945
import com.intellij.psi.PsiReference
4046
import com.intellij.psi.createSmartPointer
4147
import com.intellij.ui.LightColors
4248
import com.intellij.ui.awt.RelativePoint
4349
import java.awt.Point
50+
import java.awt.Toolkit
51+
import java.awt.datatransfer.StringSelection
4452
import javax.swing.JComponent
4553

4654
abstract class SrgActionBase : AnAction() {
@@ -72,11 +80,29 @@ abstract class SrgActionBase : AnAction() {
7280
withSrgTarget(parent, srgMap, e, data)
7381
}?.onError {
7482
showBalloon(it.message ?: "No MCP data available", e)
75-
} ?: showBalloon("No mappings found", e)
83+
} ?: withMojTarget(data, e)
7684
}
7785

7886
abstract fun withSrgTarget(parent: PsiElement, srgMap: Mappings, e: AnActionEvent, data: ActionData)
7987

88+
fun withMojTarget(data: ActionData, e: AnActionEvent){
89+
val editor = data.editor
90+
val element = data.element
91+
92+
if (element !is PsiIdentifier) {
93+
showBalloon("Invalid element", e)
94+
return
95+
}
96+
97+
val target = when (val parent = element.parent) {
98+
is PsiMember -> parent
99+
is PsiReference -> parent.resolve()
100+
else -> null
101+
} ?: return showBalloon("Invalid element", e)
102+
103+
doCopy(target, element, editor, e)
104+
}
105+
80106
companion object {
81107
fun showBalloon(message: String, e: AnActionEvent) {
82108
val balloon = JBPopupFactory.getInstance()
@@ -137,5 +163,44 @@ abstract class SrgActionBase : AnAction() {
137163
balloon.show(at, Balloon.Position.below)
138164
}
139165
}
166+
167+
fun doCopy(target: PsiElement, element: PsiElement, editor: Editor?, e: AnActionEvent?) {
168+
when (target) {
169+
is PsiClass -> {
170+
val text = "public ${target.fullQualifiedName}"
171+
copyToClipboard(editor, element, text)
172+
}
173+
is PsiField -> {
174+
val containing = target.containingClass?.fullQualifiedName
175+
?: return maybeShow("Could not get owner of field", e)
176+
val desc = target.type.descriptor
177+
val text = "public $containing ${target.name}"
178+
copyToClipboard(editor, element, text)
179+
}
180+
is PsiMethod -> {
181+
val containing = target.containingClass?.fullQualifiedName
182+
?: return maybeShow("Could not get owner of method", e)
183+
val desc = target.descriptor ?: return maybeShow("Could not get descriptor of method", e)
184+
val text = "public $containing ${target.internalName}$desc"
185+
copyToClipboard(editor, element, text)
186+
}
187+
else -> maybeShow("Invalid element", e)
188+
}
189+
}
190+
191+
private fun copyToClipboard(editor: Editor?, element: PsiElement, text: String) {
192+
val stringSelection = StringSelection(text)
193+
val clpbrd = Toolkit.getDefaultToolkit().systemClipboard
194+
clpbrd.setContents(stringSelection, null)
195+
if (editor != null) {
196+
showSuccessBalloon(editor, element, "Copied: \"$text\"")
197+
}
198+
}
199+
200+
private fun maybeShow(text: String, e: AnActionEvent?) {
201+
if (e != null) {
202+
showBalloon(text, e)
203+
}
204+
}
140205
}
141206
}

0 commit comments

Comments
 (0)