Implement new minimal extension point for providing (new) script definitions to idea

#KT-27817 fixed
This commit is contained in:
Ilya Chernikov
2018-10-08 15:01:49 +02:00
parent afc78be58e
commit 0e66c64a17
12 changed files with 139 additions and 0 deletions
+1
View File
@@ -40,6 +40,7 @@ dependencies {
compile(project(":plugins:uast-kotlin"))
compile(project(":plugins:uast-kotlin-idea"))
compile(project(":kotlin-script-util")) { isTransitive = false }
compile(project(":kotlin-scripting-intellij"))
compile(commonDep("org.jetbrains.kotlinx", "kotlinx-coroutines-core")) { isTransitive = false }
+1
View File
@@ -40,6 +40,7 @@ dependencies {
compile(project(":plugins:uast-kotlin"))
compile(project(":plugins:uast-kotlin-idea"))
compile(project(":kotlin-script-util")) { isTransitive = false }
compile(project(":kotlin-scripting-intellij"))
compile(commonDep("org.jetbrains.kotlinx", "kotlinx-coroutines-core")) { isTransitive = false }
+1
View File
@@ -40,6 +40,7 @@ dependencies {
compile(project(":plugins:uast-kotlin"))
compile(project(":plugins:uast-kotlin-idea"))
compile(project(":kotlin-script-util")) { isTransitive = false }
compile(project(":kotlin-scripting-intellij"))
compile(commonDep("org.jetbrains.kotlinx", "kotlinx-coroutines-core")) { isTransitive = false }
+1
View File
@@ -40,6 +40,7 @@ dependencies {
compile(project(":plugins:uast-kotlin"))
compile(project(":plugins:uast-kotlin-idea"))
compile(project(":kotlin-script-util")) { isTransitive = false }
compile(project(":kotlin-scripting-intellij"))
compile(commonDep("org.jetbrains.kotlinx", "kotlinx-coroutines-core")) { isTransitive = false }
+1
View File
@@ -40,6 +40,7 @@ dependencies {
compile(project(":plugins:uast-kotlin"))
compile(project(":plugins:uast-kotlin-idea"))
compile(project(":kotlin-script-util")) { isTransitive = false }
compile(project(":kotlin-scripting-intellij"))
compile(commonDep("org.jetbrains.kotlinx", "kotlinx-coroutines-core")) { isTransitive = false }
+8
View File
@@ -46,6 +46,10 @@
<extensionPoint qualifiedName="org.jetbrains.kotlin.scriptRelatedModulesProvider"
interface="org.jetbrains.kotlin.idea.core.script.dependencies.ScriptRelatedModulesProvider"
area="IDEA_PROJECT"/>
<extensionPoint qualifiedName="org.jetbrains.kotlin.scriptDefinitionsProvider"
interface="kotlin.script.experimental.intellij.ScriptDefinitionsProvider"
area="IDEA_PROJECT"/>
</extensionPoints>
<extensions defaultExtensionNs="org.jetbrains.kotlin">
@@ -56,6 +60,10 @@
id="ScriptTemplatesFromCompilerSettingsProvider"
implementation="org.jetbrains.kotlin.idea.script.ScriptTemplatesFromCompilerSettingsProvider"/>
<scriptDefinitionContributor
id="BridgeScriptDefinitionsContributor"
implementation="org.jetbrains.kotlin.idea.script.BridgeScriptDefinitionsContributor"/>
<scriptDefinitionContributor
id="ScriptTemplatesFromDependenciesProvider"
implementation="org.jetbrains.kotlin.idea.script.ScriptTemplatesFromDependenciesProvider"/>
@@ -38,6 +38,10 @@
<extensionPoint qualifiedName="org.jetbrains.kotlin.scriptRelatedModulesProvider"
interface="org.jetbrains.kotlin.idea.core.script.dependencies.ScriptRelatedModulesProvider"
area="IDEA_PROJECT"/>
<extensionPoint qualifiedName="org.jetbrains.kotlin.scriptDefinitionsProvider"
interface="kotlin.script.experimental.intellij.ScriptDefinitionsProvider"
area="IDEA_PROJECT"/>
</extensionPoints>
<extensions defaultExtensionNs="org.jetbrains.kotlin">
@@ -48,6 +52,10 @@
id="ScriptTemplatesFromCompilerSettingsProvider"
implementation="org.jetbrains.kotlin.idea.script.ScriptTemplatesFromCompilerSettingsProvider"/>
<scriptDefinitionContributor
id="BridgeScriptDefinitionsContributor"
implementation="org.jetbrains.kotlin.idea.script.BridgeScriptDefinitionsContributor"/>
<scriptDefinitionContributor
id="ScriptTemplatesFromDependenciesProvider"
implementation="org.jetbrains.kotlin.idea.script.ScriptTemplatesFromDependenciesProvider"/>
@@ -0,0 +1,62 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.script
import com.intellij.openapi.extensions.Extensions
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.idea.core.script.ScriptDefinitionContributor
import org.jetbrains.kotlin.idea.core.script.loadDefinitionsFromTemplates
import org.jetbrains.kotlin.script.KotlinScriptDefinition
import org.jetbrains.kotlin.scripting.compiler.plugin.ScriptDefinitionsFromClasspathDiscoverySource
import kotlin.script.experimental.intellij.ScriptDefinitionsProvider
class BridgeScriptDefinitionsContributor(private val project: Project) : ScriptDefinitionContributor {
override val id: String = "BridgeScriptDefinitionsContributor"
override fun getDefinitions(): List<KotlinScriptDefinition> {
val extensions = Extensions.getArea(project).getExtensionPoint(ScriptDefinitionsProvider.EP_NAME).extensions
val messageCollector = LoggingMessageCollector()
return extensions.flatMap { provider ->
val explicitClasses = provider.getDefinitionClasses().toList()
val classPath = provider.getDefinitionsClassPath().toList()
val explicitDefinitions =
if (explicitClasses.isNotEmpty()) loadDefinitionsFromTemplates(explicitClasses, classPath)
else emptyList()
val discoveredDefinitions =
if (provider.useDiscovery()) emptySequence()
else ScriptDefinitionsFromClasspathDiscoverySource(classPath, emptyMap(), messageCollector).definitions
explicitDefinitions + discoveredDefinitions
}
}
}
private class LoggingMessageCollector : MessageCollector {
private val log = Logger.getInstance("ScriptDefinitionsProviders")
private var hasErrors = false
override fun clear() {
hasErrors = false
}
override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation?) {
when (severity) {
CompilerMessageSeverity.ERROR, CompilerMessageSeverity.EXCEPTION -> {
log.error(message)
hasErrors = true
}
CompilerMessageSeverity.WARNING, CompilerMessageSeverity.STRONG_WARNING -> log.warn(message)
else -> log.info(message)
}
}
override fun hasErrors(): Boolean = hasErrors
}
@@ -0,0 +1,26 @@
plugins {
kotlin("jvm")
id("jps-compatible")
}
jvmTarget = "1.6"
dependencies {
compile(project(":kotlin-script-runtime"))
compile(project(":kotlin-stdlib"))
compile(project(":kotlin-scripting-common"))
compileOnly(intellijCoreDep()) { includeJars("intellij-core") }
}
sourceSets {
"main" { projectDefault() }
"test" { }
}
standardPublicJars()
ideaPlugin()
publish()
@@ -0,0 +1,27 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package kotlin.script.experimental.intellij
import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.openapi.extensions.Extensions
import com.intellij.openapi.project.Project
import java.io.File
interface ScriptDefinitionsProvider {
val id: String
fun getDefinitionClasses(): Iterable<String>
fun getDefinitionsClassPath(): Iterable<File>
fun useDiscovery(): Boolean
companion object {
val EP_NAME: ExtensionPointName<ScriptDefinitionsProvider> =
ExtensionPointName.create<ScriptDefinitionsProvider>("org.jetbrains.kotlin.scriptDefinitionsProvider")
}
}
@@ -8,6 +8,7 @@ plugins {
dependencies {
compile(project(":kotlin-scripting-compiler"))
compile(project(":kotlin-scripting-intellij"))
compileOnly(project(":idea:idea-gradle"))
compileOnly(project(":idea:idea-core"))
compileOnly(intellijDep())
+2
View File
@@ -172,6 +172,7 @@ include ":kotlin-build-common",
":kotlin-scripting-common",
":kotlin-scripting-jvm",
":kotlin-scripting-jvm-host",
":kotlin-scripting-intellij",
":kotlin-scripting-compiler",
":kotlin-scripting-idea",
":kotlin-scripting-compiler-embeddable",
@@ -293,6 +294,7 @@ project(':kotlin-annotations-android').projectDir = "$rootDir/libraries/tools/ko
project(':kotlin-scripting-common').projectDir = "$rootDir/libraries/scripting/common" as File
project(':kotlin-scripting-jvm').projectDir = "$rootDir/libraries/scripting/jvm" as File
project(':kotlin-scripting-jvm-host').projectDir = "$rootDir/libraries/scripting/jvm-host" as File
project(':kotlin-scripting-intellij').projectDir = "$rootDir/libraries/scripting/intellij" as File
project(':kotlin-scripting-compiler').projectDir = "$rootDir/plugins/scripting/scripting-cli" as File
project(':kotlin-scripting-compiler-embeddable').projectDir = "$rootDir/plugins/scripting/scripting-embeddable" as File
project(':kotlin-scripting-idea').projectDir = "$rootDir/plugins/scripting/scripting-idea" as File