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
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,79 @@ abstract class ConfigurePythonActionAbstract : AnAction() {
e.getData(CommonDataKeys.VIRTUAL_FILE)?.let {
if (it.isDirectory) it else it.parent
} ?: return
val pythonExecutable = PythonSdkUtil.getPythonExecutable(selectedPath.path) ?: return

val pythonExecutable = PythonSdkUtil.getPythonExecutable(selectedPath.path)
if (pythonExecutable == null) {
notifyError(project, "No Python executable found in ${selectedPath.name}")
return
}

val sdk: Sdk =
PyConfigurableInterpreterList
.getInstance(project)
.model
.projectSdks
.values
.firstOrNull { it.homePath == pythonExecutable }
?: (SdkConfigurationUtil.createAndAddSDK(pythonExecutable, PythonSdkType.getInstance()) ?: return)
?: run {
val newSdk = SdkConfigurationUtil.createAndAddSDK(pythonExecutable, PythonSdkType.getInstance())
if (newSdk == null) {
notifyError(project, "Failed to create SDK from $pythonExecutable")
return
}
newSdk
}

when (val result = setSdk(project, selectedPath, sdk)) {
is SetSdkResult.Success -> notifySuccess(project, result.target, sdk)
is SetSdkResult.Error -> notifyError(project, result.message)
}
}

val notificationFor = setSdk(project, selectedPath, sdk) ?: return
private fun notifySuccess(
project: Project,
target: String,
sdk: Sdk,
) {
NotificationGroupManager
.getInstance()
.getNotificationGroup("Python SDK change")
.createNotification(
"Python SDK Updated",
"Updated SDK for $notificationFor to:\n${sdk.name} " +
"Updated SDK for $target to:\n${sdk.name} " +
"of type ${sdk.interpreterType.toString().lowercase()} " +
sdk.executionType.toString().lowercase(),
NotificationType.INFORMATION,
).notify(project)
}

private fun notifyError(
project: Project,
message: String,
) {
NotificationGroupManager
.getInstance()
.getNotificationGroup("Python SDK change")
.createNotification(
"Python SDK Error",
message,
NotificationType.ERROR,
).notify(project)
}

protected abstract fun setSdk(
project: Project,
selectedPath: VirtualFile,
sdk: Sdk,
): String?
): SetSdkResult

sealed class SetSdkResult {
data class Success(
val target: String,
) : SetSdkResult()

data class Error(
val message: String,
) : SetSdkResult()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ class ConfigurePythonActionModule : ConfigurePythonActionAbstract() {
project: Project,
selectedPath: VirtualFile,
sdk: Sdk,
): String? {
val module = ProjectFileIndex.getInstance(project).getModuleForFile(selectedPath, false) ?: return null
): SetSdkResult {
val module =
ProjectFileIndex.getInstance(project).getModuleForFile(selectedPath, false)
?: return SetSdkResult.Error("No module found for ${selectedPath.name}")
ModuleRootModificationUtil.setModuleSdk(module, sdk)
return "module ${module.name}"
return SetSdkResult.Success("module ${module.name}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class ConfigurePythonActionProject : ConfigurePythonActionAbstract() {
project: Project,
selectedPath: VirtualFile,
sdk: Sdk,
): String {
): SetSdkResult {
SdkConfigurationUtil.setDirectoryProjectSdk(project, sdk)
return "project ${project.name}"
return SetSdkResult.Success("project ${project.name}")
}
}