REPL, Refactoring: Get rid of nasty enum-like String literals
This commit is contained in:
+19
-29
@@ -18,26 +18,14 @@ package org.jetbrains.kotlin.cli.jvm.repl.messages
|
|||||||
|
|
||||||
import com.intellij.openapi.util.text.StringUtil
|
import com.intellij.openapi.util.text.StringUtil
|
||||||
import com.intellij.util.LineSeparator
|
import com.intellij.util.LineSeparator
|
||||||
|
import org.jetbrains.kotlin.utils.repl.ReplEscapeType
|
||||||
import java.io.PrintStream
|
import java.io.PrintStream
|
||||||
|
import org.jetbrains.kotlin.utils.repl.ReplEscapeType.*
|
||||||
|
|
||||||
val END_LINE: String = LineSeparator.getSystemLineSeparator().separatorString
|
internal val END_LINE: String = LineSeparator.getSystemLineSeparator().separatorString
|
||||||
val XML_PREAMBLE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
internal val XML_PREAMBLE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
||||||
|
|
||||||
class ReplSystemOutWrapperForIde(standardOut: PrintStream) : PrintStream(standardOut, true), ReplWriter {
|
class ReplSystemOutWrapperForIde(standardOut: PrintStream) : PrintStream(standardOut, true), ReplWriter {
|
||||||
private enum class EscapeType {
|
|
||||||
INITIAL_PROMPT,
|
|
||||||
HELP_PROMPT,
|
|
||||||
USER_OUTPUT,
|
|
||||||
REPL_RESULT,
|
|
||||||
READLINE_START,
|
|
||||||
READLINE_END,
|
|
||||||
REPL_INCOMPLETE,
|
|
||||||
COMPILE_ERROR,
|
|
||||||
RUNTIME_ERROR,
|
|
||||||
INTERNAL_ERROR,
|
|
||||||
SUCCESS
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun print(x: Boolean) = printWithEscaping(x.toString())
|
override fun print(x: Boolean) = printWithEscaping(x.toString())
|
||||||
override fun print(x: Char) = printWithEscaping(x.toString())
|
override fun print(x: Char) = printWithEscaping(x.toString())
|
||||||
override fun print(x: Int) = printWithEscaping(x.toString())
|
override fun print(x: Int) = printWithEscaping(x.toString())
|
||||||
@@ -47,25 +35,27 @@ class ReplSystemOutWrapperForIde(standardOut: PrintStream) : PrintStream(standar
|
|||||||
override fun print(x: String) = printWithEscaping(x)
|
override fun print(x: String) = printWithEscaping(x)
|
||||||
override fun print(x: Any?) = printWithEscaping(x.toString())
|
override fun print(x: Any?) = printWithEscaping(x.toString())
|
||||||
|
|
||||||
private fun printlnWithEscaping(text: String, escapeType: EscapeType = EscapeType.USER_OUTPUT) = printWithEscaping("$text\n", escapeType)
|
private fun printlnWithEscaping(text: String, escapeType: ReplEscapeType = USER_OUTPUT) {
|
||||||
|
printWithEscaping("$text\n", escapeType)
|
||||||
|
}
|
||||||
|
|
||||||
private fun printWithEscaping(text: String, escapeType: EscapeType = EscapeType.USER_OUTPUT) {
|
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: EscapeType): String {
|
private fun xmlEscape(s: String, escapeType: ReplEscapeType): String {
|
||||||
val singleLine = StringUtil.replace(s, SOURCE_CHARS, XML_REPLACEMENTS)
|
val singleLine = StringUtil.replace(s, SOURCE_CHARS, XML_REPLACEMENTS)
|
||||||
return "$XML_PREAMBLE<output type=\"$escapeType\">${StringUtil.escapeXml(singleLine)}</output>"
|
return "$XML_PREAMBLE<output type=\"$escapeType\">${StringUtil.escapeXml(singleLine)}</output>"
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun printlnWelcomeMessage(x: String) = printlnWithEscaping(x, EscapeType.INITIAL_PROMPT)
|
override fun printlnWelcomeMessage(x: String) = printlnWithEscaping(x, INITIAL_PROMPT)
|
||||||
override fun printlnHelpMessage(x: String) = printlnWithEscaping(x, EscapeType.HELP_PROMPT)
|
override fun printlnHelpMessage(x: String) = printlnWithEscaping(x, HELP_PROMPT)
|
||||||
override fun outputCommandResult(x: String) = printlnWithEscaping(x, EscapeType.REPL_RESULT)
|
override fun outputCommandResult(x: String) = printlnWithEscaping(x, REPL_RESULT)
|
||||||
override fun notifyReadLineStart() = printlnWithEscaping("", EscapeType.READLINE_START)
|
override fun notifyReadLineStart() = printlnWithEscaping("", READLINE_START)
|
||||||
override fun notifyReadLineEnd() = printlnWithEscaping("", EscapeType.READLINE_END)
|
override fun notifyReadLineEnd() = printlnWithEscaping("", READLINE_END)
|
||||||
override fun notifyCommandSuccess() = printlnWithEscaping("", EscapeType.SUCCESS)
|
override fun notifyCommandSuccess() = printlnWithEscaping("", SUCCESS)
|
||||||
override fun notifyIncomplete() = printlnWithEscaping("", EscapeType.REPL_INCOMPLETE)
|
override fun notifyIncomplete() = printlnWithEscaping("", REPL_INCOMPLETE)
|
||||||
override fun outputCompileError(x: String) = printlnWithEscaping(x, EscapeType.COMPILE_ERROR)
|
override fun outputCompileError(x: String) = printlnWithEscaping(x, COMPILE_ERROR)
|
||||||
override fun outputRuntimeError(x: String) = printlnWithEscaping(x, EscapeType.RUNTIME_ERROR)
|
override fun outputRuntimeError(x: String) = printlnWithEscaping(x, RUNTIME_ERROR)
|
||||||
override fun sendInternalErrorReport(x: String) = printlnWithEscaping(x, EscapeType.INTERNAL_ERROR)
|
override fun sendInternalErrorReport(x: String) = printlnWithEscaping(x, INTERNAL_ERROR)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* 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.utils.repl
|
||||||
|
|
||||||
|
enum class ReplEscapeType {
|
||||||
|
INITIAL_PROMPT,
|
||||||
|
HELP_PROMPT,
|
||||||
|
USER_OUTPUT,
|
||||||
|
REPL_RESULT,
|
||||||
|
READLINE_START,
|
||||||
|
READLINE_END,
|
||||||
|
REPL_INCOMPLETE,
|
||||||
|
COMPILE_ERROR,
|
||||||
|
RUNTIME_ERROR,
|
||||||
|
INTERNAL_ERROR,
|
||||||
|
SUCCESS;
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun valueOfOrNull(string: String): ReplEscapeType? {
|
||||||
|
return try {
|
||||||
|
valueOf(string)
|
||||||
|
}
|
||||||
|
catch (e: IllegalArgumentException) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,11 +24,13 @@ import com.intellij.openapi.util.text.StringUtil
|
|||||||
import org.jetbrains.annotations.NotNull
|
import org.jetbrains.annotations.NotNull
|
||||||
import org.jetbrains.kotlin.console.actions.logError
|
import org.jetbrains.kotlin.console.actions.logError
|
||||||
import org.jetbrains.kotlin.diagnostics.Severity
|
import org.jetbrains.kotlin.diagnostics.Severity
|
||||||
|
import org.jetbrains.kotlin.utils.repl.ReplEscapeType
|
||||||
import org.w3c.dom.Element
|
import org.w3c.dom.Element
|
||||||
import org.xml.sax.InputSource
|
import org.xml.sax.InputSource
|
||||||
import java.io.ByteArrayInputStream
|
import java.io.ByteArrayInputStream
|
||||||
import java.nio.charset.Charset
|
import java.nio.charset.Charset
|
||||||
import javax.xml.parsers.DocumentBuilderFactory
|
import javax.xml.parsers.DocumentBuilderFactory
|
||||||
|
import org.jetbrains.kotlin.utils.repl.ReplEscapeType.*
|
||||||
|
|
||||||
val XML_REPLACEMENTS: Array<String> = arrayOf("#n", "#diez")
|
val XML_REPLACEMENTS: Array<String> = arrayOf("#n", "#diez")
|
||||||
val SOURCE_CHARS: Array<String> = arrayOf("\n", "#")
|
val SOURCE_CHARS: Array<String> = arrayOf("\n", "#")
|
||||||
@@ -76,25 +78,25 @@ class ReplOutputHandler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
val root = output.firstChild as Element
|
val root = output.firstChild as Element
|
||||||
val outputType = root.getAttribute("type")
|
val outputType = ReplEscapeType.valueOfOrNull(root.getAttribute("type"))
|
||||||
val content = StringUtil.replace(root.textContent, XML_REPLACEMENTS, SOURCE_CHARS)
|
val content = StringUtil.replace(root.textContent, XML_REPLACEMENTS, SOURCE_CHARS)
|
||||||
|
|
||||||
when (outputType) {
|
when (outputType) {
|
||||||
"INITIAL_PROMPT" -> buildWarningIfNeededBeforeInit(content)
|
INITIAL_PROMPT -> buildWarningIfNeededBeforeInit(content)
|
||||||
"HELP_PROMPT" -> outputProcessor.printHelp(content)
|
HELP_PROMPT -> outputProcessor.printHelp(content)
|
||||||
"USER_OUTPUT" -> outputProcessor.printUserOutput(content)
|
USER_OUTPUT -> outputProcessor.printUserOutput(content)
|
||||||
"REPL_RESULT" -> outputProcessor.printResultWithGutterIcon(content)
|
REPL_RESULT -> outputProcessor.printResultWithGutterIcon(content)
|
||||||
"READLINE_START" -> runner.isReadLineMode = true
|
READLINE_START -> runner.isReadLineMode = true
|
||||||
"READLINE_END" -> runner.isReadLineMode = false
|
READLINE_END -> runner.isReadLineMode = false
|
||||||
"REPL_INCOMPLETE",
|
REPL_INCOMPLETE,
|
||||||
"COMPILE_ERROR" -> outputProcessor.highlightCompilerErrors(createCompilerMessages(content))
|
COMPILE_ERROR -> outputProcessor.highlightCompilerErrors(createCompilerMessages(content))
|
||||||
"RUNTIME_ERROR" -> outputProcessor.printRuntimeError("${content.trim()}\n")
|
RUNTIME_ERROR -> outputProcessor.printRuntimeError("${content.trim()}\n")
|
||||||
"INTERNAL_ERROR" -> outputProcessor.printInternalErrorMessage(content)
|
INTERNAL_ERROR -> outputProcessor.printInternalErrorMessage(content)
|
||||||
"SUCCESS" -> runner.commandHistory.lastUnprocessedEntry()?.entryText?.let { runner.successfulLine(it) }
|
SUCCESS -> runner.commandHistory.lastUnprocessedEntry()?.entryText?.let { runner.successfulLine(it) }
|
||||||
else -> logError(ReplOutputHandler::class.java, "Unexpected output type:\n$outputType")
|
null -> logError(ReplOutputHandler::class.java, "Unexpected output type:\n$outputType")
|
||||||
}
|
}
|
||||||
|
|
||||||
if (outputType in setOf("SUCCESS", "COMPILE_ERROR", "INTERNAL_ERROR", "RUNTIME_ERROR", "READLINE_END")) {
|
if (outputType in setOf(SUCCESS, COMPILE_ERROR, INTERNAL_ERROR, RUNTIME_ERROR, READLINE_END)) {
|
||||||
runner.commandHistory.entryProcessed()
|
runner.commandHistory.entryProcessed()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user