@@ -2,6 +2,7 @@ package org.jetbrains.kotlinx.jupyter.repl.impl
2
2
3
3
import kotlinx.coroutines.runBlocking
4
4
import org.jetbrains.kotlinx.jupyter.ReplEvalRuntimeException
5
+ import org.jetbrains.kotlinx.jupyter.VariablesUsagesPerCellWatcher
5
6
import org.jetbrains.kotlinx.jupyter.api.Code
6
7
import org.jetbrains.kotlinx.jupyter.api.FieldValue
7
8
import org.jetbrains.kotlinx.jupyter.api.VariableState
@@ -64,12 +65,10 @@ internal class InternalEvaluatorImpl(
64
65
65
66
override val variablesHolder = mutableMapOf<String , VariableState >()
66
67
67
- override val cellVariables = mutableMapOf<Int , MutableSet <String >>()
68
+ override val cellVariables: Map <Int , Set <String >>
69
+ get() = variablesWatcher.cellVariables
68
70
69
- /* *
70
- * Tells in which cell a variable was declared
71
- */
72
- private val varsDeclarationInfo: MutableMap <String , Int > = mutableMapOf ()
71
+ private val variablesWatcher: VariablesUsagesPerCellWatcher <Int , String > = VariablesUsagesPerCellWatcher ()
73
72
74
73
private var isExecuting = false
75
74
@@ -143,18 +142,15 @@ internal class InternalEvaluatorImpl(
143
142
}
144
143
145
144
private fun updateVariablesState (cellId : Int ) {
146
- // remove known modifying usages in this cell
147
- cellVariables[cellId]?.removeIf {
148
- varsDeclarationInfo[it] != cellId
149
- }
145
+ variablesWatcher.removeOldUsages(cellId)
150
146
151
147
variablesHolder.forEach {
152
148
val state = it.value as VariableStateImpl
153
149
val oldValue = state.stringValue
154
150
state.update()
155
151
156
152
if (state.stringValue != oldValue) {
157
- cellVariables[cellId]?.add( it.key)
153
+ variablesWatcher.addUsage(cellId, it.key)
158
154
}
159
155
}
160
156
}
@@ -168,21 +164,13 @@ internal class InternalEvaluatorImpl(
168
164
fields.forEach { property ->
169
165
property as KProperty1 <Any , * >
170
166
val state = VariableStateImpl (property, cellClassInstance)
167
+ variablesWatcher.addDeclaration(cellId, property.name)
171
168
172
- // redeclaration of any type
173
- if (varsDeclarationInfo.containsKey(property.name)) {
174
- val oldCellId = varsDeclarationInfo[property.name]
175
- if (oldCellId != cellId) {
176
- cellVariables[oldCellId]?.remove(property.name)
177
- }
178
- }
179
169
// it was val, now it's var
180
170
if (property is KMutableProperty1 ) {
181
171
variablesHolder.remove(property.name)
182
172
}
183
- varsDeclarationInfo[property.name] = cellId
184
- cellVariables[cellId]?.add(property.name)
185
- // invariant with changes: cache --> varsMap, other way is not allowed
173
+
186
174
if (property !is KMutableProperty1 ) {
187
175
variablesHolder[property.name] = state
188
176
return @forEach
@@ -193,15 +181,8 @@ internal class InternalEvaluatorImpl(
193
181
}
194
182
195
183
private fun updateDataAfterExecution (lastExecutionCellId : Int , resultValue : ResultValue ) {
196
- cellVariables.putIfAbsent(lastExecutionCellId, mutableSetOf ())
197
-
198
- val visibleDeclarations = getVisibleVariables(resultValue, lastExecutionCellId)
199
- visibleDeclarations.forEach {
200
- val cellSet = cellVariables[lastExecutionCellId] ? : return @forEach
201
- varsDeclarationInfo[it.key] = lastExecutionCellId
202
- cellSet + = it.key
203
- }
204
- variablesHolder + = visibleDeclarations
184
+ variablesWatcher.ensureStorageCreation(lastExecutionCellId)
185
+ variablesHolder + = getVisibleVariables(resultValue, lastExecutionCellId)
205
186
206
187
updateVariablesState(lastExecutionCellId)
207
188
}
0 commit comments