diff --git a/idea/src/org/jetbrains/kotlin/idea/script/ScriptTemplatesFromDependenciesProvider.kt b/idea/src/org/jetbrains/kotlin/idea/script/ScriptTemplatesFromDependenciesProvider.kt index 1ac3ce5c197..53765d6b844 100644 --- a/idea/src/org/jetbrains/kotlin/idea/script/ScriptTemplatesFromDependenciesProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/script/ScriptTemplatesFromDependenciesProvider.kt @@ -10,10 +10,7 @@ import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.ReadAction import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.project.Project -import com.intellij.openapi.roots.ModuleRootEvent -import com.intellij.openapi.roots.ModuleRootListener -import com.intellij.openapi.roots.OrderEnumerator -import com.intellij.openapi.roots.ProjectFileIndex +import com.intellij.openapi.roots.* import com.intellij.openapi.vfs.* import com.intellij.util.concurrency.AppExecutorUtil import com.intellij.util.indexing.FileBasedIndex @@ -123,31 +120,40 @@ class ScriptTemplatesFromDependenciesProvider(private val project: Project) : Sc .expireWith(project) .submit(AppExecutorUtil.getAppExecutorService()) .onSuccess { roots -> - val jarFS = JarFileSystem.getInstance() roots.forEach { root -> if (logger.isDebugEnabled) { logger.debug("root matching SCRIPT_DEFINITION_MARKERS_PATH found: ${root.path}") } + val orderEntriesForFile = ProjectFileIndex.getInstance(project).getOrderEntriesForFile(root) + .filter { + if (it is ModuleSourceOrderEntry) { + !ModuleRootManager.getInstance(it.ownerModule).fileIndex.isInTestSourceContent(root) + } else { + it is LibraryOrSdkOrderEntry + } + } + .takeIf { it.isNotEmpty() } ?: return@forEach + root.findFileByRelativePath(SCRIPT_DEFINITION_MARKERS_PATH)?.children?.forEach { resourceFile -> if (resourceFile.isValid && !resourceFile.isDirectory) { templates.add(resourceFile.name.removeSuffix(SCRIPT_DEFINITION_MARKERS_EXTENSION_WITH_DOT)) } } - val templateSource = jarFS.getVirtualFileForJar(root) ?: root - val module = ProjectFileIndex.getInstance(project).getModuleForFile(templateSource) ?: return@forEach // assuming that all libraries are placed into classes roots // TODO: extract exact library dependencies instead of putting all module dependencies into classpath // minimizing the classpath needed to use the template by taking cp only from modules with new templates found // on the other hand the approach may fail if some module contains a template without proper classpath, while // the other has properly configured classpath, so assuming that the dependencies are set correctly everywhere - classpath.addAll( - OrderEnumerator.orderEntries(module).withoutSdk().classesRoots.mapNotNull { - it.canonicalPath?.removeSuffix("!/").let(::File) - } - ) + orderEntriesForFile.forEach { + classpath.addAll( + OrderEnumerator.orderEntries(it.ownerModule).withoutSdk().classesRoots.mapNotNull { + it.canonicalPath?.removeSuffix("!/").let(::File) + } + ) + } } } .onProcessed {