Skip to content

Commit a824c95

Browse files
andraskgaborbernat
andauthored
Add support for showing python version in tree view (#51)
* Add support for showing python version in tree view Resolves: #10 * Update .gitignore --------- Co-authored-by: Bernát Gábor <[email protected]>
1 parent 7553ffb commit a824c95

File tree

5 files changed

+79
-41
lines changed

5 files changed

+79
-41
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.gradle
22
.idea
33
.intellijPlatform
4+
.kotlin
45
build

src/main/kotlin/com/github/pyvenvmanage/VenvIconProvider.kt

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.github.pyvenvmanage
2+
3+
import com.intellij.ide.projectView.PresentationData
4+
import com.intellij.ide.projectView.ProjectViewNode
5+
import com.intellij.ide.projectView.ProjectViewNodeDecorator
6+
import com.intellij.ui.SimpleTextAttributes
7+
8+
import com.jetbrains.python.icons.PythonIcons.Python.Virtualenv
9+
10+
class VenvProjectViewNodeDecorator : ProjectViewNodeDecorator {
11+
override fun decorate(
12+
node: ProjectViewNode<*>,
13+
data: PresentationData,
14+
) {
15+
val pyVenvCfgPath = VenvUtils.getPyVenvCfg(node.getVirtualFile())
16+
if (pyVenvCfgPath != null) {
17+
val pythonVersion = VenvUtils.getPythonVersionFromPyVenv(pyvenvCfgPath = pyVenvCfgPath)
18+
val fileName: String? = data.getPresentableText()
19+
data.clearText()
20+
data.addText(fileName, SimpleTextAttributes.REGULAR_ATTRIBUTES)
21+
data.addText(" [" + pythonVersion + "]", SimpleTextAttributes.GRAY_ATTRIBUTES)
22+
data.setIcon(Virtualenv)
23+
}
24+
}
25+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.github.pyvenvmanage
2+
3+
import java.io.IOException
4+
import java.nio.charset.StandardCharsets
5+
import java.nio.file.Files
6+
import java.nio.file.Path
7+
import java.util.Properties
8+
9+
import com.intellij.openapi.vfs.VirtualFile
10+
11+
import com.jetbrains.python.sdk.PythonSdkUtil
12+
13+
object VenvUtils {
14+
@JvmStatic
15+
fun getPyVenvCfg(file: VirtualFile?): Path? {
16+
if (file != null && file.isDirectory()) {
17+
val venvRootPath = file.getPath()
18+
val pythonExecutable = PythonSdkUtil.getPythonExecutable(venvRootPath)
19+
if (pythonExecutable != null) {
20+
val pyvenvFile = file.findChild("pyvenv.cfg")
21+
if (pyvenvFile != null) {
22+
return Path.of(pyvenvFile.getPath())
23+
}
24+
}
25+
}
26+
return null
27+
}
28+
29+
@JvmStatic
30+
fun getPythonVersionFromPyVenv(pyvenvCfgPath: Path): String {
31+
val unknownVersion = "unknown"
32+
33+
val props = Properties()
34+
35+
try {
36+
Files.newBufferedReader(pyvenvCfgPath, StandardCharsets.UTF_8).use { reader ->
37+
props.load(reader)
38+
}
39+
} catch (e: IOException) {
40+
return unknownVersion // file could not be read
41+
}
42+
43+
val version = props.getProperty("version_info")
44+
if (version != null) {
45+
return version.trim { it <= ' ' }
46+
}
47+
48+
return unknownVersion
49+
}
50+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<depends>com.intellij.modules.platform</depends>
88

99
<extensions defaultExtensionNs="com.intellij">
10-
<notificationGroup id="Python SDK change" displayType="BALLOON"/>
11-
<iconProvider implementation="com.github.pyvenvmanage.VenvIconProvider" />
12-
<iconLayerProvider implementation="com.github.pyvenvmanage.VenvIconProvider" />
10+
<notificationGroup id="SDK changed notification"
11+
displayType="BALLOON"/>
12+
<projectViewNodeDecorator implementation="com.github.pyvenvmanage.VenvProjectViewNodeDecorator"/>
1313
</extensions>
1414

1515
<actions>

0 commit comments

Comments
 (0)