summaryrefslogtreecommitdiff
path: root/plugins/kotlin/frontend-independent/src/org/jetbrains/kotlin/idea/search/KotlinSearchSupport.kt
blob: dff3b26b24f4f1710f52fdb9c83d8a3a7f33fefa (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// 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.search

import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.NlsSafe
import com.intellij.psi.*
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.search.SearchScope
import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchOptions
import org.jetbrains.kotlin.idea.util.application.getServiceSafe
import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.ImportPath

interface KotlinSearchUsagesSupport {

    interface ConstructorCallHandle {
        fun referencedTo(element: KtElement): Boolean
    }

    companion object {
        fun getInstance(project: Project): KotlinSearchUsagesSupport = project.getServiceSafe()

        val KtParameter.dataClassComponentMethodName: String?
            get() = getInstance(project).dataClassComponentMethodName(this)

        val KtExpression.hasType: Boolean
            get() = getInstance(project).hasType(this)

        val PsiClass.isSamInterface: Boolean
            get() = getInstance(project).isSamInterface(this)

        fun <T : PsiNamedElement> List<T>.filterDataClassComponentsIfDisabled(kotlinOptions: KotlinReferencesSearchOptions): List<T> =
            firstOrNull()?.let {
                getInstance(it.project).filterDataClassComponentsIfDisabled(this, kotlinOptions)
            } ?: this

        fun PsiReference.isCallableOverrideUsage(declaration: KtNamedDeclaration): Boolean =
            getInstance(declaration.project).isCallableOverrideUsage(this, declaration)

        fun PsiReference.isUsageInContainingDeclaration(declaration: KtNamedDeclaration): Boolean =
            getInstance(declaration.project).isUsageInContainingDeclaration(this, declaration)

        fun PsiReference.isExtensionOfDeclarationClassUsage(declaration: KtNamedDeclaration): Boolean =
            getInstance(declaration.project).isExtensionOfDeclarationClassUsage(this, declaration)

        fun PsiElement.getReceiverTypeSearcherInfo(isDestructionDeclarationSearch: Boolean): ReceiverTypeSearcherInfo? =
            getInstance(project).getReceiverTypeSearcherInfo(this, isDestructionDeclarationSearch)

        fun KtFile.forceResolveReferences(elements: List<KtElement>) =
            getInstance(project).forceResolveReferences(this, elements)

        fun PsiFile.scriptDefinitionExists(): Boolean =
            getInstance(project).scriptDefinitionExists(this)

        fun KtFile.getDefaultImports(): List<ImportPath> =
            getInstance(project).getDefaultImports(this)

        fun forEachKotlinOverride(
            ktClass: KtClass,
            members: List<KtNamedDeclaration>,
            scope: SearchScope,
            searchDeeply: Boolean,
            processor: (superMember: PsiElement, overridingMember: PsiElement) -> Boolean
        ): Boolean = getInstance(ktClass.project).forEachKotlinOverride(ktClass, members, scope, searchDeeply, processor)

        fun PsiMethod.forEachOverridingMethod(
            scope: SearchScope = runReadAction { useScope() },
            processor: (PsiMethod) -> Boolean
        ): Boolean = getInstance(project).forEachOverridingMethod(this, scope, processor)

        fun findDeepestSuperMethodsNoWrapping(method: PsiElement): List<PsiElement> =
            getInstance(method.project).findDeepestSuperMethodsNoWrapping(method)

        fun findTypeAliasByShortName(shortName: String, project: Project, scope: GlobalSearchScope): Collection<KtTypeAlias> =
            getInstance(project).findTypeAliasByShortName(shortName, project, scope)

        fun isInProjectSource(element: PsiElement, includeScriptsOutsideSourceRoots: Boolean = false): Boolean =
            getInstance(element.project).isInProjectSource(element, includeScriptsOutsideSourceRoots)

        fun KtDeclaration.isOverridable(): Boolean =
            getInstance(project).isOverridable(this)

        fun KtClass.isInheritable(): Boolean =
            getInstance(project).isInheritable(this)

        @NlsSafe
        fun formatJavaOrLightMethod(method: PsiMethod): String =
            getInstance(method.project).formatJavaOrLightMethod(method)

        @NlsSafe
        fun formatClass(classOrObject: KtClassOrObject): String =
            getInstance(classOrObject.project).formatClass(classOrObject)

        fun KtDeclaration.expectedDeclarationIfAny(): KtDeclaration? =
            getInstance(project).expectedDeclarationIfAny(this)

        fun KtDeclaration.isExpectDeclaration(): Boolean =
            getInstance(project).isExpectDeclaration(this)

        fun KtDeclaration.actualsForExpected(module: Module? = null): Set<KtDeclaration> =
            getInstance(project).actualsForExpected(this, module)

        fun PsiElement.canBeResolvedWithFrontEnd(): Boolean =
            getInstance(project).canBeResolvedWithFrontEnd(this)

        fun createConstructorHandle(ktDeclaration: KtDeclaration): ConstructorCallHandle =
            getInstance(ktDeclaration.project).createConstructorHandle(ktDeclaration)

        fun createConstructorHandle(psiMethod: PsiMethod): ConstructorCallHandle =
            getInstance(psiMethod.project).createConstructorHandle(psiMethod)
    }

    fun actualsForExpected(declaration: KtDeclaration, module: Module? = null): Set<KtDeclaration>

    fun dataClassComponentMethodName(element: KtParameter): String?

    fun hasType(element: KtExpression): Boolean

    fun isSamInterface(psiClass: PsiClass): Boolean

    fun <T : PsiNamedElement> filterDataClassComponentsIfDisabled(elements: List<T>, kotlinOptions: KotlinReferencesSearchOptions): List<T>

    fun isCallableOverrideUsage(reference: PsiReference, declaration: KtNamedDeclaration): Boolean

    fun isUsageInContainingDeclaration(reference: PsiReference, declaration: KtNamedDeclaration): Boolean

    fun isExtensionOfDeclarationClassUsage(reference: PsiReference, declaration: KtNamedDeclaration): Boolean

    fun getReceiverTypeSearcherInfo(psiElement: PsiElement, isDestructionDeclarationSearch: Boolean): ReceiverTypeSearcherInfo?

    fun forceResolveReferences(file: KtFile, elements: List<KtElement>)

    fun scriptDefinitionExists(file: PsiFile): Boolean

    fun getDefaultImports(file: KtFile): List<ImportPath>

    fun forEachKotlinOverride(
        ktClass: KtClass,
        members: List<KtNamedDeclaration>,
        scope: SearchScope,
        searchDeeply: Boolean,
        processor: (superMember: PsiElement, overridingMember: PsiElement) -> Boolean
    ): Boolean

    fun forEachOverridingMethod(
        method: PsiMethod,
        scope: SearchScope,
        processor: (PsiMethod) -> Boolean
    ): Boolean

    fun findDeepestSuperMethodsNoWrapping(method: PsiElement): List<PsiElement>

    fun findSuperMethodsNoWrapping(method: PsiElement): List<PsiElement>

    fun findTypeAliasByShortName(shortName: String, project: Project, scope: GlobalSearchScope): Collection<KtTypeAlias>

    fun isInProjectSource(element: PsiElement, includeScriptsOutsideSourceRoots: Boolean = false): Boolean

    fun isOverridable(declaration: KtDeclaration): Boolean

    fun isInheritable(ktClass: KtClass): Boolean

    fun formatJavaOrLightMethod(method: PsiMethod): String

    fun formatClass(classOrObject: KtClassOrObject): String

    fun expectedDeclarationIfAny(declaration: KtDeclaration): KtDeclaration?

    fun isExpectDeclaration(declaration: KtDeclaration): Boolean

    fun canBeResolvedWithFrontEnd(element: PsiElement): Boolean

    fun createConstructorHandle(ktDeclaration: KtDeclaration): ConstructorCallHandle

    fun createConstructorHandle(psiMethod: PsiMethod): ConstructorCallHandle
}