@@ -2,15 +2,9 @@ package org.elixir_lang.action
22
33import com.intellij.openapi.actionSystem.AnAction
44import com.intellij.openapi.actionSystem.AnActionEvent
5- import com.intellij.openapi.application.ApplicationManager
6- import com.intellij.openapi.application.edtWriteAction
7- import com.intellij.openapi.progress.runBlockingCancellable
8- import com.intellij.openapi.projectRoots.ProjectJdkTable
9- import com.intellij.openapi.projectRoots.impl.ProjectJdkImpl
105import org.elixir_lang.notification.setup_sdk.Notifier
11- import org.elixir_lang.sdk.elixir.Type as ElixirSdkType
6+ import org.elixir_lang.sdk.SdkRegistrar
127import org.elixir_lang.sdk.erlang.Type as ErlangSdkType
13- import org.elixir_lang.sdk.erlang_dependent.SdkAdditionalData
148import java.io.File
159
1610/* *
@@ -56,7 +50,7 @@ class AddDevelopmentSdksAction : AnAction() {
5650
5751 // Create Elixir SDK
5852 if (! elixirPath.isNullOrBlank()) {
59- val createdElixirSdk = createElixirSdk(elixirPath, erlangSdk)
53+ val createdElixirSdk = createElixirSdk(elixirPath, erlangSdk, project )
6054 elixirSdkCreated = createdElixirSdk != null
6155 if (elixirSdkCreated) {
6256 Notifier .sdkRefreshSuccess(project, 1 , 1 , 0 , 0 )
@@ -76,91 +70,30 @@ class AddDevelopmentSdksAction : AnAction() {
7670 return null
7771 }
7872
79- val projectJdkTable = ProjectJdkTable .getInstance()
80- val erlangSdkType = ErlangSdkType ()
81-
82- // Check if SDK with this home path already exists
83- val existingSdk = projectJdkTable.allJdks.find {
84- it.sdkType is ErlangSdkType && it.homePath == homePath
85- }
86- if (existingSdk != null ) {
87- return existingSdk
88- }
89-
90- val sdkName = erlangSdkType.suggestSdkName(" Erlang (Dev)" , homePath)
91- val sdk = ProjectJdkImpl (sdkName, erlangSdkType)
92-
93- val modificator = sdk.sdkModificator
94- modificator.homePath = homePath
95-
96- if (sdk.versionString == null ) {
97- return null
98- }
99-
100- modificator.commitChanges()
101-
102- runBlockingCancellable {
103- edtWriteAction {
104- projectJdkTable.addJdk(sdk)
105- erlangSdkType.setupSdkPaths(sdk)
106- }
107- }
108-
109- return sdk
73+ return SdkRegistrar .registerOrUpdateErlangSdk(homePath)
11074 }
11175
11276 private fun createElixirSdk (
11377 homePath : String ,
114- erlangSdk : com.intellij.openapi.projectRoots.Sdk ?
78+ erlangSdk : com.intellij.openapi.projectRoots.Sdk ? ,
79+ project : com.intellij.openapi.project.Project
11580 ): com.intellij.openapi.projectRoots.Sdk ? {
11681 if (! File (homePath).exists()) {
11782 return null
11883 }
11984
120- val projectJdkTable = ProjectJdkTable .getInstance()
121- val elixirSdkType = ElixirSdkType .instance
122-
123- // Check if SDK with this home path already exists
124- val existingSdk = projectJdkTable.allJdks.find {
125- it.sdkType is ElixirSdkType && it.homePath == homePath
126- }
127- if (existingSdk != null ) {
128- return existingSdk
129- }
130-
131- val sdkName = elixirSdkType.suggestSdkName(" Elixir (Dev)" , homePath)
132- val sdk = ProjectJdkImpl (sdkName, elixirSdkType)
133-
134- val modificator = sdk.sdkModificator
135- modificator.homePath = homePath
136-
137- if (sdk.versionString == null ) {
138- return null
139- }
140-
141- // Set the Erlang SDK as internal dependency if available
14285 val actualErlangSdk = erlangSdk ? : findExistingErlangSdk()
143- if (actualErlangSdk != null ) {
144- modificator.sdkAdditionalData = SdkAdditionalData (actualErlangSdk, sdk)
145- }
146-
147- modificator.commitChanges()
148-
149- runBlockingCancellable {
150- edtWriteAction {
151- projectJdkTable.addJdk(sdk)
152- elixirSdkType.setupSdkPaths(sdk)
153- }
154- }
155-
156- return sdk
86+ return SdkRegistrar .registerOrUpdateElixirSdk(
87+ homePath = homePath,
88+ erlangSdk = actualErlangSdk,
89+ project = project,
90+ )
15791 }
15892
159- private fun findExistingErlangSdk (): com.intellij.openapi.projectRoots.Sdk ? {
160- return ProjectJdkTable .getInstance().allJdks.find {
161- it.sdkType is ErlangSdkType
162- }
163- }
93+ private fun findExistingErlangSdk (): com.intellij.openapi.projectRoots.Sdk ? =
94+ com.intellij.openapi.projectRoots.ProjectJdkTable .getInstance()
95+ .getSdksOfType(ErlangSdkType .instance)
96+ .firstOrNull()
16497
16598 override fun update (e : AnActionEvent ) {
16699 // Only enable if we have SDK paths configured
0 commit comments