Skip to content

Commit fb43d6b

Browse files
authored
fix: variable reference presentations differ from referenced variables (#859)
* wip: LiveVariableNode super class * fix: variable reference presentations differ from referenced variables
1 parent 39e78e0 commit fb43d6b

File tree

7 files changed

+106
-94
lines changed

7 files changed

+106
-94
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Source++, the open-source live coding platform.
3+
* Copyright (C) 2022 CodeBrig, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package spp.jetbrains.marker.js.presentation
18+
19+
import com.intellij.icons.AllIcons
20+
import com.intellij.ide.projectView.PresentationData
21+
import com.intellij.ui.SimpleTextAttributes
22+
import com.intellij.ui.treeStructure.SimpleNode
23+
import com.intellij.xdebugger.impl.ui.XDebuggerUIConstants
24+
import spp.jetbrains.marker.presentation.LiveVariableNode
25+
import spp.protocol.instrument.variable.LiveVariable
26+
import spp.protocol.instrument.variable.LiveVariableScope
27+
28+
/**
29+
* todo: description.
30+
*
31+
* @since 0.7.0
32+
* @author [Brandon Fergerson](mailto:bfergerson@apache.org)
33+
*/
34+
@Suppress("MagicNumber")
35+
class JavascriptVariableNode(
36+
variable: LiveVariable,
37+
nodeMap: MutableMap<String, Array<SimpleNode>>
38+
) : LiveVariableNode(variable, nodeMap) {
39+
40+
override fun createVariableNode(
41+
variable: LiveVariable,
42+
nodeMap: MutableMap<String, Array<SimpleNode>>
43+
): SimpleNode {
44+
return JavascriptVariableNode(variable, nodeMap)
45+
}
46+
47+
override fun update(presentation: PresentationData) {
48+
if (variable.scope == LiveVariableScope.GENERATED_METHOD) {
49+
presentation.addText(variable.name + " = ", SimpleTextAttributes.GRAYED_ATTRIBUTES)
50+
presentation.setIcon(AllIcons.Nodes.Method)
51+
} else {
52+
presentation.addText(variable.name + " = ", XDebuggerUIConstants.VALUE_NAME_ATTRIBUTES)
53+
}
54+
55+
if (childCount > 0) {
56+
presentation.setIcon(AllIcons.Nodes.Variable)
57+
presentation.addText("todo", SimpleTextAttributes.REGULAR_ATTRIBUTES)
58+
} else {
59+
presentation.setIcon(AllIcons.Nodes.Field)
60+
presentation.addText(variable.value.toString(), SimpleTextAttributes.REGULAR_ATTRIBUTES)
61+
}
62+
}
63+
}

marker/js-marker/src/main/kotlin/spp/jetbrains/marker/js/presentation/JavascriptVariableRootNode.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class JavascriptVariableRootNode(
3838
private val scheme = DebuggerUIUtil.getColorScheme(null)
3939

4040
override fun getChildren(): Array<SimpleNode> {
41-
return variables.map { JavascriptVariableSimpleNode(it, mutableMapOf()) }.toTypedArray()
41+
return variables.map { JavascriptVariableNode(it, mutableMapOf()) }.toTypedArray()
4242
}
4343

4444
override fun update(presentation: PresentationData) {
Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ import com.intellij.openapi.editor.DefaultLanguageHighlighterColors.NUMBER
2222
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors.STRING
2323
import com.intellij.ui.SimpleTextAttributes.*
2424
import com.intellij.ui.treeStructure.SimpleNode
25-
import com.intellij.xdebugger.impl.ui.DebuggerUIUtil
2625
import com.intellij.xdebugger.impl.ui.XDebuggerUIConstants
2726
import io.vertx.core.json.JsonArray
2827
import io.vertx.core.json.JsonObject
29-
import org.apache.commons.lang3.EnumUtils
30-
import spp.jetbrains.marker.ErrorVariableSimpleNode
28+
import spp.jetbrains.marker.presentation.LiveVariableNode
3129
import spp.protocol.instrument.variable.LiveVariable
3230
import spp.protocol.instrument.variable.LiveVariableScope
3331

@@ -38,10 +36,10 @@ import spp.protocol.instrument.variable.LiveVariableScope
3836
* @author [Brandon Fergerson](mailto:bfergerson@apache.org)
3937
*/
4038
@Suppress("MagicNumber")
41-
class JVMVariableSimpleNode(
42-
val variable: LiveVariable,
43-
private val nodeMap: MutableMap<String, Array<SimpleNode>>
44-
) : SimpleNode() {
39+
class JVMVariableNode(
40+
variable: LiveVariable,
41+
nodeMap: MutableMap<String, Array<SimpleNode>>
42+
) : LiveVariableNode(variable, nodeMap) {
4543

4644
private val primitives = setOf(
4745
"java.lang.String",
@@ -62,50 +60,12 @@ class JVMVariableSimpleNode(
6260
"java.lang.Float",
6361
"java.lang.Double"
6462
)
65-
private val scheme = DebuggerUIUtil.getColorScheme(null)
6663

67-
override fun getChildren(): Array<SimpleNode> {
68-
if (variable.value == null && variable.liveIdentity != null) {
69-
//found reference, use children of referenced node
70-
return nodeMap[variable.liveIdentity!!] ?: arrayOf()
71-
}
72-
73-
val children = if (variable.value is JsonArray) {
74-
(variable.value as JsonArray).map { JsonObject.mapFrom(it) }.map {
75-
if (it.getString("@skip") != null) {
76-
ErrorVariableSimpleNode(JsonObject.mapFrom(it).map)
77-
} else {
78-
JVMVariableSimpleNode(toLiveVariable(it), nodeMap)
79-
}
80-
}.toList().toTypedArray()
81-
} else if (variable.value is LiveVariable) {
82-
arrayOf(JVMVariableSimpleNode(variable.value as LiveVariable, nodeMap) as SimpleNode)
83-
} else {
84-
emptyArray()
85-
}
86-
87-
//add children to nodeMap for reference lookup
88-
if (variable.liveIdentity != null) {
89-
nodeMap[variable.liveIdentity!!] = children
90-
}
91-
return children
92-
}
93-
94-
private fun toLiveVariable(it: JsonObject): LiveVariable {
95-
var varValue = it.getValue("value")
96-
if (varValue is JsonArray && varValue.size() == 1 &&
97-
(varValue.first() as JsonObject).containsKey("liveClazz")
98-
) {
99-
varValue = toLiveVariable(varValue.first() as JsonObject)
100-
}
101-
return LiveVariable(
102-
name = it.getString("name"),
103-
value = varValue,
104-
lineNumber = it.getInteger("lineNumber") ?: -1,
105-
scope = EnumUtils.getEnum(LiveVariableScope::class.java, it.getString("scope")),
106-
liveClazz = it.getString("liveClazz"),
107-
liveIdentity = it.getString("liveIdentity")
108-
)
64+
override fun createVariableNode(
65+
variable: LiveVariable,
66+
nodeMap: MutableMap<String, Array<SimpleNode>>
67+
): SimpleNode {
68+
return JVMVariableNode(variable, nodeMap)
10969
}
11070

11171
override fun update(presentation: PresentationData) {
@@ -188,6 +148,4 @@ class JVMVariableSimpleNode(
188148
}
189149
}
190150
}
191-
192-
override fun getEqualityObjects(): Array<Any> = arrayOf(variable)
193151
}
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import com.intellij.ide.projectView.PresentationData
2020
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors
2121
import com.intellij.ui.SimpleTextAttributes
2222
import com.intellij.ui.treeStructure.SimpleNode
23-
import com.intellij.xdebugger.impl.ui.DebuggerUIUtil
2423
import com.intellij.xdebugger.impl.ui.XDebuggerUIConstants
2524
import io.vertx.core.json.JsonObject
25+
import spp.jetbrains.marker.presentation.LiveVariableNode
2626
import spp.protocol.instrument.variable.LiveVariable
2727

2828
/**
@@ -32,9 +32,16 @@ import spp.protocol.instrument.variable.LiveVariable
3232
* @author [Brandon Fergerson](mailto:bfergerson@apache.org)
3333
*/
3434
@Suppress("MagicNumber")
35-
class PythonVariableSimpleNode(private val variable: LiveVariable) : SimpleNode() {
35+
class PythonVariableNode(
36+
variable: LiveVariable
37+
) : LiveVariableNode(variable, mutableMapOf()) {
3638

37-
private val scheme = DebuggerUIUtil.getColorScheme(null)
39+
override fun createVariableNode(
40+
variable: LiveVariable,
41+
nodeMap: MutableMap<String, Array<SimpleNode>>
42+
): SimpleNode {
43+
return PythonVariableNode(variable)
44+
}
3845

3946
override fun getChildren(): Array<SimpleNode> {
4047
if (variable.liveClazz == "<class 'dict'>") {
@@ -44,7 +51,7 @@ class PythonVariableSimpleNode(private val variable: LiveVariable) : SimpleNode(
4451
)
4552
val children = mutableListOf<SimpleNode>()
4653
dict.map.forEach {
47-
children.add(PythonVariableSimpleNode(LiveVariable("'" + it.key + "'", it.value)))
54+
children.add(PythonVariableNode(LiveVariable("'" + it.key + "'", it.value)))
4855
}
4956
return children.toTypedArray()
5057
}
@@ -74,6 +81,7 @@ class PythonVariableSimpleNode(private val variable: LiveVariable) : SimpleNode(
7481
)
7582
)
7683
}
84+
7785
variable.liveClazz == "<class 'str'>" -> {
7886
presentation.addText(
7987
"\"" + variable.value + "\"",
@@ -82,6 +90,7 @@ class PythonVariableSimpleNode(private val variable: LiveVariable) : SimpleNode(
8290
)
8391
)
8492
}
93+
8594
else -> {
8695
presentation.addText(
8796
variable.value.toString(),
@@ -92,6 +101,4 @@ class PythonVariableSimpleNode(private val variable: LiveVariable) : SimpleNode(
92101
}
93102
}
94103
}
95-
96-
override fun getEqualityObjects(): Array<Any> = arrayOf(variable)
97104
}

marker/py-marker/src/main/kotlin/spp/jetbrains/marker/py/presentation/PythonVariableRootNode.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class PythonVariableRootNode(val variables: List<LiveVariable>, val scope: LiveV
3535
private val scheme = DebuggerUIUtil.getColorScheme(null)
3636

3737
override fun getChildren(): Array<SimpleNode> {
38-
return variables.map { PythonVariableSimpleNode(it) }.toTypedArray()
38+
return variables.map { PythonVariableNode(it) }.toTypedArray()
3939
}
4040

4141
override fun update(presentation: PresentationData) {
Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,10 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package spp.jetbrains.marker.js.presentation
17+
package spp.jetbrains.marker.presentation
1818

19-
import com.intellij.icons.AllIcons
20-
import com.intellij.ide.projectView.PresentationData
21-
import com.intellij.ui.SimpleTextAttributes
2219
import com.intellij.ui.treeStructure.SimpleNode
2320
import com.intellij.xdebugger.impl.ui.DebuggerUIUtil
24-
import com.intellij.xdebugger.impl.ui.XDebuggerUIConstants
2521
import io.vertx.core.json.JsonArray
2622
import io.vertx.core.json.JsonObject
2723
import org.apache.commons.lang3.EnumUtils
@@ -35,17 +31,20 @@ import spp.protocol.instrument.variable.LiveVariableScope
3531
* @since 0.7.0
3632
* @author [Brandon Fergerson](mailto:bfergerson@apache.org)
3733
*/
38-
@Suppress("MagicNumber")
39-
class JavascriptVariableSimpleNode(
40-
private val variable: LiveVariable,
34+
abstract class LiveVariableNode(
35+
val variable: LiveVariable,
4136
private val nodeMap: MutableMap<String, Array<SimpleNode>>
4237
) : SimpleNode() {
4338

44-
private val scheme = DebuggerUIUtil.getColorScheme(null)
39+
val scheme = DebuggerUIUtil.getColorScheme(null)
40+
41+
abstract fun createVariableNode(
42+
variable: LiveVariable,
43+
nodeMap: MutableMap<String, Array<SimpleNode>>
44+
): SimpleNode
4545

46-
//todo: LiveSimpleNode super class
4746
override fun getChildren(): Array<SimpleNode> {
48-
if (variable.value == null && variable.liveIdentity != null) {
47+
if (variable.liveIdentity != null && nodeMap.containsKey(variable.liveIdentity)) {
4948
//found reference, use children of referenced node
5049
return nodeMap[variable.liveIdentity!!] ?: arrayOf()
5150
}
@@ -55,17 +54,17 @@ class JavascriptVariableSimpleNode(
5554
if (it.getString("@skip") != null) {
5655
ErrorVariableSimpleNode(JsonObject.mapFrom(it).map)
5756
} else {
58-
JavascriptVariableSimpleNode(toLiveVariable(it), nodeMap)
57+
createVariableNode(toLiveVariable(it), nodeMap)
5958
}
6059
}.toList().toTypedArray()
6160
} else if (variable.value is LiveVariable) {
62-
arrayOf(JavascriptVariableSimpleNode(variable.value as LiveVariable, nodeMap) as SimpleNode)
61+
arrayOf(createVariableNode(variable.value as LiveVariable, nodeMap))
6362
} else {
6463
emptyArray()
6564
}
6665

6766
//add children to nodeMap for reference lookup
68-
if (variable.liveIdentity != null) {
67+
if (variable.liveIdentity != null && children.isNotEmpty()) {
6968
nodeMap[variable.liveIdentity!!] = children
7069
}
7170
return children
@@ -88,22 +87,6 @@ class JavascriptVariableSimpleNode(
8887
)
8988
}
9089

91-
override fun update(presentation: PresentationData) {
92-
if (variable.scope == LiveVariableScope.GENERATED_METHOD) {
93-
presentation.addText(variable.name + " = ", SimpleTextAttributes.GRAYED_ATTRIBUTES)
94-
presentation.setIcon(AllIcons.Nodes.Method)
95-
} else {
96-
presentation.addText(variable.name + " = ", XDebuggerUIConstants.VALUE_NAME_ATTRIBUTES)
97-
}
98-
99-
if (childCount > 0) {
100-
presentation.setIcon(AllIcons.Nodes.Variable)
101-
presentation.addText("todo", SimpleTextAttributes.REGULAR_ATTRIBUTES)
102-
} else {
103-
presentation.setIcon(AllIcons.Nodes.Field)
104-
presentation.addText(variable.value.toString(), SimpleTextAttributes.REGULAR_ATTRIBUTES)
105-
}
106-
}
107-
10890
override fun getEqualityObjects(): Array<Any> = arrayOf(variable)
91+
override fun toString(): String = variable.name
10992
}

plugin/src/main/kotlin/spp/jetbrains/sourcemarker/service/instrument/breakpoint/tree/VariableRootSimpleNode.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package spp.jetbrains.sourcemarker.service.instrument.breakpoint.tree
1919
import com.intellij.ui.treeStructure.SimpleNode
2020
import com.intellij.util.containers.hash.LinkedHashMap
2121
import spp.jetbrains.marker.js.presentation.JavascriptVariableRootNode
22-
import spp.jetbrains.marker.jvm.presentation.JVMVariableSimpleNode
22+
import spp.jetbrains.marker.jvm.presentation.JVMVariableNode
2323
import spp.jetbrains.marker.py.presentation.PythonVariableRootNode
2424
import spp.jetbrains.sourcemarker.service.instrument.breakpoint.StackFrameManager
2525
import spp.protocol.artifact.ArtifactLanguage
@@ -76,10 +76,11 @@ class VariableRootSimpleNode : SimpleNode() {
7676
}
7777

7878
else -> {
79-
val simpleNodeMap: MutableMap<String, JVMVariableSimpleNode> = LinkedHashMap()
79+
val simpleNodeMap: MutableMap<String, JVMVariableNode> = LinkedHashMap()
80+
val nodeReferenceMap = mutableMapOf<String, Array<SimpleNode>>()
8081
vars.forEach {
8182
if (it.name.isNotEmpty()) {
82-
simpleNodeMap[it.name] = JVMVariableSimpleNode(it, mutableMapOf())
83+
simpleNodeMap[it.name] = JVMVariableNode(it, nodeReferenceMap)
8384
}
8485
}
8586
simpleNodeMap.values.sortedWith { p0, p1 ->

0 commit comments

Comments
 (0)