[cli-repl][ide-console] Show runtime errors in IDE console

This commit is contained in:
Dmitry Kovanikov
2015-08-24 16:31:35 +03:00
committed by Pavel V. Talanov
parent e23441907c
commit a17363459a
6 changed files with 73 additions and 39 deletions
@@ -20,6 +20,7 @@ import com.intellij.execution.process.OSProcessHandler
import com.intellij.openapi.util.Key
import com.intellij.openapi.util.TextRange
import com.intellij.openapi.util.text.StringUtil
import org.jetbrains.kotlin.console.actions.logError
import org.jetbrains.kotlin.console.highlight.KotlinReplOutputHighlighter
import org.jetbrains.kotlin.diagnostics.Severity
import org.w3c.dom.Element
@@ -50,32 +51,35 @@ public class KotlinReplOutputHandler(
when (outputType) {
"INITIAL_PROMPT" -> super.notifyTextAvailable("$content\n", key)
"USER_OUTPUT" -> outputHighlighter.printUserOutput(content)
"REPL_RESULT" -> outputHighlighter.printResultWithGutterIcon("$content\n")
"REPL_RESULT" -> outputHighlighter.printResultWithGutterIcon(content)
"REPL_INCOMPLETE" -> outputHighlighter.changeIndicatorOnIncomplete()
"ERROR" -> {
val compilerMessages = arrayListOf<SeverityDetails>()
val report = dBuilder.parse(strToSource(content, Charsets.UTF_16))
val entries = report.getElementsByTagName("reportEntry")
for (i in 0..entries.length - 1) {
val reportEntry = entries.item(i) as Element
val severityLevel = reportEntry.getAttribute("severity").toSeverity()
val rangeStart = reportEntry.getAttribute("rangeStart").toInt()
val rangeEnd = reportEntry.getAttribute("rangeEnd").toInt()
val description = reportEntry.textContent
compilerMessages.add(SeverityDetails(severityLevel, description, TextRange(rangeStart, rangeEnd)))
}
outputHighlighter.highlightErrors(compilerMessages)
}
else -> super.notifyTextAvailable("UNEXPECTED TEXT: $content\n", key)
"COMPILE_ERROR" -> outputHighlighter.highlightCompilerErrors(createCompilerMessages(content))
"RUNTIME_ERROR" -> outputHighlighter.printRuntimeError(content)
"INNER_ERROR" -> logError(javaClass, content)
}
}
private fun strToSource(s: String, encoding: Charset = Charsets.UTF_8) = InputSource(ByteArrayInputStream(s.toByteArray(encoding)))
private fun createCompilerMessages(runtimeErrorsReport: String): List<SeverityDetails> {
val compilerMessages = arrayListOf<SeverityDetails>()
val report = dBuilder.parse(strToSource(runtimeErrorsReport, Charsets.UTF_16))
val entries = report.getElementsByTagName("reportEntry")
for (i in 0..entries.length - 1) {
val reportEntry = entries.item(i) as Element
val severityLevel = reportEntry.getAttribute("severity").toSeverity()
val rangeStart = reportEntry.getAttribute("rangeStart").toInt()
val rangeEnd = reportEntry.getAttribute("rangeEnd").toInt()
val description = reportEntry.textContent
compilerMessages.add(SeverityDetails(severityLevel, description, TextRange(rangeStart, rangeEnd)))
}
return compilerMessages
}
private fun String.toSeverity() = when (this) {
"ERROR" -> Severity.ERROR
"WARNING" -> Severity.WARNING
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.console.highlight
import com.intellij.codeInsight.daemon.impl.HighlightInfo
import com.intellij.codeInsight.daemon.impl.HighlightInfoType
import com.intellij.execution.ui.ConsoleViewContentType
import com.intellij.lang.annotation.HighlightSeverity
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.editor.colors.CodeInsightColors
@@ -84,7 +85,7 @@ public class KotlinReplOutputHighlighter(
runner.changeEditorIndicatorIcon(consoleView.consoleEditor, ReplIcons.INCOMPLETE_INDICATOR)
}
fun highlightErrors(compilerMessages: List<SeverityDetails>) = WriteCommandAction.runWriteCommandAction(runner.project) {
fun highlightCompilerErrors(compilerMessages: List<SeverityDetails>) = WriteCommandAction.runWriteCommandAction(runner.project) {
resetConsoleEditorIndicator()
historyManager.lastCommandType = ReplOutputType.ERROR
@@ -112,6 +113,12 @@ public class KotlinReplOutputHighlighter(
}
}
fun printRuntimeError(errorText: String) {
resetConsoleEditorIndicator()
historyManager.lastCommandType = ReplOutputType.ERROR
consoleView.print("\t$errorText", ConsoleViewContentType.ERROR_OUTPUT)
}
private fun getAttributesForSeverity(start: Int, end: Int, severity: Severity): TextAttributes {
val attributes = when (severity) {
Severity.ERROR -> getAttributesForSeverity(HighlightInfoType.ERROR, HighlightSeverity.ERROR, CodeInsightColors.ERRORS_ATTRIBUTES, start, end)