Move declarationsSearch package from idea to idea-analysis

It will be used in later commits by idea-analysis code
This commit is contained in:
Denis Zharkov
2015-03-17 10:40:28 +03:00
committed by Alexey Sedunov
parent 0debd1ce6f
commit 488754de7d
3 changed files with 0 additions and 0 deletions
@@ -0,0 +1,46 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.search.declarationsSearch
import com.intellij.psi.PsiClass
import com.intellij.util.Query
import com.intellij.psi.PsiModifier
import com.intellij.psi.PsiAnonymousClass
import org.jetbrains.kotlin.psi.JetClassOrObject
import org.jetbrains.kotlin.asJava.LightClassUtil
import com.intellij.psi.PsiElement
import com.intellij.psi.search.searches.ClassInheritorsSearch
import com.intellij.util.EmptyQuery
public fun HierarchySearchRequest<PsiElement>.searchInheritors(): Query<PsiClass> {
val psiClass: PsiClass? = when (originalElement) {
is JetClassOrObject -> LightClassUtil.getPsiClass(originalElement)
is PsiClass -> originalElement
else -> null
}
if (psiClass == null) return EmptyQuery.getEmptyQuery()
return ClassInheritorsSearch.search(
psiClass,
searchScope,
searchDeeply,
/* checkInheritance = */ true,
/* includeAnonymous = */ true
)
}
fun PsiClass.isInheritable(): Boolean = !(this is PsiAnonymousClass || hasModifierProperty(PsiModifier.FINAL))
@@ -0,0 +1,121 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.search.declarationsSearch
import com.intellij.psi.search.SearchScope
import com.intellij.util.Query
import com.intellij.util.QueryFactory
import com.intellij.util.Processor
import com.intellij.openapi.application.QueryExecutorBase
import com.intellij.psi.PsiElement
import com.intellij.openapi.project.Project
import com.intellij.util.EmptyQuery
import java.util.Stack
import java.util.HashSet
import com.intellij.openapi.progress.ProgressIndicatorProvider
import org.jetbrains.kotlin.psi.psiUtil.contains
trait DeclarationSearchRequest<in T> {
val project: Project
val searchScope: SearchScope
}
public trait SearchRequestWithElement<T : PsiElement> : DeclarationSearchRequest<T> {
val originalElement: T
override val project: Project get() = originalElement.getProject()
}
abstract class DeclarationsSearch<T: PsiElement, R: DeclarationSearchRequest<T>>: QueryFactory<T, R>() {
{
registerExecutor(
object : QueryExecutorBase<T, R>(true) {
override fun processQuery(queryParameters: R, consumer: Processor<T>) {
doSearch(queryParameters, consumer)
}
}
)
}
protected abstract fun doSearch(request: R, consumer: Processor<T>)
protected open fun isApplicable(request: R): Boolean = true
fun search(request: R): Query<T> = if (isApplicable(request)) createUniqueResultsQuery(request) else EmptyQuery.getEmptyQuery<T>()
}
public class HierarchySearchRequest<T: PsiElement> (
override val originalElement: T,
override val searchScope: SearchScope,
val searchDeeply: Boolean = true
) : SearchRequestWithElement<T> {
fun copy<U: PsiElement>(newOriginalElement: U): HierarchySearchRequest<U> =
HierarchySearchRequest(newOriginalElement, searchScope, searchDeeply)
}
trait HierarchyTraverser<T> {
protected fun nextElements(current: T): Iterable<T>
protected fun shouldDescend(element: T): Boolean
fun forEach(initialElement: T, body: (T) -> Unit) {
val stack = Stack<T>()
val processed = HashSet<T>()
stack.push(initialElement)
while (!stack.empty) {
ProgressIndicatorProvider.checkCanceled()
val current = stack.pop()!!
if (!processed.add(current)) continue
for (next in nextElements(current)) {
ProgressIndicatorProvider.checkCanceled()
body(next)
if (shouldDescend(next)) {
stack.push(next)
}
}
}
}
}
fun <T: PsiElement> Processor<T>.consumeHierarchy(request: SearchRequestWithElement<T>, traverser: HierarchyTraverser<T>) {
traverser.forEach(request.originalElement) { element ->
if (element in request.searchScope) {
process(element)
}
}
}
abstract class HierarchySearch<T: PsiElement>(
protected val traverser: HierarchyTraverser<T>
): DeclarationsSearch<T, HierarchySearchRequest<T>>() {
protected open fun doSearchAll(request: HierarchySearchRequest<T>, consumer: Processor<T>) {
consumer.consumeHierarchy(request, traverser)
}
protected abstract fun doSearchDirect(request: HierarchySearchRequest<T>, consumer: Processor<T>)
protected override fun doSearch(request: HierarchySearchRequest<T>, consumer: Processor<T>) {
if (request.searchDeeply) {
doSearchAll(request, consumer)
}
else {
doSearchDirect(request, consumer)
}
}
}
@@ -0,0 +1,105 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.search.declarationsSearch
import com.intellij.psi.PsiMethod
import com.intellij.psi.util.PsiUtil
import org.jetbrains.kotlin.psi.JetDeclaration
import com.intellij.psi.PsiElement
import com.intellij.util.Processor
import com.intellij.util.Query
import java.util.HashMap
import org.jetbrains.kotlin.psi.psiUtil.*
import java.util.Collections
import com.intellij.psi.PsiClass
import com.intellij.psi.util.TypeConversionUtil
import com.intellij.psi.PsiSubstitutor
import com.intellij.psi.util.MethodSignatureUtil
import com.intellij.psi.PsiModifier
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.search.searches.DirectClassInheritorsSearch
import com.intellij.util.EmptyQuery
import com.intellij.util.MergeQuery
import org.jetbrains.kotlin.asJava.toLightMethods
import org.jetbrains.kotlin.idea.search.allScope
fun PsiElement.isOverridableElement(): Boolean = when (this) {
is PsiMethod -> PsiUtil.canBeOverriden(this)
is JetDeclaration -> isOverridable()
else -> false
}
public fun HierarchySearchRequest<PsiElement>.searchOverriders(): Query<PsiMethod> {
val psiMethods = originalElement.toLightMethods()
if (psiMethods.isEmpty()) return EmptyQuery.getEmptyQuery()
return psiMethods
.map { psiMethod -> KotlinPsiMethodOverridersSearch.search(copy(psiMethod)) }
.reduce {(query1, query2) -> MergeQuery(query1, query2)}
}
public object KotlinPsiMethodOverridersSearch : HierarchySearch<PsiMethod>(PsiMethodOverridingHierarchyTraverser) {
fun searchDirectOverriders(psiMethod: PsiMethod): Iterable<PsiMethod> {
fun PsiMethod.isAcceptable(inheritor: PsiClass, baseMethod: PsiMethod, baseClass: PsiClass): Boolean =
when {
hasModifierProperty(PsiModifier.STATIC) -> false
baseMethod.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) ->
JavaPsiFacade.getInstance(getProject()).arePackagesTheSame(baseClass, inheritor)
else -> true
}
val psiClass = psiMethod.getContainingClass()
if (psiClass == null) return Collections.emptyList()
val classToMethod = HashMap<PsiClass, PsiMethod>()
val classTraverser = object : HierarchyTraverser<PsiClass> {
override fun nextElements(current: PsiClass): Iterable<PsiClass> =
DirectClassInheritorsSearch.search(
current,
current.getProject().allScope(),
/* checkInheritance = */ true,
/* includeAnonymous = */ true
)
override fun shouldDescend(element: PsiClass): Boolean =
element.isInheritable() && !classToMethod.containsKey(element)
}
classTraverser.forEach(psiClass) { inheritor ->
val substitutor = TypeConversionUtil.getSuperClassSubstitutor(psiClass, inheritor, PsiSubstitutor.EMPTY)
val signature = psiMethod.getSignature(substitutor)
val candidate = MethodSignatureUtil.findMethodBySuperSignature(inheritor, signature, false)
if (candidate != null && candidate.isAcceptable(inheritor, psiMethod, psiClass)) {
classToMethod.put(inheritor, candidate)
}
}
return classToMethod.values()
}
protected override fun isApplicable(request: HierarchySearchRequest<PsiMethod>): Boolean =
request.originalElement.isOverridableElement()
override fun doSearchDirect(request: HierarchySearchRequest<PsiMethod>, consumer: Processor<PsiMethod>) {
searchDirectOverriders(request.originalElement).forEach { method -> consumer.process(method) }
}
}
object PsiMethodOverridingHierarchyTraverser: HierarchyTraverser<PsiMethod> {
override fun nextElements(current: PsiMethod): Iterable<PsiMethod> = KotlinPsiMethodOverridersSearch.searchDirectOverriders(current)
override fun shouldDescend(element: PsiMethod): Boolean = PsiUtil.canBeOverriden(element)
}