diff --git a/python/src/com/jetbrains/python/packaging/toolwindow/PyPackagingToolWindowService.kt b/python/src/com/jetbrains/python/packaging/toolwindow/PyPackagingToolWindowService.kt index 34e31d7f7d0e..73e4cfddf7db 100644 --- a/python/src/com/jetbrains/python/packaging/toolwindow/PyPackagingToolWindowService.kt +++ b/python/src/com/jetbrains/python/packaging/toolwindow/PyPackagingToolWindowService.kt @@ -4,8 +4,10 @@ package com.jetbrains.python.packaging.toolwindow import com.intellij.notification.NotificationGroupManager import com.intellij.notification.NotificationType import com.intellij.openapi.Disposable +import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.EDT import com.intellij.openapi.application.readAction +import com.intellij.openapi.application.writeAction import com.intellij.openapi.components.Service import com.intellij.openapi.components.service import com.intellij.openapi.diagnostic.thisLogger @@ -18,6 +20,7 @@ import com.intellij.openapi.projectRoots.Sdk import com.intellij.openapi.roots.ModuleRootEvent import com.intellij.openapi.roots.ModuleRootListener import com.intellij.openapi.util.text.StringUtil +import com.intellij.openapi.vfs.VirtualFileManager import com.intellij.platform.ide.progress.withBackgroundProgress import com.intellij.platform.util.progress.reportRawProgress import com.jetbrains.python.PyBundle @@ -39,6 +42,7 @@ import com.jetbrains.python.sdk.pythonSdk import com.jetbrains.python.statistics.modules import kotlinx.coroutines.* import org.jetbrains.annotations.Nls +import kotlin.coroutines.CoroutineContext @Service(Service.Level.PROJECT) class PyPackagingToolWindowService(val project: Project, val serviceScope: CoroutineScope) : Disposable { @@ -108,44 +112,56 @@ class PyPackagingToolWindowService(val project: Project, val serviceScope: Corou suspend fun installPackage(specification: PythonPackageSpecification, options: List = emptyList()) { PythonPackagesToolwindowStatisticsCollector.installPackageEvent.log(project) - val result = runPackagingOperationOrShowErrorDialog(manager.sdk, message("python.new.project.install.failed.title", specification.name), specification.name) { manager.installPackage(specification, options) } - if (result.isSuccess) showPackagingNotification(message("python.packaging.notification.installed", specification.name)) + + if (result.isSuccess) { + handleActionCompleted(message("python.packaging.notification.installed", specification.name)) + } } suspend fun deletePackage(selectedPackage: InstalledPackage) { PythonPackagesToolwindowStatisticsCollector.uninstallPackageEvent.log(project) - val result = runPackagingOperationOrShowErrorDialog(manager.sdk, message("python.packaging.operation.failed.title")) { + val result = runPackagingOperationOrShowErrorDialog(manager.sdk, message("python.packaging.operation.failed.title")) { manager.uninstallPackage(selectedPackage.instance) } - if (result.isSuccess) showPackagingNotification(message("python.packaging.notification.deleted", selectedPackage.name)) + + if (result.isSuccess) { + handleActionCompleted(message("python.packaging.notification.deleted", selectedPackage.name)) + } } suspend fun updatePackage(specification: PythonPackageSpecification) { val result = runPackagingOperationOrShowErrorDialog(manager.sdk, message("python.packaging.notification.update.failed", specification.name), specification.name) { manager.updatePackage(specification) } - if (result.isSuccess) showPackagingNotification(message("python.packaging.notification.updated", specification.name, specification.versionSpecs)) + + if (result.isSuccess) { + handleActionCompleted(message("python.packaging.notification.updated", specification.name, specification.versionSpecs)) + } } internal suspend fun initForSdk(sdk: Sdk?) { if (sdk == null) { toolWindowPanel?.packageListController?.setLoadingState(false) } - if (sdk == currentSdk) + + if (sdk == currentSdk) { return + } withContext(Dispatchers.EDT) { toolWindowPanel?.startLoadingSdk() } + val previousSdk = currentSdk currentSdk = sdk if (sdk == null) { return } - manager = PythonPackageManager.forSdk(project, currentSdk!!) + + manager = PythonPackageManager.forSdk(project, sdk) manager.repositoryManager.initCaches() runPackagingOperationOrShowErrorDialog(sdk, message("python.packaging.operation.failed.title")) { manager.reloadPackages() @@ -191,7 +207,6 @@ class PyPackagingToolWindowService(val project: Project, val serviceScope: Corou }) } - suspend fun refreshInstalledPackages() { val packages = manager.installedPackages.map { val repository = installedPackages.values.find { pkg -> pkg.name == it.name }?.repository ?: PyEmptyPackagePackageRepository @@ -259,6 +274,11 @@ class PyPackagingToolWindowService(val project: Project, val serviceScope: Corou } } + private suspend fun handleActionCompleted(text: @Nls String) { + VirtualFileManager.getInstance().asyncRefresh() + showPackagingNotification(text) + } + private suspend fun showPackagingNotification(text: @Nls String) { val notification = NotificationGroupManager.getInstance() .getNotificationGroup("PythonPackages") @@ -291,7 +311,6 @@ class PyPackagingToolWindowService(val project: Project, val serviceScope: Corou serviceScope.cancel() } - fun reloadPackages() { serviceScope.launch(Dispatchers.IO) { withBackgroundProgress(project, message("python.packaging.loading.packages.progress.text"), cancellable = false) {