Remove optimization in KotlinScriptDependenciesClassFinder
Scope can contains only classes for one script, but allScriptDependenciesScope contains classes for all scripts
This commit is contained in:
-6
@@ -24,12 +24,6 @@ class KotlinScriptDependenciesClassFinder(
|
|||||||
.getAllScriptsDependenciesClassFiles().filter { it.isValid }.toList()
|
.getAllScriptsDependenciesClassFiles().filter { it.isValid }.toList()
|
||||||
|
|
||||||
override fun findClass(qualifiedName: String, scope: GlobalSearchScope): PsiClass? {
|
override fun findClass(qualifiedName: String, scope: GlobalSearchScope): PsiClass? {
|
||||||
val scriptDependencies =
|
|
||||||
ScriptConfigurationManager.getInstance(project).getAllScriptsDependenciesClassFiles()
|
|
||||||
val firstJarInDependencies = scriptDependencies.firstOrNull() ?: return null
|
|
||||||
|
|
||||||
if (!scope.contains(firstJarInDependencies)) return null
|
|
||||||
|
|
||||||
val classByFileName = super.findClass(qualifiedName, scope)
|
val classByFileName = super.findClass(qualifiedName, scope)
|
||||||
if (classByFileName != null) {
|
if (classByFileName != null) {
|
||||||
return classByFileName.isInScope(scope)
|
return classByFileName.isInScope(scope)
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package custom.project;
|
||||||
|
|
||||||
|
public class Some {
|
||||||
|
// some class for library
|
||||||
|
}
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
This module is needed to have some module in tested project.
|
||||||
|
This will lead to absence of sdk roots in ScriptConfigurationManager.allScriptClassRoots,
|
||||||
|
because they are indexed by IDEA as module SDK roots
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
plugins()
|
||||||
|
|
||||||
|
// TEMPLATES: template-classes-names:custom.scriptDefinition.TemplateForFirstLoadedScript,custom.scriptDefinition.TemplateForMainScript
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
plugins()
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
package custom.project;
|
||||||
|
|
||||||
|
public class Project {
|
||||||
|
public void plugins() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
+74
@@ -0,0 +1,74 @@
|
|||||||
|
package custom.scriptDefinition
|
||||||
|
|
||||||
|
import java.io.File
|
||||||
|
import kotlin.script.experimental.annotations.KotlinScript
|
||||||
|
import kotlin.script.experimental.api.*
|
||||||
|
import kotlin.script.experimental.host.ScriptingHostConfiguration
|
||||||
|
import kotlin.script.experimental.host.ScriptingHostConfigurationKeys
|
||||||
|
import kotlin.script.experimental.jvm.JvmDependency
|
||||||
|
import kotlin.script.experimental.jvm.dependenciesFromClassContext
|
||||||
|
import kotlin.script.experimental.jvm.jdkHome
|
||||||
|
import kotlin.script.experimental.jvm.jvm
|
||||||
|
import kotlin.script.experimental.util.PropertiesCollection
|
||||||
|
|
||||||
|
val ScriptingHostConfigurationKeys.getEnvironment by PropertiesCollection.key<() -> Map<String, Any?>?>()
|
||||||
|
|
||||||
|
@KotlinScript(
|
||||||
|
displayName = "Definition for scriptFirstLoaded.mykts",
|
||||||
|
fileExtension = "mykts",
|
||||||
|
compilationConfiguration = DefinitionForFirstLoadedScript::class
|
||||||
|
)
|
||||||
|
open class TemplateForFirstLoadedScript(val args: Array<String>)
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
object DefinitionForFirstLoadedScript : ScriptCompilationConfiguration(
|
||||||
|
{
|
||||||
|
ide {
|
||||||
|
acceptedLocations(ScriptAcceptedLocation.Everywhere)
|
||||||
|
}
|
||||||
|
refineConfiguration {
|
||||||
|
beforeCompiling { context ->
|
||||||
|
val environment = context.compilationConfiguration[ScriptCompilationConfiguration.hostConfiguration]?.let {
|
||||||
|
it[ScriptingHostConfiguration.getEnvironment]?.invoke()
|
||||||
|
}.orEmpty()
|
||||||
|
|
||||||
|
// lib-classes as script dependencies: they will be the first in allScriptDependenciesClassFiles
|
||||||
|
context.compilationConfiguration.with {
|
||||||
|
dependencies(JvmDependency(environment["lib-classes"] as File))
|
||||||
|
}.asSuccess()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
@KotlinScript(
|
||||||
|
displayName = "Definition for main script",
|
||||||
|
fileExtension = "kts",
|
||||||
|
compilationConfiguration = DefinitionForMainScript::class
|
||||||
|
)
|
||||||
|
open class TemplateForMainScript(val args: Array<String>)
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
object DefinitionForMainScript : ScriptCompilationConfiguration(
|
||||||
|
{
|
||||||
|
baseClass(custom.project.Project::class)
|
||||||
|
ide {
|
||||||
|
acceptedLocations(ScriptAcceptedLocation.Everywhere)
|
||||||
|
}
|
||||||
|
refineConfiguration {
|
||||||
|
beforeCompiling { context ->
|
||||||
|
val environment = context.compilationConfiguration[ScriptCompilationConfiguration.hostConfiguration]?.let {
|
||||||
|
it[ScriptingHostConfiguration.getEnvironment]?.invoke()
|
||||||
|
}.orEmpty()
|
||||||
|
|
||||||
|
// template-classes as script dependencies: they will be the second in allScriptDependenciesClassFiles
|
||||||
|
context.compilationConfiguration.with {
|
||||||
|
dependencies(JvmDependency(environment["template-classes"] as File))
|
||||||
|
|
||||||
|
implicitReceivers(custom.project.Project::class)
|
||||||
|
}.asSuccess()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
@@ -114,13 +114,23 @@ abstract class AbstractScriptConfigurationTest : KotlinCompletionTestCase() {
|
|||||||
myModule = createTestModuleFromDir(it)
|
myModule = createTestModuleFromDir(it)
|
||||||
}
|
}
|
||||||
|
|
||||||
File(path).listFiles { file -> file.name.startsWith("module") }.filter { it.exists() }.forEach {
|
File(path)
|
||||||
val newModule = createTestModuleFromDir(it)
|
.listFiles { file -> file.name.startsWith("module") }
|
||||||
assert(myModule != null) { "Main module should exists" }
|
?.filter { it.exists() }
|
||||||
ModuleRootModificationUtil.addDependency(myModule, newModule)
|
?.forEach {
|
||||||
|
val newModule = createTestModuleFromDir(it)
|
||||||
|
assert(myModule != null) { "Main module should exists" }
|
||||||
|
ModuleRootModificationUtil.addDependency(myModule, newModule)
|
||||||
|
}
|
||||||
|
|
||||||
|
File(path).listFiles { file ->
|
||||||
|
file.name.startsWith("script") && file.name != SCRIPT_NAME
|
||||||
|
}?.forEach {
|
||||||
|
createFileAndSyncDependencies(it)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (module != null) {
|
// If script is inside module
|
||||||
|
if (module != null && mainScriptFile.parentFile.name.toLowerCase().contains("module")) {
|
||||||
module.addDependency(
|
module.addDependency(
|
||||||
projectLibrary(
|
projectLibrary(
|
||||||
"script-runtime",
|
"script-runtime",
|
||||||
@@ -337,18 +347,18 @@ abstract class AbstractScriptConfigurationTest : KotlinCompletionTestCase() {
|
|||||||
//TODO: tmpDir would be enough, but there is tricky fail under AS otherwise
|
//TODO: tmpDir would be enough, but there is tricky fail under AS otherwise
|
||||||
val outDir = KotlinTestUtils.tmpDirForReusableFolder("${getTestName(false)}${srcDir.name}Out")
|
val outDir = KotlinTestUtils.tmpDirForReusableFolder("${getTestName(false)}${srcDir.name}Out")
|
||||||
|
|
||||||
val kotlinSourceFiles = FileUtil.findFilesByMask(Pattern.compile(".+\\.kt$"), srcDir)
|
|
||||||
if (kotlinSourceFiles.isNotEmpty()) {
|
|
||||||
MockLibraryUtil.compileKotlin(srcDir.path, outDir, extraClasspath = *classpath)
|
|
||||||
}
|
|
||||||
|
|
||||||
val javaSourceFiles = FileUtil.findFilesByMask(Pattern.compile(".+\\.java$"), srcDir)
|
val javaSourceFiles = FileUtil.findFilesByMask(Pattern.compile(".+\\.java$"), srcDir)
|
||||||
if (javaSourceFiles.isNotEmpty()) {
|
if (javaSourceFiles.isNotEmpty()) {
|
||||||
KotlinTestUtils.compileJavaFiles(
|
KotlinTestUtils.compileJavaFiles(
|
||||||
javaSourceFiles,
|
javaSourceFiles,
|
||||||
listOf("-cp", StringUtil.join(listOf(*classpath, outDir), File.pathSeparator), "-d", outDir.path)
|
listOf("-cp", StringUtil.join(listOf(*classpath), File.pathSeparator), "-d", outDir.path)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
val kotlinSourceFiles = FileUtil.findFilesByMask(Pattern.compile(".+\\.kt$"), srcDir)
|
||||||
|
if (kotlinSourceFiles.isNotEmpty()) {
|
||||||
|
MockLibraryUtil.compileKotlin(srcDir.path, outDir, extraClasspath = *arrayOf(*classpath, outDir.path))
|
||||||
|
}
|
||||||
|
|
||||||
return outDir
|
return outDir
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+21
-11
@@ -114,13 +114,23 @@ abstract class AbstractScriptConfigurationTest : KotlinCompletionTestCase() {
|
|||||||
myModule = createTestModuleFromDir(it)
|
myModule = createTestModuleFromDir(it)
|
||||||
}
|
}
|
||||||
|
|
||||||
File(path).listFiles { file -> file.name.startsWith("module") }.filter { it.exists() }.forEach {
|
File(path)
|
||||||
val newModule = createTestModuleFromDir(it)
|
.listFiles { file -> file.name.startsWith("module") }
|
||||||
assert(myModule != null) { "Main module should exists" }
|
?.filter { it.exists() }
|
||||||
ModuleRootModificationUtil.addDependency(myModule, newModule)
|
?.forEach {
|
||||||
|
val newModule = createTestModuleFromDir(it)
|
||||||
|
assert(myModule != null) { "Main module should exists" }
|
||||||
|
ModuleRootModificationUtil.addDependency(myModule, newModule)
|
||||||
|
}
|
||||||
|
|
||||||
|
File(path).listFiles { file ->
|
||||||
|
file.name.startsWith("script") && file.name != SCRIPT_NAME
|
||||||
|
}?.forEach {
|
||||||
|
createFileAndSyncDependencies(it)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (module != null) {
|
// If script is inside module
|
||||||
|
if (module != null && mainScriptFile.parentFile.name.toLowerCase().contains("module")) {
|
||||||
module.addDependency(
|
module.addDependency(
|
||||||
projectLibrary(
|
projectLibrary(
|
||||||
"script-runtime",
|
"script-runtime",
|
||||||
@@ -337,18 +347,18 @@ abstract class AbstractScriptConfigurationTest : KotlinCompletionTestCase() {
|
|||||||
//TODO: tmpDir would be enough, but there is tricky fail under AS otherwise
|
//TODO: tmpDir would be enough, but there is tricky fail under AS otherwise
|
||||||
val outDir = KotlinTestUtils.tmpDirForReusableFolder("${getTestName(false)}${srcDir.name}Out")
|
val outDir = KotlinTestUtils.tmpDirForReusableFolder("${getTestName(false)}${srcDir.name}Out")
|
||||||
|
|
||||||
val kotlinSourceFiles = FileUtil.findFilesByMask(Pattern.compile(".+\\.kt$"), srcDir)
|
|
||||||
if (kotlinSourceFiles.isNotEmpty()) {
|
|
||||||
MockLibraryUtil.compileKotlin(srcDir.path, outDir, extraClasspath = *classpath)
|
|
||||||
}
|
|
||||||
|
|
||||||
val javaSourceFiles = FileUtil.findFilesByMask(Pattern.compile(".+\\.java$"), srcDir)
|
val javaSourceFiles = FileUtil.findFilesByMask(Pattern.compile(".+\\.java$"), srcDir)
|
||||||
if (javaSourceFiles.isNotEmpty()) {
|
if (javaSourceFiles.isNotEmpty()) {
|
||||||
KotlinTestUtils.compileJavaFiles(
|
KotlinTestUtils.compileJavaFiles(
|
||||||
javaSourceFiles,
|
javaSourceFiles,
|
||||||
listOf("-cp", StringUtil.join(listOf(*classpath, outDir), File.pathSeparator), "-d", outDir.path)
|
listOf("-cp", StringUtil.join(listOf(*classpath), File.pathSeparator), "-d", outDir.path)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
val kotlinSourceFiles = FileUtil.findFilesByMask(Pattern.compile(".+\\.kt$"), srcDir)
|
||||||
|
if (kotlinSourceFiles.isNotEmpty()) {
|
||||||
|
MockLibraryUtil.compileKotlin(srcDir.path, outDir, extraClasspath = *arrayOf(*classpath, outDir.path))
|
||||||
|
}
|
||||||
|
|
||||||
return outDir
|
return outDir
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Generated
+5
@@ -105,6 +105,11 @@ public class ScriptConfigurationHighlightingTestGenerated extends AbstractScript
|
|||||||
runTest("idea/testData/script/definition/highlighting/multiModule/");
|
runTest("idea/testData/script/definition/highlighting/multiModule/");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("multipleScripts")
|
||||||
|
public void testMultipleScripts() throws Exception {
|
||||||
|
runTest("idea/testData/script/definition/highlighting/multipleScripts/");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("nestedClass")
|
@TestMetadata("nestedClass")
|
||||||
public void testNestedClass() throws Exception {
|
public void testNestedClass() throws Exception {
|
||||||
runTest("idea/testData/script/definition/highlighting/nestedClass/");
|
runTest("idea/testData/script/definition/highlighting/nestedClass/");
|
||||||
|
|||||||
Reference in New Issue
Block a user