reload vfs upon packaging operations to show any possible changes to project files; #PY-78157 fixed

(cherry picked from commit d7d41aa8cccec13c74736fbee036a9fcd78edeae)

GitOrigin-RevId: 72592940511dd4b3b18a6d44e27e4121bb74a204
This commit is contained in:
Aleksandr Sorotskii
2024-12-18 19:24:12 +01:00
committed by intellij-monorepo-bot
parent 2df265c932
commit 170ad83859

View File

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