From f12f36c1e1e3052e6988f6445d684aaa57cc6da3 Mon Sep 17 00:00:00 2001 From: Thomas Scheinecker Date: Fri, 2 Feb 2018 18:55:31 +0100 Subject: [PATCH] utilize kotline reified type parameters in CocoReferenceContributor --- .../reference/CocoReferenceContributor.kt | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/at/scheinecker/intellij/coco/reference/CocoReferenceContributor.kt b/src/main/kotlin/at/scheinecker/intellij/coco/reference/CocoReferenceContributor.kt index b661e7d..65f8ba8 100644 --- a/src/main/kotlin/at/scheinecker/intellij/coco/reference/CocoReferenceContributor.kt +++ b/src/main/kotlin/at/scheinecker/intellij/coco/reference/CocoReferenceContributor.kt @@ -1,17 +1,11 @@ package at.scheinecker.intellij.coco.reference -import at.scheinecker.intellij.coco.psi.HasCocoCharacterReference -import at.scheinecker.intellij.coco.psi.HasCocoCompilerReference -import at.scheinecker.intellij.coco.psi.HasCocoProductionReference -import at.scheinecker.intellij.coco.psi.HasCocoTokenReference import com.intellij.openapi.util.TextRange import com.intellij.patterns.PlatformPatterns import com.intellij.psi.* import com.intellij.util.ProcessingContext import org.apache.commons.lang.StringUtils -import java.util.function.BiFunction - class CocoReferenceContributor : PsiReferenceContributor() { private fun getRelativeTextRange(element: PsiElement): TextRange { val startOffsetInParent = element.startOffsetInParent @@ -19,14 +13,14 @@ class CocoReferenceContributor : PsiReferenceContributor() { } override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) { - register(registrar, HasCocoCompilerReference::class.java, BiFunction { element, textRange -> CocoCompilerReference(element, textRange) }) - register(registrar, HasCocoCharacterReference::class.java, BiFunction { element, textRange -> CocoCharacterReference(element, textRange) }) - register(registrar, HasCocoProductionReference::class.java, BiFunction { element, textRange -> CocoProductionReference(element, textRange) }) - register(registrar, HasCocoTokenReference::class.java, BiFunction { element, textRange -> CocoTokenReference(element, textRange) }) + register(registrar, ::CocoCompilerReference) + register(registrar, ::CocoCharacterReference) + register(registrar, ::CocoProductionReference) + register(registrar, ::CocoTokenReference) } - private fun register(registrar: PsiReferenceRegistrar, hasIdentClass: Class, psiReferenceProvider: BiFunction) { - registrar.registerReferenceProvider(PlatformPatterns.psiElement(hasIdentClass), + private inline fun register(registrar: PsiReferenceRegistrar, crossinline psiReferenceProvider: (T, TextRange) -> PsiReference) { + registrar.registerReferenceProvider(PlatformPatterns.psiElement(T::class.java), object : PsiReferenceProvider() { override fun getReferencesByElement(element: PsiElement, context: ProcessingContext): Array { val hasIdent = element as T @@ -34,7 +28,7 @@ class CocoReferenceContributor : PsiReferenceContributor() { if (ident != null) { if (StringUtils.isNotBlank(hasIdent.name)) { - return arrayOf(psiReferenceProvider.apply(hasIdent, getRelativeTextRange(ident))) + return arrayOf(psiReferenceProvider.invoke(hasIdent, getRelativeTextRange(ident))) } }