From 2fbbf2400dea6d288dc9d5b05f50559d9013d305 Mon Sep 17 00:00:00 2001 From: Natalia Selezneva Date: Tue, 11 Dec 2018 11:12:34 +0300 Subject: [PATCH] Refactoring: unify methods to report errors in scratches --- .../scratch/compile/KtCompilingExecutor.kt | 45 +++++++++---------- .../scratch/repl/KtScratchReplExecutor.kt | 7 +-- idea/testData/scratch/unresolved.comp.after | 3 +- .../scratch/unresolvedMultiline.comp.after | 3 +- 4 files changed, 25 insertions(+), 33 deletions(-) diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/compile/KtCompilingExecutor.kt b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/compile/KtCompilingExecutor.kt index 48c31bca019..2042343546c 100644 --- a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/compile/KtCompilingExecutor.kt +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/compile/KtCompilingExecutor.kt @@ -63,16 +63,14 @@ class KtCompilingExecutor(file: ScratchFile) : ScratchExecutor(file) { override fun execute() { handler.onStart(file) - val module = file.getModule() ?: return error("Module should be selected") - val psiFile = file.getPsiFile() as? KtFile ?: return error("Couldn't find KtFile for current editor") + val module = file.getModule() ?: return errorOccurs("Module should be selected", isFatal = true) + val psiFile = file.getPsiFile() as? KtFile ?: return errorOccurs("Couldn't find KtFile for current editor", isFatal = true) - if (!checkForErrors(psiFile)) { - return error("Compilation Error") - } + if (!checkForErrors(psiFile)) return val result = runReadAction { KtScratchSourceFileProcessor().process(file) } when (result) { - is KtScratchSourceFileProcessor.Result.Error -> return error(result.message) + is KtScratchSourceFileProcessor.Result.Error -> return errorOccurs(result.message, isFatal = true) is KtScratchSourceFileProcessor.Result.OK -> { LOG.printDebugMessage("After processing by KtScratchSourceFileProcessor:\n ${result.code}") @@ -96,21 +94,26 @@ class KtCompilingExecutor(file: ScratchFile) : ScratchExecutor(file) { LOG.printDebugMessage(commandLine.commandLineString) - val handler = CapturingProcessHandler(commandLine) - val executionResult = handler.runProcessWithProgressIndicator(indicator, TIMEOUT_MS) + val processHandler = CapturingProcessHandler(commandLine) + val executionResult = processHandler.runProcessWithProgressIndicator(indicator, TIMEOUT_MS) when { - executionResult.isTimeout -> error("Couldn't get scratch execution result - stopped by timeout ($TIMEOUT_MS ms)") - executionResult.isCancelled -> error("Couldn't get scratch execution result - cancelled by user") - else -> ProcessOutputParser().parse(executionResult) + executionResult.isTimeout -> { + errorOccurs("Couldn't get scratch execution result - stopped by timeout ($TIMEOUT_MS ms)") + } + executionResult.isCancelled -> { + errorOccurs("Couldn't get scratch execution result - cancelled by user") + } + else -> { + ProcessOutputParser().parse(executionResult) + } } } finally { tempDir.delete() + handler.onFinish(file) } } catch (e: Throwable) { - LOG.info(result.code, e) - handler.error(file, e.message ?: "Couldn't compile ${psiFile.name}") - } finally { - handler.onFinish(file) + LOG.printDebugMessage(result.code) + errorOccurs(e.message ?: "Couldn't compile ${psiFile.name}", e, isFatal = true) } } }.queue() @@ -192,14 +195,14 @@ class KtCompilingExecutor(file: ScratchFile) : ScratchExecutor(file) { try { AnalyzingUtils.checkForSyntacticErrors(psiFile) } catch (e: IllegalArgumentException) { - handler.error(file, e.message ?: "Couldn't compile ${psiFile.name}") + errorOccurs(e.message ?: "Couldn't compile ${psiFile.name}", isFatal = true) return@runReadAction false } val analysisResult = psiFile.analyzeWithAllCompilerChecks() if (analysisResult.isError()) { - handler.error(file, analysisResult.error.message ?: "Couldn't compile ${psiFile.name}") + errorOccurs(analysisResult.error.message ?: "Couldn't compile ${psiFile.name}", isFatal = true) return@runReadAction false } @@ -226,17 +229,13 @@ class KtCompilingExecutor(file: ScratchFile) : ScratchExecutor(file) { handler.error(file, errorText) } } + handler.onFinish(file) return@runReadAction false } return@runReadAction true } } - private fun error(message: String) { - handler.error(file, message) - handler.onFinish(file) - } - private fun ScratchFile.findExpression(psiElement: PsiElement): ScratchExpression? { val elementLine = psiElement.getLineNumber() return runReadAction { getExpressions().firstOrNull { elementLine in it.lineStart..it.lineEnd } } @@ -272,7 +271,7 @@ class KtCompilingExecutor(file: ScratchFile) : ScratchExecutor(file) { val lineWoPrefix = line.removePrefix(KtScratchSourceFileProcessor.GENERATED_OUTPUT_PREFIX) if (isResultEnd(lineWoPrefix)) { val extractedLineInfo = extractLineInfoFrom(lineWoPrefix) - ?: return error("Couldn't extract line info from line: $lineWoPrefix") + ?: return errorOccurs("Couldn't extract line info from line: $lineWoPrefix", isFatal = true) val (startLine, endLine) = extractedLineInfo val scratchExpression = file.findExpression(startLine, endLine) if (scratchExpression == null) { diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/repl/KtScratchReplExecutor.kt b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/repl/KtScratchReplExecutor.kt index 68b4426bf12..73b3a6d9f56 100644 --- a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/repl/KtScratchReplExecutor.kt +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/repl/KtScratchReplExecutor.kt @@ -42,7 +42,7 @@ class KtScratchReplExecutor(file: ScratchFile) : ScratchExecutor(file) { override fun execute() { handler.onStart(file) - val module = file.getModule() ?: return error("Module should be selected") + val module = file.getModule() ?: return errorOccurs("Module should be selected", isFatal = true) val cmdLine = KotlinConsoleKeeper.createCommandLine(module) LOG.printDebugMessage("Execute REPL: ${cmdLine.commandLineString}") @@ -79,11 +79,6 @@ class KtScratchReplExecutor(file: ScratchFile) : ScratchExecutor(file) { processInputOS.flush() } - private fun error(file: ScratchFile, message: String) { - handler.error(file, message) - handler.onFinish(file) - } - private class ReplHistory { private var entries = arrayListOf() private var processedEntriesCount: Int = 0 diff --git a/idea/testData/scratch/unresolved.comp.after b/idea/testData/scratch/unresolved.comp.after index d26bde7bf33..94155b2605c 100644 --- a/idea/testData/scratch/unresolved.comp.after +++ b/idea/testData/scratch/unresolved.comp.after @@ -1,3 +1,2 @@ foo() // ERROR: Unresolved reference: foo -/** unresolved.kts:1 Unresolved reference: foo */ -/** Compilation Error */ \ No newline at end of file +/** unresolved.kts:1 Unresolved reference: foo */ \ No newline at end of file diff --git a/idea/testData/scratch/unresolvedMultiline.comp.after b/idea/testData/scratch/unresolvedMultiline.comp.after index f026513a32c..420b70f5d09 100644 --- a/idea/testData/scratch/unresolvedMultiline.comp.after +++ b/idea/testData/scratch/unresolvedMultiline.comp.after @@ -9,5 +9,4 @@ fun goo(a: String) { // ERROR: Unresolved reference: goo /** unresolvedMultiline.kts:1 Cannot choose among the following candidates without completing type inference: @HidesMembers public inline fun Iterable.forEach(action: (???) -> Unit): Unit defined in kotlin.collections @HidesMembers public inline fun Map.forEach(action: (Map.Entry) -> Unit): Unit defined in kotlin.collections */ -/** unresolvedMultiline.kts:5 Unresolved reference: goo */ -/** Compilation Error */ \ No newline at end of file +/** unresolvedMultiline.kts:5 Unresolved reference: goo */ \ No newline at end of file