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) {
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 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)) {
@@ -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")
@@ -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}")