From c7d39b612cf2fe0b9937bcf5d75a118bf7c52c4a Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Fri, 27 Dec 2019 16:49:03 +0300 Subject: [PATCH] Simplify adding Kotlin sdk by inlining internals of ProjectSdksModel Because of using cloning sdks inside ProjectSdksModel tests fail with sdk leaked errors. --- .../kotlin/idea/framework/KotlinSdkType.kt | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/framework/KotlinSdkType.kt b/idea/src/org/jetbrains/kotlin/idea/framework/KotlinSdkType.kt index b56e25013d6..154e0206ee5 100644 --- a/idea/src/org/jetbrains/kotlin/idea/framework/KotlinSdkType.kt +++ b/idea/src/org/jetbrains/kotlin/idea/framework/KotlinSdkType.kt @@ -9,7 +9,6 @@ import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.projectRoots.* import com.intellij.openapi.projectRoots.impl.ProjectJdkImpl import com.intellij.openapi.projectRoots.impl.SdkConfigurationUtil -import com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel import com.intellij.util.Consumer import org.jdom.Element import org.jetbrains.kotlin.idea.KotlinIcons @@ -28,13 +27,19 @@ class KotlinSdkType : SdkType("KotlinSDK") { @JvmOverloads fun setUpIfNeeded(checkIfNeeded: () -> Boolean = { true }) { - with(ProjectSdksModel()) { - reset(null) - if (sdks.any { it.sdkType is KotlinSdkType }) return - if (!checkIfNeeded()) return //do not create Kotlin SDK - addSdk(INSTANCE, defaultHomePath, null) - ApplicationManager.getApplication().invokeAndWait { - runWriteAction { apply(null, true) } + val projectSdks: Array = ProjectJdkTable.getInstance().allJdks + if (projectSdks.any { it.sdkType is KotlinSdkType }) return + if (!checkIfNeeded()) return // do not create Kotlin SDK + + val newSdkName = SdkConfigurationUtil.createUniqueSdkName(INSTANCE, defaultHomePath, projectSdks.toList()) + val newJdk = ProjectJdkImpl(newSdkName, INSTANCE) + newJdk.homePath = defaultHomePath + INSTANCE.setupSdkPaths(newJdk) + + ApplicationManager.getApplication().invokeAndWait { + runWriteAction { + if (ProjectJdkTable.getInstance().allJdks.any { it.sdkType is KotlinSdkType }) return@runWriteAction + ProjectJdkTable.getInstance().addJdk(newJdk) } } }