From b48b3b3237de4c476108dd341b205265968202ce Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Fri, 10 Mar 2017 15:27:41 +0100 Subject: [PATCH] Show "Configure Kotlin" notification at most once after project opening Don't show notification after every sync; trigger it from rootsChanged handler because the root model is not yet updated at syncDone point #KT-16590 Fixed --- .../ui/KotlinConfigurationCheckerComponent.kt | 53 ++++++++++++------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/configuration/ui/KotlinConfigurationCheckerComponent.kt b/idea/src/org/jetbrains/kotlin/idea/configuration/ui/KotlinConfigurationCheckerComponent.kt index d71f5e22aea..d1ee3319e0e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/configuration/ui/KotlinConfigurationCheckerComponent.kt +++ b/idea/src/org/jetbrains/kotlin/idea/configuration/ui/KotlinConfigurationCheckerComponent.kt @@ -16,11 +16,14 @@ package org.jetbrains.kotlin.idea.configuration.ui +import com.intellij.ProjectTopics import com.intellij.notification.NotificationDisplayType import com.intellij.notification.NotificationsConfiguration import com.intellij.openapi.components.AbstractProjectComponent import com.intellij.openapi.project.DumbService import com.intellij.openapi.project.Project +import com.intellij.openapi.roots.ModuleRootAdapter +import com.intellij.openapi.roots.ModuleRootEvent import com.intellij.openapi.startup.StartupManager import org.jetbrains.kotlin.idea.configuration.getModulesWithKotlinFiles import org.jetbrains.kotlin.idea.configuration.showConfigureKotlinNotificationIfNeeded @@ -30,11 +33,27 @@ import org.jetbrains.kotlin.idea.versions.findOutdatedKotlinLibraries import org.jetbrains.kotlin.idea.versions.notifyOutdatedKotlinRuntime import java.util.concurrent.atomic.AtomicInteger -class KotlinConfigurationCheckerComponent protected constructor(project: Project) : AbstractProjectComponent(project) { - private var syncCount = AtomicInteger() +class KotlinConfigurationCheckerComponent(project: Project) : AbstractProjectComponent(project) { + private val syncDepth = AtomicInteger() + @Volatile private var notificationPostponed = false init { NotificationsConfiguration.getNotificationsConfiguration().register(CONFIGURE_NOTIFICATION_GROUP_ID, NotificationDisplayType.STICKY_BALLOON, true) + + val connection = project.messageBus.connect() + connection.subscribe(ProjectTopics.PROJECT_ROOTS, object : ModuleRootAdapter() { + override fun rootsChanged(event: ModuleRootEvent?) { + if (notificationPostponed && !isSyncing) { + DumbService.getInstance(myProject).smartInvokeLater { + if (!isSyncing) { + notificationPostponed = false + showConfigureKotlinNotificationIfNeeded(myProject, + collectModulesWithOutdatedRuntime(findOutdatedKotlinLibraries(myProject))) + } + } + } + } + }) } override fun projectOpened() { @@ -50,31 +69,27 @@ class KotlinConfigurationCheckerComponent protected constructor(project: Project if (!libraries.isEmpty()) { notifyOutdatedKotlinRuntime(myProject, libraries) } - if (syncCount.get() == 0) { + if (syncDepth.get() == 0) { showConfigureKotlinNotificationIfNeeded(myProject, collectModulesWithOutdatedRuntime(libraries)) } - } - } - } - - val isSyncing: Boolean get() = syncCount.get() > 0 - - fun syncStarted() { - syncCount.incrementAndGet() - } - - fun syncDone() { - if (syncCount.decrementAndGet() == 0) { - DumbService.getInstance(myProject).smartInvokeLater { - if (!isSyncing) { - showConfigureKotlinNotificationIfNeeded(myProject, - collectModulesWithOutdatedRuntime(findOutdatedKotlinLibraries(myProject))) + else { + notificationPostponed = true } } } } + val isSyncing: Boolean get() = syncDepth.get() > 0 + + fun syncStarted() { + syncDepth.incrementAndGet() + } + + fun syncDone() { + syncDepth.decrementAndGet() + } + companion object { val CONFIGURE_NOTIFICATION_GROUP_ID = "Configure Kotlin in Project"