Skip to content

Commit

Permalink
feat(usage): add cached for reference
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Oct 7, 2022
1 parent 228f8eb commit 6ffeab4
Showing 1 changed file with 48 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,68 @@ import com.feakin.intellij.FkFile
import com.feakin.intellij.FkFileType
import com.feakin.intellij.psi.*
import com.feakin.intellij.resolve.ref.FkReferenceBase
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Key
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiManager
import com.intellij.psi.search.FileTypeIndex
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.*

class FkContextDeclReferenceImpl(
element: FkContextDeclaration
) : FkReferenceBase<FkContextDeclaration>(element) {
override fun multiResolve(): List<FkElement> {
val project = element.project
val scope = GlobalSearchScope.allScope(project)
val collection = mutableListOf<FkElement>()

val virtualFiles: Collection<VirtualFile> = FileTypeIndex.getFiles(FkFileType, scope)
virtualFiles.forEach { virtualFile ->
val fkFile = PsiManager.getInstance(project).findFile(virtualFile) as FkFile
val fkDeclarations = PsiTreeUtil.getChildrenOfType(fkFile, FkDeclaration::class.java)

val contextMaps: Array<out FkContextMapDeclaration>? = fkDeclarations?.mapNotNull {
PsiTreeUtil.getChildrenOfType(it, FkContextMapDeclaration::class.java)?.toList()
}?.flatten()?.toTypedArray()

val mapBodys: Array<out FkContextMapBody>? = contextMaps?.mapNotNull {
PsiTreeUtil.getChildrenOfType(it, FkContextMapBody::class.java)?.toList()
}?.flatten()?.toTypedArray()

mapBodys?.forEach { contextMapDeclaration ->
PsiTreeUtil.getChildrenOfType(contextMapDeclaration, FkContextName::class.java)
?.forEach { contextName ->
if (contextName.text == element.name) {
collection.add(contextName)
}
}
}
}

return collection
return element.inference
}

override fun isReferenceTo(element: PsiElement): Boolean {
return element is FkContextDeclaration && super.isReferenceTo(element)
}
}


private val CONTEXT_NAME_INFERENCE_KEY: Key<CachedValue<List<FkElement>>> = Key.create("CONTEXT_NAME_INFERENCE_KEY")

typealias InferenceResult = List<FkElement>;

val FkElement.inference: InferenceResult
get() = CachedValuesManager.getCachedValue(this, CONTEXT_NAME_INFERENCE_KEY) {
CachedValueProvider.Result.create(
inferContextName(this as FkNamedElement),
PsiModificationTracker.MODIFICATION_COUNT
)
}

private fun inferContextName(
element: FkNamedElement
): InferenceResult {
val project = element.project
val scope = GlobalSearchScope.allScope(project)
val collection = mutableListOf<FkElement>()

val virtualFiles: Collection<VirtualFile> = FileTypeIndex.getFiles(FkFileType, scope)
virtualFiles.forEach { virtualFile ->
val fkFile = PsiManager.getInstance(project).findFile(virtualFile) as FkFile
val fkDeclarations = PsiTreeUtil.getChildrenOfType(fkFile, FkDeclaration::class.java)

val contextMaps: Array<out FkContextMapDeclaration>? = fkDeclarations?.mapNotNull {
PsiTreeUtil.getChildrenOfType(it, FkContextMapDeclaration::class.java)?.toList()
}?.flatten()?.toTypedArray()

val mapBodys: Array<out FkContextMapBody>? = contextMaps?.mapNotNull {
PsiTreeUtil.getChildrenOfType(it, FkContextMapBody::class.java)?.toList()
}?.flatten()?.toTypedArray()

mapBodys?.forEach { contextMapDeclaration ->
PsiTreeUtil.getChildrenOfType(contextMapDeclaration, FkContextName::class.java)
?.forEach { contextName ->
if (contextName.text == element.name) {
collection.add(contextName)
}
}
}
}
return collection
}

0 comments on commit 6ffeab4

Please sign in to comment.