From 4f135a5fe5212b894d5e8c2c5f7b024dd60d639d Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Tue, 26 Feb 2019 12:12:24 +0100 Subject: [PATCH] Move REPL implementations to the scripting compiler impl module --- .../common/extensions/ReplFactoryExtension.kt | 27 +++++++ .../extensions/ScriptEvaluationExtension.kt | 1 - .../cli/common/extensions/ShellExtension.kt | 27 +++++++ .../jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt | 12 ++- .../cli/jvm/compiler/KotlinCoreEnvironment.kt | 4 +- .../cli/jvm/plugins/ServiceLoaderLite.kt | 7 +- .../configuration/ConsoleReplConfiguration.kt | 39 --------- .../repl/configuration/ReplConfiguration.kt | 32 -------- .../SnippetExecutionInterceptor.kt | 25 ------ .../jvm/repl/reader/IdeReplCommandReader.kt | 24 ------ .../cli/jvm/repl/reader/ReplCommandReader.kt | 24 ------ .../cli/jvm/repl/writer/ConsoleReplWriter.kt | 31 ------- .../kotlin/cli/jvm/repl/writer/ReplWriter.kt | 30 ------- compiler/daemon/build.gradle.kts | 1 - .../kotlin/daemon/CompileServiceImpl.kt | 4 +- .../kotlin/daemon/KotlinRemoteReplService.kt | 80 +++++++++++-------- .../daemon/RemoteReplStateFacadeImpl.kt | 9 ++- compiler/tests-java8/build.gradle.kts | 1 + .../kotlin/jvm/repl/ReplCompilerJava8Test.kt | 4 +- .../kotlin/cli/jvm/repl/GenericReplTest.kt | 1 + .../kotlin/daemon/CompilerDaemonTest.kt | 12 ++- .../repl/AbstractReplInterpreterTest.kt | 9 ++- .../KotlinJsr223JvmLocalScriptEngine.kt | 2 +- ...elegatePackageMemberDeclarationProvider.kt | 2 +- .../scripting}/repl/GenericCompilerState.kt | 29 +++---- .../scripting}/repl/GenericReplChecker.kt | 24 ++---- .../scripting}/repl/GenericReplCompiler.kt | 25 ++---- .../scripting}/repl/ReplCodeAnalyzer.kt | 43 +++++----- .../scripting}/repl/ReplExceptionReporter.kt | 19 +---- .../scripting}/repl/ReplFromTerminal.kt | 23 ++---- .../kotlin/scripting}/repl/ReplInterpreter.kt | 26 +++--- .../configuration/ConsoleReplConfiguration.kt | 28 +++++++ .../configuration/IdeReplConfiguration.kt | 36 +++------ .../repl/configuration/ReplConfiguration.kt | 21 +++++ .../SnippetExecutionInterceptor.kt | 14 ++++ .../ConsoleDiagnosticMessageHolder.kt | 17 +--- .../repl/messages/DiagnosticMessageHolder.kt | 2 +- .../messages/IdeDiagnosticMessageHolder.kt | 17 +--- .../repl/reader/ConsoleReplCommandReader.kt | 19 +---- .../repl/reader/IdeReplCommandReader.kt | 13 +++ .../repl/reader/ReplCommandReader.kt | 13 +++ .../repl/reader/ReplSystemInWrapper.kt | 19 +---- .../repl/writer/ConsoleReplWriter.kt | 20 +++++ .../writer/IdeSystemOutWrapperReplWriter.kt | 20 ++--- .../scripting/repl/writer/ReplWriter.kt | 19 +++++ .../plugin/JvmCliReplShellExtension.kt | 27 +++++++ .../plugin/JvmStandardReplFactoryExtension.kt | 45 +++++++++++ .../compiler/plugin/pluginRegisrar.kt | 15 +++- 48 files changed, 456 insertions(+), 486 deletions(-) create mode 100644 compiler/cli/src/org/jetbrains/kotlin/cli/common/extensions/ReplFactoryExtension.kt create mode 100644 compiler/cli/src/org/jetbrains/kotlin/cli/common/extensions/ShellExtension.kt delete mode 100644 compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/ConsoleReplConfiguration.kt delete mode 100644 compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/ReplConfiguration.kt delete mode 100644 compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/SnippetExecutionInterceptor.kt delete mode 100644 compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/reader/IdeReplCommandReader.kt delete mode 100644 compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/reader/ReplCommandReader.kt delete mode 100644 compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/ConsoleReplWriter.kt delete mode 100644 compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/ReplWriter.kt rename {compiler/cli/src/org/jetbrains/kotlin/cli/jvm => plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting}/repl/DelegatePackageMemberDeclarationProvider.kt (98%) rename {compiler/cli/src/org/jetbrains/kotlin/cli/jvm => plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting}/repl/GenericCompilerState.kt (64%) rename {compiler/cli/src/org/jetbrains/kotlin/cli/jvm => plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting}/repl/GenericReplChecker.kt (80%) rename {compiler/cli/src/org/jetbrains/kotlin/cli/jvm => plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting}/repl/GenericReplCompiler.kt (85%) rename {compiler/cli/src/org/jetbrains/kotlin/cli/jvm => plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting}/repl/ReplCodeAnalyzer.kt (89%) rename {compiler/cli/src/org/jetbrains/kotlin/cli/jvm => plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting}/repl/ReplExceptionReporter.kt (50%) rename {compiler/cli/src/org/jetbrains/kotlin/cli/jvm => plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting}/repl/ReplFromTerminal.kt (87%) rename {compiler/cli/src/org/jetbrains/kotlin/cli/jvm => plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting}/repl/ReplInterpreter.kt (84%) create mode 100644 plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/configuration/ConsoleReplConfiguration.kt rename {compiler/cli/src/org/jetbrains/kotlin/cli/jvm => plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting}/repl/configuration/IdeReplConfiguration.kt (53%) create mode 100644 plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/configuration/ReplConfiguration.kt create mode 100644 plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/configuration/SnippetExecutionInterceptor.kt rename {compiler/cli/src/org/jetbrains/kotlin/cli/jvm => plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting}/repl/messages/ConsoleDiagnosticMessageHolder.kt (56%) rename {compiler/cli/src/org/jetbrains/kotlin/cli/jvm => plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting}/repl/messages/DiagnosticMessageHolder.kt (93%) rename {compiler/cli/src/org/jetbrains/kotlin/cli/jvm => plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting}/repl/messages/IdeDiagnosticMessageHolder.kt (72%) rename {compiler/cli/src/org/jetbrains/kotlin/cli/jvm => plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting}/repl/reader/ConsoleReplCommandReader.kt (65%) create mode 100644 plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/reader/IdeReplCommandReader.kt create mode 100644 plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/reader/ReplCommandReader.kt rename {compiler/cli/src/org/jetbrains/kotlin/cli/jvm => plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting}/repl/reader/ReplSystemInWrapper.kt (78%) create mode 100644 plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/writer/ConsoleReplWriter.kt rename {compiler/cli/src/org/jetbrains/kotlin/cli/jvm => plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting}/repl/writer/IdeSystemOutWrapperReplWriter.kt (74%) create mode 100644 plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/writer/ReplWriter.kt create mode 100644 plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/JvmCliReplShellExtension.kt create mode 100644 plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/JvmStandardReplFactoryExtension.kt diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/extensions/ReplFactoryExtension.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/common/extensions/ReplFactoryExtension.kt new file mode 100644 index 00000000000..6be2715333d --- /dev/null +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/extensions/ReplFactoryExtension.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2010-2019 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.cli.common.extensions + +import com.intellij.core.JavaCoreProjectEnvironment +import org.jetbrains.kotlin.cli.common.repl.ReplCompiler +import org.jetbrains.kotlin.config.CompilerConfiguration +import org.jetbrains.kotlin.extensions.ProjectExtensionDescriptor +import java.io.File + +interface ReplFactoryExtension { + companion object : ProjectExtensionDescriptor( + "org.jetbrains.kotlin.replFactoryExtension", + ReplFactoryExtension::class.java + ) + + fun makeReplCompiler( + templateClassName: String, + templateClasspath: List, + baseClassLoader: ClassLoader?, + configuration: CompilerConfiguration, + projectEnvironment: JavaCoreProjectEnvironment + ): ReplCompiler +} diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/extensions/ScriptEvaluationExtension.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/common/extensions/ScriptEvaluationExtension.kt index 2b921676d98..5cbd483cc74 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/extensions/ScriptEvaluationExtension.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/extensions/ScriptEvaluationExtension.kt @@ -19,7 +19,6 @@ interface ScriptEvaluationExtension { fun isAccepted(arguments: CommonCompilerArguments): Boolean - // TODO: it would be nice to split KotlinCoreEnvironment to actual environment and compilation/project configuration fun eval( arguments: CommonCompilerArguments, configuration: CompilerConfiguration, diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/extensions/ShellExtension.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/common/extensions/ShellExtension.kt new file mode 100644 index 00000000000..770c490a855 --- /dev/null +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/extensions/ShellExtension.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2010-2019 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.cli.common.extensions + +import com.intellij.core.JavaCoreProjectEnvironment +import org.jetbrains.kotlin.cli.common.ExitCode +import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments +import org.jetbrains.kotlin.config.CompilerConfiguration +import org.jetbrains.kotlin.extensions.ProjectExtensionDescriptor + +interface ShellExtension { + companion object : ProjectExtensionDescriptor( + "org.jetbrains.kotlin.shellExtension", + ShellExtension::class.java + ) + + fun isAccepted(arguments: CommonCompilerArguments): Boolean + + fun run( + arguments: CommonCompilerArguments, + configuration: CompilerConfiguration, + projectEnvironment: JavaCoreProjectEnvironment + ): ExitCode +} diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt index 2c4fa8ceb08..fa94f098d7d 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt @@ -24,6 +24,7 @@ import org.jetbrains.kotlin.cli.common.ExitCode.* import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments import org.jetbrains.kotlin.cli.common.config.addKotlinSourceRoot import org.jetbrains.kotlin.cli.common.extensions.ScriptEvaluationExtension +import org.jetbrains.kotlin.cli.common.extensions.ShellExtension import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.* import org.jetbrains.kotlin.cli.common.messages.FilteringMessageCollector import org.jetbrains.kotlin.cli.common.messages.MessageCollector @@ -36,7 +37,6 @@ import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler import org.jetbrains.kotlin.cli.jvm.plugins.PluginCliParser -import org.jetbrains.kotlin.cli.jvm.repl.ReplFromTerminal import org.jetbrains.kotlin.codegen.CompilationException import org.jetbrains.kotlin.config.* import org.jetbrains.kotlin.incremental.components.ExpectActualTracker @@ -98,13 +98,17 @@ class K2JVMCompiler : CLICompiler() { if (arguments.script) { val scriptingEvaluator = ScriptEvaluationExtension.getInstances(projectEnvironment.project).find { it.isAccepted(arguments) } if (scriptingEvaluator == null) { - messageCollector.report(ERROR, "Unable to evaluate script, no scripting plugin found") + messageCollector.report(ERROR, "Unable to evaluate script, no scripting plugin loaded") return COMPILATION_ERROR } return scriptingEvaluator.eval(arguments, configuration, projectEnvironment) } else { - ReplFromTerminal.run(rootDisposable, configuration) - return ExitCode.OK + val shell = ShellExtension.getInstances(projectEnvironment.project).find { it.isAccepted(arguments) } + if (shell == null) { + messageCollector.report(ERROR, "Unable to run REPL, no scripting plugin loaded") + return COMPILATION_ERROR + } + return shell.run(arguments, configuration, projectEnvironment) } } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinCoreEnvironment.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinCoreEnvironment.kt index 28b05fb0e33..66253391b98 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinCoreEnvironment.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinCoreEnvironment.kt @@ -75,6 +75,7 @@ import org.jetbrains.kotlin.cli.common.config.ContentRoot import org.jetbrains.kotlin.cli.common.config.KotlinSourceRoot import org.jetbrains.kotlin.cli.common.config.kotlinSourceRoots import org.jetbrains.kotlin.cli.common.extensions.ScriptEvaluationExtension +import org.jetbrains.kotlin.cli.common.extensions.ShellExtension import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.ERROR import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.STRONG_WARNING @@ -460,7 +461,7 @@ class KotlinCoreEnvironment private constructor( // used in the daemon for jar cache cleanup val applicationEnvironment: JavaCoreApplicationEnvironment? get() = ourApplicationEnvironment - internal fun getOrCreateApplicationEnvironmentForProduction( + fun getOrCreateApplicationEnvironmentForProduction( parentDisposable: Disposable, configuration: CompilerConfiguration ): JavaCoreApplicationEnvironment { synchronized(APPLICATION_LOCK) { @@ -614,6 +615,7 @@ class KotlinCoreEnvironment private constructor( ExtraImportsProviderExtension.registerExtensionPoint(project) IrGenerationExtension.registerExtensionPoint(project) ScriptEvaluationExtension.registerExtensionPoint(project) + ShellExtension.registerExtensionPoint(project) } internal fun registerExtensionsFromPlugins(project: MockProject, configuration: CompilerConfiguration) { diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/plugins/ServiceLoaderLite.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/plugins/ServiceLoaderLite.kt index f387ee771f1..f2559ac4a06 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/plugins/ServiceLoaderLite.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/plugins/ServiceLoaderLite.kt @@ -9,9 +9,6 @@ import java.io.File import java.io.IOError import java.lang.Character.isJavaIdentifierPart import java.lang.Character.isJavaIdentifierStart -import java.lang.IllegalArgumentException -import java.lang.RuntimeException -import java.lang.UnsupportedOperationException import java.net.URLClassLoader import java.nio.file.FileSystemNotFoundException import java.nio.file.Paths @@ -44,6 +41,10 @@ object ServiceLoaderLite { } } + return loadImplementations(service, files, classLoader) + } + + fun loadImplementations(service: Class, files: List, classLoader: ClassLoader): MutableList { val implementations = mutableListOf() for (className in findImplementations(service, files)) { diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/ConsoleReplConfiguration.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/ConsoleReplConfiguration.kt deleted file mode 100644 index d13f726fd17..00000000000 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/ConsoleReplConfiguration.kt +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.cli.jvm.repl.configuration - -import org.jetbrains.kotlin.cli.jvm.repl.ReplExceptionReporter -import org.jetbrains.kotlin.cli.jvm.repl.messages.ConsoleDiagnosticMessageHolder -import org.jetbrains.kotlin.cli.jvm.repl.reader.ConsoleReplCommandReader -import org.jetbrains.kotlin.cli.jvm.repl.writer.ConsoleReplWriter - -class ConsoleReplConfiguration : ReplConfiguration { - override val writer = ConsoleReplWriter() - - override val exceptionReporter - get() = ReplExceptionReporter - - override val commandReader = ConsoleReplCommandReader() - - override val allowIncompleteLines: Boolean - get() = true - - override val executionInterceptor - get() = SnippetExecutionInterceptor - - override fun createDiagnosticHolder() = ConsoleDiagnosticMessageHolder() -} diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/ReplConfiguration.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/ReplConfiguration.kt deleted file mode 100644 index 773eedf1cf1..00000000000 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/ReplConfiguration.kt +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.cli.jvm.repl.configuration - -import org.jetbrains.kotlin.cli.jvm.repl.ReplExceptionReporter -import org.jetbrains.kotlin.cli.jvm.repl.messages.* -import org.jetbrains.kotlin.cli.jvm.repl.reader.ReplCommandReader -import org.jetbrains.kotlin.cli.jvm.repl.writer.ReplWriter - -interface ReplConfiguration { - val writer: ReplWriter - val exceptionReporter: ReplExceptionReporter - val commandReader: ReplCommandReader - val allowIncompleteLines: Boolean - - val executionInterceptor: SnippetExecutionInterceptor - fun createDiagnosticHolder(): DiagnosticMessageHolder -} diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/SnippetExecutionInterceptor.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/SnippetExecutionInterceptor.kt deleted file mode 100644 index 24df3455b6d..00000000000 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/SnippetExecutionInterceptor.kt +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.cli.jvm.repl.configuration - -interface SnippetExecutionInterceptor { - fun execute(block: () -> T): T - - companion object Plain : SnippetExecutionInterceptor { - override fun execute(block: () -> T) = block() - } -} diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/reader/IdeReplCommandReader.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/reader/IdeReplCommandReader.kt deleted file mode 100644 index 836c03c2328..00000000000 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/reader/IdeReplCommandReader.kt +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.cli.jvm.repl.reader - -import org.jetbrains.kotlin.cli.jvm.repl.ReplFromTerminal - -class IdeReplCommandReader : ReplCommandReader { - override fun readLine(next: ReplFromTerminal.WhatNextAfterOneLine) = readLine() - override fun flushHistory() = Unit -} diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/reader/ReplCommandReader.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/reader/ReplCommandReader.kt deleted file mode 100644 index 267f6b2d7bc..00000000000 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/reader/ReplCommandReader.kt +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.cli.jvm.repl.reader - -import org.jetbrains.kotlin.cli.jvm.repl.ReplFromTerminal - -interface ReplCommandReader { - fun readLine(next: ReplFromTerminal.WhatNextAfterOneLine): String? - fun flushHistory() -} diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/ConsoleReplWriter.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/ConsoleReplWriter.kt deleted file mode 100644 index 51db30aa32f..00000000000 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/ConsoleReplWriter.kt +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.cli.jvm.repl.writer - -class ConsoleReplWriter : ReplWriter { - override fun printlnWelcomeMessage(x: String) = println(x) - override fun printlnHelpMessage(x: String) = println(x) - override fun outputCompileError(x: String) = println(x) - override fun outputCommandResult(x: String) = println(x) - override fun outputRuntimeError(x: String) = println(x) - - override fun notifyReadLineStart() {} - override fun notifyReadLineEnd() {} - override fun notifyIncomplete() {} - override fun notifyCommandSuccess() {} - override fun sendInternalErrorReport(x: String) {} -} diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/ReplWriter.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/ReplWriter.kt deleted file mode 100644 index 3cd3cd69e3e..00000000000 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/ReplWriter.kt +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2010-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.cli.jvm.repl.writer - -interface ReplWriter { - fun printlnWelcomeMessage(x: String) - fun printlnHelpMessage(x: String) - fun outputCommandResult(x: String) - fun notifyReadLineStart() - fun notifyReadLineEnd() - fun notifyIncomplete() - fun notifyCommandSuccess() - fun outputCompileError(x: String) - fun outputRuntimeError(x: String) - fun sendInternalErrorReport(x: String) -} diff --git a/compiler/daemon/build.gradle.kts b/compiler/daemon/build.gradle.kts index 9c9d15b5102..e450b12197a 100644 --- a/compiler/daemon/build.gradle.kts +++ b/compiler/daemon/build.gradle.kts @@ -11,7 +11,6 @@ dependencies { compile(project(":kotlin-build-common")) compile(commonDep("org.fusesource.jansi", "jansi")) compile(commonDep("org.jline", "jline")) - compileOnly(project(":kotlin-scripting-compiler")) compileOnly(intellijCoreDep()) { includeJars("intellij-core") } compileOnly(intellijDep()) { includeIntellijCoreJarDependencies(project) } } diff --git a/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt b/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt index bc44d270e42..a5395103949 100644 --- a/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt +++ b/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt @@ -608,7 +608,7 @@ class CompileServiceImpl( ) val messageCollector = KeepFirstErrorMessageCollector(compilerMessagesStream) val repl = KotlinJvmReplService( - disposable, port, templateClasspath, templateClassName, + disposable, port, compilerId, templateClasspath, templateClassName, messageCollector, operationsTracer ) val sessionId = state.sessions.leaseSession(ClientOrSessionProxy(aliveFlagPath, repl, disposable)) @@ -661,7 +661,7 @@ class CompileServiceImpl( val disposable = Disposer.newDisposable() val messageCollector = CompileServicesFacadeMessageCollector(servicesFacade, compilationOptions) val repl = KotlinJvmReplService( - disposable, port, templateClasspath, templateClassName, + disposable, port, compilerId, templateClasspath, templateClassName, messageCollector, null ) val sessionId = state.sessions.leaseSession(ClientOrSessionProxy(aliveFlagPath, repl, disposable)) diff --git a/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinRemoteReplService.kt b/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinRemoteReplService.kt index 599f2a0e791..5177786847b 100644 --- a/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinRemoteReplService.kt +++ b/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinRemoteReplService.kt @@ -17,20 +17,18 @@ package org.jetbrains.kotlin.daemon import com.intellij.openapi.Disposable +import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys +import org.jetbrains.kotlin.cli.common.extensions.ReplFactoryExtension import org.jetbrains.kotlin.cli.common.messages.* -import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.ERROR -import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.INFO import org.jetbrains.kotlin.cli.common.repl.* +import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots -import org.jetbrains.kotlin.cli.jvm.repl.GenericReplCompiler -import org.jetbrains.kotlin.cli.jvm.repl.GenericReplCompilerState +import org.jetbrains.kotlin.cli.jvm.plugins.ServiceLoaderLite import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar import org.jetbrains.kotlin.config.* import org.jetbrains.kotlin.daemon.common.CompileService +import org.jetbrains.kotlin.daemon.common.CompilerId import org.jetbrains.kotlin.daemon.common.RemoteOperationsTracer -import org.jetbrains.kotlin.script.KotlinScriptDefinition -import org.jetbrains.kotlin.script.KotlinScriptDefinitionFromAnnotatedTemplate -import org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationComponentRegistrar import org.jetbrains.kotlin.utils.PathUtil import java.io.File import java.io.PrintStream @@ -38,12 +36,14 @@ import java.net.URLClassLoader import java.util.* import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.locks.ReentrantReadWriteLock +import java.util.logging.Logger import kotlin.concurrent.read import kotlin.concurrent.write open class KotlinJvmReplService( disposable: Disposable, val portForServers: Int, + val compilerId: CompilerId, templateClasspath: List, templateClassName: String, protected val messageCollector: MessageCollector, @@ -51,40 +51,46 @@ open class KotlinJvmReplService( protected val operationsTracer: RemoteOperationsTracer? ) : ReplCompileAction, ReplCheckAction, CreateReplStageStateAction { + private val log by lazy { Logger.getLogger("replService") } + protected val configuration = CompilerConfiguration().apply { + put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector) addJvmClasspathRoots(PathUtil.kotlinPathsForCompiler.let { listOf(it.stdlibPath, it.reflectPath, it.scriptRuntimePath) }) addJvmClasspathRoots(templateClasspath) put(CommonConfigurationKeys.MODULE_NAME, "kotlin-script") languageVersionSettings = LanguageVersionSettingsImpl( LanguageVersion.LATEST_STABLE, ApiVersion.LATEST_STABLE, mapOf(AnalysisFlags.skipMetadataVersionCheck to true) ) - configureScripting() + configureScripting(compilerId) } - protected fun makeScriptDefinition(templateClasspath: List, templateClassName: String): KotlinScriptDefinition? { - val classloader = URLClassLoader(templateClasspath.map { it.toURI().toURL() }.toTypedArray(), this::class.java.classLoader) - - try { - val cls = classloader.loadClass(templateClassName) - val def = KotlinScriptDefinitionFromAnnotatedTemplate(cls.kotlin, emptyMap()) - messageCollector.report(INFO, "New script definition $templateClassName: files pattern = \"${def.scriptFilePattern}\", " + - "resolver = ${def.dependencyResolver.javaClass.name}") - return def - } - catch (ex: ClassNotFoundException) { - messageCollector.report(ERROR, "Cannot find script definition template class $templateClassName") - } - catch (ex: Exception) { - messageCollector.report(ERROR, "Error processing script definition template $templateClassName: ${ex.message}") - } - return null - } - - private val scriptDef = makeScriptDefinition(templateClasspath, templateClassName) - private val replCompiler: ReplCompiler? by lazy { - if (scriptDef == null) null - else GenericReplCompiler(disposable, scriptDef, configuration, messageCollector) + try { + val projectEnvironment = + KotlinCoreEnvironment.ProjectEnvironment( + disposable, + KotlinCoreEnvironment.getOrCreateApplicationEnvironmentForProduction(disposable, configuration) + ) + ReplFactoryExtension.registerExtensionPoint(projectEnvironment.project) + projectEnvironment.registerExtensionsFromPlugins(configuration) + val replFactories = ReplFactoryExtension.getInstances(projectEnvironment.project) + if (replFactories.isEmpty()) { + throw java.lang.IllegalStateException("no scripting plugin loaded") + } else if (replFactories.size > 1) { + throw java.lang.IllegalStateException("several scripting plugins loaded") + } + + replFactories.first().makeReplCompiler( + templateClassName, + templateClasspath, + this::class.java.classLoader, + configuration, + projectEnvironment + ) + } catch (ex: Throwable) { + messageCollector.report(CompilerMessageSeverity.ERROR, "Unable to construct repl compiler: ${ex.message}") + throw IllegalStateException("Unable to use scripting/REPL in the daemon: ${ex.message}", ex) + } } protected val statesLock = ReentrantReadWriteLock() @@ -125,7 +131,7 @@ open class KotlinJvmReplService( fun createRemoteState(port: Int = portForServers): RemoteReplStateFacadeServer = statesLock.write { val id = getValidId(stateIdCounter) { id -> states.none { it.key.getId() == id} } - val stateFacade = RemoteReplStateFacadeServer(id, createState().asState(GenericReplCompilerState::class.java), port) + val stateFacade = RemoteReplStateFacadeServer(id, createState(), port) states.put(stateFacade, true) stateFacade } @@ -175,9 +181,15 @@ inline internal fun getValidId(counter: AtomicInteger, check: (Int) -> Boolean): return newId } -private fun CompilerConfiguration.configureScripting() { +private fun CompilerConfiguration.configureScripting(compilerId: CompilerId) { val error = try { - add(ComponentRegistrar.PLUGIN_COMPONENT_REGISTRARS, ScriptingCompilerConfigurationComponentRegistrar()) + val componentRegistrars = + (this::class.java.classLoader as? URLClassLoader)?.let { + ServiceLoaderLite.loadImplementations(ComponentRegistrar::class.java, it) + } ?: ServiceLoaderLite.loadImplementations( + ComponentRegistrar::class.java, compilerId.compilerClasspath.map(::File), this::class.java.classLoader + ) + addAll(ComponentRegistrar.PLUGIN_COMPONENT_REGISTRARS, componentRegistrars) null } catch (e: NoClassDefFoundError) { e diff --git a/compiler/daemon/src/org/jetbrains/kotlin/daemon/RemoteReplStateFacadeImpl.kt b/compiler/daemon/src/org/jetbrains/kotlin/daemon/RemoteReplStateFacadeImpl.kt index 75ad0f3578e..f8674e3fc41 100644 --- a/compiler/daemon/src/org/jetbrains/kotlin/daemon/RemoteReplStateFacadeImpl.kt +++ b/compiler/daemon/src/org/jetbrains/kotlin/daemon/RemoteReplStateFacadeImpl.kt @@ -17,15 +17,16 @@ package org.jetbrains.kotlin.daemon import org.jetbrains.kotlin.cli.common.repl.ILineId -import org.jetbrains.kotlin.cli.jvm.repl.GenericReplCompilerState +import org.jetbrains.kotlin.cli.common.repl.IReplStageState import org.jetbrains.kotlin.daemon.common.LoopbackNetworkInterface import org.jetbrains.kotlin.daemon.common.ReplStateFacade import org.jetbrains.kotlin.daemon.common.SOCKET_ANY_FREE_PORT import java.rmi.server.UnicastRemoteObject -class RemoteReplStateFacadeServer(val _id: Int, - val state: GenericReplCompilerState, - port: Int = SOCKET_ANY_FREE_PORT +class RemoteReplStateFacadeServer( + val _id: Int, + val state: IReplStageState<*>, + port: Int = SOCKET_ANY_FREE_PORT ) : ReplStateFacade, UnicastRemoteObject(port, LoopbackNetworkInterface.clientLoopbackSocketFactory, LoopbackNetworkInterface.serverLoopbackSocketFactory) { diff --git a/compiler/tests-java8/build.gradle.kts b/compiler/tests-java8/build.gradle.kts index b3a1b610d1a..70ecde2a84e 100644 --- a/compiler/tests-java8/build.gradle.kts +++ b/compiler/tests-java8/build.gradle.kts @@ -6,6 +6,7 @@ plugins { } dependencies { + testCompile(project(":kotlin-scripting-compiler-impl")) testCompile(projectTests(":compiler:tests-common")) testCompileOnly(intellijCoreDep()) { includeJars("intellij-core") } testCompile(projectTests(":generators:test-generator")) diff --git a/compiler/tests-java8/tests/org/jetbrains/kotlin/jvm/repl/ReplCompilerJava8Test.kt b/compiler/tests-java8/tests/org/jetbrains/kotlin/jvm/repl/ReplCompilerJava8Test.kt index 258cab7ffe5..ccdeafed8dc 100644 --- a/compiler/tests-java8/tests/org/jetbrains/kotlin/jvm/repl/ReplCompilerJava8Test.kt +++ b/compiler/tests-java8/tests/org/jetbrains/kotlin/jvm/repl/ReplCompilerJava8Test.kt @@ -25,14 +25,14 @@ import org.jetbrains.kotlin.cli.common.repl.ReplCompileResult import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler -import org.jetbrains.kotlin.cli.jvm.repl.GenericReplCompiler -import org.jetbrains.kotlin.cli.jvm.repl.KOTLIN_REPL_JVM_TARGET_PROPERTY import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.config.JVMConfigurationKeys import org.jetbrains.kotlin.config.JvmTarget import org.jetbrains.kotlin.integration.KotlinIntegrationTestBase import org.jetbrains.kotlin.script.StandardScriptDefinition import org.jetbrains.kotlin.script.loadScriptingPlugin +import org.jetbrains.kotlin.scripting.repl.GenericReplCompiler +import org.jetbrains.kotlin.scripting.repl.KOTLIN_REPL_JVM_TARGET_PROPERTY import org.jetbrains.kotlin.test.ConfigurationKind import org.jetbrains.kotlin.test.KotlinTestUtils import org.jetbrains.kotlin.test.TestJdkKind diff --git a/compiler/tests/org/jetbrains/kotlin/cli/jvm/repl/GenericReplTest.kt b/compiler/tests/org/jetbrains/kotlin/cli/jvm/repl/GenericReplTest.kt index c86fc3a007f..980d0583d45 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/jvm/repl/GenericReplTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/cli/jvm/repl/GenericReplTest.kt @@ -29,6 +29,7 @@ import org.jetbrains.kotlin.integration.KotlinIntegrationTestBase import org.jetbrains.kotlin.script.KotlinScriptDefinition import org.jetbrains.kotlin.script.KotlinScriptDefinitionFromAnnotatedTemplate import org.jetbrains.kotlin.script.loadScriptingPlugin +import org.jetbrains.kotlin.scripting.repl.GenericReplCompiler import org.jetbrains.kotlin.test.ConfigurationKind import org.jetbrains.kotlin.test.KotlinTestUtils import org.jetbrains.kotlin.test.TestJdkKind diff --git a/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt b/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt index db3f4a6f304..81ac6367506 100644 --- a/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt @@ -39,9 +39,12 @@ import java.rmi.server.UnicastRemoteObject import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit import kotlin.concurrent.thread -import kotlin.script.dependencies.* -import kotlin.script.experimental.dependencies.* +import kotlin.script.dependencies.Environment +import kotlin.script.dependencies.ScriptContents +import kotlin.script.experimental.dependencies.DependenciesResolver import kotlin.script.experimental.dependencies.DependenciesResolver.ResolveResult +import kotlin.script.experimental.dependencies.ScriptDependencies +import kotlin.script.experimental.dependencies.asSuccess import kotlin.script.templates.ScriptTemplateDefinition import kotlin.test.fail @@ -727,16 +730,17 @@ class CompilerDaemonTest : KotlinIntegrationTestBase() { daemon, null, CompileService.TargetPlatform.JVM, emptyArray(), TestMessageCollector(), classpathFromClassloader(), ScriptWithNoParam::class.qualifiedName!! ) + repl.createState() } catch (e: Exception) { TestCase.assertEquals( - "Unable to use scripting/REPL in the daemon, no kotlin-scripting-compiler.jar or its dependencies are found in the compiler classpath", + "Unable to use scripting/REPL in the daemon: no scripting plugin loaded", e.message ) isErrorThrown = true } finally { repl?.dispose() } - TestCase.assertTrue("Expecting exception that kotlin-scripting-plugin is not found in the classpath", isErrorThrown) + TestCase.assertTrue("Expecting exception that scripting plugin is not loaded", isErrorThrown) } } diff --git a/compiler/tests/org/jetbrains/kotlin/repl/AbstractReplInterpreterTest.kt b/compiler/tests/org/jetbrains/kotlin/repl/AbstractReplInterpreterTest.kt index 5db000622b8..2720e83954d 100644 --- a/compiler/tests/org/jetbrains/kotlin/repl/AbstractReplInterpreterTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/repl/AbstractReplInterpreterTest.kt @@ -18,9 +18,9 @@ package org.jetbrains.kotlin.repl import com.intellij.openapi.util.text.StringUtil import org.jetbrains.kotlin.cli.common.repl.ReplEvalResult -import org.jetbrains.kotlin.cli.jvm.repl.configuration.ConsoleReplConfiguration -import org.jetbrains.kotlin.cli.jvm.repl.ReplInterpreter import org.jetbrains.kotlin.script.loadScriptingPlugin +import org.jetbrains.kotlin.scripting.repl.ReplInterpreter +import org.jetbrains.kotlin.scripting.repl.configuration.ConsoleReplConfiguration import org.jetbrains.kotlin.test.ConfigurationKind import org.jetbrains.kotlin.test.KotlinTestUtils import org.jetbrains.kotlin.test.TestJdkKind @@ -86,7 +86,10 @@ abstract class AbstractReplInterpreterTest : KtUsefulTestCase() { protected fun doTest(path: String) { val configuration = KotlinTestUtils.newConfiguration(ConfigurationKind.ALL, TestJdkKind.MOCK_JDK) loadScriptingPlugin(configuration) - val repl = ReplInterpreter(testRootDisposable, configuration, ConsoleReplConfiguration()) + val repl = ReplInterpreter( + testRootDisposable, configuration, + ConsoleReplConfiguration() + ) for ((code, expected) in loadLines(File(path))) { val lineResult = repl.eval(code) diff --git a/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/jsr223/KotlinJsr223JvmLocalScriptEngine.kt b/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/jsr223/KotlinJsr223JvmLocalScriptEngine.kt index f5077c7b998..fde4b81f276 100644 --- a/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/jsr223/KotlinJsr223JvmLocalScriptEngine.kt +++ b/libraries/tools/kotlin-script-util/src/main/kotlin/org/jetbrains/kotlin/script/jsr223/KotlinJsr223JvmLocalScriptEngine.kt @@ -21,12 +21,12 @@ import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector import org.jetbrains.kotlin.cli.common.repl.* import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots import org.jetbrains.kotlin.cli.jvm.config.addJvmSdkRoots -import org.jetbrains.kotlin.cli.jvm.repl.GenericReplCompiler import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar import org.jetbrains.kotlin.config.* import org.jetbrains.kotlin.script.KotlinScriptDefinition import org.jetbrains.kotlin.script.KotlinScriptDefinitionFromAnnotatedTemplate import org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationComponentRegistrar +import org.jetbrains.kotlin.scripting.repl.GenericReplCompiler import org.jetbrains.kotlin.utils.PathUtil import java.io.File import java.net.URLClassLoader diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/DelegatePackageMemberDeclarationProvider.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/DelegatePackageMemberDeclarationProvider.kt similarity index 98% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/DelegatePackageMemberDeclarationProvider.kt rename to plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/DelegatePackageMemberDeclarationProvider.kt index 0301f58140e..0b2a54b12b8 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/DelegatePackageMemberDeclarationProvider.kt +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/DelegatePackageMemberDeclarationProvider.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.jetbrains.kotlin.cli.jvm.repl +package org.jetbrains.kotlin.scripting.repl import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.KtDestructuringDeclarationEntry diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericCompilerState.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/GenericCompilerState.kt similarity index 64% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericCompilerState.kt rename to plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/GenericCompilerState.kt index 20da9d88104..84046c6d549 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericCompilerState.kt +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/GenericCompilerState.kt @@ -1,30 +1,19 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2019 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.cli.jvm.repl +package org.jetbrains.kotlin.scripting.repl import org.jetbrains.kotlin.cli.common.repl.* import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -import org.jetbrains.kotlin.cli.jvm.repl.messages.DiagnosticMessageHolder import org.jetbrains.kotlin.descriptors.ScriptDescriptor import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.scripting.repl.messages.DiagnosticMessageHolder import java.util.concurrent.locks.ReentrantReadWriteLock import kotlin.script.experimental.dependencies.ScriptDependencies -class ReplCompilerStageHistory(private val state: GenericReplCompilerState) : BasicReplStageHistory(state.lock) { +class ReplCompilerStageHistory(private val state: org.jetbrains.kotlin.scripting.repl.GenericReplCompilerState) : BasicReplStageHistory(state.lock) { override fun reset(): Iterable { val removedCompiledLines = super.reset() @@ -60,17 +49,17 @@ abstract class GenericReplCheckerState : IReplStageState { val errorHolder: DiagnosticMessageHolder ) - var lastLineState: LineState? = null // for transferring state to the compiler in most typical case + var lastLineState: org.jetbrains.kotlin.scripting.repl.GenericReplCheckerState.LineState? = null // for transferring state to the compiler in most typical case } class GenericReplCompilerState(environment: KotlinCoreEnvironment, override val lock: ReentrantReadWriteLock = ReentrantReadWriteLock()) : - IReplStageState, GenericReplCheckerState() { + IReplStageState, org.jetbrains.kotlin.scripting.repl.GenericReplCheckerState() { - override val history = ReplCompilerStageHistory(this) + override val history = org.jetbrains.kotlin.scripting.repl.ReplCompilerStageHistory(this) override val currentGeneration: Int get() = (history as BasicReplStageHistory<*>).currentGeneration.get() - val analyzerEngine = ReplCodeAnalyzer(environment) + val analyzerEngine = org.jetbrains.kotlin.scripting.repl.ReplCodeAnalyzer(environment) var lastDependencies: ScriptDependencies? = null } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplChecker.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/GenericReplChecker.kt similarity index 80% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplChecker.kt rename to plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/GenericReplChecker.kt index 349f8b606b3..0db174361ab 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplChecker.kt +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/GenericReplChecker.kt @@ -1,20 +1,9 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2019 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.cli.jvm.repl +package org.jetbrains.kotlin.scripting.repl import com.intellij.openapi.Disposable import com.intellij.openapi.util.text.StringUtil @@ -28,7 +17,6 @@ import org.jetbrains.kotlin.cli.common.messages.MessageCollector import org.jetbrains.kotlin.cli.common.repl.* import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -import org.jetbrains.kotlin.cli.jvm.repl.messages.ConsoleDiagnosticMessageHolder import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.config.JVMConfigurationKeys import org.jetbrains.kotlin.config.JvmTarget @@ -36,6 +24,7 @@ import org.jetbrains.kotlin.idea.KotlinLanguage import org.jetbrains.kotlin.parsing.KotlinParserDefinition import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.script.KotlinScriptDefinition +import org.jetbrains.kotlin.scripting.repl.messages.ConsoleDiagnosticMessageHolder import kotlin.concurrent.write const val KOTLIN_REPL_JVM_TARGET_PROPERTY = "kotlin.repl.jvm.target" @@ -69,7 +58,7 @@ open class GenericReplChecker( override fun check(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCheckResult { state.lock.write { - val checkerState = state.asState(GenericReplCheckerState::class.java) + val checkerState = state.asState(org.jetbrains.kotlin.scripting.repl.GenericReplCheckerState::class.java) val scriptFileName = makeScriptBaseName(codeLine) val virtualFile = LightVirtualFile( @@ -87,7 +76,8 @@ open class GenericReplChecker( val syntaxErrorReport = AnalyzerWithCompilerReport.reportSyntaxErrors(psiFile, errorHolder) if (!syntaxErrorReport.isHasErrors) { - checkerState.lastLineState = GenericReplCheckerState.LineState(codeLine, psiFile, errorHolder) + checkerState.lastLineState = + org.jetbrains.kotlin.scripting.repl.GenericReplCheckerState.LineState(codeLine, psiFile, errorHolder) } return when { diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplCompiler.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/GenericReplCompiler.kt similarity index 85% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplCompiler.kt rename to plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/GenericReplCompiler.kt index ebd5b5e824b..eb880ec5ce6 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplCompiler.kt +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/GenericReplCompiler.kt @@ -1,20 +1,9 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2019 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.cli.jvm.repl +package org.jetbrains.kotlin.scripting.repl import com.intellij.openapi.Disposable import com.intellij.openapi.util.Disposer @@ -50,15 +39,17 @@ open class GenericReplCompiler( messageCollector: MessageCollector ) : this(Disposer.newDisposable(), scriptDefinition, compilerConfiguration, messageCollector) - private val checker = GenericReplChecker(disposable, scriptDefinition, compilerConfiguration, messageCollector) + private val checker = + GenericReplChecker(disposable, scriptDefinition, compilerConfiguration, messageCollector) - override fun createState(lock: ReentrantReadWriteLock): IReplStageState<*> = GenericReplCompilerState(checker.environment, lock) + override fun createState(lock: ReentrantReadWriteLock): IReplStageState<*> = + org.jetbrains.kotlin.scripting.repl.GenericReplCompilerState(checker.environment, lock) override fun check(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCheckResult = checker.check(state, codeLine) override fun compile(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCompileResult { state.lock.write { - val compilerState = state.asState(GenericReplCompilerState::class.java) + val compilerState = state.asState(org.jetbrains.kotlin.scripting.repl.GenericReplCompilerState::class.java) val (psiFile, errorHolder) = run { if (compilerState.lastLineState == null || compilerState.lastLineState!!.codeLine != codeLine) { diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplCodeAnalyzer.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/ReplCodeAnalyzer.kt similarity index 89% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplCodeAnalyzer.kt rename to plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/ReplCodeAnalyzer.kt index 0a477661bad..317c4817729 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplCodeAnalyzer.kt +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/ReplCodeAnalyzer.kt @@ -1,22 +1,11 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2019 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. */ @file:Suppress("UNUSED_PARAMETER") -package org.jetbrains.kotlin.cli.jvm.repl +package org.jetbrains.kotlin.scripting.repl import org.jetbrains.kotlin.cli.common.repl.CompiledReplCodeLine import org.jetbrains.kotlin.cli.common.repl.ILineId @@ -85,7 +74,8 @@ class ReplCodeAnalyzer(environment: KotlinCoreEnvironment) { data class Successful(override val scriptDescriptor: ClassDescriptorWithResolutionScopes, override val diagnostics: Diagnostics) : ReplLineAnalysisResult - data class WithErrors(override val diagnostics: Diagnostics) : ReplLineAnalysisResult { + data class WithErrors(override val diagnostics: Diagnostics) : + ReplLineAnalysisResult { override val scriptDescriptor: ClassDescriptorWithResolutionScopes? get() = null } } @@ -133,7 +123,10 @@ class ReplCodeAnalyzer(environment: KotlinCoreEnvironment) { try { rootPackageProvider.addDelegateProvider(provider) } catch (e: UninitializedPropertyAccessException) { - rootPackageProvider = AdaptablePackageMemberDeclarationProvider(provider) + rootPackageProvider = + AdaptablePackageMemberDeclarationProvider( + provider + ) } } @@ -155,7 +148,7 @@ class ReplCodeAnalyzer(environment: KotlinCoreEnvironment) { class AdaptablePackageMemberDeclarationProvider( private var delegateProvider: PackageMemberDeclarationProvider - ) : DelegatePackageMemberDeclarationProvider(delegateProvider) { + ) : org.jetbrains.kotlin.scripting.repl.DelegatePackageMemberDeclarationProvider(delegateProvider) { fun addDelegateProvider(provider: PackageMemberDeclarationProvider) { delegateProvider = CombinedPackageMemberDeclarationProvider(listOf(provider, delegateProvider)) @@ -182,7 +175,10 @@ class ReplCodeAnalyzer(environment: KotlinCoreEnvironment) { } fun submitLine(ktFile: KtFile, codeLine: ReplCodeLine) { - val line = LineInfo.SubmittedLine(ktFile, successfulLines.lastValue()) + val line = LineInfo.SubmittedLine( + ktFile, + successfulLines.lastValue() + ) submittedLines[ktFile] = line ktFile.fileScopesCustomizer = object : FileScopesCustomizer { override fun createFileScopes(fileScopeFactory: FileScopeFactory): FileScopes { @@ -192,13 +188,20 @@ class ReplCodeAnalyzer(environment: KotlinCoreEnvironment) { } fun lineSuccess(ktFile: KtFile, codeLine: ReplCodeLine, scriptDescriptor: ClassDescriptorWithResolutionScopes) { - val successfulLine = LineInfo.SuccessfulLine(ktFile, successfulLines.lastValue(), scriptDescriptor) + val successfulLine = LineInfo.SuccessfulLine( + ktFile, + successfulLines.lastValue(), + scriptDescriptor + ) submittedLines[ktFile] = successfulLine successfulLines.add(CompiledReplCodeLine(ktFile.name, codeLine), successfulLine) } fun lineFailure(ktFile: KtFile, codeLine: ReplCodeLine) { - submittedLines[ktFile] = LineInfo.FailedLine(ktFile, successfulLines.lastValue()) + submittedLines[ktFile] = LineInfo.FailedLine( + ktFile, + successfulLines.lastValue() + ) } private fun lineInfo(ktFile: KtFile) = submittedLines[ktFile] diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplExceptionReporter.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/ReplExceptionReporter.kt similarity index 50% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplExceptionReporter.kt rename to plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/ReplExceptionReporter.kt index 88eb5a38db5..f392cdca185 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplExceptionReporter.kt +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/ReplExceptionReporter.kt @@ -1,22 +1,11 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2019 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.cli.jvm.repl +package org.jetbrains.kotlin.scripting.repl -import org.jetbrains.kotlin.cli.jvm.repl.writer.ReplWriter +import org.jetbrains.kotlin.scripting.repl.writer.ReplWriter import java.io.PrintWriter import java.io.StringWriter diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplFromTerminal.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/ReplFromTerminal.kt similarity index 87% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplFromTerminal.kt rename to plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/ReplFromTerminal.kt index 46cd6a2340d..878bdc89574 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplFromTerminal.kt +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/ReplFromTerminal.kt @@ -1,20 +1,9 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2019 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.cli.jvm.repl +package org.jetbrains.kotlin.scripting.repl import com.intellij.openapi.Disposable import com.intellij.openapi.util.io.FileUtil @@ -22,11 +11,11 @@ import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys import org.jetbrains.kotlin.cli.common.messages.GroupingMessageCollector import org.jetbrains.kotlin.cli.common.repl.ReplEvalResult import org.jetbrains.kotlin.cli.common.repl.replUnescapeLineBreaks -import org.jetbrains.kotlin.cli.jvm.repl.configuration.ConsoleReplConfiguration -import org.jetbrains.kotlin.cli.jvm.repl.configuration.IdeReplConfiguration -import org.jetbrains.kotlin.cli.jvm.repl.configuration.ReplConfiguration import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.config.KotlinCompilerVersion +import org.jetbrains.kotlin.scripting.repl.configuration.ConsoleReplConfiguration +import org.jetbrains.kotlin.scripting.repl.configuration.IdeReplConfiguration +import org.jetbrains.kotlin.scripting.repl.configuration.ReplConfiguration import java.io.File import java.io.IOException import java.io.PrintWriter diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/ReplInterpreter.kt similarity index 84% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.kt rename to plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/ReplInterpreter.kt index 0a1e1c9769c..18cf70d3943 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.kt +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/ReplInterpreter.kt @@ -1,20 +1,9 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2019 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.cli.jvm.repl +package org.jetbrains.kotlin.scripting.repl import com.intellij.openapi.Disposable import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys @@ -25,9 +14,9 @@ import org.jetbrains.kotlin.cli.common.messages.MessageRenderer import org.jetbrains.kotlin.cli.common.repl.* import org.jetbrains.kotlin.cli.jvm.config.JvmClasspathRoot import org.jetbrains.kotlin.cli.jvm.config.JvmModulePathRoot -import org.jetbrains.kotlin.cli.jvm.repl.configuration.ReplConfiguration import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.script.KotlinScriptDefinition +import org.jetbrains.kotlin.scripting.repl.configuration.ReplConfiguration import java.io.PrintWriter import java.net.URLClassLoader import java.util.concurrent.atomic.AtomicInteger @@ -81,7 +70,12 @@ class ReplInterpreter( // TODO: add script definition with project-based resolving for IDEA repl private val scriptCompiler: ReplCompiler by lazy { - GenericReplCompiler(disposable, REPL_LINE_AS_SCRIPT_DEFINITION, configuration, messageCollector) + GenericReplCompiler( + disposable, + REPL_LINE_AS_SCRIPT_DEFINITION, + configuration, + messageCollector + ) } private val scriptEvaluator: ReplFullEvaluator by lazy { GenericReplCompilingEvaluator(scriptCompiler, classpathRoots, classLoader, null, ReplRepeatingMode.REPEAT_ANY_PREVIOUS) diff --git a/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/configuration/ConsoleReplConfiguration.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/configuration/ConsoleReplConfiguration.kt new file mode 100644 index 00000000000..46a8245777d --- /dev/null +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/configuration/ConsoleReplConfiguration.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2010-2019 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.scripting.repl.configuration + +import org.jetbrains.kotlin.scripting.repl.ReplExceptionReporter +import org.jetbrains.kotlin.scripting.repl.messages.ConsoleDiagnosticMessageHolder +import org.jetbrains.kotlin.scripting.repl.reader.ConsoleReplCommandReader +import org.jetbrains.kotlin.scripting.repl.writer.ConsoleReplWriter + +class ConsoleReplConfiguration : ReplConfiguration { + override val writer = ConsoleReplWriter() + + override val exceptionReporter + get() = ReplExceptionReporter + + override val commandReader = ConsoleReplCommandReader() + + override val allowIncompleteLines: Boolean + get() = true + + override val executionInterceptor + get() = SnippetExecutionInterceptor + + override fun createDiagnosticHolder() = ConsoleDiagnosticMessageHolder() +} diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/IdeReplConfiguration.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/configuration/IdeReplConfiguration.kt similarity index 53% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/IdeReplConfiguration.kt rename to plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/configuration/IdeReplConfiguration.kt index aa8976f66e3..de90647ec41 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/IdeReplConfiguration.kt +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/configuration/IdeReplConfiguration.kt @@ -1,35 +1,25 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2019 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.cli.jvm.repl.configuration +package org.jetbrains.kotlin.scripting.repl.configuration -import org.jetbrains.kotlin.cli.jvm.repl.IdeReplExceptionReporter -import org.jetbrains.kotlin.cli.jvm.repl.ReplExceptionReporter -import org.jetbrains.kotlin.cli.jvm.repl.messages.IdeDiagnosticMessageHolder -import org.jetbrains.kotlin.cli.jvm.repl.reader.IdeReplCommandReader -import org.jetbrains.kotlin.cli.jvm.repl.reader.ReplCommandReader -import org.jetbrains.kotlin.cli.jvm.repl.reader.ReplSystemInWrapper -import org.jetbrains.kotlin.cli.jvm.repl.writer.IdeSystemOutWrapperReplWriter -import org.jetbrains.kotlin.cli.jvm.repl.writer.ReplWriter +import org.jetbrains.kotlin.scripting.repl.IdeReplExceptionReporter +import org.jetbrains.kotlin.scripting.repl.ReplExceptionReporter +import org.jetbrains.kotlin.scripting.repl.messages.IdeDiagnosticMessageHolder +import org.jetbrains.kotlin.scripting.repl.reader.IdeReplCommandReader +import org.jetbrains.kotlin.scripting.repl.reader.ReplCommandReader +import org.jetbrains.kotlin.scripting.repl.reader.ReplSystemInWrapper +import org.jetbrains.kotlin.scripting.repl.writer.IdeSystemOutWrapperReplWriter +import org.jetbrains.kotlin.scripting.repl.writer.ReplWriter class IdeReplConfiguration : ReplConfiguration { override val allowIncompleteLines: Boolean get() = false - override val executionInterceptor: SnippetExecutionInterceptor = object : SnippetExecutionInterceptor { + override val executionInterceptor: SnippetExecutionInterceptor = object : + SnippetExecutionInterceptor { override fun execute(block: () -> T): T { try { sinWrapper.isReplScriptExecuting = true diff --git a/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/configuration/ReplConfiguration.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/configuration/ReplConfiguration.kt new file mode 100644 index 00000000000..a3663d61a47 --- /dev/null +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/configuration/ReplConfiguration.kt @@ -0,0 +1,21 @@ +/* + * Copyright 2010-2019 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.scripting.repl.configuration + +import org.jetbrains.kotlin.scripting.repl.ReplExceptionReporter +import org.jetbrains.kotlin.scripting.repl.messages.DiagnosticMessageHolder +import org.jetbrains.kotlin.scripting.repl.reader.ReplCommandReader +import org.jetbrains.kotlin.scripting.repl.writer.ReplWriter + +interface ReplConfiguration { + val writer: ReplWriter + val exceptionReporter: ReplExceptionReporter + val commandReader: ReplCommandReader + val allowIncompleteLines: Boolean + + val executionInterceptor: SnippetExecutionInterceptor + fun createDiagnosticHolder(): DiagnosticMessageHolder +} diff --git a/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/configuration/SnippetExecutionInterceptor.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/configuration/SnippetExecutionInterceptor.kt new file mode 100644 index 00000000000..1c543514d75 --- /dev/null +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/configuration/SnippetExecutionInterceptor.kt @@ -0,0 +1,14 @@ +/* + * Copyright 2010-2019 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.scripting.repl.configuration + +interface SnippetExecutionInterceptor { + fun execute(block: () -> T): T + + companion object Plain : SnippetExecutionInterceptor { + override fun execute(block: () -> T) = block() + } +} diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ConsoleDiagnosticMessageHolder.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/messages/ConsoleDiagnosticMessageHolder.kt similarity index 56% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ConsoleDiagnosticMessageHolder.kt rename to plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/messages/ConsoleDiagnosticMessageHolder.kt index 6ce0b89b176..ed1c199c55d 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ConsoleDiagnosticMessageHolder.kt +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/messages/ConsoleDiagnosticMessageHolder.kt @@ -1,20 +1,9 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2019 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.cli.jvm.repl.messages +package org.jetbrains.kotlin.scripting.repl.messages import org.jetbrains.kotlin.cli.common.messages.GroupingMessageCollector import org.jetbrains.kotlin.cli.common.messages.MessageCollectorBasedReporter diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/DiagnosticMessageHolder.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/messages/DiagnosticMessageHolder.kt similarity index 93% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/DiagnosticMessageHolder.kt rename to plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/messages/DiagnosticMessageHolder.kt index 1de7294fbe5..cfd047c63d5 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/DiagnosticMessageHolder.kt +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/messages/DiagnosticMessageHolder.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.jetbrains.kotlin.cli.jvm.repl.messages +package org.jetbrains.kotlin.scripting.repl.messages import org.jetbrains.kotlin.cli.common.messages.DiagnosticMessageReporter diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/IdeDiagnosticMessageHolder.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/messages/IdeDiagnosticMessageHolder.kt similarity index 72% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/IdeDiagnosticMessageHolder.kt rename to plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/messages/IdeDiagnosticMessageHolder.kt index 4ab937297ff..bd839c576db 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/IdeDiagnosticMessageHolder.kt +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/messages/IdeDiagnosticMessageHolder.kt @@ -1,20 +1,9 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2019 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.cli.jvm.repl.messages +package org.jetbrains.kotlin.scripting.repl.messages import com.intellij.openapi.util.text.StringUtil import com.intellij.psi.PsiFile diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/reader/ConsoleReplCommandReader.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/reader/ConsoleReplCommandReader.kt similarity index 65% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/reader/ConsoleReplCommandReader.kt rename to plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/reader/ConsoleReplCommandReader.kt index 5a96585a5ca..f7008a54f4d 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/reader/ConsoleReplCommandReader.kt +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/reader/ConsoleReplCommandReader.kt @@ -1,22 +1,11 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2019 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.cli.jvm.repl.reader +package org.jetbrains.kotlin.scripting.repl.reader -import org.jetbrains.kotlin.cli.jvm.repl.ReplFromTerminal +import org.jetbrains.kotlin.scripting.repl.ReplFromTerminal import org.jline.reader.EndOfFileException import org.jline.reader.LineReader import org.jline.reader.LineReaderBuilder diff --git a/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/reader/IdeReplCommandReader.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/reader/IdeReplCommandReader.kt new file mode 100644 index 00000000000..2da9b544ebc --- /dev/null +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/reader/IdeReplCommandReader.kt @@ -0,0 +1,13 @@ +/* + * Copyright 2010-2019 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.scripting.repl.reader + +import org.jetbrains.kotlin.scripting.repl.ReplFromTerminal + +class IdeReplCommandReader : ReplCommandReader { + override fun readLine(next: ReplFromTerminal.WhatNextAfterOneLine) = readLine() + override fun flushHistory() = Unit +} diff --git a/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/reader/ReplCommandReader.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/reader/ReplCommandReader.kt new file mode 100644 index 00000000000..21d34a7bb87 --- /dev/null +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/reader/ReplCommandReader.kt @@ -0,0 +1,13 @@ +/* + * Copyright 2010-2019 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.scripting.repl.reader + +import org.jetbrains.kotlin.scripting.repl.ReplFromTerminal + +interface ReplCommandReader { + fun readLine(next: ReplFromTerminal.WhatNextAfterOneLine): String? + fun flushHistory() +} diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/reader/ReplSystemInWrapper.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/reader/ReplSystemInWrapper.kt similarity index 78% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/reader/ReplSystemInWrapper.kt rename to plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/reader/ReplSystemInWrapper.kt index 0d990e06089..01c1745f090 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/reader/ReplSystemInWrapper.kt +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/reader/ReplSystemInWrapper.kt @@ -1,23 +1,12 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2019 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.cli.jvm.repl.reader +package org.jetbrains.kotlin.scripting.repl.reader import org.jetbrains.kotlin.cli.common.repl.replUnescapeLineBreaks -import org.jetbrains.kotlin.cli.jvm.repl.writer.ReplWriter +import org.jetbrains.kotlin.scripting.repl.writer.ReplWriter import org.w3c.dom.Element import org.xml.sax.InputSource import java.io.ByteArrayInputStream diff --git a/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/writer/ConsoleReplWriter.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/writer/ConsoleReplWriter.kt new file mode 100644 index 00000000000..f7fe3ef25d9 --- /dev/null +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/writer/ConsoleReplWriter.kt @@ -0,0 +1,20 @@ +/* + * Copyright 2010-2019 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.scripting.repl.writer + +class ConsoleReplWriter : ReplWriter { + override fun printlnWelcomeMessage(x: String) = println(x) + override fun printlnHelpMessage(x: String) = println(x) + override fun outputCompileError(x: String) = println(x) + override fun outputCommandResult(x: String) = println(x) + override fun outputRuntimeError(x: String) = println(x) + + override fun notifyReadLineStart() {} + override fun notifyReadLineEnd() {} + override fun notifyIncomplete() {} + override fun notifyCommandSuccess() {} + override fun sendInternalErrorReport(x: String) {} +} diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/IdeSystemOutWrapperReplWriter.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/writer/IdeSystemOutWrapperReplWriter.kt similarity index 74% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/IdeSystemOutWrapperReplWriter.kt rename to plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/writer/IdeSystemOutWrapperReplWriter.kt index c1b89c0be14..04b3eb3591c 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/IdeSystemOutWrapperReplWriter.kt +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/writer/IdeSystemOutWrapperReplWriter.kt @@ -1,20 +1,9 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2019 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.cli.jvm.repl.writer +package org.jetbrains.kotlin.scripting.repl.writer import org.jetbrains.kotlin.cli.common.repl.replAddLineBreak import org.jetbrains.kotlin.cli.common.repl.replOutputAsXml @@ -22,7 +11,8 @@ import org.jetbrains.kotlin.utils.repl.ReplEscapeType import org.jetbrains.kotlin.utils.repl.ReplEscapeType.* import java.io.PrintStream -class IdeSystemOutWrapperReplWriter(standardOut: PrintStream) : PrintStream(standardOut, true), ReplWriter { +class IdeSystemOutWrapperReplWriter(standardOut: PrintStream) : PrintStream(standardOut, true), + ReplWriter { override fun print(x: Boolean) = printWithEscaping(x.toString()) override fun print(x: Char) = printWithEscaping(x.toString()) override fun print(x: Int) = printWithEscaping(x.toString()) diff --git a/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/writer/ReplWriter.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/writer/ReplWriter.kt new file mode 100644 index 00000000000..46429aaac44 --- /dev/null +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/repl/writer/ReplWriter.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2010-2019 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.scripting.repl.writer + +interface ReplWriter { + fun printlnWelcomeMessage(x: String) + fun printlnHelpMessage(x: String) + fun outputCommandResult(x: String) + fun notifyReadLineStart() + fun notifyReadLineEnd() + fun notifyIncomplete() + fun notifyCommandSuccess() + fun outputCompileError(x: String) + fun outputRuntimeError(x: String) + fun sendInternalErrorReport(x: String) +} diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/JvmCliReplShellExtension.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/JvmCliReplShellExtension.kt new file mode 100644 index 00000000000..e09b43296d3 --- /dev/null +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/JvmCliReplShellExtension.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2010-2019 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.scripting.compiler.plugin + +import com.intellij.core.JavaCoreProjectEnvironment +import org.jetbrains.kotlin.cli.common.ExitCode +import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments +import org.jetbrains.kotlin.cli.common.extensions.ShellExtension +import org.jetbrains.kotlin.config.CompilerConfiguration +import org.jetbrains.kotlin.scripting.repl.ReplFromTerminal + +class JvmCliReplShellExtension : ShellExtension { + + override fun isAccepted(arguments: CommonCompilerArguments): Boolean = true + + override fun run( + arguments: CommonCompilerArguments, + configuration: CompilerConfiguration, + projectEnvironment: JavaCoreProjectEnvironment + ): ExitCode { + ReplFromTerminal.run(projectEnvironment.parentDisposable, configuration) + return ExitCode.OK + } +} \ No newline at end of file diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/JvmStandardReplFactoryExtension.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/JvmStandardReplFactoryExtension.kt new file mode 100644 index 00000000000..9999c81b1f6 --- /dev/null +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/JvmStandardReplFactoryExtension.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2010-2019 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.scripting.compiler.plugin + +import com.intellij.core.JavaCoreProjectEnvironment +import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys +import org.jetbrains.kotlin.cli.common.extensions.ReplFactoryExtension +import org.jetbrains.kotlin.cli.common.repl.ReplCompiler +import org.jetbrains.kotlin.config.CompilerConfiguration +import org.jetbrains.kotlin.script.KotlinScriptDefinition +import org.jetbrains.kotlin.script.KotlinScriptDefinitionFromAnnotatedTemplate +import org.jetbrains.kotlin.scripting.repl.GenericReplCompiler +import java.io.File +import java.net.URLClassLoader + +class JvmStandardReplFactoryExtension : ReplFactoryExtension { + + override fun makeReplCompiler( + templateClassName: String, + templateClasspath: List, + baseClassLoader: ClassLoader?, + configuration: CompilerConfiguration, + projectEnvironment: JavaCoreProjectEnvironment + ): ReplCompiler = GenericReplCompiler( + projectEnvironment.parentDisposable, + makeScriptDefinition(templateClasspath, templateClassName, baseClassLoader), + configuration, + configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY) + ) + + private fun makeScriptDefinition( + templateClasspath: List, templateClassName: String, baseClassLoader: ClassLoader? + ): KotlinScriptDefinition = try { + val classloader = URLClassLoader(templateClasspath.map { it.toURI().toURL() }.toTypedArray(), baseClassLoader) + val cls = classloader.loadClass(templateClassName) + KotlinScriptDefinitionFromAnnotatedTemplate(cls.kotlin, emptyMap()) + } catch (ex: ClassNotFoundException) { + throw IllegalStateException("Cannot find script definition template class $templateClassName", ex) + } catch (ex: Exception) { + throw IllegalStateException("Error loading script definition template $templateClassName", ex) + } +} \ No newline at end of file diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/pluginRegisrar.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/pluginRegisrar.kt index d29dbc12434..876883515bf 100644 --- a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/pluginRegisrar.kt +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/pluginRegisrar.kt @@ -7,11 +7,14 @@ package org.jetbrains.kotlin.scripting.compiler.plugin import com.intellij.mock.MockProject import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys +import org.jetbrains.kotlin.cli.common.extensions.ReplFactoryExtension import org.jetbrains.kotlin.cli.common.extensions.ScriptEvaluationExtension +import org.jetbrains.kotlin.cli.common.extensions.ShellExtension import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.extensions.CollectAdditionalSourcesExtension import org.jetbrains.kotlin.extensions.CompilerConfigurationExtension +import org.jetbrains.kotlin.extensions.ProjectExtensionDescriptor import org.jetbrains.kotlin.resolve.extensions.ExtraImportsProviderExtension import org.jetbrains.kotlin.resolve.extensions.SyntheticResolveExtension import org.jetbrains.kotlin.script.ScriptDefinitionProvider @@ -23,11 +26,21 @@ import org.jetbrains.kotlin.scripting.legacy.CliScriptReportSink import org.jetbrains.kotlin.scripting.shared.extensions.ScriptExtraImportsProviderExtension import org.jetbrains.kotlin.scripting.shared.extensions.ScriptingResolveExtension +private fun ProjectExtensionDescriptor.registerExtensionIfRequired(project: MockProject, extension: T) { + try { + registerExtension(project, extension) + } catch (ex: IllegalArgumentException) { + // ignore + } +} + class ScriptingCompilerConfigurationComponentRegistrar : ComponentRegistrar { override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) { CompilerConfigurationExtension.registerExtension(project, ScriptingCompilerConfigurationExtension(project)) CollectAdditionalSourcesExtension.registerExtension(project, ScriptingCollectAdditionalSourcesExtension(project)) - ScriptEvaluationExtension.registerExtension(project, JvmCliScriptEvaluationExtension()) + ScriptEvaluationExtension.registerExtensionIfRequired(project, JvmCliScriptEvaluationExtension()) + ShellExtension.registerExtensionIfRequired(project, JvmCliReplShellExtension()) + ReplFactoryExtension.registerExtensionIfRequired(project, JvmStandardReplFactoryExtension()) val scriptDefinitionProvider = CliScriptDefinitionProvider() project.registerService(ScriptDefinitionProvider::class.java, scriptDefinitionProvider)