All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlinx.jupyter.magics.CompoundCodePreprocessor.kt Maven / Gradle / Ivy

Go to download

Implementation of REPL compiler and preprocessor for Jupyter dialect of Kotlin (IDE-compatible)

There is a newer version: 0.12.0-290
Show newest version
package org.jetbrains.kotlinx.jupyter.magics

import org.jetbrains.kotlinx.jupyter.api.CodePreprocessor
import org.jetbrains.kotlinx.jupyter.api.KotlinKernelHost

/**
 * Containing [preprocessors]' [process] are run in reversed order: last added processors
 * are run first
 */
class CompoundCodePreprocessor(
    private val preprocessors: MutableList
) : CodePreprocessor {
    constructor(vararg preprocessors: CodePreprocessor) : this(preprocessors.toMutableList())

    override fun process(code: String, host: KotlinKernelHost): CodePreprocessor.Result {
        return preprocessors.foldRight(CodePreprocessor.Result(code, emptyList())) { preprocessor, result ->
            if (preprocessor.accepts(result.code)) {
                val newResult = preprocessor.process(result.code, host)
                CodePreprocessor.Result(newResult.code, result.libraries + newResult.libraries)
            } else result
        }
    }

    fun add(preprocessor: CodePreprocessor) {
        preprocessors.add(preprocessor)
    }

    fun addAll(preprocessors: Iterable) {
        this.preprocessors.addAll(preprocessors)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy