Translate KotlinDirectInheritorsSearcher to Kotlin. Reuse PsiClass.isInheritor()

This commit is contained in:
Alexey Sedunov
2013-12-06 18:29:08 +04:00
parent 6913f42a97
commit eb6ae2f772
3 changed files with 61 additions and 81 deletions
@@ -1,80 +0,0 @@
/*
* Copyright 2010-2013 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.jet.plugin.search;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.QueryExecutorBase;
import com.intellij.psi.PsiClass;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.searches.DirectClassInheritorsSearch;
import com.intellij.util.Processor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
import org.jetbrains.jet.lang.psi.JetClassOrObject;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.plugin.libraries.JetSourceNavigationHelper;
import org.jetbrains.jet.plugin.search.usagesSearch.UsagesSearchPackage;
import org.jetbrains.jet.plugin.stubindex.JetSuperClassIndex;
import java.util.Collection;
public class KotlinDirectInheritorsSearcher extends QueryExecutorBase<PsiClass, DirectClassInheritorsSearch.SearchParameters> {
public KotlinDirectInheritorsSearcher() {
super(true);
}
@Override
public void processQuery(
@NotNull final DirectClassInheritorsSearch.SearchParameters queryParameters,
@NotNull final Processor<PsiClass> consumer
) {
ApplicationManager.getApplication().runReadAction(new Runnable() {
@Override
public void run() {
PsiClass clazz = queryParameters.getClassToProcess();
String qualifiedName = clazz.getQualifiedName();
if (qualifiedName == null) {
return;
}
String name = clazz.getName();
if (name == null || !(queryParameters.getScope() instanceof GlobalSearchScope)) return;
GlobalSearchScope scope = (GlobalSearchScope) queryParameters.getScope();
Collection<JetClassOrObject> candidates = JetSuperClassIndex.getInstance().get(name, clazz.getProject(), scope);
for (JetClassOrObject candidate : candidates) {
ClassDescriptor classDescriptor = (ClassDescriptor) UsagesSearchPackage.getDescriptor(candidate);
if (classDescriptor == null) continue;
for (JetType type : classDescriptor.getTypeConstructor().getSupertypes()) {
ClassifierDescriptor declarationDescriptor = type.getConstructor().getDeclarationDescriptor();
if (declarationDescriptor != null) {
String fqName = DescriptorUtils.getFQName(declarationDescriptor).asString();
if (qualifiedName.equals(fqName)) {
PsiClass psiClass = JetSourceNavigationHelper.getOriginalPsiClassOrCreateLightClass(candidate);
if (psiClass != null) {
consumer.process(psiClass);
}
}
}
}
}
}
});
}
}
@@ -0,0 +1,58 @@
/*
* Copyright 2010-2013 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.jet.plugin.search.ideaExtensions
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.QueryExecutorBase
import com.intellij.psi.PsiClass
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.search.searches.DirectClassInheritorsSearch
import com.intellij.util.Processor
import org.jetbrains.jet.lang.psi.JetClassOrObject
import org.jetbrains.jet.plugin.libraries.JetSourceNavigationHelper
import org.jetbrains.jet.plugin.stubindex.JetSuperClassIndex
import org.jetbrains.jet.lang.resolve.*
import java.util.Collections
public open class KotlinDirectInheritorsSearcher() : QueryExecutorBase<PsiClass, DirectClassInheritorsSearch.SearchParameters>(true) {
public override fun processQuery(queryParameters: DirectClassInheritorsSearch.SearchParameters, consumer: Processor<PsiClass>): Unit {
val baseClass = queryParameters.getClassToProcess()
if (baseClass == null) return
fun candidates(): Iterator<JetClassOrObject> {
val name = baseClass.getName()
val originalScope = queryParameters.getScope()
val scope = if (originalScope is GlobalSearchScope)
originalScope
else baseClass.getContainingFile()?.let { file -> GlobalSearchScope.fileScope(file) }
if (name != null && scope != null) {
return JetSuperClassIndex.getInstance().get(name, baseClass.getProject(), scope).iterator()
}
return Collections.emptyList<JetClassOrObject>().iterator()
}
ApplicationManager.getApplication()?.runReadAction {
candidates()
.map { candidate -> JetSourceNavigationHelper.getOriginalPsiClassOrCreateLightClass(candidate)}
.filterNotNull()
.filter { candidate -> candidate.isInheritor(baseClass, false) }
.forEach { candidate -> consumer.process(candidate) }
}
}
}
@@ -30,6 +30,7 @@ public class JetSuperClassIndex extends StringStubIndexExtension<JetClassOrObjec
private static final JetSuperClassIndex ourInstance = new JetSuperClassIndex();
@NotNull
public static JetSuperClassIndex getInstance() {
return ourInstance;
}
@@ -42,8 +43,9 @@ public class JetSuperClassIndex extends StringStubIndexExtension<JetClassOrObjec
return KEY;
}
@NotNull
@Override
public Collection<JetClassOrObject> get(String s, Project project, @NotNull GlobalSearchScope scope) {
public Collection<JetClassOrObject> get(String s, Project project, GlobalSearchScope scope) {
return super.get(s, project, JetSourceFilterScope.kotlinSourcesAndLibraries(scope));
}
}