Refactoring: unify methods to report errors in scratches

This commit is contained in:
Natalia Selezneva
2018-12-11 11:12:34 +03:00
parent 2f32c6e164
commit 2fbbf2400d
4 changed files with 25 additions and 33 deletions
@@ -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) {
@@ -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<ScratchExpression>()
private var processedEntriesCount: Int = 0
+1 -2
View File
@@ -1,3 +1,2 @@
foo() // ERROR: Unresolved reference: foo
/** unresolved.kts:1 Unresolved reference: foo */
/** Compilation Error */
/** unresolved.kts:1 Unresolved reference: foo */
+1 -2
View File
@@ -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 <T> Iterable<???>.forEach(action: (???) -> Unit): Unit defined in kotlin.collections
@HidesMembers public inline fun <K, V> Map<out ???, ???>.forEach(action: (Map.Entry<???, ???>) -> Unit): Unit defined in kotlin.collections */
/** unresolvedMultiline.kts:5 Unresolved reference: goo */
/** Compilation Error */
/** unresolvedMultiline.kts:5 Unresolved reference: goo */