From 59a32ff94d89aa2f5ecdb6eea91f03047b66aa69 Mon Sep 17 00:00:00 2001 From: Natalia Selezneva Date: Mon, 10 Dec 2018 09:20:12 +0300 Subject: [PATCH] Refactoring: use output handlers to make action enabled or disabled --- .../kotlin/idea/scratch/ScratchExecutor.kt | 11 ++++ .../idea/scratch/actions/RunScratchAction.kt | 52 +++++++++---------- .../scratch/compile/KtCompilingExecutor.kt | 2 + .../scratch/repl/KtScratchReplExecutor.kt | 4 +- 4 files changed, 40 insertions(+), 29 deletions(-) diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ScratchExecutor.kt b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ScratchExecutor.kt index f48bca60ec7..b86808939fa 100644 --- a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ScratchExecutor.kt +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ScratchExecutor.kt @@ -26,4 +26,15 @@ abstract class ScratchExecutor(protected val file: ScratchFile) { fun addOutputHandler(outputHandler: ScratchOutputHandler) { handlers.add(outputHandler) } + + fun errorOccurs(message: String, e: Throwable? = null, isFatal: Boolean = false) { + handlers.forEach { + it.error(file, message) + if (isFatal) { + it.onFinish(file) + } + } + + if (e != null) LOG.error(e) + } } \ No newline at end of file diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/actions/RunScratchAction.kt b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/actions/RunScratchAction.kt index b3c2691ba15..42d391c4904 100644 --- a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/actions/RunScratchAction.kt +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/actions/RunScratchAction.kt @@ -54,52 +54,48 @@ class RunScratchAction : ScratchAction( val provider = ScratchFileLanguageProvider.get(psiFile.language) ?: return - val handler = provider.getOutputHandler() - handler.onStart(scratchFile) - log.printDebugMessage("Run Action: isMakeBeforeRun = $isMakeBeforeRun, isRepl = $isRepl") + val defaultOutputHandler = provider.getOutputHandler() + val module = scratchPanel.getModule() + + @Suppress("FoldInitializerAndIfToElvis") if (module == null) { - handler.error(scratchFile, "Module should be selected") - handler.onFinish(scratchFile) - return + return defaultOutputHandler.error(scratchFile, "Module should be selected") } - fun executeScratch() { - val executor = if (isRepl) provider.createReplExecutor(scratchFile) else provider.createCompilingExecutor(scratchFile) - if (executor == null) { - handler.error(scratchFile, "Couldn't run ${psiFile.name}") - handler.onFinish(scratchFile) - return + val executor = if (isRepl) provider.createReplExecutor(scratchFile) else provider.createCompilingExecutor(scratchFile) + + @Suppress("FoldInitializerAndIfToElvis") + if (executor == null) { + return defaultOutputHandler.error(scratchFile, "Couldn't run ${psiFile.name}") + } + + executor.addOutputHandler(defaultOutputHandler) + + executor.addOutputHandler(object : ScratchOutputHandlerAdapter() { + override fun onStart(file: ScratchFile) { + e.presentation.isEnabled = false } - e.presentation.isEnabled = false - - executor.addOutputHandler(handler) - - executor.addOutputHandler(object : ScratchOutputHandlerAdapter() { - override fun onFinish(file: ScratchFile) { - e.presentation.isEnabled = true - } - }) + override fun onFinish(file: ScratchFile) { + e.presentation.isEnabled = true + } + }) + fun executeScratch() { try { executor.execute() } catch (ex: Throwable) { - handler.error(scratchFile, "Exception occurs during Run Scratch Action") - handler.onFinish(scratchFile) - - e.presentation.isEnabled = true - - log.error(ex) + executor.errorOccurs("Exception occurs during Run Scratch Action", ex, true) } } if (isMakeBeforeRun) { ProjectTaskManager.getInstance(project).build(arrayOf(module)) { result -> if (result.isAborted || result.errors > 0) { - handler.error(scratchFile, "There were compilation errors in module ${module.name}") + executor.errorOccurs("There were compilation errors in module ${module.name}") } if (DumbService.isDumb(project)) { 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 8069bc9dda1..babedf45b7d 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 @@ -59,6 +59,8 @@ class KtCompilingExecutor(file: ScratchFile) : ScratchExecutor(file) { } override fun execute() { + handlers.forEach { it.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") 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 e0d903e1507..1089a7f4842 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 @@ -40,7 +40,9 @@ class KtScratchReplExecutor(file: ScratchFile) : ScratchExecutor(file) { private lateinit var osProcessHandler: OSProcessHandler override fun execute() { - val module = file.getModule() ?: return error(file, "Module should be selected") + handlers.forEach { it.onStart(file) } + + val module = file.getModule() ?: return error("Module should be selected") val cmdLine = KotlinConsoleKeeper.createCommandLine(module) LOG.printDebugMessage("Execute REPL: ${cmdLine.commandLineString}")