summaryrefslogtreecommitdiff
path: root/plugins/kotlin/analysis/src/org/jetbrains/kotlin/idea/stubindex/KotlinFileStubForIde.kt
blob: 677589a14848817fd47b00516b7fa98b38bc4871 (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
61
62
63
64
65
66
67
68
69
// 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.stubindex

import com.intellij.psi.stubs.PsiClassHolderFileStub
import com.intellij.util.io.StringRef
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.stubs.KotlinFileStub
import org.jetbrains.kotlin.psi.stubs.impl.KotlinFileStubImpl

class KotlinFileStubForIde(
    ktFile: KtFile?,
    packageName: StringRef,
    isScript: Boolean,
    private val facadeFqNameRef: StringRef?,
    val partSimpleName: StringRef?,
    val facadePartSimpleNames: List<StringRef?>?
) : KotlinFileStubImpl(ktFile, packageName, isScript), KotlinFileStub, PsiClassHolderFileStub<KtFile> {

    private fun StringRef.relativeToPackage() = getPackageFqName().child(Name.identifier(this.string))

    val partFqName: FqName?
        get() = partSimpleName?.relativeToPackage()
    val facadeFqName: FqName?
        get() = facadeFqNameRef?.let { FqName(it.string) }

    constructor(ktFile: KtFile?, packageName: String, isScript: Boolean) : this(
        ktFile,
        StringRef.fromString(packageName)!!,
        isScript,
        null,
        null,
        null
    )

    companion object {
        fun forFile(packageFqName: FqName, isScript: Boolean): KotlinFileStubImpl = KotlinFileStubForIde(
            ktFile = null,
            packageName = StringRef.fromString(packageFqName.asString())!!,
            facadeFqNameRef = null,
            partSimpleName = null,
            facadePartSimpleNames = null,
            isScript = isScript
        )

        fun forFileFacadeStub(facadeFqName: FqName): KotlinFileStubImpl = KotlinFileStubForIde(
            ktFile = null,
            packageName = facadeFqName.parent().stringRef(),
            facadeFqNameRef = facadeFqName.stringRef(),
            partSimpleName = facadeFqName.shortName().stringRef(),
            facadePartSimpleNames = null,
            isScript = false
        )

        fun forMultifileClassStub(packageFqName: FqName, facadeFqName: FqName, partNames: List<String>?): KotlinFileStubImpl = KotlinFileStubForIde(
            ktFile = null,
            packageName = packageFqName.stringRef(),
            facadeFqNameRef = facadeFqName.stringRef(),
            partSimpleName = null,
            facadePartSimpleNames = partNames?.map { StringRef.fromString(it) },
            isScript = false
        )

        private fun FqName.stringRef() = StringRef.fromString(asString())!!
        private fun Name.stringRef() = StringRef.fromString(asString())!!
    }
}