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 build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ repositories {

dependencies {
testImplementation(libs.jupiter)
testImplementation(libs.mockk)
testRuntimeOnly(libs.jupiterEngine)
testRuntimeOnly(libs.junitPlatformLauncher)
testRuntimeOnly("junit:junit:4.13.2") // legacy JUnit 4 support
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ intelliJPlatform = "2.10.5"
junitPlatformLauncher= "6.0.0"
jupiter = "6.0.1"
kotlin = "2.3.0"
mockk = "1.14.7"
kover = "0.9.4"
ktlint = "14.0.1"
remoteRobot = "0.11.23"
Expand All @@ -15,6 +16,7 @@ versionUpdate = "1.0.1"
[libraries]
jupiter = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "jupiter" }
jupiterEngine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "jupiter" }
mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
remoteRobot = { module = "com.intellij.remoterobot:remote-robot", version.ref = "remoteRobot" }
remoteRobotFixtures = { module = "com.intellij.remoterobot:remote-fixtures", version.ref = "remoteRobot" }
junitPlatformLauncher = { module = "org.junit.platform:junit-platform-launcher", version.ref = "junitPlatformLauncher" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.pyvenvmanage

import java.util.concurrent.ConcurrentHashMap

import com.intellij.ide.projectView.PresentationData
import com.intellij.ide.projectView.ProjectViewNode
import com.intellij.ide.projectView.ProjectViewNodeDecorator
Expand All @@ -8,18 +10,23 @@ import com.intellij.ui.SimpleTextAttributes
import com.jetbrains.python.icons.PythonIcons.Python.Virtualenv

class VenvProjectViewNodeDecorator : ProjectViewNodeDecorator {
private val versionCache = ConcurrentHashMap<String, String?>()

override fun decorate(
node: ProjectViewNode<*>,
data: PresentationData,
) {
val pyVenvCfgPath = VenvUtils.getPyVenvCfg(node.getVirtualFile())
if (pyVenvCfgPath != null) {
val pythonVersion = VenvUtils.getPythonVersionFromPyVenv(pyvenvCfgPath = pyVenvCfgPath)
if (pythonVersion != null) {
val fileName: String? = data.getPresentableText()
data.clearText()
data.addText(fileName, SimpleTextAttributes.REGULAR_ATTRIBUTES)
data.addText(" [$pythonVersion]", SimpleTextAttributes.GRAY_ATTRIBUTES)
VenvUtils.getPyVenvCfg(node.virtualFile)?.let { pyVenvCfgPath ->
val pythonVersion =
versionCache.computeIfAbsent(pyVenvCfgPath.toString()) {
VenvUtils.getPythonVersionFromPyVenv(pyVenvCfgPath)
}
pythonVersion?.let { version ->
data.presentableText?.let { fileName ->
data.clearText()
data.addText(fileName, SimpleTextAttributes.REGULAR_ATTRIBUTES)
data.addText(" [$version]", SimpleTextAttributes.GRAY_ATTRIBUTES)
}
}
data.setIcon(Virtualenv)
}
Expand Down
50 changes: 15 additions & 35 deletions src/main/kotlin/com/github/pyvenvmanage/VenvUtils.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.pyvenvmanage

import java.io.IOException
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Path
Expand All @@ -11,38 +10,19 @@ 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 props = Properties()

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

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

return null
}
fun getPyVenvCfg(file: VirtualFile?): Path? =
file
?.takeIf { it.isDirectory }
?.takeIf { PythonSdkUtil.getPythonExecutable(it.path) != null }
?.findChild("pyvenv.cfg")
?.let { Path.of(it.path) }

fun getPythonVersionFromPyVenv(pyvenvCfgPath: Path): String? =
runCatching {
Properties()
.apply {
Files.newBufferedReader(pyvenvCfgPath, StandardCharsets.UTF_8).use { load(it) }
}.getProperty("version")
?.trim()
}.getOrNull()
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.github.pyvenvmanage.actions

import com.intellij.notification.NotificationGroupManager
import com.intellij.notification.NotificationType
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.Sdk
import com.intellij.openapi.projectRoots.impl.SdkConfigurationUtil
import com.intellij.openapi.ui.MessageType
import com.intellij.openapi.vfs.VirtualFile

import com.jetbrains.python.configuration.PyConfigurableInterpreterList
Expand All @@ -21,36 +21,22 @@ abstract class ConfigurePythonActionAbstract : AnAction() {
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT

override fun update(e: AnActionEvent) {
// Enable action menu when the selected path is the:
// - virtual environment root
// - virtual environment binary (Scripts) folder
// - any files within the binary folder.
e.presentation.isEnabledAndVisible =
when (val selectedPath = e.getData(CommonDataKeys.VIRTUAL_FILE)) {
null -> {
false
e.getData(CommonDataKeys.VIRTUAL_FILE)?.let { selectedPath ->
if (selectedPath.isDirectory) {
PythonSdkUtil.getPythonExecutable(selectedPath.path) != null
} else {
PythonSdkUtil.isVirtualEnv(selectedPath.path)
}

else -> {
when (selectedPath.isDirectory) {
true -> {
// check if there is a python executable available under this folder -> name match for binary
PythonSdkUtil.getPythonExecutable(selectedPath.path) != null
}

false -> {
// check for presence of the activate_this.py + activate alongside or pyvenv.cfg above
PythonSdkUtil.isVirtualEnv(selectedPath.path)
}
}
}
}
} ?: false
}

override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return
var selectedPath = e.getData(CommonDataKeys.VIRTUAL_FILE) ?: return
selectedPath = if (selectedPath.isDirectory) selectedPath else selectedPath.parent
val selectedPath =
e.getData(CommonDataKeys.VIRTUAL_FILE)?.let {
if (it.isDirectory) it else it.parent
} ?: return
val pythonExecutable = PythonSdkUtil.getPythonExecutable(selectedPath.path) ?: return
val sdk: Sdk =
PyConfigurableInterpreterList
Expand All @@ -66,10 +52,11 @@ abstract class ConfigurePythonActionAbstract : AnAction() {
.getInstance()
.getNotificationGroup("Python SDK change")
.createNotification(
"Python SDK Updated",
"Updated SDK for $notificationFor to:\n${sdk.name} " +
"of type ${sdk.interpreterType.toString().lowercase()} " +
sdk.executionType.toString().lowercase(),
MessageType.INFO,
NotificationType.INFORMATION,
).notify(project)
}

Expand Down
57 changes: 57 additions & 0 deletions src/test/kotlin/com/github/pyvenvmanage/UITest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,61 @@ class UITest {
}
}
}

@Test
fun testVenvDirectoryShowsPythonVersion() {
remoteRobot.idea {
with(projectViewTree) {
// The venv directory should display the Python version in brackets
waitFor(ofSeconds(10)) {
hasText { it.text.contains("[") && it.text.contains("]") }
}
}
}
}

@Test
fun testVenvDirectoryHasVenvIcon() {
remoteRobot.idea {
with(projectViewTree) {
// Verify the venv directory is decorated (has venv text visible)
waitFor(ofSeconds(10)) {
hasText("ve")
}
}
}
}

@Test
fun testContextMenuOnNonVenvDirectory() {
remoteRobot.idea {
with(projectViewTree) {
// Right-click on a non-venv directory should not show interpreter options
findText("demo").click(MouseButton.RIGHT_BUTTON)
waitFor(ofSeconds(2)) {
// The action menu should be visible but interpreter options should not be enabled
runCatching {
remoteRobot.actionMenuItem("Set as Project Interpreter")
false // If found, test should handle it
}.getOrDefault(true) // If not found, that's expected
}
}
}
}

@Test
fun testContextMenuOnPythonFile() {
remoteRobot.idea {
with(projectViewTree) {
// Right-click on a Python file should not show interpreter options
findText("main.py").click(MouseButton.RIGHT_BUTTON)
waitFor(ofSeconds(2)) {
runCatching {
remoteRobot.actionMenuItem("Set as Project Interpreter")
false
}.getOrDefault(true)
}
}
}
}
}
Loading