[jdk] Add SdkConfigurationUtil#createIncompleteSDK

This makes it possible to get an incomplete JDK in the new project wizard for detected JDKs, and initiate JDK paths setup in the background.

#IDEA-363220 Fixed
#IDEA-358703 Fixed

(cherry picked from commit 4b62235b1243df8d2bc8ff24bb7932ee954c7633)

GitOrigin-RevId: 130c333fd60ff654d550a5db1bf232de9c3adec0
This commit is contained in:
Louis Vignier
2024-11-18 11:40:09 +01:00
committed by intellij-monorepo-bot
parent 5f38ba337c
commit c830b530a1
4 changed files with 38 additions and 17 deletions

View File

@@ -140,7 +140,7 @@ fun projectWizardJdkComboBox(
val selected = combo.selectedItem
if (selected is DetectedJdk) {
registerJdk(selected.home, combo)
sdkProperty.set(service<AddJdkService>().createIncompleteJdk(selected.home))
}
setProjectJdk.invoke(sdkProperty.get())
@@ -463,21 +463,6 @@ private fun selectAndAddJdk(combo: ProjectWizardJdkComboBox) {
}
}
private fun registerJdk(path: String, combo: ProjectWizardJdkComboBox) {
service<AddJdkService>().createJdkFromPath(path) {
JdkComboBoxCollector.jdkRegistered(it)
combo.detectedJDKs.find { detected -> FileUtil.pathsEqual(detected.home, path) }?.let { item ->
combo.removeItem(item)
combo.detectedJDKs.remove(item)
}
val comboItem = ExistingJdk(it)
val index = combo.lastRegisteredJdkIndex
combo.registered.add(comboItem)
combo.insertItemAt(comboItem, index)
combo.selectedIndex = index
}
}
private fun addDownloadItem(extension: SdkDownload, combo: ComboBox<ProjectWizardJdkIntent>) {
val config = ProjectStructureConfigurable.getInstance(DefaultProjectFactory.getInstance().defaultProject)
combo.popup?.hide()

View File

@@ -19,6 +19,10 @@ import org.jetbrains.annotations.ApiStatus
@Service(Service.Level.APP)
@ApiStatus.Internal
class AddJdkService(private val coroutineScope: CoroutineScope) {
/**
* Creates a JDK and calls [onJdkAdded] later if JDK creation was successful.
* If you need to acquire a [Sdk] immediately, use [createIncompleteJdk].
*/
fun createJdkFromPath(path: String, onJdkAdded: (Sdk) -> Unit = {}) {
coroutineScope.launch {
val jdk = withContext(Dispatchers.EDT + ModalityState.any().asContextElement()) {
@@ -27,6 +31,21 @@ class AddJdkService(private val coroutineScope: CoroutineScope) {
if (jdk != null) onJdkAdded.invoke(jdk)
}
}
/**
* Creates and registers an incomplete JDK with a unique name pointing to the given [path].
* Initiates the JDK setup in a coroutine.
*/
fun createIncompleteJdk(path: String): Sdk? {
val jdkType = JavaSdk.getInstance()
val sdk = SdkConfigurationUtil.createIncompleteSDK(path, jdkType) ?: return null
coroutineScope.launch {
jdkType.setupSdkPaths(sdk)
}
return sdk
}
}
@Service(Service.Level.PROJECT)

View File

@@ -13922,6 +13922,7 @@ f:com.intellij.openapi.projectRoots.impl.SdkConfigurationUtil
- s:addSdk(com.intellij.openapi.projectRoots.Sdk):V
- s:configureDirectoryProjectSdk(com.intellij.openapi.project.Project,java.util.Comparator,com.intellij.openapi.projectRoots.SdkType[]):V
- s:createAndAddSDK(java.lang.String,com.intellij.openapi.projectRoots.SdkType):com.intellij.openapi.projectRoots.Sdk
- s:createIncompleteSDK(java.lang.String,com.intellij.openapi.projectRoots.SdkType):com.intellij.openapi.projectRoots.Sdk
- s:createSdk(com.intellij.openapi.project.Project,com.intellij.openapi.projectRoots.Sdk[],com.intellij.util.NullableConsumer,Z,com.intellij.openapi.projectRoots.SdkType[]):V
- s:createSdk(com.intellij.openapi.project.Project,com.intellij.openapi.projectRoots.Sdk[],com.intellij.util.NullableConsumer,com.intellij.openapi.projectRoots.SdkType[]):V
- s:createSdk(java.util.Collection,com.intellij.openapi.vfs.VirtualFile,com.intellij.openapi.projectRoots.SdkType,com.intellij.openapi.projectRoots.SdkAdditionalData,java.lang.String):com.intellij.openapi.projectRoots.Sdk

View File

@@ -32,8 +32,8 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.ide.PooledThreadExecutor;
import java.awt.*;
import java.util.List;
import java.util.*;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@@ -293,6 +293,22 @@ public final class SdkConfigurationUtil {
return null;
}
/// Tries to create an SDK identified by path; if successful, add the SDK to the global SDK table.
/// Contrary to [#createAndAddSDK(String, SdkType)], SDK paths are not setup.
/// @param path identifies the SDK
/// @return newly created incomplete SDK, or null.
public static @Nullable Sdk createIncompleteSDK(@NotNull String path, @NotNull SdkType sdkType) {
VirtualFile sdkHome = WriteAction.compute(() -> {
return LocalFileSystem.getInstance().refreshAndFindFileByPath(FileUtil.toSystemIndependentName(path));
});
if (sdkHome == null) return null;
Sdk newSdk = createSdk(Arrays.asList(ProjectJdkTable.getInstance().getAllJdks()), sdkHome, sdkType, null, null);
addSdk(newSdk);
return newSdk;
}
public static @NotNull String createUniqueSdkName(@NotNull SdkType type, @NotNull String home, final Collection<? extends Sdk> sdks) {
return createUniqueSdkName(type.suggestSdkName(null, home), sdks);
}