org.jetbrains.kotlin.idea.references.KotlinPsiReferenceProvider.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-compiler-embeddable Show documentation
Show all versions of kotlin-compiler-embeddable Show documentation
the Kotlin compiler embeddable
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.references
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiReference
import com.intellij.util.containers.MultiMap
import org.jetbrains.kotlin.psi.KtElement
interface KotlinPsiReferenceProvider {
fun getReferencesByElement(element: PsiElement): Array
}
interface KotlinReferenceProviderContributor {
fun registerReferenceProviders(registrar: KotlinPsiReferenceRegistrar)
companion object {
fun getInstance(project: Project): KotlinReferenceProviderContributor =
project.getService(KotlinReferenceProviderContributor::class.java)
}
}
class KotlinPsiReferenceRegistrar {
val providers: MultiMap, KotlinPsiReferenceProvider> = MultiMap(LinkedHashMap())
inline fun registerProvider(crossinline factory: (E) -> PsiReference?) {
registerMultiProvider { element ->
factory(element)?.let { reference -> arrayOf(reference) } ?: PsiReference.EMPTY_ARRAY
}
}
inline fun registerMultiProvider(crossinline factory: (E) -> Array) {
val provider: KotlinPsiReferenceProvider = object : KotlinPsiReferenceProvider {
override fun getReferencesByElement(element: PsiElement): Array {
return factory(element as E)
}
}
registerMultiProvider(E::class.java, provider)
}
fun registerMultiProvider(klass: Class, provider: KotlinPsiReferenceProvider) {
providers.putValue(klass, provider)
}
}