summaryrefslogtreecommitdiff
path: root/plugins/kotlin/analysis/src/org/jetbrains/kotlin/idea/vfilefinder/KotlinLibraryKindIndex.kt
blob: e4b87624d27a7c87f826332c4ccacc0c9857b3c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package org.jetbrains.kotlin.idea.vfilefinder

import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.components.serviceOrNull
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileVisitor
import com.intellij.openapi.vfs.VirtualFileWithId
import org.jetbrains.kotlin.idea.caches.FileAttributeService
import org.jetbrains.kotlin.serialization.deserialization.MetadataPackageFragment

enum class KnownLibraryKindForIndex {
    COMMON, JS, UNKNOWN
}

private val KOTLIN_LIBRARY_KIND_FILE_ATTRIBUTE: String = "kotlin-library-kind".apply {
    serviceOrNull<FileAttributeService>()?.register(this, 1)
}

// TODO: Detect library kind for Jar file using IdePlatformKindResolution.
fun VirtualFile.getLibraryKindForJar(): KnownLibraryKindForIndex {
    if (this !is VirtualFileWithId) return detectLibraryKindFromJarContentsForIndex(this)

    val service =
        serviceOrNull<FileAttributeService>()
            ?: return detectLibraryKindFromJarContentsForIndex(this)

    service
        .readEnumAttribute(KOTLIN_LIBRARY_KIND_FILE_ATTRIBUTE, this, KnownLibraryKindForIndex::class.java)
        ?.let { return it.value }

    return detectLibraryKindFromJarContentsForIndex(this).also { newValue ->
        service.writeEnumAttribute(KOTLIN_LIBRARY_KIND_FILE_ATTRIBUTE, this, newValue)
    }
}

private fun detectLibraryKindFromJarContentsForIndex(jarRoot: VirtualFile): KnownLibraryKindForIndex {
    var result: KnownLibraryKindForIndex? = null
    VfsUtil.visitChildrenRecursively(jarRoot, object : VirtualFileVisitor<Unit>() {
        override fun visitFile(file: VirtualFile): Boolean =
            when (file.extension) {
                "class" -> false

                "kjsm" -> {
                    result = KnownLibraryKindForIndex.JS
                    false
                }

                MetadataPackageFragment.METADATA_FILE_EXTENSION -> {
                    result = KnownLibraryKindForIndex.COMMON
                    false
                }

                else -> true
            }
    })
    return result ?: KnownLibraryKindForIndex.UNKNOWN
}