[ide-console] Show warning notification after Build and Restart action in case of failed compilation

This commit is contained in:
Dmitry Kovanikov
2015-08-26 19:38:18 +03:00
committed by Pavel V. Talanov
parent 43f3c9d603
commit 3271fae7ac
3 changed files with 26 additions and 30 deletions
@@ -44,11 +44,11 @@ public class KotlinConsoleKeeper(val project: Project) {
fun putVirtualFileToConsole(virtualFile: VirtualFile, console: KotlinConsoleRunner) = consoleMap.put(virtualFile, console)
fun removeConsole(virtualFile: VirtualFile) = consoleMap.remove(virtualFile)
fun run(module: Module, testMode: Boolean = false): KotlinConsoleRunner? {
fun run(module: Module, previousCompilationFailed: Boolean = false, testMode: Boolean = false): KotlinConsoleRunner? {
val path = module.moduleFilePath
val cmdLine = createCommandLine(module) ?: return errorNotification(project, "Module SDK not found") let { null }
val consoleRunner = KotlinConsoleRunner(cmdLine, module, testMode, project, REPL_TITLE, path)
val consoleRunner = KotlinConsoleRunner(cmdLine, module, previousCompilationFailed, testMode, project, REPL_TITLE, path)
consoleRunner.initAndRun()
consoleRunner.setupGutters()
@@ -48,6 +48,7 @@ import kotlin.properties.Delegates
public class KotlinConsoleRunner(
private val cmdLine: GeneralCommandLine,
private val module: Module,
private val previousCompilationFailed: Boolean,
private val testMode: Boolean,
myProject: Project,
title: String,
@@ -105,7 +106,7 @@ public class KotlinConsoleRunner(
disposableDescriptor = contentDescriptor
val actionList = arrayListOf<AnAction>(
BuildAndRestartConsoleAction(project, module, defaultExecutor, contentDescriptor, restarter, testMode),
BuildAndRestartConsoleAction(project, module, defaultExecutor, contentDescriptor, previousCompilationFailed, testMode),
createConsoleExecAction(consoleExecuteActionHandler),
createCloseAction(defaultExecutor, contentDescriptor)
)
@@ -116,12 +117,6 @@ public class KotlinConsoleRunner(
override fun createConsoleExecAction(consoleExecuteActionHandler: ProcessBackedConsoleExecuteActionHandler)
= ConsoleExecuteAction(consoleView, consoleExecuteActionHandler, "KotlinShellExecute", consoleExecuteActionHandler)
private val restarter = object : Consumer<Module> {
override fun consume(module: Module) {
KotlinConsoleKeeper.getInstance(project).run(module)
}
}
override fun constructConsoleTitle(title: String) = "$title (in module ${module.name})"
private fun setupPlaceholder(editor: EditorEx) {
@@ -27,11 +27,10 @@ import com.intellij.notification.Notifications
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.compiler.CompileContext
import com.intellij.openapi.compiler.CompileStatusNotification
import com.intellij.openapi.compiler.CompilerManager
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.util.Consumer
import org.jetbrains.kotlin.console.KotlinConsoleKeeper
import javax.swing.event.HyperlinkEvent
private fun buildAndRestartMessage(module: Module) = "Build module '${module.name}' and restart"
@@ -41,33 +40,36 @@ public class BuildAndRestartConsoleAction(
private val module: Module,
private val executor: Executor,
private val contentDescriptor: RunContentDescriptor,
private val restarter: Consumer<Module>,
private val previousCompilationFailed: Boolean,
private val testMode: Boolean
) : AnAction("Build and restart", buildAndRestartMessage(module), AllIcons.Actions.Restart) {
init {
if (!testMode) showOutdatedClassedNotificationIfNeeded()
if (!testMode && !previousCompilationFailed) showOutdatedClassesNotificationIfNeeded()
}
override fun actionPerformed(_: AnActionEvent) = compileModule()
private fun compileModule() {
if (ExecutionManager.getInstance(project).contentManager.removeRunContent(executor, contentDescriptor)) {
CompilerManager.getInstance(project).make(module, object : CompileStatusNotification {
override fun finished(aborted: Boolean, errors: Int, warnings: Int, compileContext: CompileContext): Unit
= if (!module.isDisposed) restarter.consume(module)
})
CompilerManager.getInstance(project).make(module) {
aborted: Boolean, errors: Int, warnings: Int, compileContext: CompileContext ->
if (!module.isDisposed) {
val compilationFailed = aborted || errors > 0
if (compilationFailed) warningNotification("Compilation wasn't ended properly")
KotlinConsoleKeeper.getInstance(project).run(module, previousCompilationFailed = compilationFailed)
}
}
}
}
private fun showOutdatedClassedNotificationIfNeeded() {
private fun showOutdatedClassesNotificationIfNeeded() {
val compilerManager = CompilerManager.getInstance(project)
val compilerScope = compilerManager.createModuleCompileScope(module, true)
if (compilerManager.isUpToDate(compilerScope)) return
val warningTag = "KOTLIN REPL WARNING"
val warningTitle = "Kotlin REPL Configuration Warning"
val message = "Youre running the REPL with outdated classes<br><a href=\"Build and restart\">${buildAndRestartMessage(module)}</a>"
val hyperlinkListener = object : NotificationListener {
override fun hyperlinkUpdate(notification: Notification, event: HyperlinkEvent) {
if (event.eventType == HyperlinkEvent.EventType.ACTIVATED) {
@@ -77,14 +79,13 @@ public class BuildAndRestartConsoleAction(
}
}
Notifications.Bus.notify(
Notification(
warningTag, warningTitle,
"Youre running the REPL with outdated classes<br><a href=\"Build and restart\">${buildAndRestartMessage(module)}</a>",
NotificationType.WARNING,
hyperlinkListener
),
project
)
warningNotification(message, hyperlinkListener)
}
private fun warningNotification(message: String, hyperlinkListener: NotificationListener? = null) {
val warningTag = "KOTLIN REPL WARNING"
val warningTitle = "Kotlin REPL Configuration Warning"
Notifications.Bus.notify(Notification(warningTag, warningTitle, message, NotificationType.WARNING, hyperlinkListener), project)
}
}