Skip to content

Commit

Permalink
utilize kotline reified type parameters in CocoReferenceContributor
Browse files Browse the repository at this point in the history
  • Loading branch information
tsalzinger committed Feb 2, 2018
1 parent bf28c1a commit f12f36c
Showing 1 changed file with 7 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
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
return TextRange(startOffsetInParent, startOffsetInParent + element.textLength)
}

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 <T : PsiNameIdentifierOwner> register(registrar: PsiReferenceRegistrar, hasIdentClass: Class<T>, psiReferenceProvider: BiFunction<T, TextRange, PsiReference>) {
registrar.registerReferenceProvider(PlatformPatterns.psiElement(hasIdentClass),
private inline fun <reified T : PsiNameIdentifierOwner> 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<PsiReference> {
val hasIdent = element as T
val ident = hasIdent.nameIdentifier

if (ident != null) {
if (StringUtils.isNotBlank(hasIdent.name)) {
return arrayOf(psiReferenceProvider.apply(hasIdent, getRelativeTextRange(ident)))
return arrayOf(psiReferenceProvider.invoke(hasIdent, getRelativeTextRange(ident)))
}
}

Expand Down

0 comments on commit f12f36c

Please sign in to comment.