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

org.jetbrains.kotlinx.jupyter.repl.CompletionResult.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.repl

import org.jetbrains.annotations.TestOnly
import org.jetbrains.kotlinx.jupyter.compiler.util.CodeInterval
import org.jetbrains.kotlinx.jupyter.messaging.AbstractMessageContent
import org.jetbrains.kotlinx.jupyter.messaging.CompleteErrorReply
import org.jetbrains.kotlinx.jupyter.messaging.CompleteReply
import org.jetbrains.kotlinx.jupyter.messaging.Paragraph
import kotlin.script.experimental.api.SourceCodeCompletionVariant

abstract class CompletionResult {
    abstract val message: AbstractMessageContent

    open class Success(
        private val matches: List,
        private val bounds: CodeInterval,
        private val metadata: List,
        private val text: String,
        private val cursor: Int,
    ) : CompletionResult() {
        init {
            assert(matches.size == metadata.size)
        }

        override val message: AbstractMessageContent
            get() = CompleteReply(
                matches,
                bounds.from,
                bounds.to,
                Paragraph(cursor, text),
                CompleteReply.Metadata(
                    metadata.map {
                        CompleteReply.ExperimentalType(
                            it.text,
                            it.tail,
                            bounds.from,
                            bounds.to,
                        )
                    },
                    metadata.map {
                        CompleteReply.ExtendedMetadataEntry(
                            it.text,
                            it.displayText,
                            it.icon,
                            it.tail,
                            it.deprecationLevel?.name,
                        )
                    },
                ),
            )

        @TestOnly
        fun sortedMatches(): List = matches.sorted()

        @TestOnly
        fun matches(): List = matches

        @TestOnly
        fun sortedRaw(): List = metadata.sortedBy { it.text }
    }

    class Empty(
        text: String,
        cursor: Int,
    ) : Success(emptyList(), CodeInterval(cursor, cursor), emptyList(), text, cursor)

    class Error(
        private val errorName: String,
        private val errorValue: String,
        private val traceBack: List,
    ) : CompletionResult() {
        override val message: AbstractMessageContent
            get() = CompleteErrorReply(errorName, errorValue, traceBack)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy