Skip to content

Commit 73c2a0e

Browse files
authored
Refactor ViewAsImageAction to use XDebuggerTreeBackendOnlyActionBase
1 parent 13b4f35 commit 73c2a0e

File tree

1 file changed

+3
-43
lines changed

1 file changed

+3
-43
lines changed

src/main/kotlin/com/github/srwi/pixellens/actions/ViewAsImageAction.kt

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,19 @@ import com.intellij.notification.Notifications
1010
import com.intellij.openapi.actionSystem.ActionUpdateThread
1111
import com.intellij.openapi.actionSystem.AnAction
1212
import com.intellij.openapi.actionSystem.AnActionEvent
13-
import com.intellij.openapi.actionSystem.DataContext
1413
import com.intellij.openapi.progress.ProgressIndicator
1514
import com.intellij.openapi.progress.ProgressManager
1615
import com.intellij.openapi.progress.Task
1716
import com.intellij.openapi.project.Project
17+
import com.intellij.xdebugger.impl.ui.tree.actions.XDebuggerTreeBackendOnlyActionBase
1818
import com.jetbrains.python.debugger.PyDebugValue
1919
import com.jetbrains.python.debugger.PyFrameAccessor
20-
import java.lang.reflect.Modifier
2120
import javax.swing.SwingUtilities
2221

2322
class ViewAsImageAction : AnAction() {
2423
override fun actionPerformed(e: AnActionEvent) {
2524
val project = e.project ?: return
26-
val value = getSelectedPyDebugValue(e.dataContext) ?: return
25+
val value = XDebuggerTreeBackendOnlyActionBase.getSelectedValue(e.dataContext) as PyDebugValue? ?: return
2726

2827
if (!checkPythonCompatibility(value.frameAccessor, project)) {
2928
return
@@ -84,7 +83,7 @@ class ViewAsImageAction : AnAction() {
8483
override fun update(e: AnActionEvent) {
8584
super.update(e)
8685
try {
87-
val value = getSelectedPyDebugValue(e.dataContext) ?: throw IllegalStateException("No selected debug value")
86+
val value = XDebuggerTreeBackendOnlyActionBase.getSelectedValue(e.dataContext) as PyDebugValue
8887
val imageProvider = ImageProviderFactory.getImageProvider(value.typeQualifier as String)
8988
e.presentation.isVisible = imageProvider.typeSupported(value)
9089
e.presentation.isEnabled = imageProvider.shapeSupported(value)
@@ -100,10 +99,6 @@ class ViewAsImageAction : AnAction() {
10099
return if (value.parent == null) value.name else value.evaluationExpression
101100
}
102101

103-
private fun getSelectedPyDebugValue(dataContext: DataContext): PyDebugValue? {
104-
return XDebuggerSelectionCompat.getSelectedValue(dataContext) as? PyDebugValue
105-
}
106-
107102
private fun checkPythonCompatibility(frameAccessor: PyFrameAccessor, project: Project): Boolean {
108103
val pythonVersion = Python.getInterpreterVersion(frameAccessor)
109104

@@ -145,38 +140,3 @@ class ViewAsImageAction : AnAction() {
145140
return isJupyter
146141
}
147142
}
148-
149-
private object XDebuggerSelectionCompat {
150-
private val candidateClassNames = listOf(
151-
"com.intellij.xdebugger.impl.ui.tree.actions.XDebuggerTreeBackendOnlyActionBase",
152-
"com.intellij.xdebugger.impl.ui.tree.actions.XDebuggerTreeActionBase",
153-
)
154-
155-
fun getSelectedValue(dataContext: DataContext): Any? {
156-
for (className in candidateClassNames) {
157-
val value = runCatching { invokeSelectionMethod(className, dataContext) }.getOrNull()
158-
if (value != null) {
159-
return value
160-
}
161-
}
162-
return null
163-
}
164-
165-
private fun invokeSelectionMethod(className: String, dataContext: DataContext): Any? {
166-
val actionClass = Class.forName(className, false, XDebuggerSelectionCompat::class.java.classLoader)
167-
val staticMethod = actionClass.methods.firstOrNull {
168-
it.name == "getSelectedValue" && it.parameterCount == 1 && Modifier.isStatic(it.modifiers)
169-
}
170-
if (staticMethod != null) {
171-
return staticMethod.invoke(null, dataContext)
172-
}
173-
174-
val companionField = actionClass.getDeclaredField("Companion")
175-
val companion = companionField.get(null)
176-
val companionMethod = companion.javaClass.methods.firstOrNull {
177-
it.name == "getSelectedValue" && it.parameterCount == 1
178-
} ?: return null
179-
180-
return companionMethod.invoke(companion, dataContext)
181-
}
182-
}

0 commit comments

Comments
 (0)