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() { override fun execute() {
handler.onStart(file) 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 psiFile = file.getPsiFile() as? KtFile ?: return error("Couldn't find KtFile for current editor") val psiFile = file.getPsiFile() as? KtFile ?: return errorOccurs("Couldn't find KtFile for current editor", isFatal = true)
if (!checkForErrors(psiFile)) { if (!checkForErrors(psiFile)) return
return error("Compilation Error")
}
val result = runReadAction { KtScratchSourceFileProcessor().process(file) } val result = runReadAction { KtScratchSourceFileProcessor().process(file) }
when (result) { when (result) {
is KtScratchSourceFileProcessor.Result.Error -> return error(result.message) is KtScratchSourceFileProcessor.Result.Error -> return errorOccurs(result.message, isFatal = true)
is KtScratchSourceFileProcessor.Result.OK -> { is KtScratchSourceFileProcessor.Result.OK -> {
LOG.printDebugMessage("After processing by KtScratchSourceFileProcessor:\n ${result.code}") LOG.printDebugMessage("After processing by KtScratchSourceFileProcessor:\n ${result.code}")
@@ -96,21 +94,26 @@ class KtCompilingExecutor(file: ScratchFile) : ScratchExecutor(file) {
LOG.printDebugMessage(commandLine.commandLineString) LOG.printDebugMessage(commandLine.commandLineString)
val handler = CapturingProcessHandler(commandLine) val processHandler = CapturingProcessHandler(commandLine)
val executionResult = handler.runProcessWithProgressIndicator(indicator, TIMEOUT_MS) val executionResult = processHandler.runProcessWithProgressIndicator(indicator, TIMEOUT_MS)
when { when {
executionResult.isTimeout -> error("Couldn't get scratch execution result - stopped by timeout ($TIMEOUT_MS ms)") executionResult.isTimeout -> {
executionResult.isCancelled -> error("Couldn't get scratch execution result - cancelled by user") errorOccurs("Couldn't get scratch execution result - stopped by timeout ($TIMEOUT_MS ms)")
else -> ProcessOutputParser().parse(executionResult) }
executionResult.isCancelled -> {
errorOccurs("Couldn't get scratch execution result - cancelled by user")
}
else -> {
ProcessOutputParser().parse(executionResult)
}
} }
} finally { } finally {
tempDir.delete() tempDir.delete()
handler.onFinish(file)
} }
} catch (e: Throwable) { } catch (e: Throwable) {
LOG.info(result.code, e) LOG.printDebugMessage(result.code)
handler.error(file, e.message ?: "Couldn't compile ${psiFile.name}") errorOccurs(e.message ?: "Couldn't compile ${psiFile.name}", e, isFatal = true)
} finally {
handler.onFinish(file)
} }
} }
}.queue() }.queue()
@@ -192,14 +195,14 @@ class KtCompilingExecutor(file: ScratchFile) : ScratchExecutor(file) {
try { try {
AnalyzingUtils.checkForSyntacticErrors(psiFile) AnalyzingUtils.checkForSyntacticErrors(psiFile)
} catch (e: IllegalArgumentException) { } 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 return@runReadAction false
} }
val analysisResult = psiFile.analyzeWithAllCompilerChecks() val analysisResult = psiFile.analyzeWithAllCompilerChecks()
if (analysisResult.isError()) { 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 return@runReadAction false
} }
@@ -226,17 +229,13 @@ class KtCompilingExecutor(file: ScratchFile) : ScratchExecutor(file) {
handler.error(file, errorText) handler.error(file, errorText)
} }
} }
handler.onFinish(file)
return@runReadAction false return@runReadAction false
} }
return@runReadAction true return@runReadAction true
} }
} }
private fun error(message: String) {
handler.error(file, message)
handler.onFinish(file)
}
private fun ScratchFile.findExpression(psiElement: PsiElement): ScratchExpression? { private fun ScratchFile.findExpression(psiElement: PsiElement): ScratchExpression? {
val elementLine = psiElement.getLineNumber() val elementLine = psiElement.getLineNumber()
return runReadAction { getExpressions().firstOrNull { elementLine in it.lineStart..it.lineEnd } } 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) val lineWoPrefix = line.removePrefix(KtScratchSourceFileProcessor.GENERATED_OUTPUT_PREFIX)
if (isResultEnd(lineWoPrefix)) { if (isResultEnd(lineWoPrefix)) {
val extractedLineInfo = extractLineInfoFrom(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 (startLine, endLine) = extractedLineInfo
val scratchExpression = file.findExpression(startLine, endLine) val scratchExpression = file.findExpression(startLine, endLine)
if (scratchExpression == null) { if (scratchExpression == null) {
@@ -42,7 +42,7 @@ class KtScratchReplExecutor(file: ScratchFile) : ScratchExecutor(file) {
override fun execute() { override fun execute() {
handler.onStart(file) 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) val cmdLine = KotlinConsoleKeeper.createCommandLine(module)
LOG.printDebugMessage("Execute REPL: ${cmdLine.commandLineString}") LOG.printDebugMessage("Execute REPL: ${cmdLine.commandLineString}")
@@ -79,11 +79,6 @@ class KtScratchReplExecutor(file: ScratchFile) : ScratchExecutor(file) {
processInputOS.flush() processInputOS.flush()
} }
private fun error(file: ScratchFile, message: String) {
handler.error(file, message)
handler.onFinish(file)
}
private class ReplHistory { private class ReplHistory {
private var entries = arrayListOf<ScratchExpression>() private var entries = arrayListOf<ScratchExpression>()
private var processedEntriesCount: Int = 0 private var processedEntriesCount: Int = 0
+1 -2
View File
@@ -1,3 +1,2 @@
foo() // ERROR: Unresolved reference: foo foo() // ERROR: Unresolved reference: foo
/** unresolved.kts:1 Unresolved reference: foo */ /** unresolved.kts:1 Unresolved reference: foo */
/** Compilation Error */
+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: /** 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 <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 */ @HidesMembers public inline fun <K, V> Map<out ???, ???>.forEach(action: (Map.Entry<???, ???>) -> Unit): Unit defined in kotlin.collections */
/** unresolvedMultiline.kts:5 Unresolved reference: goo */ /** unresolvedMultiline.kts:5 Unresolved reference: goo */
/** Compilation Error */