All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.idea.references.KotlinPsiReferenceProvider.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-RC
Show newest version
/*
 * 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.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)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy