Translate FindUsagesUtils to Kotlin

This commit is contained in:
Alexey Sedunov
2013-10-18 18:38:58 +04:00
parent 8ca3f6a1ad
commit 992c7055a5
4 changed files with 73 additions and 63 deletions
@@ -1,59 +0,0 @@
package org.jetbrains.jet.plugin.findUsages;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiConstructorCall;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
public class FindUsagesUtils {
private FindUsagesUtils() {
}
private static DeclarationDescriptor getCallDescriptor(PsiElement element, BindingContext bindingContext) {
JetConstructorCalleeExpression constructorCalleeExpression =
PsiTreeUtil.getParentOfType(element, JetConstructorCalleeExpression.class);
if (constructorCalleeExpression != null) {
JetReferenceExpression classReference = constructorCalleeExpression.getConstructorReferenceExpression();
return bindingContext.get(BindingContext.REFERENCE_TARGET, classReference);
}
JetCallExpression callExpression = PsiTreeUtil.getParentOfType(element, JetCallExpression.class);
if (callExpression != null) {
JetExpression callee = callExpression.getCalleeExpression();
if (callee instanceof JetReferenceExpression) {
return bindingContext.get(BindingContext.REFERENCE_TARGET, (JetReferenceExpression) callee);
}
}
return null;
}
public static boolean isConstructorUsage(PsiElement element, JetClassOrObject jetClassOrObject) {
PsiConstructorCall constructorCall = PsiTreeUtil.getParentOfType(element, PsiConstructorCall.class);
if (constructorCall != null && constructorCall == element.getParent()) {
PsiMethod constructor = constructorCall.resolveConstructor();
if (constructor == null) return false;
PsiClass constructorClass = constructor.getContainingClass();
return constructorClass != null && constructorClass.getNavigationElement() == jetClassOrObject;
}
if (!(element instanceof JetElement)) return false;
BindingContext bindingContext =
AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) element.getContainingFile()).getBindingContext();
DeclarationDescriptor descriptor = getCallDescriptor(element, bindingContext);
if (!(descriptor instanceof ConstructorDescriptor)) return false;
DeclarationDescriptor containingDescriptor = descriptor.getContainingDeclaration();
return containingDescriptor != null
&& BindingContextUtils.descriptorToDeclaration(bindingContext, containingDescriptor) == jetClassOrObject;
}
}
@@ -40,7 +40,7 @@ import org.jetbrains.jet.lang.psi.JetClass;
import org.jetbrains.jet.lang.psi.JetDeclaration;
import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.lang.psi.JetProperty;
import org.jetbrains.jet.plugin.findUsages.FindUsagesUtils;
import org.jetbrains.jet.plugin.findUsages.FindUsagesPackage;
import org.jetbrains.jet.plugin.findUsages.KotlinFindUsagesHandlerFactory;
import org.jetbrains.jet.plugin.findUsages.dialogs.KotlinFindClassUsagesDialog;
import org.jetbrains.jet.plugin.findUsages.options.KotlinClassFindUsagesOptions;
@@ -80,7 +80,7 @@ public class KotlinFindClassUsagesHandler extends KotlinFindUsagesHandler<JetCla
new ReferencesSearch.SearchParameters(jetClass, kotlinOptions.searchScope, false)
).findAll();
for (PsiReference ref : references) {
boolean constructorUsage = FindUsagesUtils.isConstructorUsage(ref.getElement(), jetClass);
boolean constructorUsage = FindUsagesPackage.isConstructorUsage(ref.getElement(), jetClass);
if ((constructorUsage && !kotlinOptions.searchConstructorUsages)
|| (!constructorUsage && !kotlinOptions.isUsages)) continue;
@@ -0,0 +1,69 @@
/*
* 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.findUsages
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiConstructorCall
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiMethod
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor
import org.jetbrains.jet.lang.psi.*
import org.jetbrains.jet.lang.psi.psiUtil.*
import org.jetbrains.jet.lang.resolve.BindingContext
import org.jetbrains.jet.lang.resolve.BindingContextUtils
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache
fun PsiElement.isConstructorUsage(jetClassOrObject: JetClassOrObject): Boolean {
fun getCallDescriptor(bindingContext: BindingContext): DeclarationDescriptor? {
val constructorCalleeExpression = getParentByType(javaClass<JetConstructorCalleeExpression>())
if (constructorCalleeExpression != null) {
return bindingContext.get(BindingContext.REFERENCE_TARGET, constructorCalleeExpression.getConstructorReferenceExpression())
}
val callExpression = getParentByType(javaClass<JetCallExpression>())
if (callExpression != null) {
val callee = callExpression.getCalleeExpression()
if (callee is JetReferenceExpression) {
return bindingContext.get(BindingContext.REFERENCE_TARGET, callee)
}
}
return null
}
fun checkJavaUsage(): Boolean {
val call = getParentByType(javaClass<PsiConstructorCall>())
return call != null && call == getParent()
&& call.resolveConstructor()?.getContainingClass()?.getNavigationElement() == jetClassOrObject
}
fun checkKotlinUsage(): Boolean {
val file = getContainingFile()
if (file !is JetFile) return false
val bindingContext = AnalyzerFacadeWithCache.analyzeFileWithCache(file).getBindingContext()
val descriptor = getCallDescriptor(bindingContext)
if (descriptor !is ConstructorDescriptor) return false
return BindingContextUtils.descriptorToDeclaration(bindingContext, descriptor.getContainingDeclaration()) == jetClassOrObject
}
return checkJavaUsage() || checkKotlinUsage()
}
@@ -35,7 +35,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.asJava.LightClassUtil;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.plugin.findUsages.FindUsagesUtils;
import org.jetbrains.jet.plugin.findUsages.FindUsagesPackage;
import org.jetbrains.jet.plugin.hierarchy.HierarchyUtils;
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
import org.jetbrains.jet.plugin.references.JetPsiReference;
@@ -150,7 +150,7 @@ public abstract class KotlinCallerMethodsTreeStructure extends KotlinCallTreeStr
new Condition<PsiReference>() {
@Override
public boolean value(PsiReference reference) {
return FindUsagesUtils.isConstructorUsage(reference.getElement(), classOrObject);
return FindUsagesPackage.isConstructorUsage(reference.getElement(), classOrObject);
}
},
defaultQueryProcessor(descriptor, methodToDescriptorMap, false)