diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplChecker.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplChecker.kt index ca45e0eea3d..fecc29c1a2b 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplChecker.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplChecker.kt @@ -29,7 +29,7 @@ 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.ReplTerminalDiagnosticMessageHolder +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 @@ -43,8 +43,8 @@ const val KOTLIN_REPL_JVM_TARGET_PROPERTY = "kotlin.repl.jvm.target" open class GenericReplChecker( disposable: Disposable, - val scriptDefinition: KotlinScriptDefinition, - val compilerConfiguration: CompilerConfiguration, + private val scriptDefinition: KotlinScriptDefinition, + private val compilerConfiguration: CompilerConfiguration, messageCollector: MessageCollector ) : ReplCheckAction { @@ -65,7 +65,7 @@ open class GenericReplChecker( private val psiFileFactory: PsiFileFactoryImpl = PsiFileFactory.getInstance(environment.project) as PsiFileFactoryImpl - internal fun createDiagnosticHolder() = ReplTerminalDiagnosticMessageHolder() + private fun createDiagnosticHolder() = ConsoleDiagnosticMessageHolder() override fun check(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCheckResult { state.lock.write { @@ -88,7 +88,7 @@ open class GenericReplChecker( return when { syntaxErrorReport.isHasErrors && syntaxErrorReport.isAllErrorsAtEof -> ReplCheckResult.Incomplete() - syntaxErrorReport.isHasErrors -> ReplCheckResult.Error(errorHolder.renderedDiagnostics) + syntaxErrorReport.isHasErrors -> ReplCheckResult.Error(errorHolder.renderMessage()) else -> ReplCheckResult.Ok() } } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplCompiler.kt index 5cfdf6ecbf5..204eaa58969 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplCompiler.kt @@ -42,8 +42,8 @@ import kotlin.concurrent.write // WARNING: not thread safe, assuming external synchronization open class GenericReplCompiler(disposable: Disposable, - protected val scriptDefinition: KotlinScriptDefinition, - protected val compilerConfiguration: CompilerConfiguration, + scriptDefinition: KotlinScriptDefinition, + private val compilerConfiguration: CompilerConfiguration, messageCollector: MessageCollector ) : ReplCompiler { @@ -82,7 +82,7 @@ open class GenericReplCompiler(disposable: Disposable, val analysisResult = compilerState.analyzerEngine.analyzeReplLine(psiFile, codeLine) AnalyzerWithCompilerReport.reportDiagnostics(analysisResult.diagnostics, errorHolder) val scriptDescriptor = when (analysisResult) { - is ReplCodeAnalyzer.ReplLineAnalysisResult.WithErrors -> return ReplCompileResult.Error(errorHolder.renderedDiagnostics) + is ReplCodeAnalyzer.ReplLineAnalysisResult.WithErrors -> return ReplCompileResult.Error(errorHolder.renderMessage()) is ReplCodeAnalyzer.ReplLineAnalysisResult.Successful -> analysisResult.scriptDescriptor else -> error("Unexpected result ${analysisResult::class.java}") } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplExceptionReporter.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplExceptionReporter.kt index 3c842152df8..f0f8bfed260 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplExceptionReporter.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplExceptionReporter.kt @@ -16,7 +16,7 @@ package org.jetbrains.kotlin.cli.jvm.repl -import org.jetbrains.kotlin.cli.jvm.repl.messages.ReplWriter +import org.jetbrains.kotlin.cli.jvm.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/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplFromTerminal.kt index e321cce7d27..08bf805b0b8 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplFromTerminal.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplFromTerminal.kt @@ -21,6 +21,9 @@ import com.intellij.openapi.util.io.FileUtil 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.jvm.repl.configuration.ConsoleReplConfiguration +import org.jetbrains.kotlin.cli.jvm.repl.configuration.ReplConfiguration +import org.jetbrains.kotlin.cli.jvm.repl.configuration.IdeReplConfiguration import org.jetbrains.kotlin.cli.jvm.repl.messages.unescapeLineBreaks import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.config.KotlinCompilerVersion @@ -167,7 +170,7 @@ class ReplFromTerminal( fun run(disposable: Disposable, configuration: CompilerConfiguration) { val replIdeMode = System.getProperty("kotlin.repl.ideMode") == "true" - val replConfiguration = if (replIdeMode) ReplForIdeConfiguration() else ConsoleReplConfiguration() + val replConfiguration = if (replIdeMode) IdeReplConfiguration() else ConsoleReplConfiguration() return try { ReplFromTerminal(disposable, configuration, replConfiguration).doRun() } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.kt index 40069845b06..5600f6c6b11 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.kt @@ -24,6 +24,7 @@ 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.config.JVMConfigurationKeys import org.jetbrains.kotlin.script.KotlinScriptDefinition 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 new file mode 100644 index 00000000000..2043950a2a5 --- /dev/null +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/ConsoleReplConfiguration.kt @@ -0,0 +1,39 @@ +/* + * 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() +} \ No newline at end of file diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplConfiguration.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/IdeReplConfiguration.kt similarity index 62% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplConfiguration.kt rename to compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/IdeReplConfiguration.kt index b85dbf66edd..667d5422edb 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplConfiguration.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/IdeReplConfiguration.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * 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. @@ -14,24 +14,18 @@ * limitations under the License. */ -package org.jetbrains.kotlin.cli.jvm.repl +package org.jetbrains.kotlin.cli.jvm.repl.configuration -import org.jetbrains.kotlin.cli.jvm.repl.messages.* -import org.jetbrains.kotlin.cli.jvm.repl.reader.ConsoleReplCommandReader +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 -interface ReplConfiguration { - val writer: ReplWriter - val exceptionReporter: ReplExceptionReporter - val commandReader: ReplCommandReader - val allowIncompleteLines: Boolean - - val executionInterceptor: SnippetExecutionInterceptor - fun createDiagnosticHolder(): DiagnosticMessageHolder -} - -class ReplForIdeConfiguration : ReplConfiguration { +class IdeReplConfiguration : ReplConfiguration { override val allowIncompleteLines: Boolean get() = false @@ -46,7 +40,7 @@ class ReplForIdeConfiguration : ReplConfiguration { } } - override fun createDiagnosticHolder() = ReplIdeDiagnosticMessageHolder() + override fun createDiagnosticHolder() = IdeDiagnosticMessageHolder() override val writer: ReplWriter override val exceptionReporter: ReplExceptionReporter @@ -58,7 +52,7 @@ class ReplForIdeConfiguration : ReplConfiguration { // wrapper for `out` is required to escape every input in [ideMode]; // if [ideMode == false] then just redirects all input to [System.out] // if user calls [System.setOut(...)] then undefined behaviour - val soutWrapper = ReplSystemOutWrapperForIde(System.out) + val soutWrapper = IdeSystemOutWrapperReplWriter(System.out) System.setOut(soutWrapper) // wrapper for `in` is required to give user possibility of calling @@ -70,21 +64,4 @@ class ReplForIdeConfiguration : ReplConfiguration { exceptionReporter = IdeReplExceptionReporter(writer) commandReader = IdeReplCommandReader() } -} - -class ConsoleReplConfiguration : ReplConfiguration { - override val writer = ReplConsoleWriter() - - override val exceptionReporter - get() = ReplExceptionReporter.DoNothing - - override val commandReader = ConsoleReplCommandReader() - - override val allowIncompleteLines: Boolean - get() = true - - override val executionInterceptor - get() = SnippetExecutionInterceptor.Plain - - override fun createDiagnosticHolder() = ReplTerminalDiagnosticMessageHolder() } \ No newline at end of file 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 new file mode 100644 index 00000000000..02b303f3c80 --- /dev/null +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/ReplConfiguration.kt @@ -0,0 +1,32 @@ +/* + * 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 +} \ No newline at end of file diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/SnippetExecutionInterceptor.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/SnippetExecutionInterceptor.kt similarity index 93% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/SnippetExecutionInterceptor.kt rename to compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/SnippetExecutionInterceptor.kt index 79811a2e0c1..89aa72b982d 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/SnippetExecutionInterceptor.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/configuration/SnippetExecutionInterceptor.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.jetbrains.kotlin.cli.jvm.repl +package org.jetbrains.kotlin.cli.jvm.repl.configuration interface SnippetExecutionInterceptor { fun execute(block: () -> T): T diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ReplTerminalDiagnosticMessageHolder.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ConsoleDiagnosticMessageHolder.kt similarity index 76% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ReplTerminalDiagnosticMessageHolder.kt rename to compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ConsoleDiagnosticMessageHolder.kt index 7552516419d..629b2d15de9 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ReplTerminalDiagnosticMessageHolder.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ConsoleDiagnosticMessageHolder.kt @@ -22,20 +22,16 @@ import org.jetbrains.kotlin.cli.common.messages.MessageRenderer import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector import java.io.ByteArrayOutputStream import java.io.PrintStream -import java.nio.ByteBuffer -class ReplTerminalDiagnosticMessageHolder : MessageCollectorBasedReporter, DiagnosticMessageHolder { +class ConsoleDiagnosticMessageHolder : MessageCollectorBasedReporter, DiagnosticMessageHolder { private val outputStream = ByteArrayOutputStream() override val messageCollector: GroupingMessageCollector = GroupingMessageCollector( PrintingMessageCollector(PrintStream(outputStream), MessageRenderer.WITHOUT_PATHS, false), - false - ) + false) - override val renderedDiagnostics: String - get() { - messageCollector.flush() - val bytes = outputStream.toByteArray() - return Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString() - } + override fun renderMessage(): String { + messageCollector.flush() + return outputStream.toString("UTF-8") + } } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/DiagnosticMessageHolder.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/DiagnosticMessageHolder.kt index 362d9dc7202..c963717c91e 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/DiagnosticMessageHolder.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/DiagnosticMessageHolder.kt @@ -19,5 +19,5 @@ package org.jetbrains.kotlin.cli.jvm.repl.messages import org.jetbrains.kotlin.cli.common.messages.DiagnosticMessageReporter interface DiagnosticMessageHolder : DiagnosticMessageReporter { - val renderedDiagnostics: String + fun renderMessage(): String } \ No newline at end of file diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/IdeDiagnosticMessageHolder.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/IdeDiagnosticMessageHolder.kt new file mode 100644 index 00000000000..4ab937297ff --- /dev/null +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/IdeDiagnosticMessageHolder.kt @@ -0,0 +1,57 @@ +/* + * 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.messages + +import com.intellij.openapi.util.text.StringUtil +import com.intellij.psi.PsiFile +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.diagnostics.DiagnosticUtils +import org.w3c.dom.ls.DOMImplementationLS +import javax.xml.parsers.DocumentBuilderFactory + +class IdeDiagnosticMessageHolder : DiagnosticMessageHolder { + private val diagnostics = arrayListOf>() + + override fun report(diagnostic: Diagnostic, file: PsiFile, render: String) { + diagnostics.add(Pair(diagnostic, render)) + } + + override fun renderMessage(): String { + val docFactory = DocumentBuilderFactory.newInstance() + val docBuilder = docFactory.newDocumentBuilder() + val errorReport = docBuilder.newDocument() + + val rootElement = errorReport.createElement("report") + errorReport.appendChild(rootElement) + + for ((diagnostic, message) in diagnostics) { + val errorRange = DiagnosticUtils.firstRange(diagnostic.textRanges) + + val reportEntry = errorReport.createElement("reportEntry") + reportEntry.setAttribute("severity", diagnostic.severity.toString()) + reportEntry.setAttribute("rangeStart", errorRange.startOffset.toString()) + reportEntry.setAttribute("rangeEnd", errorRange.endOffset.toString()) + reportEntry.appendChild(errorReport.createTextNode(StringUtil.escapeXml(message))) + + rootElement.appendChild(reportEntry) + } + + val domImplementation = errorReport.implementation as DOMImplementationLS + val lsSerializer = domImplementation.createLSSerializer() + return lsSerializer.writeToString(errorReport) + } +} diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ReplIdeDiagnosticMessageHolder.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ReplIdeDiagnosticMessageHolder.kt deleted file mode 100644 index 3215474e171..00000000000 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ReplIdeDiagnosticMessageHolder.kt +++ /dev/null @@ -1,58 +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.messages - -import com.intellij.openapi.util.text.StringUtil -import com.intellij.psi.PsiFile -import org.jetbrains.kotlin.diagnostics.Diagnostic -import org.jetbrains.kotlin.diagnostics.DiagnosticUtils -import org.w3c.dom.ls.DOMImplementationLS -import javax.xml.parsers.DocumentBuilderFactory - -class ReplIdeDiagnosticMessageHolder : DiagnosticMessageHolder { - private val diagnostics = arrayListOf>() - - override fun report(diagnostic: Diagnostic, file: PsiFile, render: String) { - diagnostics.add(Pair(diagnostic, render)) - } - - override val renderedDiagnostics: String - get() { - val docFactory = DocumentBuilderFactory.newInstance() - val docBuilder = docFactory.newDocumentBuilder() - val errorReport = docBuilder.newDocument() - - val rootElement = errorReport.createElement("report") - errorReport.appendChild(rootElement) - - for ((diagnostic, message) in diagnostics) { - val errorRange = DiagnosticUtils.firstRange(diagnostic.textRanges) - - val reportEntry = errorReport.createElement("reportEntry") - reportEntry.setAttribute("severity", diagnostic.severity.toString()) - reportEntry.setAttribute("rangeStart", errorRange.startOffset.toString()) - reportEntry.setAttribute("rangeEnd", errorRange.endOffset.toString()) - reportEntry.appendChild(errorReport.createTextNode(StringUtil.escapeXml(message))) - - rootElement.appendChild(reportEntry) - } - - val domImplementation = errorReport.implementation as DOMImplementationLS - val lsSerializer = domImplementation.createLSSerializer() - return lsSerializer.writeToString(errorReport) - } -} diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/UnescapeUtils.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/UnescapeUtils.kt index 93f0a38c456..1600429b16e 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/UnescapeUtils.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/UnescapeUtils.kt @@ -17,24 +17,9 @@ package org.jetbrains.kotlin.cli.jvm.repl.messages import com.intellij.openapi.util.text.StringUtil -import org.w3c.dom.Element -import org.xml.sax.InputSource -import java.io.ByteArrayInputStream -import javax.xml.parsers.DocumentBuilderFactory // using '#' to avoid collisions with xml escaping internal val SOURCE_CHARS: Array = arrayOf("\n", "#") internal val XML_REPLACEMENTS: Array = arrayOf("#n", "#diez") -fun parseXml(inputMessage: String): String { - fun strToSource(s: String) = InputSource(ByteArrayInputStream(s.toByteArray())) - - val docFactory = DocumentBuilderFactory.newInstance() - val docBuilder = docFactory.newDocumentBuilder() - val input = docBuilder.parse(strToSource(inputMessage)) - - val root = input.firstChild as Element - return root.textContent -} - fun unescapeLineBreaks(s: String) = StringUtil.replace(s, XML_REPLACEMENTS, SOURCE_CHARS) \ No newline at end of file diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ReplSystemInWrapper.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/reader/ReplSystemInWrapper.kt similarity index 76% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ReplSystemInWrapper.kt rename to compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/reader/ReplSystemInWrapper.kt index 1a18aba58cc..a35b37edc2f 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ReplSystemInWrapper.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/reader/ReplSystemInWrapper.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. + * 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. @@ -14,10 +14,17 @@ * limitations under the License. */ -package org.jetbrains.kotlin.cli.jvm.repl.messages +package org.jetbrains.kotlin.cli.jvm.repl.reader +import org.jetbrains.kotlin.cli.jvm.repl.messages.unescapeLineBreaks +import org.jetbrains.kotlin.cli.jvm.repl.writer.END_LINE +import org.jetbrains.kotlin.cli.jvm.repl.writer.ReplWriter +import org.w3c.dom.Element +import org.xml.sax.InputSource +import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.InputStream +import javax.xml.parsers.DocumentBuilderFactory class ReplSystemInWrapper( private val stdin: InputStream, @@ -87,4 +94,15 @@ class ReplSystemInWrapper( isLastByteProcessed = true } } +} + +private fun parseXml(inputMessage: String): String { + fun strToSource(s: String) = InputSource(ByteArrayInputStream(s.toByteArray())) + + val docFactory = DocumentBuilderFactory.newInstance() + val docBuilder = docFactory.newDocumentBuilder() + val input = docBuilder.parse(strToSource(inputMessage)) + + val root = input.firstChild as Element + return root.textContent } \ No newline at end of file diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ReplConsoleWriter.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/ConsoleReplWriter.kt similarity index 89% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ReplConsoleWriter.kt rename to compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/ConsoleReplWriter.kt index c8f89446ffa..51db30aa32f 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ReplConsoleWriter.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/ConsoleReplWriter.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * 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. @@ -14,9 +14,9 @@ * limitations under the License. */ -package org.jetbrains.kotlin.cli.jvm.repl.messages +package org.jetbrains.kotlin.cli.jvm.repl.writer -class ReplConsoleWriter : ReplWriter { +class ConsoleReplWriter : ReplWriter { override fun printlnWelcomeMessage(x: String) = println(x) override fun printlnHelpMessage(x: String) = println(x) override fun outputCompileError(x: String) = println(x) diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ReplSystemOutWrapperForIde.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/IdeSystemOutWrapperReplWriter.kt similarity index 84% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ReplSystemOutWrapperForIde.kt rename to compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/IdeSystemOutWrapperReplWriter.kt index 85ffb12a4bc..f18b66a8ac4 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ReplSystemOutWrapperForIde.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/IdeSystemOutWrapperReplWriter.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * 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. @@ -14,10 +14,12 @@ * limitations under the License. */ -package org.jetbrains.kotlin.cli.jvm.repl.messages +package org.jetbrains.kotlin.cli.jvm.repl.writer import com.intellij.openapi.util.text.StringUtil import com.intellij.util.LineSeparator +import org.jetbrains.kotlin.cli.jvm.repl.messages.SOURCE_CHARS +import org.jetbrains.kotlin.cli.jvm.repl.messages.XML_REPLACEMENTS import org.jetbrains.kotlin.utils.repl.ReplEscapeType import java.io.PrintStream import org.jetbrains.kotlin.utils.repl.ReplEscapeType.* @@ -25,7 +27,7 @@ import org.jetbrains.kotlin.utils.repl.ReplEscapeType.* internal val END_LINE: String = LineSeparator.getSystemLineSeparator().separatorString internal val XML_PREAMBLE = "" -class ReplSystemOutWrapperForIde(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()) @@ -40,12 +42,12 @@ class ReplSystemOutWrapperForIde(standardOut: PrintStream) : PrintStream(standar } private fun printWithEscaping(text: String, escapeType: ReplEscapeType = USER_OUTPUT) { - super.print("${xmlEscape(text, escapeType)}$END_LINE") + super.print("${xmlEscape(text, escapeType)}${END_LINE}") } private fun xmlEscape(s: String, escapeType: ReplEscapeType): String { val singleLine = StringUtil.replace(s, SOURCE_CHARS, XML_REPLACEMENTS) - return "$XML_PREAMBLE${StringUtil.escapeXml(singleLine)}" + return "${XML_PREAMBLE}${StringUtil.escapeXml(singleLine)}" } override fun printlnWelcomeMessage(x: String) = printlnWithEscaping(x, INITIAL_PROMPT) diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ReplWriter.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/ReplWriter.kt similarity index 95% rename from compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ReplWriter.kt rename to compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/ReplWriter.kt index ccefb6c1be2..163c877f551 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/messages/ReplWriter.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/writer/ReplWriter.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.jetbrains.kotlin.cli.jvm.repl.messages +package org.jetbrains.kotlin.cli.jvm.repl.writer interface ReplWriter { fun printlnWelcomeMessage(x: String) diff --git a/compiler/tests/org/jetbrains/kotlin/repl/AbstractReplInterpreterTest.kt b/compiler/tests/org/jetbrains/kotlin/repl/AbstractReplInterpreterTest.kt index 4d84eacdda6..3b5762e4b43 100644 --- a/compiler/tests/org/jetbrains/kotlin/repl/AbstractReplInterpreterTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/repl/AbstractReplInterpreterTest.kt @@ -18,7 +18,7 @@ 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.ConsoleReplConfiguration +import org.jetbrains.kotlin.cli.jvm.repl.configuration.ConsoleReplConfiguration import org.jetbrains.kotlin.cli.jvm.repl.ReplInterpreter import org.jetbrains.kotlin.test.ConfigurationKind import org.jetbrains.kotlin.test.KotlinTestUtils