ReplOutputProcessor: cleanup code

This commit is contained in:
Dmitry Gridin
2019-08-14 17:26:59 +07:00
parent ab4001a698
commit df438377e8
@@ -30,15 +30,15 @@ import com.intellij.openapi.editor.markup.HighlighterTargetArea
import com.intellij.openapi.editor.markup.TextAttributes import com.intellij.openapi.editor.markup.TextAttributes
import com.intellij.psi.PsiDocumentManager import com.intellij.psi.PsiDocumentManager
import org.jetbrains.kotlin.console.actions.logError import org.jetbrains.kotlin.console.actions.logError
import org.jetbrains.kotlin.console.gutter.IconWithTooltip
import org.jetbrains.kotlin.console.gutter.ConsoleErrorRenderer import org.jetbrains.kotlin.console.gutter.ConsoleErrorRenderer
import org.jetbrains.kotlin.console.gutter.ConsoleIndicatorRenderer import org.jetbrains.kotlin.console.gutter.ConsoleIndicatorRenderer
import org.jetbrains.kotlin.console.gutter.IconWithTooltip
import org.jetbrains.kotlin.console.gutter.ReplIcons import org.jetbrains.kotlin.console.gutter.ReplIcons
import org.jetbrains.kotlin.diagnostics.Severity import org.jetbrains.kotlin.diagnostics.Severity
import kotlin.math.max import kotlin.math.max
class ReplOutputProcessor( class ReplOutputProcessor(
private val runner: KotlinConsoleRunner private val runner: KotlinConsoleRunner
) { ) {
private val project = runner.project private val project = runner.project
private val consoleView = runner.consoleView as LanguageConsoleImpl private val consoleView = runner.consoleView as LanguageConsoleImpl
@@ -63,7 +63,7 @@ class ReplOutputProcessor(
if (iconWithTooltip == null) return if (iconWithTooltip == null) return
historyMarkup.addRangeHighlighter( historyMarkup.addRangeHighlighter(
startOffset, endOffset, HighlighterLayer.LAST, null, HighlighterTargetArea.EXACT_RANGE startOffset, endOffset, HighlighterLayer.LAST, null, HighlighterTargetArea.EXACT_RANGE
).apply { gutterIconRenderer = ConsoleIndicatorRenderer(iconWithTooltip) } ).apply { gutterIconRenderer = ConsoleIndicatorRenderer(iconWithTooltip) }
} }
@@ -83,7 +83,10 @@ class ReplOutputProcessor(
fun printBuildInfoWarningIfNeeded() { fun printBuildInfoWarningIfNeeded() {
if (ApplicationManager.getApplication().isUnitTestMode) return if (ApplicationManager.getApplication().isUnitTestMode) return
if (runner.previousCompilationFailed) return printWarningMessage("There were compilation errors in module ${runner.module.name}", false) if (runner.previousCompilationFailed) return printWarningMessage(
"There were compilation errors in module ${runner.module.name}",
false
)
if (runner.compilerHelper.moduleIsUpToDate()) return if (runner.compilerHelper.moduleIsUpToDate()) return
val compilerWarningMessage = "Youre running the REPL with outdated classes: " val compilerWarningMessage = "Youre running the REPL with outdated classes: "
@@ -107,8 +110,8 @@ class ReplOutputProcessor(
fun highlightCompilerErrors(compilerMessages: List<SeverityDetails>) = WriteCommandAction.runWriteCommandAction(project) { fun highlightCompilerErrors(compilerMessages: List<SeverityDetails>) = WriteCommandAction.runWriteCommandAction(project) {
val commandHistory = runner.commandHistory val commandHistory = runner.commandHistory
val lastUnprocessedHistoryEntry = commandHistory.lastUnprocessedEntry() ?: return@runWriteCommandAction logError( val lastUnprocessedHistoryEntry = commandHistory.lastUnprocessedEntry() ?: return@runWriteCommandAction logError(
ReplOutputProcessor::class.java, ReplOutputProcessor::class.java,
"Processed more commands than were sent. Sent commands: ${commandHistory.size}. Processed: ${commandHistory.processedEntriesCount}" "Processed more commands than were sent. Sent commands: ${commandHistory.size}. Processed: ${commandHistory.processedEntriesCount}"
) )
val lastCommandStartOffset = lastUnprocessedHistoryEntry.rangeInHistoryDocument.startOffset val lastCommandStartOffset = lastUnprocessedHistoryEntry.rangeInHistoryDocument.startOffset
val lastCommandStartLine = historyDocument.getLineNumber(lastCommandStartOffset) val lastCommandStartLine = historyDocument.getLineNumber(lastCommandStartOffset)
@@ -128,7 +131,7 @@ class ReplOutputProcessor(
val textAttributes = getAttributesForSeverity(cmdStart, cmdEnd, message.severity) val textAttributes = getAttributesForSeverity(cmdStart, cmdEnd, message.severity)
historyMarkup.addRangeHighlighter( historyMarkup.addRangeHighlighter(
cmdStart, cmdEnd, HighlighterLayer.LAST, textAttributes, HighlighterTargetArea.EXACT_RANGE cmdStart, cmdEnd, HighlighterLayer.LAST, textAttributes, HighlighterTargetArea.EXACT_RANGE
) )
} }
Pair(highlighters.first(), messages) Pair(highlighters.first(), messages)
@@ -153,22 +156,35 @@ class ReplOutputProcessor(
} }
private fun getAttributesForSeverity(start: Int, end: Int, severity: Severity): TextAttributes = when (severity) { private fun getAttributesForSeverity(start: Int, end: Int, severity: Severity): TextAttributes = when (severity) {
Severity.ERROR -> Severity.ERROR ->
getAttributesForSeverity(HighlightInfoType.ERROR, HighlightSeverity.ERROR, CodeInsightColors.ERRORS_ATTRIBUTES, start, end) getAttributesForSeverity(HighlightInfoType.ERROR, HighlightSeverity.ERROR, CodeInsightColors.ERRORS_ATTRIBUTES, start, end)
Severity.WARNING -> Severity.WARNING ->
getAttributesForSeverity(HighlightInfoType.WARNING, HighlightSeverity.WARNING, CodeInsightColors.WARNINGS_ATTRIBUTES, start, end) getAttributesForSeverity(
Severity.INFO -> HighlightInfoType.WARNING,
getAttributesForSeverity(HighlightInfoType.WEAK_WARNING, HighlightSeverity.WEAK_WARNING, CodeInsightColors.WEAK_WARNING_ATTRIBUTES, start, end) HighlightSeverity.WARNING,
CodeInsightColors.WARNINGS_ATTRIBUTES,
start,
end
)
Severity.INFO ->
getAttributesForSeverity(
HighlightInfoType.WEAK_WARNING,
HighlightSeverity.WEAK_WARNING,
CodeInsightColors.WEAK_WARNING_ATTRIBUTES,
start,
end
)
} }
private fun getAttributesForSeverity( private fun getAttributesForSeverity(
infoType: HighlightInfoType, infoType: HighlightInfoType,
severity: HighlightSeverity, severity: HighlightSeverity,
insightColors: TextAttributesKey, insightColors: TextAttributesKey,
start: Int, start: Int,
end: Int end: Int
): TextAttributes { ): TextAttributes {
val highlightInfo = HighlightInfo.newHighlightInfo(infoType).range(start, end).severity(severity).textAttributes(insightColors).create() val highlightInfo =
HighlightInfo.newHighlightInfo(infoType).range(start, end).severity(severity).textAttributes(insightColors).create()
val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(consoleView.consoleEditor.document) val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(consoleView.consoleEditor.document)
val colorScheme = consoleView.consoleEditor.colorsScheme val colorScheme = consoleView.consoleEditor.colorsScheme