KotlinScriptConfigurationManager, minor: Extract utility to get transform list of file to vfs roots
This commit is contained in:
committed by
Ilya Chernikov
parent
500554e58c
commit
350abbe8cd
+20
-15
@@ -73,11 +73,11 @@ class KotlinScriptConfigurationManager(
|
||||
private val cacheLock = ReentrantReadWriteLock()
|
||||
|
||||
private val allScriptsClasspathCache = ClearableLazyValue(cacheLock) {
|
||||
scriptExternalImportsProvider.getKnownCombinedClasspath().distinct().mapNotNull { it.classpathEntryToVfs() }
|
||||
toVfsRoots(scriptExternalImportsProvider.getKnownCombinedClasspath().distinct())
|
||||
}
|
||||
|
||||
private val allLibrarySourcesCache = ClearableLazyValue(cacheLock) {
|
||||
scriptExternalImportsProvider.getKnownSourceRoots().distinct().mapNotNull { it.classpathEntryToVfs() }
|
||||
toVfsRoots(scriptExternalImportsProvider.getKnownSourceRoots().distinct())
|
||||
}
|
||||
|
||||
private fun notifyRootsChanged() {
|
||||
@@ -86,24 +86,14 @@ class KotlinScriptConfigurationManager(
|
||||
}
|
||||
}
|
||||
|
||||
fun getScriptClasspath(file: VirtualFile): List<VirtualFile> =
|
||||
scriptExternalImportsProvider.getExternalImports(file)?.classpath?.mapNotNull { it.classpathEntryToVfs() } ?: emptyList()
|
||||
fun getScriptClasspath(file: VirtualFile): List<VirtualFile> = toVfsRoots(
|
||||
scriptExternalImportsProvider.getExternalImports(file)?.classpath ?: emptyList()
|
||||
)
|
||||
|
||||
fun getAllScriptsClasspath(): List<VirtualFile> = allScriptsClasspathCache.get()
|
||||
|
||||
fun getAllLibrarySources(): List<VirtualFile> = allLibrarySourcesCache.get()
|
||||
|
||||
private fun File.classpathEntryToVfs(): VirtualFile? {
|
||||
val res = when {
|
||||
!exists() -> null
|
||||
isDirectory -> StandardFileSystems.local()?.findFileByPath(this.canonicalPath) ?: null
|
||||
isFile -> StandardFileSystems.jar()?.findFileByPath(this.canonicalPath + URLUtil.JAR_SEPARATOR) ?: null
|
||||
else -> null
|
||||
}
|
||||
// TODO: report this somewhere, but do not throw: assert(res != null, { "Invalid classpath entry '$this': exists: ${exists()}, is directory: $isDirectory, is file: $isFile" })
|
||||
return res
|
||||
}
|
||||
|
||||
fun getAllScriptsClasspathScope() = NonClasspathDirectoriesScope(getAllScriptsClasspath())
|
||||
|
||||
fun getAllLibrarySourcesScope() = NonClasspathDirectoriesScope(getAllLibrarySources())
|
||||
@@ -145,6 +135,21 @@ class KotlinScriptConfigurationManager(
|
||||
@JvmStatic
|
||||
fun getInstance(project: Project): KotlinScriptConfigurationManager =
|
||||
ServiceManager.getService(project, KotlinScriptConfigurationManager::class.java)
|
||||
|
||||
fun toVfsRoots(roots: Iterable<File>): List<VirtualFile> {
|
||||
return roots.mapNotNull { it.classpathEntryToVfs() }
|
||||
}
|
||||
|
||||
private fun File.classpathEntryToVfs(): VirtualFile? {
|
||||
val res = when {
|
||||
!exists() -> null
|
||||
isDirectory -> StandardFileSystems.local()?.findFileByPath(this.canonicalPath) ?: null
|
||||
isFile -> StandardFileSystems.jar()?.findFileByPath(this.canonicalPath + URLUtil.JAR_SEPARATOR) ?: null
|
||||
else -> null
|
||||
}
|
||||
// TODO: report this somewhere, but do not throw: assert(res != null, { "Invalid classpath entry '$this': exists: ${exists()}, is directory: $isDirectory, is file: $isFile" })
|
||||
return res
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user