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

org.jetbrains.kotlinx.jupyter.repl.CompletionResult.kt Maven / Gradle / Ivy

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

import org.jetbrains.annotations.TestOnly
import org.jetbrains.kotlinx.jupyter.CompleteReply
import org.jetbrains.kotlinx.jupyter.ErrorReply
import org.jetbrains.kotlinx.jupyter.MessageContent
import org.jetbrains.kotlinx.jupyter.Paragraph
import org.jetbrains.kotlinx.jupyter.compiler.util.CodeInterval
import kotlin.script.experimental.api.SourceCodeCompletionVariant

abstract class CompletionResult {
    abstract val message: MessageContent

    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: MessageContent
            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 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: MessageContent
            get() = ErrorReply(errorName, errorValue, traceBack)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy