Skip to content

Instantly share code, notes, and snippets.

@alshain
Last active May 19, 2019 21:50
Show Gist options
  • Save alshain/d6a3da646790759aff64fb3376f5d6df to your computer and use it in GitHub Desktop.
Save alshain/d6a3da646790759aff64fb3376f5d6df to your computer and use it in GitHub Desktop.
Kotlin Scripting Knowledge

Links

Proposal Discussion

How does the IntelliJ Scripting Console index IntelliJ JARs automatically?

The dependencyResolver of StandardIdeScriptDefinition checks whether the "RootType" is IdeConsoleRootType, in that case it adds the IDEs + Kotlin's JARs (detected via URLClassLoader, mainly) to the classpath.

See here: https://github.com/JetBrains/kotlin/blob/c248dd29c21699ab22ff6fd7a77f1395c9b58632/idea/idea-core/src/org/jetbrains/kotlin/idea/core/script/ScriptDefinitionsManager.kt#L319

JAR detection from the classloader:

https://github.com/JetBrains/kotlin/blob/c248dd29c21699ab22ff6fd7a77f1395c9b58632/libraries/scripting/jvm/src/kotlin/script/experimental/jvm/util/jvmClasspathUtil.kt#L118

Important Classes

  • ScriptDefinitionsProvider
  • KotlinScriptDefinition
  • (ScriptDefinitionContributor outdated??), provide list of KotlinScriptDefinition, is an extension point
interface ScriptDefinitionContributor {
    val id: String

    fun getDefinitions(): List<KotlinScriptDefinition>
    fun isReady() = true

    companion object {
        val EP_NAME: ExtensionPointName<ScriptDefinitionContributor> =
            ExtensionPointName.create<ScriptDefinitionContributor>("org.jetbrains.kotlin.scriptDefinitionContributor")

        inline fun <reified T> find(project: Project) =
            Extensions.getArea(project).getExtensionPoint(ScriptDefinitionContributor.EP_NAME).extensions.filterIsInstance<T>().firstOrNull()
    }

}

Source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment