From 388604d4a915015e3a1e1c87ff3da2d2e7dc9b61 Mon Sep 17 00:00:00 2001 From: Vitaly Legchilkin Date: Mon, 10 Mar 2025 19:30:43 +0100 Subject: [PATCH] [python] Code Review Changes(PY-60410) GitOrigin-RevId: c4054e33a65db334dd5c9c2a7e2e6a9b38766eb2 --- ...armCommunityCustomizationBundle.properties | 1 + .../configuration/PyHatchSdkConfiguration.kt | 9 ++++++--- .../com/intellij/python/hatch/cli/HatchEnv.kt | 8 +++++--- .../src/com/intellij/python/hatch/hatch.kt | 20 +++++++++++++++---- .../sdk/add/v2/hatch/HatchUIComponents.kt | 3 ++- .../python/sdk/hatch/HatchSdkFlavorAndData.kt | 1 + 6 files changed, 31 insertions(+), 11 deletions(-) diff --git a/python/ide/impl/resources/messages/PyCharmCommunityCustomizationBundle.properties b/python/ide/impl/resources/messages/PyCharmCommunityCustomizationBundle.properties index 1668677d1e66..2e8a742e15f6 100644 --- a/python/ide/impl/resources/messages/PyCharmCommunityCustomizationBundle.properties +++ b/python/ide/impl/resources/messages/PyCharmCommunityCustomizationBundle.properties @@ -59,6 +59,7 @@ sdk.notification.label.set.up.poetry.environment.from.pyproject.toml.dependencie notification.group.pro.advertiser=PyCharm Professional recommended sdk.set.up.hatch.environment=Set up Hatch 'default' environment +sdk.set.up.hatch.project.analysis=Hatch project analysis sdk.set.up.uv.environment=Set up an uv environment using {0} new.project.python.group.name=Python diff --git a/python/ide/impl/src/com/intellij/pycharm/community/ide/impl/configuration/PyHatchSdkConfiguration.kt b/python/ide/impl/src/com/intellij/pycharm/community/ide/impl/configuration/PyHatchSdkConfiguration.kt index 80f71f65d96f..9534732af30c 100644 --- a/python/ide/impl/src/com/intellij/pycharm/community/ide/impl/configuration/PyHatchSdkConfiguration.kt +++ b/python/ide/impl/src/com/intellij/pycharm/community/ide/impl/configuration/PyHatchSdkConfiguration.kt @@ -13,17 +13,20 @@ import com.intellij.python.hatch.getHatchService import com.intellij.util.concurrency.annotations.RequiresBackgroundThread import com.jetbrains.python.getOrNull import com.jetbrains.python.orLogException -import com.jetbrains.python.sdk.hatch.createSdk import com.jetbrains.python.sdk.configuration.PyProjectSdkConfigurationExtension +import com.jetbrains.python.sdk.hatch.createSdk -class PyHatchSdkConfiguration : PyProjectSdkConfigurationExtension { +internal class PyHatchSdkConfiguration : PyProjectSdkConfigurationExtension { companion object { private val LOGGER = Logger.getInstance(PyHatchSdkConfiguration::class.java) } @RequiresBackgroundThread override fun getIntention(module: Module): @IntentionName String? { - val isReadyAndHaveOwnership = runWithModalProgressBlocking(module.project, "Hatch Project Analysis") { + val isReadyAndHaveOwnership = runWithModalProgressBlocking( + module.project, + PyCharmCommunityCustomizationBundle.message("sdk.set.up.hatch.project.analysis") + ) { val hatchService = module.getHatchService().getOr { return@runWithModalProgressBlocking false } hatchService.isHatchManagedProject().getOrNull() == true } diff --git a/python/python-hatch/src/com/intellij/python/hatch/cli/HatchEnv.kt b/python/python-hatch/src/com/intellij/python/hatch/cli/HatchEnv.kt index a13947709b09..77616dfb3c4b 100644 --- a/python/python-hatch/src/com/intellij/python/hatch/cli/HatchEnv.kt +++ b/python/python-hatch/src/com/intellij/python/hatch/cli/HatchEnv.kt @@ -112,6 +112,10 @@ const val ENV_TYPE_VIRTUAL: String = "virtual" * Manage project environments */ class HatchEnv(runtime: HatchRuntime) : HatchCommand("env", runtime) { + companion object { + private val SHOW_RESPONSE_REGEX = """^\s+Standalone\s*\n((?:[+|].*[+|]\n)+)(?:\s+Matrices\s*\n((?:[+|].*[+|]\n)+))?$""".toRegex() + } + enum class CreateResult { Created, AlreadyExists, @@ -228,9 +232,7 @@ class HatchEnv(runtime: HatchRuntime) : HatchCommand("env", runtime) { suspend fun show(vararg envs: String, internal: Boolean = false): Result { val options = listOf(internal to "--internal").makeOptions() - val expectedOutput = """^\s+Standalone\s*\n((?:[+|].*[+|]\n)+)(?:\s+Matrices\s*\n((?:[+|].*[+|]\n)+))?$""".toRegex() - - return executeAndMatch("show", "--ascii", *options, *envs, expectedOutput = expectedOutput) { matchResult -> + return executeAndMatch("show", "--ascii", *options, *envs, expectedOutput = SHOW_RESPONSE_REGEX) { matchResult -> val (standaloneTable, matricesTable) = matchResult.destructured val standalone = standaloneTable.parseAsciiTable()?.parseHatchEnvironments()?.map { it.first } ?: emptyList() val matrices = matricesTable.parseAsciiTable()?.parseHatchEnvironments()?.mapNotNull { diff --git a/python/python-hatch/src/com/intellij/python/hatch/hatch.kt b/python/python-hatch/src/com/intellij/python/hatch/hatch.kt index 3a32c1b2ff9f..25dc71d1d427 100644 --- a/python/python-hatch/src/com/intellij/python/hatch/hatch.kt +++ b/python/python-hatch/src/com/intellij/python/hatch/hatch.kt @@ -42,20 +42,32 @@ class FileSystemOperationHatchError(eelFsError: EelFsError) : HatchError( data class HatchVirtualEnvironment( val hatchEnvironment: HatchEnvironment, - val pythonVirtualEnvironment: PythonVirtualEnvironment, + val pythonVirtualEnvironment: PythonVirtualEnvironment?, ) { companion object { val AVAILABLE_ENVIRONMENTS_FOR_NEW_PROJECT: List = listOf( - HatchVirtualEnvironment(HatchEnvironment.DEFAULT, PythonVirtualEnvironment.NotExisting()) + HatchVirtualEnvironment(HatchEnvironment.DEFAULT, null) ) } } +/** + * Represents a Python virtual environment that can be either existing or non-existing. + */ sealed interface PythonVirtualEnvironment { - val pythonHomePath: PythonHomePath? + val pythonHomePath: PythonHomePath + /** + * Represents an existing Python virtual environment. + * The environment was verified and the Python version was already discovered. + */ data class Existing(override val pythonHomePath: PythonHomePath, val pythonVersion: String) : PythonVirtualEnvironment - data class NotExisting(override val pythonHomePath: PythonHomePath? = null) : PythonVirtualEnvironment + + /** + * Represents a non-existing Python virtual environment. + * This class is used for cases where the Python virtual environment is expected or referenced but does not exist on the file system. + */ + data class NotExisting(override val pythonHomePath: PythonHomePath) : PythonVirtualEnvironment } data class ProjectStructure( diff --git a/python/src/com/jetbrains/python/sdk/add/v2/hatch/HatchUIComponents.kt b/python/src/com/jetbrains/python/sdk/add/v2/hatch/HatchUIComponents.kt index 224c344dadee..9d9e49de2326 100644 --- a/python/src/com/jetbrains/python/sdk/add/v2/hatch/HatchUIComponents.kt +++ b/python/src/com/jetbrains/python/sdk/add/v2/hatch/HatchUIComponents.kt @@ -76,12 +76,13 @@ private class HatchEnvComboBoxListCellRenderer : ColoredListCellRenderer AllIcons.Nodes.Folder is PythonVirtualEnvironment.Existing -> PythonIcons.Python.PythonClosed is PythonVirtualEnvironment.NotExisting -> AllIcons.Nodes.Folder } append(value.hatchEnvironment.name, SimpleTextAttributes.REGULAR_ATTRIBUTES) - value.pythonVirtualEnvironment.pythonHomePath?.let { pythonHomePath -> + value.pythonVirtualEnvironment?.pythonHomePath?.let { pythonHomePath -> append("\t", SimpleTextAttributes.REGULAR_ATTRIBUTES) append(pythonHomePath.toString(), SimpleTextAttributes.GRAYED_SMALL_ATTRIBUTES) } diff --git a/python/src/com/jetbrains/python/sdk/hatch/HatchSdkFlavorAndData.kt b/python/src/com/jetbrains/python/sdk/hatch/HatchSdkFlavorAndData.kt index e041e06c253d..f19829669674 100644 --- a/python/src/com/jetbrains/python/sdk/hatch/HatchSdkFlavorAndData.kt +++ b/python/src/com/jetbrains/python/sdk/hatch/HatchSdkFlavorAndData.kt @@ -14,6 +14,7 @@ object HatchSdkFlavor : CPythonSdkFlavor() { override fun getIcon(): Icon = PythonHatchIcons.Logo override fun getFlavorDataClass(): Class = HatchSdkFlavorData::class.java override fun isValidSdkPath(pathStr: String): Boolean = false + override fun isPlatformIndependent(): Boolean = true } class HatchSdkFlavorProvider : PythonFlavorProvider {