[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
}