Skip to content

Commit 5545b5a

Browse files
nikolay-egorovileasile
authored andcommitted
Get unchangedVariables from notebook to reflect after execution state properly
1 parent a92bb3f commit 5545b5a

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ internal class InternalEvaluatorImpl(
5252
return variablesWatcher.findDeclarationAddress(variableName) ?: -1
5353
}
5454

55+
override fun getUnchangedVariables(): Set<String> {
56+
return variablesWatcher.getUnchangedVariables()
57+
}
58+
5559
override var writeCompiledClasses: Boolean
5660
get() = classWriter != null
5761
set(value) {
@@ -168,7 +172,6 @@ internal class InternalEvaluatorImpl(
168172
}.toHashSet()
169173
val ans = mutableMapOf<String, VariableStateImpl>()
170174
fields.forEach { property ->
171-
// if (property.name.startsWith("script$")) return@forEach
172175
if (!memberKPropertiesNames.contains(property.name)) return@forEach
173176

174177
val state = VariableStateImpl(property, cellClassInstance)

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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import kotlin.test.assertEquals
2626
import kotlin.test.assertFails
2727
import kotlin.test.assertFailsWith
2828
import kotlin.test.assertFalse
29+
import kotlin.test.assertNotEquals
2930
import kotlin.test.assertNotNull
3031
import kotlin.test.fail
3132

@@ -1056,4 +1057,43 @@ class ReplVarsSerializationTest : AbstractSingleReplTest() {
10561057
}
10571058
}
10581059
}
1060+
1061+
1062+
@Test
1063+
fun testUnchangedVariables() {
1064+
eval(
1065+
"""
1066+
private val x = "abcd"
1067+
var f = 47
1068+
internal val z = 47
1069+
""".trimIndent(),
1070+
jupyterId = 1
1071+
)
1072+
val state = repl.notebook.unchangedVariables()
1073+
val setOfCell = setOf("x", "f", "z")
1074+
assertTrue(state.isNotEmpty())
1075+
assertEquals(setOfCell, state)
1076+
1077+
eval(
1078+
"""
1079+
private val x = 341
1080+
f += x
1081+
protected val z = "abcd"
1082+
""".trimIndent(),
1083+
jupyterId = 2
1084+
)
1085+
assertTrue(state.isEmpty())
1086+
val setOfPrevCell = setOf("f")
1087+
assertNotEquals(setOfCell, setOfPrevCell)
1088+
1089+
eval(
1090+
"""
1091+
private val x = 341
1092+
protected val z = "abcd"
1093+
""".trimIndent(),
1094+
jupyterId = 2
1095+
)
1096+
assertTrue(state.isNotEmpty())
1097+
assertEquals(state, setOfPrevCell)
1098+
}
10591099
}

0 commit comments

Comments
 (0)