[python] cleanup: Introduce PathSortener.shorten and obey case sensitivity.

1. utility method added
2. paths checked with correct case sensitivity

GitOrigin-RevId: a2d0917df2a1e4f0a1d3d33fbb1f403d1342cf8c
This commit is contained in:
Ilya.Kazakevich
2025-11-14 05:37:37 +00:00
committed by intellij-monorepo-bot
parent 7d26f2e8a6
commit 3a3053b63b
2 changed files with 16 additions and 20 deletions
@@ -4,10 +4,7 @@ package com.jetbrains.python
import com.intellij.openapi.diagnostic.fileLogger
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.NlsSafe
import com.intellij.platform.eel.EelApi
import com.intellij.platform.eel.EelExecApi
import com.intellij.platform.eel.environmentVariables
import com.intellij.platform.eel.isWindows
import com.intellij.platform.eel.*
import com.intellij.platform.eel.path.EelPath
import com.intellij.platform.eel.provider.asEelPath
import com.intellij.platform.eel.provider.getEelDescriptor
@@ -21,9 +18,14 @@ import java.nio.file.Path
* 2. Convert paths *on the same eel* using [toString]
*/
@ApiStatus.Internal
class PathShortener private constructor(private val map: List<Pair<EelPath, String>>) {
class PathShortener private constructor(private val map: List<Pair<EelPath, String>>, private val ignoreCase: Boolean) {
companion object {
suspend fun create(project: Project): PathShortener = create(project.getEelDescriptor().toEelApi())
/**
* Reading env variables takes time. For multiple calls, prefer [create] and reuse.
*/
suspend fun shorten(path: Path): @NlsSafe String = create(path.getEelDescriptor().toEelApi()).toString(path)
suspend fun create(eelApi: EelApi): PathShortener {
val eelDescriptor = eelApi.descriptor
val map = buildList {
@@ -44,7 +46,12 @@ class PathShortener private constructor(private val map: List<Pair<EelPath, Stri
// Valid both for Windows and **nix
add(Pair(eelApi.fs.user.home, "~"))
}
return PathShortener(map)
val ignoreCase = when (eelApi.platform) {
is EelPlatform.Darwin -> true // Despite SUS, OS X is case-insensitive
is EelPlatform.Windows -> true
is EelPlatform.Posix -> false
}
return PathShortener(map, ignoreCase = ignoreCase)
}
private val logger = fileLogger()
@@ -58,7 +65,7 @@ class PathShortener private constructor(private val map: List<Pair<EelPath, Stri
var result = path.asEelPath().toString()
for ((key, replaceWith) in map) {
assert(pathDescriptor == key.descriptor) { "path is on $pathDescriptor, replacer is on ${key.descriptor}" }
result = result.replace(key.toString(), replaceWith)
result = result.replace(key.toString(), replaceWith, ignoreCase = ignoreCase)
}
return result
}
@@ -1,15 +1,13 @@
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.python.community.services.internal.impl
import com.intellij.platform.eel.EelPlatform
import com.intellij.platform.eel.provider.asNioPath
import com.intellij.platform.eel.provider.getEelDescriptor
import com.intellij.python.community.execService.python.validatePythonAndGetInfo
import com.intellij.python.community.services.internal.impl.VanillaPythonWithPythonInfoImpl.Companion.concurrentLimit
import com.intellij.python.community.services.internal.impl.VanillaPythonWithPythonInfoImpl.Companion.createByPythonBinary
import com.intellij.python.community.services.shared.PythonInfoComparator
import com.intellij.python.community.services.shared.PythonWithPythonInfo
import com.intellij.python.community.services.shared.VanillaPythonWithPythonInfo
import com.jetbrains.python.PathShortener
import com.jetbrains.python.PythonBinary
import com.jetbrains.python.PythonInfo
import com.jetbrains.python.Result
@@ -21,8 +19,6 @@ import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit
import org.jetbrains.annotations.ApiStatus.Internal
import org.jetbrains.annotations.Nls
import kotlin.io.path.pathString
import kotlin.io.path.relativeTo
@Internal
class VanillaPythonWithPythonInfoImpl internal constructor(
@@ -73,14 +69,7 @@ class VanillaPythonWithPythonInfoImpl internal constructor(
}
override suspend fun getReadableName(): @Nls String {
val eelApi = pythonBinary.getEelDescriptor().toEelApi()
val home = eelApi.userInfo.home.asNioPath()
val separator = when (eelApi.platform) {
is EelPlatform.Windows -> "\\"
is EelPlatform.Posix -> "/"
}
val pythonString = (if (pythonBinary.startsWith(home)) "~$separator" + pythonBinary.relativeTo(home).pathString
else pythonBinary.pathString)
val pythonString = PathShortener.shorten(pythonBinary)
return "$pythonString ($pythonInfo)"
}