Skip to content

Commit

Permalink
Try better interpreter names
Browse files Browse the repository at this point in the history
Signed-off-by: Bernát Gábor <[email protected]>
  • Loading branch information
gaborbernat committed Aug 12, 2024
1 parent 9088834 commit 4482228
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.gradle
.kotlin
.idea
.intellijPlatform
build
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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.diagnostic.Logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.Sdk
import com.intellij.openapi.projectRoots.impl.SdkConfigurationUtil
Expand All @@ -18,6 +19,8 @@ import com.jetbrains.python.statistics.executionType
import com.jetbrains.python.statistics.interpreterType

abstract class ConfigurePythonActionAbstract : AnAction() {
private val logger = Logger.getInstance(SdkConfigurationUtil::class.java)

override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT

override fun update(e: AnActionEvent) {
Expand All @@ -28,12 +31,14 @@ abstract class ConfigurePythonActionAbstract : AnAction() {
e.presentation.isEnabledAndVisible =
when (val selectedPath = e.getData(CommonDataKeys.VIRTUAL_FILE)) {
null -> false

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)
Expand All @@ -54,7 +59,7 @@ abstract class ConfigurePythonActionAbstract : AnAction() {
.projectSdks
.values
.firstOrNull { it.homePath == pythonExecutable }
?: (SdkConfigurationUtil.createAndAddSDK(pythonExecutable, PythonSdkType.getInstance()) ?: return)
?: (createSdk(pythonExecutable, project) ?: return)

val notificationFor = setSdk(project, selectedPath, sdk) ?: return
NotificationGroupManager
Expand All @@ -68,6 +73,40 @@ abstract class ConfigurePythonActionAbstract : AnAction() {
).notify(project)
}

private fun createSdk(
pythonExecutable: String,
project: Project,
): Sdk? {
logger.info("Create SDK for $pythonExecutable inside ${project.name}")
val sdk = SdkConfigurationUtil.createAndAddSDK(pythonExecutable, PythonSdkType.getInstance()) ?: return null

// create project level interpreters
// val sdk =
// SdkConfigurationUtil.setupSdk(
// ProjectJdkTable.getInstance().allJdks,
// sdkHome,
// PythonSdkType.getInstance(),
// false,
// null,
// null,
// )
// ?: return null

// LOG.info("Add SDK ${sdk.name} for ${project.name}")
// SdkConfigurationUtil.addSdk(sdk)

// set better name for interpreter
// val base =
// Paths.get(project.basePath ?: return null).relativize(
// (sdk.homeDirectory ?: return null)
// .toNioPath()
// .parent.parent,
// )
// sdk.sdkModificator.name = "${project.name} ${sdk.versionString} ($base)"
logger.info("Created SDK ${sdk.name} for ${project.name}")
return sdk
}

protected abstract fun setSdk(
project: Project,
selectedPath: VirtualFile,
Expand Down

0 comments on commit 4482228

Please sign in to comment.