[ide-console] String constants small refactoring

This commit is contained in:
Dmitry Kovanikov
2015-08-25 15:45:29 +03:00
committed by Pavel V. Talanov
parent 27e015c406
commit b19010184a
5 changed files with 23 additions and 44 deletions
@@ -22,6 +22,8 @@ import com.intellij.openapi.util.text.StringUtil
import org.jetbrains.kotlin.console.actions.logError
import org.jetbrains.kotlin.console.highlight.KotlinHistoryHighlighter
private val XML_PREAMBLE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
public class KotlinConsoleExecutor(
private val runner: KotlinConsoleRunner,
private val historyManager: KotlinConsoleHistoryManager
@@ -49,11 +51,11 @@ public class KotlinConsoleExecutor(
val processInputOS = processHandler.processInput ?: return logError(javaClass, "<p>Broken process stream</p>")
val charset = (processHandler as? BaseOSProcessHandler)?.charset ?: Charsets.UTF_8
val xmlRes = "${ReplConstants.XML_PREAMBLE}" +
val xmlRes = "$XML_PREAMBLE" +
"<input>" +
"${StringUtil.escapeXml(
StringUtil.replace(command.trim(), ReplConstants.SOURCE_CHARS, ReplConstants.XML_REPLACEMENTS)
)}" +
"${StringUtil.escapeXml(
StringUtil.replace(command.trim(), SOURCE_CHARS, XML_REPLACEMENTS)
)}" +
"</input>"
val bytes = ("$xmlRes\n").toByteArray(charset)
processInputOS.write(bytes)
@@ -34,7 +34,6 @@ import org.jetbrains.kotlin.console.actions.errorNotification
import org.jetbrains.kotlin.utils.PathUtil
import java.io.File
import java.util.concurrent.ConcurrentHashMap
import kotlin.platform.platformStatic
public class KotlinConsoleKeeper(val project: Project) {
private val consoleMap: MutableMap<VirtualFile, KotlinConsoleRunner> = ConcurrentHashMap()
@@ -45,7 +44,7 @@ public class KotlinConsoleKeeper(val project: Project) {
fun run(module: Module): KotlinConsoleRunner? {
val path = module.moduleFilePath
val cmdLine = createCommandLine(module) ?: return errorNotification(project, "<p>Module SDK not found</p>") let { null }
val cmdLine = createCommandLine(module) ?: return errorNotification(project, "Module SDK not found") let { null }
val consoleRunner = KotlinConsoleRunner(path, cmdLine, module, project, REPL_TITLE)
consoleRunner.initAndRun()
@@ -113,6 +112,6 @@ public class KotlinConsoleKeeper(val project: Project) {
companion object {
private val REPL_TITLE = "Kotlin REPL"
platformStatic fun getInstance(project: Project) = ServiceManager.getService(project, javaClass<KotlinConsoleKeeper>())
jvmStatic fun getInstance(project: Project) = ServiceManager.getService(project, javaClass<KotlinConsoleKeeper>())
}
}
@@ -29,6 +29,11 @@ import java.io.ByteArrayInputStream
import java.nio.charset.Charset
import javax.xml.parsers.DocumentBuilderFactory
private val XML_PREFIX = "<?xml"
public val XML_REPLACEMENTS: Array<String> = arrayOf("#n", "#diez")
public val SOURCE_CHARS: Array<String> = arrayOf("\n", "#")
data class SeverityDetails(val severity: Severity, val description: String, val range: TextRange)
public class KotlinReplOutputHandler(
@@ -41,12 +46,12 @@ public class KotlinReplOutputHandler(
override fun notifyTextAvailable(text: String, key: Key<*>?) {
// skip "/usr/lib/jvm/java-8-oracle/bin/java -cp ..." intro
if (!text.startsWith(ReplConstants.XML_PREFIX)) return super.notifyTextAvailable(text, key)
if (!text.startsWith(XML_PREFIX)) return super.notifyTextAvailable(text, key)
val output = dBuilder.parse(strToSource(text))
val root = output.firstChild as Element
val outputType = root.getAttribute("type")
val content = StringUtil.replace(root.textContent, ReplConstants.XML_REPLACEMENTS, ReplConstants.SOURCE_CHARS).trim()
val content = StringUtil.replace(root.textContent, XML_REPLACEMENTS, SOURCE_CHARS).trim()
when (outputType) {
"INITIAL_PROMPT" -> super.notifyTextAvailable("$content\n", key)
@@ -1,25 +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.console
public object ReplConstants {
public val XML_PREFIX: String = "<?xml"
public val XML_PREAMBLE: String = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
public val SOURCE_CHARS: Array<String> = arrayOf("\n", "#")
public val XML_REPLACEMENTS: Array<String> = arrayOf("#n", "#diez")
}
@@ -27,19 +27,16 @@ import com.intellij.openapi.vfs.VirtualFile
import org.jetbrains.kotlin.console.KotlinConsoleKeeper
fun errorNotification(project: Project?, message: String) {
val tag = "KOTLIN REPL ERROR"
val title = "Kotlin REPL Configuration Error"
Notifications.Bus.notify(Notification(tag, title, message, NotificationType.ERROR), project)
val errorTag = "KOTLIN REPL ERROR"
val errorTitle = "Kotlin REPL Configuration Error"
Notifications.Bus.notify(Notification(errorTag, errorTitle, message, NotificationType.ERROR), project)
}
fun logError(cl: Class<*>, message: String) {
val logger = Logger.getInstance(cl)
logger.error(message)
}
fun logError(cl: Class<*>, message: String) = with(Logger.getInstance(cl)) { error(message) }
public class RunKotlinConsoleAction : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return errorNotification(null, "<p>Project not found</p>")
val project = e.project ?: return errorNotification(null, "Project not found")
KotlinConsoleModuleDialog(project).showIfNeeded(e.dataContext)
}
@@ -47,8 +44,9 @@ public class RunKotlinConsoleAction : AnAction() {
public class KtExecuteCommandAction(private val consoleFile: VirtualFile) : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return errorNotification(null, "<p>Cannot find project</p>")
val ktConsole = KotlinConsoleKeeper.getInstance(project).getConsoleByVirtualFile(consoleFile) ?: return errorNotification(project, "<p>Action performed in an invalid console</p>")
val project = e.project ?: return errorNotification(null, "Cannot find project")
val ktConsole = KotlinConsoleKeeper.getInstance(project).getConsoleByVirtualFile(consoleFile)
?: return errorNotification(project, "Action performed in an invalid console")
ktConsole.executor.executeCommand()
}