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

completion-config.mddocs/

Completion Configuration

Configuration system for customizing code completion behavior in Kotlin scripting environments, including filtering options and name matching strategies.

Capabilities

ReplCompletionOptionsKeys

Marker interface defining the configuration keys available for REPL completion customization.

/**
 * Marker interface for REPL completion configuration keys
 */
interface ReplCompletionOptionsKeys

ReplCompletionOptionsBuilder

Builder class for constructing completion configuration options using a fluent API.

/**
 * Builder for configuring REPL completion options
 * Extends PropertiesCollection.Builder for type-safe configuration
 */
open class ReplCompletionOptionsBuilder : PropertiesCollection.Builder(), ReplCompletionOptionsKeys {
    companion object : ReplCompletionOptionsKeys
}

Usage Example:

import org.jetbrains.kotlin.scripting.ide_services.compiler.completion
import org.jetbrains.kotlin.scripting.ide_services.compiler.filterOutShadowedDescriptors
import org.jetbrains.kotlin.scripting.ide_services.compiler.nameFilter
import kotlin.script.experimental.api.ScriptCompilationConfiguration

// Configure completion options
val config = ScriptCompilationConfiguration {
    completion {
        filterOutShadowedDescriptors(false) // Include shadowed descriptors
        nameFilter { name, prefix -> 
            name.startsWith(prefix, ignoreCase = true) || 
            name.contains(prefix, ignoreCase = true) 
        }
    }
}

Configuration Options

Filter Shadowed Descriptors

Controls whether to filter out shadowed declarations in completion results.

/**
 * Configure whether to filter out shadowed descriptors in completion
 * @param value - true to filter out shadowed descriptors (default), false to include them
 */
fun ReplCompletionOptionsBuilder.filterOutShadowedDescriptors(value: Boolean)

/**
 * Property key for shadowed descriptor filtering
 * Default value: true
 */
val ReplCompletionOptionsKeys.filterOutShadowedDescriptors: Boolean

Usage Examples:

// Include shadowed descriptors (may show more results but with duplicates)
completion {
    filterOutShadowedDescriptors(false)
}

// Filter out shadowed descriptors (default behavior, cleaner results)
completion {
    filterOutShadowedDescriptors(true)
}

Name Filter Function

Customizes how completion candidates are filtered based on the typed prefix.

/**
 * Configure the name filtering function for completion
 * @param value - Function that determines if a name matches the typed prefix
 */
fun ReplCompletionOptionsBuilder.nameFilter(value: (String, String) -> Boolean)

/**
 * Property key for name filtering
 * Default value: { name, namePart -> name.startsWith(namePart) }
 */
val ReplCompletionOptionsKeys.nameFilter: (String, String) -> Boolean

Usage Examples:

// Default prefix matching
completion {
    nameFilter { name, prefix -> name.startsWith(prefix) }
}

// Case-insensitive prefix matching
completion {
    nameFilter { name, prefix -> name.startsWith(prefix, ignoreCase = true) }
}

// Fuzzy matching (contains anywhere)
completion {
    nameFilter { name, prefix -> name.contains(prefix, ignoreCase = true) }
}

// Custom matching logic
completion {
    nameFilter { name, prefix ->
        when {
            prefix.isEmpty() -> true
            name.startsWith(prefix, ignoreCase = true) -> true
            name.contains("_${prefix}", ignoreCase = true) -> true // Match snake_case
            else -> false
        }
    }
}

Script Compilation Configuration Extension

Extension property to access completion configuration within script compilation configuration.

/**
 * Extension property for accessing completion configuration builder
 * Available on ScriptCompilationConfigurationKeys
 */
val ScriptCompilationConfigurationKeys.completion: ReplCompletionOptionsBuilder

Complete Configuration Example:

import org.jetbrains.kotlin.scripting.ide_services.compiler.KJvmReplCompilerWithIdeServices
import org.jetbrains.kotlin.scripting.ide_services.compiler.completion
import org.jetbrains.kotlin.scripting.ide_services.compiler.filterOutShadowedDescriptors
import org.jetbrains.kotlin.scripting.ide_services.compiler.nameFilter
import kotlin.script.experimental.api.*

val compiler = KJvmReplCompilerWithIdeServices()

// Configure for relaxed completion (more results, fuzzy matching)
val relaxedConfig = ScriptCompilationConfiguration {
    completion {
        filterOutShadowedDescriptors(false) // Show all possible completions
        nameFilter { name, prefix ->
            // Fuzzy matching with multiple strategies
            when {
                prefix.isEmpty() -> true
                name.equals(prefix, ignoreCase = true) -> true
                name.startsWith(prefix, ignoreCase = true) -> true
                name.contains(prefix, ignoreCase = true) -> true
                // Match camelCase abbreviations (e.g., "gCS" matches "getContentSize")
                matchesCamelCaseAbbreviation(name, prefix) -> true
                else -> false
            }
        }
    }
}

// Configure for strict completion (cleaner results, exact matching)
val strictConfig = ScriptCompilationConfiguration {
    completion {
        filterOutShadowedDescriptors(true) // Clean results only
        nameFilter { name, prefix ->
            // Exact prefix matching only
            name.startsWith(prefix, ignoreCase = false)
        }
    }
}

// Use configurations
val sourceCode = "list.".toScriptSource()
val cursor = SourceCode.Position(0, 5)

val relaxedCompletions = compiler.complete(sourceCode, cursor, relaxedConfig)
val strictCompletions = compiler.complete(sourceCode, cursor, strictConfig)

fun matchesCamelCaseAbbreviation(name: String, prefix: String): Boolean {
    // Custom logic for camelCase abbreviation matching
    // Implementation would check if prefix matches uppercase letters in name
    return false // Simplified for example
}

Configuration Properties Details

Default Values

The completion system provides sensible defaults:

// Default configuration equivalent
val defaultConfig = ScriptCompilationConfiguration {
    completion {
        filterOutShadowedDescriptors(true) // Clean completion results
        nameFilter { name, namePart -> 
            name.startsWith(namePart) // Simple prefix matching
        }
    }
}

Performance Considerations

  • filterOutShadowedDescriptors(false): Can slow completion by up to 4x but provides more comprehensive results
  • filterOutShadowedDescriptors(true): Faster performance with cleaner, more relevant results
  • Complex nameFilter functions: May impact completion speed for large codebases

Integration with REPL Analysis

Completion configuration is automatically used by the REPL compiler during the completion process:

// The compiler automatically applies completion configuration
val result = compiler.complete(sourceCode, cursor, configurationWithCompletionOptions)

// Configuration is accessed internally like this:
val filterShadowed = configuration[ScriptCompilationConfiguration.completion.filterOutShadowedDescriptors]!!
val nameFilterFn = configuration[ScriptCompilationConfiguration.completion.nameFilter]!!

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