-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathCopyNeoForgeAtAction.kt
More file actions
98 lines (87 loc) · 3.85 KB
/
CopyNeoForgeAtAction.kt
File metadata and controls
98 lines (87 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* 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.platform.mcp.actions
import com.demonwav.mcdev.platform.mcp.actions.SrgActionBase.Companion.showBalloon
import com.demonwav.mcdev.platform.mcp.actions.SrgActionBase.Companion.showSuccessBalloon
import com.demonwav.mcdev.platform.mcp.at.usesSrgMemberNames
import com.demonwav.mcdev.platform.mixin.handlers.ShadowHandler
import com.demonwav.mcdev.util.descriptor
import com.demonwav.mcdev.util.getDataFromActionEvent
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiField
import com.intellij.psi.PsiMember
import com.intellij.psi.PsiMethod
import com.intellij.psi.PsiReference
import java.awt.Toolkit
import java.awt.datatransfer.StringSelection
class CopyNeoForgeAtAction : AnAction() {
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT
override fun update(e: AnActionEvent) {
e.presentation.isEnabledAndVisible = isAvailable(e)
}
private fun isAvailable(e: AnActionEvent): Boolean {
val data = getDataFromActionEvent(e) ?: return false
return data.instance.usesSrgMemberNames() == false
}
override fun actionPerformed(e: AnActionEvent) {
val data = getDataFromActionEvent(e) ?: return
var parent = data.element.parent
if (parent is PsiMember) {
val shadowTarget = ShadowHandler.getInstance()?.findFirstShadowTargetForReference(parent)?.element
if (shadowTarget != null) {
parent = shadowTarget
}
}
if (parent is PsiReference) {
parent = parent.resolve() ?: return showBalloon("Not a valid element", e)
}
when (parent) {
is PsiClass -> {
val fqn = parent.qualifiedName ?: return showBalloon("Could not find class FQN", e)
copyToClipboard(data.editor, data.element, fqn)
}
is PsiField -> {
val classFqn = parent.containingClass?.qualifiedName
?: return showBalloon("Could not find class FQN", e)
copyToClipboard(data.editor, data.element, "$classFqn ${parent.name}")
}
is PsiMethod -> {
val classFqn = parent.containingClass?.qualifiedName
?: return showBalloon("Could not find class FQN", e)
val methodDescriptor = parent.descriptor
?: return showBalloon("Could not compute method descriptor", e)
copyToClipboard(data.editor, data.element, "$classFqn ${parent.name}$methodDescriptor")
}
else -> showBalloon("Not a valid element", e)
}
return
}
private fun copyToClipboard(editor: Editor, element: PsiElement, text: String) {
val stringSelection = StringSelection(text)
val clpbrd = Toolkit.getDefaultToolkit().systemClipboard
clpbrd.setContents(stringSelection, null)
showSuccessBalloon(editor, element, "Copied $text")
}
}