Skip to content

Commit 476a883

Browse files
Get unchangedVariables from notebook to reflect after execution state properly
1 parent 033993b commit 476a883

File tree

3 files changed

+149
-2
lines changed

3 files changed

+149
-2
lines changed

src/main/kotlin/org/jetbrains/kotlinx/jupyter/repl/impl/InternalEvaluatorImpl.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ internal class InternalEvaluatorImpl(
5656
return variablesWatcher.findDeclarationAddress(variableName) ?: -1
5757
}
5858

59+
override fun getUnchangedVariables(): Set<String> {
60+
return variablesWatcher.getUnchangedVariables()
61+
}
62+
5963
override var writeCompiledClasses: Boolean
6064
get() = classWriter != null
6165
set(value) {

src/main/kotlin/org/jetbrains/kotlinx/jupyter/util.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ class VariablesUsagesPerCellWatcher<K : Any, V : Any> {
8383
*/
8484
private val variablesDeclarationInfo: MutableMap<V, K> = mutableMapOf()
8585

86+
private val unchangedVariables: MutableSet<V> = mutableSetOf()
87+
// private val unchangedVariables: MutableSet<V> = mutableSetOf()
88+
8689
fun addDeclaration(address: K, variableRef: V) {
8790
ensureStorageCreation(address)
8891

@@ -91,21 +94,35 @@ class VariablesUsagesPerCellWatcher<K : Any, V : Any> {
9194
val oldCellId = variablesDeclarationInfo[variableRef]
9295
if (oldCellId != address) {
9396
cellVariables[oldCellId]?.remove(variableRef)
97+
unchangedVariables.remove(variableRef)
9498
}
99+
} else {
100+
unchangedVariables.add(variableRef)
95101
}
96102
variablesDeclarationInfo[variableRef] = address
97103
cellVariables[address]?.add(variableRef)
98104
}
99105

100-
fun addUsage(address: K, variableRef: V) = cellVariables[address]?.add(variableRef)
106+
fun addUsage(address: K, variableRef: V) {
107+
cellVariables[address]?.add(variableRef)
108+
if (variablesDeclarationInfo[variableRef] != address) {
109+
unchangedVariables.remove(variableRef)
110+
}
111+
}
101112

102113
fun removeOldUsages(newAddress: K) {
103114
// remove known modifying usages in this cell
104115
cellVariables[newAddress]?.removeIf {
105-
variablesDeclarationInfo[it] != newAddress
116+
val predicate = variablesDeclarationInfo[it] != newAddress
117+
if (predicate) {
118+
unchangedVariables.add(it)
119+
}
120+
predicate
106121
}
107122
}
108123

124+
fun getUnchangedVariables(): Set<V> = unchangedVariables
125+
109126
fun findDeclarationAddress(variableRef: V) = variablesDeclarationInfo[variableRef]
110127

111128
fun ensureStorageCreation(address: K) = cellVariables.putIfAbsent(address, mutableSetOf())

src/test/kotlin/org/jetbrains/kotlinx/jupyter/test/repl/ReplTests.kt

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,93 @@ class ReplVarsTest : AbstractSingleReplTest() {
696696
fun testOutVarRendering() {
697697
eval("Out").resultValue.shouldNotBeNull()
698698
}
699+
700+
701+
@Test
702+
fun testSeparatePrivateDefsUsage() {
703+
eval(
704+
"""
705+
private val x = "abcd"
706+
private var f = 47
707+
""".trimIndent(),
708+
jupyterId = 1
709+
)
710+
val state = repl.notebook.cellVariables
711+
assertTrue(state[0]!!.contains("x"))
712+
713+
eval(
714+
"""
715+
val x = 341
716+
private var f = "abcd"
717+
""".trimIndent(),
718+
jupyterId = 2
719+
)
720+
assertTrue(state.isNotEmpty())
721+
assertTrue(state[0]!!.isEmpty())
722+
assertTrue(state[1]!!.contains("x"))
723+
724+
val setOfPrevCell = setOf<String>()
725+
val setOfNextCell = setOf("x", "f")
726+
assertEquals(state[0], setOfPrevCell)
727+
assertEquals(state[1], setOfNextCell)
728+
}
729+
730+
@Test
731+
fun testRecursiveVarsState() {
732+
eval(
733+
"""
734+
val l = mutableListOf<Any>()
735+
l.add(listOf(l))
736+
737+
val m = mapOf(1 to l)
738+
739+
val z = setOf(1, 2, 4)
740+
""".trimIndent(),
741+
jupyterId = 1
742+
)
743+
val state = repl.notebook.variablesState
744+
assertTrue(state.contains("l"))
745+
assertTrue(state.contains("m"))
746+
assertTrue(state.contains("z"))
747+
748+
assertEquals("ArrayList: recursive structure", state["l"]!!.stringValue)
749+
assertTrue(state["m"]!!.stringValue!!.contains(" recursive structure"))
750+
assertEquals("[1, 2, 4]", state["z"]!!.stringValue)
751+
}
752+
753+
@Test
754+
fun testSeparatePrivateCellsUsage() {
755+
eval(
756+
"""
757+
private val x = "abcd"
758+
var f = 47
759+
internal val z = 47
760+
""".trimIndent(),
761+
jupyterId = 1
762+
)
763+
val state = repl.notebook.cellVariables
764+
assertTrue(state[0]!!.contains("x"))
765+
assertTrue(state[0]!!.contains("z"))
766+
767+
eval(
768+
"""
769+
private val x = 341
770+
f += x
771+
protected val z = "abcd"
772+
""".trimIndent(),
773+
jupyterId = 2
774+
)
775+
assertTrue(state.isNotEmpty())
776+
assertTrue(state[0]!!.isNotEmpty())
777+
assertFalse(state[0]!!.contains("x"))
778+
assertFalse(state[0]!!.contains("z"))
779+
assertTrue(state[1]!!.contains("x"))
780+
781+
val setOfPrevCell = setOf("f")
782+
val setOfNextCell = setOf("x", "f", "z")
783+
assertEquals(state[0], setOfPrevCell)
784+
assertEquals(state[1], setOfNextCell)
785+
}
699786
}
700787

701788
class ReplVarsSerializationTest : AbstractSingleReplTest() {
@@ -933,4 +1020,43 @@ class ReplVarsSerializationTest : AbstractSingleReplTest() {
9331020
}
9341021
}
9351022
}
1023+
1024+
1025+
@Test
1026+
fun testUnchangedVariables() {
1027+
eval(
1028+
"""
1029+
private val x = "abcd"
1030+
var f = 47
1031+
internal val z = 47
1032+
""".trimIndent(),
1033+
jupyterId = 1
1034+
)
1035+
val state = repl.notebook.unchangedVariables()
1036+
val setOfCell = setOf("x", "f", "z")
1037+
assertTrue(state.isNotEmpty())
1038+
assertEquals(setOfCell, state)
1039+
1040+
eval(
1041+
"""
1042+
private val x = 341
1043+
f += x
1044+
protected val z = "abcd"
1045+
""".trimIndent(),
1046+
jupyterId = 2
1047+
)
1048+
assertTrue(state.isEmpty())
1049+
val setOfPrevCell = setOf("f")
1050+
assertNotEquals(setOfCell, setOfPrevCell)
1051+
1052+
eval(
1053+
"""
1054+
private val x = 341
1055+
protected val z = "abcd"
1056+
""".trimIndent(),
1057+
jupyterId = 2
1058+
)
1059+
assertTrue(state.isNotEmpty())
1060+
assertEquals(state, setOfPrevCell)
1061+
}
9361062
}

0 commit comments

Comments
 (0)