override fun processQuery()

in src/main/kotlin/org/arend/search/ArendCustomSearcher.kt [26:86]


    override fun processQuery(parameters: ReferencesSearch.SearchParameters, consumer: Processor<in PsiReference>) {
        var elementToSearch_var : PsiLocatedReferable? = null
        runReadAction {
            elementToSearch_var = when (val e = parameters.elementToSearch) {
                is PsiLocatedReferable -> e
                is ArendAliasIdentifier -> e.parent?.parent as? PsiLocatedReferable
                is ArendDefIdentifier -> if (isDefIdentifierFromNsId(e)) ((e.parent as ArendNsId).refIdentifier.resolve as? PsiLocatedReferable) else null
                else -> null
            }
        }
        val elementToSearch = elementToSearch_var ?: return
        val scope = parameters.scopeDeterminedByUser
        val project = parameters.project
        val tasks = ArrayList<Pair<String, SearchScope>>()


        runReadAction {
            val standardName = elementToSearch.refName
            val aliasName = elementToSearch.aliasName
            if (scope is GlobalSearchScope) {
                collectSearchScopes(listOf(standardName), scope.isSearchInLibraries, project).forEach {
                    val arendFile = PsiManager.getInstance(project).findFile(it) as? ArendFile
                    if (arendFile != null) {
                        tasks.add(standardName to LocalSearchScope(arendFile))
                    }
                }
                if (aliasName != null) {
                    collectSearchScopes(listOf(aliasName), scope.isSearchInLibraries, project).forEach {
                        val arendFile = PsiManager.getInstance(project).findFile(it) as? ArendFile
                        if (arendFile != null) {
                            tasks.add(aliasName to LocalSearchScope(arendFile))
                        }
                    }
                }
            } else if (aliasName != null) {
                tasks.add(Pair(aliasName, scope))
            }
            tasks.add(Pair(standardName, scope))
        }

        val searchContext = (UsageSearchContext.IN_CODE.toInt() or UsageSearchContext.IN_FOREIGN_LANGUAGES.toInt()).toShort()
        for (task in tasks) {
            parameters.optimizer.searchWord(task.first, task.second, searchContext, true, elementToSearch, object: RequestResultProcessor(){
                override fun processTextOccurrence(element: PsiElement, offsetInElement: Int, consumer: Processor<in PsiReference>): Boolean {
                    (element as? ArendNsId)?.defIdentifier?.let { dI ->
                        PsiSearchHelperImpl(project).processElementsWithWord({ element, _ ->
                            !(element is ArendRefIdentifier && element.reference.isReferenceTo(elementToSearch) && !consumer.process(element.reference))
                        }, dI.useScope, dI.name, searchContext, true)
                    }
                    for (ref in PsiReferenceService.getService().getReferences(element, PsiReferenceService.Hints(elementToSearch, offsetInElement))) { // Copypasted from SingleTargetRequestResultProcessor
                        ProgressManager.checkCanceled()
                        if (ReferenceRange.containsOffsetInElement(ref, offsetInElement) && ref.isReferenceTo(elementToSearch)) {
                            if (!consumer.process(ref))
                                return false
                        }
                    }
                    return true
                }
            })
        }
    }