Refactoring: use output handlers to make action enabled or disabled

This commit is contained in:
Natalia Selezneva
2018-12-10 09:20:12 +03:00
parent 38d836dece
commit 59a32ff94d
4 changed files with 40 additions and 29 deletions
@@ -26,4 +26,15 @@ abstract class ScratchExecutor(protected val file: ScratchFile) {
fun addOutputHandler(outputHandler: ScratchOutputHandler) { fun addOutputHandler(outputHandler: ScratchOutputHandler) {
handlers.add(outputHandler) 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)
}
} }
@@ -54,52 +54,48 @@ class RunScratchAction : ScratchAction(
val provider = ScratchFileLanguageProvider.get(psiFile.language) ?: return val provider = ScratchFileLanguageProvider.get(psiFile.language) ?: return
val handler = provider.getOutputHandler()
handler.onStart(scratchFile)
log.printDebugMessage("Run Action: isMakeBeforeRun = $isMakeBeforeRun, isRepl = $isRepl") log.printDebugMessage("Run Action: isMakeBeforeRun = $isMakeBeforeRun, isRepl = $isRepl")
val defaultOutputHandler = provider.getOutputHandler()
val module = scratchPanel.getModule() val module = scratchPanel.getModule()
@Suppress("FoldInitializerAndIfToElvis")
if (module == null) { if (module == null) {
handler.error(scratchFile, "Module should be selected") return defaultOutputHandler.error(scratchFile, "Module should be selected")
handler.onFinish(scratchFile)
return
} }
fun executeScratch() { val executor = if (isRepl) provider.createReplExecutor(scratchFile) else provider.createCompilingExecutor(scratchFile)
val executor = if (isRepl) provider.createReplExecutor(scratchFile) else provider.createCompilingExecutor(scratchFile)
if (executor == null) { @Suppress("FoldInitializerAndIfToElvis")
handler.error(scratchFile, "Couldn't run ${psiFile.name}") if (executor == null) {
handler.onFinish(scratchFile) return defaultOutputHandler.error(scratchFile, "Couldn't run ${psiFile.name}")
return }
executor.addOutputHandler(defaultOutputHandler)
executor.addOutputHandler(object : ScratchOutputHandlerAdapter() {
override fun onStart(file: ScratchFile) {
e.presentation.isEnabled = false
} }
e.presentation.isEnabled = false override fun onFinish(file: ScratchFile) {
e.presentation.isEnabled = true
executor.addOutputHandler(handler) }
})
executor.addOutputHandler(object : ScratchOutputHandlerAdapter() {
override fun onFinish(file: ScratchFile) {
e.presentation.isEnabled = true
}
})
fun executeScratch() {
try { try {
executor.execute() executor.execute()
} catch (ex: Throwable) { } catch (ex: Throwable) {
handler.error(scratchFile, "Exception occurs during Run Scratch Action") executor.errorOccurs("Exception occurs during Run Scratch Action", ex, true)
handler.onFinish(scratchFile)
e.presentation.isEnabled = true
log.error(ex)
} }
} }
if (isMakeBeforeRun) { if (isMakeBeforeRun) {
ProjectTaskManager.getInstance(project).build(arrayOf(module)) { result -> ProjectTaskManager.getInstance(project).build(arrayOf(module)) { result ->
if (result.isAborted || result.errors > 0) { 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)) { if (DumbService.isDumb(project)) {
@@ -59,6 +59,8 @@ class KtCompilingExecutor(file: ScratchFile) : ScratchExecutor(file) {
} }
override fun execute() { override fun execute() {
handlers.forEach { it.onStart(file) }
val module = file.getModule() ?: return error("Module should be selected") 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 psiFile = file.getPsiFile() as? KtFile ?: return error("Couldn't find KtFile for current editor")
@@ -40,7 +40,9 @@ class KtScratchReplExecutor(file: ScratchFile) : ScratchExecutor(file) {
private lateinit var osProcessHandler: OSProcessHandler private lateinit var osProcessHandler: OSProcessHandler
override fun execute() { 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) val cmdLine = KotlinConsoleKeeper.createCommandLine(module)
LOG.printDebugMessage("Execute REPL: ${cmdLine.commandLineString}") LOG.printDebugMessage("Execute REPL: ${cmdLine.commandLineString}")