CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-jetbrains-kotlin--kotlin-scripting-ide-services

Kotlin Scripting Compiler extension providing code completion and static analysis for IDE integration

Pending
Overview
Eval results
Files

repl-compiler.mddocs/

REPL Compiler Services

Main compiler class providing intelligent code completion and static analysis for Kotlin scripts in REPL environments.

Capabilities

KJvmReplCompilerWithIdeServices

The primary entry point for REPL compilation with IDE services. Provides both completion and analysis capabilities with full compiler integration.

/**
 * JVM REPL compiler with IDE services providing completion and analysis
 * @param hostConfiguration - Scripting host configuration, defaults to defaultJvmScriptingHostConfiguration
 */
class KJvmReplCompilerWithIdeServices(
    hostConfiguration: ScriptingHostConfiguration = defaultJvmScriptingHostConfiguration
) : KJvmReplCompilerBase<IdeLikeReplCodeAnalyzer>, ReplCompleter, ReplCodeAnalyzer

Usage Example:

import org.jetbrains.kotlin.scripting.ide_services.compiler.KJvmReplCompilerWithIdeServices
import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration

// Create compiler with default configuration
val compiler = KJvmReplCompilerWithIdeServices()

// Create compiler with custom host configuration
val customConfig = ScriptingHostConfiguration {
    jvm {
        // Custom JVM configuration
    }
}
val customCompiler = KJvmReplCompilerWithIdeServices(customConfig)

Code Completion

Provides intelligent code completion with context-aware suggestions, type information, and filtering options.

/**
 * Provides code completion at the specified cursor position
 * @param snippet - Source code to analyze
 * @param cursor - Cursor position for completion
 * @param configuration - Script compilation configuration
 * @returns ResultWithDiagnostics containing completion results
 */
suspend fun complete(
    snippet: SourceCode,
    cursor: SourceCode.Position,
    configuration: ScriptCompilationConfiguration
): ResultWithDiagnostics<ReplCompletionResult>

Usage Examples:

// Basic completion
val sourceCode = """
    val list = listOf(1, 2, 3)
    list.
""".trimIndent()

val cursor = SourceCode.Position(1, 5) // After the dot
val completions = compiler.complete(
    sourceCode.toScriptSource(),
    cursor,
    ScriptCompilationConfiguration()
)

when (completions) {
    is ResultWithDiagnostics.Success -> {
        completions.value.completions.forEach { variant ->
            println("${variant.displayText} - ${variant.tail}")
        }
    }
    is ResultWithDiagnostics.Failure -> {
        completions.reports.forEach { diagnostic ->
            println("Error: ${diagnostic.message}")
        }
    }
}

// Completion with configuration
val config = ScriptCompilationConfiguration {
    completion {
        filterOutShadowedDescriptors(false)
        nameFilter { name, prefix -> name.contains(prefix, ignoreCase = true) }
    }
}

val advancedCompletions = compiler.complete(sourceCode.toScriptSource(), cursor, config)

Code Analysis

Performs static analysis of Kotlin script code, providing error detection, type inference, and diagnostic information.

/**
 * Analyzes script code at the specified cursor position
 * @param snippet - Source code to analyze
 * @param cursor - Cursor position for analysis
 * @param configuration - Script compilation configuration
 * @returns ResultWithDiagnostics containing analysis results
 */
suspend fun analyze(
    snippet: SourceCode,
    cursor: SourceCode.Position,
    configuration: ScriptCompilationConfiguration
): ResultWithDiagnostics<ReplAnalyzerResult>

Usage Examples:

// Basic analysis  
val sourceCode = """
    val x: String = 42  // Type error
    val y = x.length
""".trimIndent()

val cursor = SourceCode.Position(1, 10)
val analysis = compiler.analyze(
    sourceCode.toScriptSource(),
    cursor,
    ScriptCompilationConfiguration()
)

when (analysis) {
    is ResultWithDiagnostics.Success -> {
        val result = analysis.value
        println("Is complete: ${result.isComplete}")
        result.renderedResultType?.let { type ->
            println("Result type: $type")
        }
        
        result.analysisDiagnostics.forEach { diagnostic ->
            println("${diagnostic.severity}: ${diagnostic.message}")
        }
    }
    is ResultWithDiagnostics.Failure -> {
        analysis.reports.forEach { diagnostic ->
            println("Analysis error: ${diagnostic.message}")
        }
    }
}

Internal Analysis Components

The compiler uses several internal components for advanced analysis:

/**
 * Internal analysis result containing detailed resolution information
 */
data class AnalyzeWithCursorResult(
    val ktScript: KtFile,
    val bindingContext: BindingContext,
    val resolutionFacade: KotlinResolutionFacadeForRepl,
    val moduleDescriptor: ModuleDescriptor,
    val cursorAbs: Int,
    val resultProperty: PropertyDescriptor?
)

Core Implementation Classes

These internal classes provide the underlying functionality but are part of the public API:

/**
 * Code analyzer providing IDE-like analysis for REPL contexts
 */
class IdeLikeReplCodeAnalyzer(
    environment: KotlinCoreEnvironment,
    implicitsResolutionFilter: ImplicitsExtensionsResolutionFilter
) : ReplCodeAnalyzerBase<IdeLikeReplCodeAnalyzer.ReplLineAnalysisResultWithStateless> {
    
    fun statelessAnalyzeWithImportedScripts(
        psiFile: KtFile,
        importedScripts: List<KtFile>,
        priority: Int
    ): ReplLineAnalysisResultWithStateless
}

/**
 * Resolution facade implementation for REPL contexts
 */
class KotlinResolutionFacadeForRepl(
    environment: KotlinCoreEnvironment,
    provider: ComponentProvider
) : ResolutionFacade {
    override val project: Project
    override val moduleDescriptor: ModuleDescriptor
}

Completion Implementation Functions

Core completion functionality:

/**
 * Main completion function for JVM REPL
 */
fun getKJvmCompletion(
    ktScript: KtFile,
    bindingContext: BindingContext,
    resolutionFacade: KotlinResolutionFacadeForRepl,
    moduleDescriptor: ModuleDescriptor,
    cursor: Int,
    configuration: ScriptCompilationConfiguration
): Sequence<SourceCodeCompletionVariant>

/**
 * Prepares code for completion analysis by inserting marker string
 */
fun prepareCodeForCompletion(code: String, cursor: Int): String

Error Handling

All completion and analysis operations return ResultWithDiagnostics which provides:

  • Success case: Contains the requested result (completions or analysis)
  • Failure case: Contains diagnostic information about compilation errors
  • Diagnostics: Both success and failure cases may include diagnostic reports

Common error scenarios:

  • Syntax errors: Invalid Kotlin syntax in the script
  • Type errors: Type mismatches and unresolved references
  • Configuration errors: Invalid script compilation configuration
  • Environment errors: Issues with the compilation environment or dependencies

Install with Tessl CLI

npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-scripting-ide-services

docs

call-types.md

completion-config.md

index.md

repl-compiler.md

resolution.md

utilities.md

tile.json