Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.gradle
.idea
.intellijPlatform
.kotlin
build
38 changes: 0 additions & 38 deletions src/main/kotlin/com/github/pyvenvmanage/VenvIconProvider.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.pyvenvmanage

import com.intellij.ide.projectView.PresentationData
import com.intellij.ide.projectView.ProjectViewNode
import com.intellij.ide.projectView.ProjectViewNodeDecorator
import com.intellij.ui.SimpleTextAttributes

import com.jetbrains.python.icons.PythonIcons.Python.Virtualenv

class VenvProjectViewNodeDecorator : ProjectViewNodeDecorator {
override fun decorate(
node: ProjectViewNode<*>,
data: PresentationData,
) {
val pyVenvCfgPath = VenvUtils.getPyVenvCfg(node.getVirtualFile())
if (pyVenvCfgPath != null) {
val pythonVersion = VenvUtils.getPythonVersionFromPyVenv(pyvenvCfgPath = pyVenvCfgPath)
val fileName: String? = data.getPresentableText()
data.clearText()
data.addText(fileName, SimpleTextAttributes.REGULAR_ATTRIBUTES)
data.addText(" [" + pythonVersion + "]", SimpleTextAttributes.GRAY_ATTRIBUTES)
data.setIcon(Virtualenv)
}
}
}
50 changes: 50 additions & 0 deletions src/main/kotlin/com/github/pyvenvmanage/VenvUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.github.pyvenvmanage

import java.io.IOException
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Path
import java.util.Properties

import com.intellij.openapi.vfs.VirtualFile

import com.jetbrains.python.sdk.PythonSdkUtil

object VenvUtils {
@JvmStatic
fun getPyVenvCfg(file: VirtualFile?): Path? {
if (file != null && file.isDirectory()) {
val venvRootPath = file.getPath()
val pythonExecutable = PythonSdkUtil.getPythonExecutable(venvRootPath)
if (pythonExecutable != null) {
val pyvenvFile = file.findChild("pyvenv.cfg")
if (pyvenvFile != null) {
return Path.of(pyvenvFile.getPath())
}
}
}
return null
}

@JvmStatic
fun getPythonVersionFromPyVenv(pyvenvCfgPath: Path): String {
val unknownVersion = "unknown"

val props = Properties()

try {
Files.newBufferedReader(pyvenvCfgPath, StandardCharsets.UTF_8).use { reader ->
props.load(reader)
}
} catch (e: IOException) {
return unknownVersion // file could not be read
}

val version = props.getProperty("version_info")
if (version != null) {
return version.trim { it <= ' ' }
}

return unknownVersion
}
}
6 changes: 3 additions & 3 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<depends>com.intellij.modules.platform</depends>

<extensions defaultExtensionNs="com.intellij">
<notificationGroup id="Python SDK change" displayType="BALLOON"/>
<iconProvider implementation="com.github.pyvenvmanage.VenvIconProvider" />
<iconLayerProvider implementation="com.github.pyvenvmanage.VenvIconProvider" />
<notificationGroup id="SDK changed notification"
displayType="BALLOON"/>
<projectViewNodeDecorator implementation="com.github.pyvenvmanage.VenvProjectViewNodeDecorator"/>
</extensions>

<actions>
Expand Down