@@ -3,6 +3,7 @@ package org.jetbrains.kotlinx.jupyter.api.libraries
3
3
import org.jetbrains.kotlinx.jupyter.api.AfterCellExecutionCallback
4
4
import org.jetbrains.kotlinx.jupyter.api.ClassAnnotationHandler
5
5
import org.jetbrains.kotlinx.jupyter.api.ClassDeclarationsCallback
6
+ import org.jetbrains.kotlinx.jupyter.api.CodeCell
6
7
import org.jetbrains.kotlinx.jupyter.api.ExecutionCallback
7
8
import org.jetbrains.kotlinx.jupyter.api.FieldHandler
8
9
import org.jetbrains.kotlinx.jupyter.api.FieldHandlerByClass
@@ -25,9 +26,9 @@ import kotlin.reflect.KMutableProperty
25
26
*/
26
27
abstract class JupyterIntegration : LibraryDefinitionProducer {
27
28
28
- abstract fun Builder.onLoaded (notebook : Notebook ? )
29
+ abstract fun Builder.onLoaded ()
29
30
30
- class Builder {
31
+ class Builder ( val notebook : Notebook ) {
31
32
32
33
private val renderers = mutableListOf<RendererTypeHandler >()
33
34
@@ -69,9 +70,11 @@ abstract class JupyterIntegration : LibraryDefinitionProducer {
69
70
fileAnnotations.add(handler)
70
71
}
71
72
72
- inline fun <reified T : Any > render (noinline renderer : (T ) -> Any ) {
73
+ inline fun <reified T : Any > render (noinline renderer : CodeCell . (T ) -> Any ) {
73
74
val execution = ResultHandlerExecution { _, property ->
74
- FieldValue (renderer(property.value as T ), property.name)
75
+ val currentCell = notebook.currentCell
76
+ ? : throw IllegalStateException (" Current cell should not be null on renderer invocation" )
77
+ FieldValue (renderer(currentCell, property.value as T ), property.name)
75
78
}
76
79
addRenderer(SubtypeRendererTypeHandler (T ::class , execution))
77
80
}
@@ -162,9 +165,9 @@ abstract class JupyterIntegration : LibraryDefinitionProducer {
162
165
)
163
166
}
164
167
165
- override fun getDefinitions (notebook : Notebook ? ): List <LibraryDefinition > {
166
- val builder = Builder ()
167
- builder.onLoaded(notebook )
168
+ override fun getDefinitions (notebook : Notebook ): List <LibraryDefinition > {
169
+ val builder = Builder (notebook )
170
+ builder.onLoaded()
168
171
return listOf (builder.getDefinition())
169
172
}
170
173
}
0 commit comments