[kotlin] K2: Simplify the process of finding JDKs for script

^KTIJ-30457 Fixed

GitOrigin-RevId: fbe83795b15e41bfc76f96d00119ee3cb28d49b9
This commit is contained in:
Vlad Koshkin
2024-07-01 15:02:38 +02:00
committed by intellij-monorepo-bot
parent e3f974fe8c
commit 3d85ac63df
5 changed files with 7 additions and 20 deletions

View File

@@ -64,5 +64,6 @@
<orderEntry type="module" module-name="intellij.platform.backend.workspace" />
<orderEntry type="module" module-name="kotlin.code-insight.utils" />
<orderEntry type="module" module-name="intellij.platform.workspace.jps" />
<orderEntry type="module" module-name="intellij.platform.externalSystem.impl" />
</component>
</module>

View File

@@ -3,13 +3,13 @@ package org.jetbrains.kotlin.idea.core.script
import com.intellij.openapi.components.service
import com.intellij.openapi.components.serviceIfCreated
import com.intellij.openapi.externalSystem.service.execution.ExternalSystemJdkUtil
import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.JavaSdkType
import com.intellij.openapi.projectRoots.ProjectJdkTable
import com.intellij.openapi.projectRoots.Sdk
import com.intellij.openapi.roots.OrderRootType
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.search.NonClasspathDirectoriesScope.compose
@@ -25,6 +25,7 @@ import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.read
import kotlin.concurrent.write
import kotlin.io.path.pathString
import kotlin.script.experimental.api.*
import kotlin.script.experimental.jvm.JvmDependency
import kotlin.script.experimental.jvm.jdkHome
@@ -75,18 +76,6 @@ class K2ScriptDependenciesProvider(project: Project) : ScriptDependenciesProvide
return compose(roots + sdk)
}
private fun getScriptSdkByJavaHome(javaHome: Path): Sdk? {
// workaround for mismatched gradle wrapper and plugin version
val javaHomeVF = try {
VfsUtil.findFile(javaHome, true)
} catch (e: Throwable) {
null
} ?: return null
return ProjectJdkTable.getInstance().allJdks
.firstOrNull { it.homeDirectory == javaHomeVF && it.canBeUsedForScript() }
}
private fun getDefaultSdk(): Sdk? {
val projectSdk = ProjectRootManager.getInstance(project).projectSdk?.takeIf { it.canBeUsedForScript() }
if (projectSdk != null) return projectSdk
@@ -148,10 +137,7 @@ class K2ScriptDependenciesProvider(project: Project) : ScriptDependenciesProvide
classes.addAll(toVfsRoots(configurationWrapper.dependenciesClassPath))
sources.addAll(toVfsRoots(configurationWrapper.dependenciesSources))
configurationWrapper.javaHome?.toPath()?.let {
val sdk = getScriptSdkByJavaHome(it)
if (sdk != null) {
sdks[it] = sdk
}
sdks[it] = ExternalSystemJdkUtil.lookupJdkByPath(it.pathString)
}
counter++

View File

@@ -26,7 +26,7 @@ abstract class AbstractK2HighlightingMetaInfoTest : AbstractHighlightingMetaInfo
val psiFile = files.first()
if (psiFile is KtFile && psiFile.isScript()) {
val scriptModel = ScriptModel(psiFile.virtualFile)
K2ScriptDependenciesProvider.getInstance(project).reloadConfigurations(setOf(scriptModel), System.getProperty("java.home"))
K2ScriptDependenciesProvider.getInstance(project).reloadConfigurations(setOf(scriptModel), null)
}
super.doMultiFileTest(files, globalDirectives)

View File

@@ -1,5 +1,5 @@
// FIR_IDENTICAL
// IGNORE_K1
// IGNORE_K2
class MyScriptClass {
fun memberFunction(i: Int) {

View File

@@ -1,5 +1,5 @@
// FIR_IDENTICAL
// IGNORE_K1
// IGNORE_K2
class MyScriptClass {
fun memberFunction(i: Int) {