Make IDE parts using light methods work with JetFunction
This commit is contained in:
committed by
Alexey Sedunov
parent
7d278f2f42
commit
0debd1ce6f
@@ -180,7 +180,7 @@ public class LightClassUtil {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PsiMethod getLightClassMethod(@NotNull JetNamedFunction function) {
|
||||
public static PsiMethod getLightClassMethod(@NotNull JetFunction function) {
|
||||
return getPsiMethodWrapper(function);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,8 @@ public fun JetClassOrObject.toLightClass(): KotlinLightClass? = LightClassUtil.g
|
||||
public fun JetDeclaration.toLightElements(): List<PsiNamedElement> =
|
||||
when (this) {
|
||||
is JetClassOrObject -> Collections.singletonList(LightClassUtil.getPsiClass(this))
|
||||
is JetNamedFunction -> Collections.singletonList(LightClassUtil.getLightClassMethod(this))
|
||||
is JetNamedFunction,
|
||||
is JetSecondaryConstructor -> Collections.singletonList(LightClassUtil.getLightClassMethod(this as JetFunction))
|
||||
is JetProperty -> LightClassUtil.getLightClassPropertyMethods(this).toList()
|
||||
is JetPropertyAccessor -> Collections.singletonList(LightClassUtil.getLightClassAccessorMethod(this))
|
||||
is JetParameter -> ArrayList<PsiNamedElement>().let { elements ->
|
||||
@@ -44,7 +45,7 @@ public fun JetDeclaration.toLightElements(): List<PsiNamedElement> =
|
||||
|
||||
public fun PsiElement.toLightMethods(): List<PsiMethod> =
|
||||
when (this) {
|
||||
is JetNamedFunction -> LightClassUtil.getLightClassMethod(this).singletonOrEmptyList()
|
||||
is JetFunction -> LightClassUtil.getLightClassMethod(this).singletonOrEmptyList()
|
||||
is JetProperty -> LightClassUtil.getLightClassPropertyMethods(this).toList()
|
||||
is JetParameter -> LightClassUtil.getLightClassPropertyMethods(this).toList()
|
||||
is JetPropertyAccessor -> LightClassUtil.getLightClassAccessorMethod(this).singletonOrEmptyList()
|
||||
@@ -55,7 +56,7 @@ public fun PsiElement.toLightMethods(): List<PsiMethod> =
|
||||
|
||||
public fun PsiElement.getRepresentativeLightMethod(): PsiMethod? =
|
||||
when (this) {
|
||||
is JetNamedFunction -> LightClassUtil.getLightClassMethod(this)
|
||||
is JetFunction -> LightClassUtil.getLightClassMethod(this)
|
||||
is JetProperty -> LightClassUtil.getLightClassPropertyMethods(this).getGetter()
|
||||
is JetParameter -> LightClassUtil.getLightClassPropertyMethods(this).getGetter()
|
||||
is JetPropertyAccessor -> LightClassUtil.getLightClassAccessorMethod(this)
|
||||
@@ -72,7 +73,7 @@ public fun JetParameter.toPsiParameter(): PsiParameter? {
|
||||
val lightParamIndex = if (owner != null && owner.isExtensionDeclaration()) paramIndex + 1 else paramIndex
|
||||
|
||||
val method: PsiMethod? = when (owner) {
|
||||
is JetNamedFunction -> LightClassUtil.getLightClassMethod(owner)
|
||||
is JetFunction -> LightClassUtil.getLightClassMethod(owner)
|
||||
is JetPropertyAccessor -> LightClassUtil.getLightClassAccessorMethod(owner)
|
||||
is JetClass -> LightClassUtil.getPsiClass(owner)?.getConstructors()?.let { constructors ->
|
||||
if (constructors.isNotEmpty()) constructors[0] else null
|
||||
|
||||
+2
-2
@@ -98,8 +98,8 @@ public class KotlinReferencesSearcher extends QueryExecutorBase<PsiReference, Re
|
||||
if (element instanceof JetClassOrObject) {
|
||||
processJetClassOrObject((JetClassOrObject) element, queryParameters);
|
||||
}
|
||||
else if (element instanceof JetNamedFunction) {
|
||||
final JetNamedFunction function = (JetNamedFunction) element;
|
||||
else if (element instanceof JetNamedFunction || element instanceof JetSecondaryConstructor) {
|
||||
final JetFunction function = (JetFunction) element;
|
||||
String name = function.getName();
|
||||
if (name != null) {
|
||||
PsiMethod method = ApplicationManager.getApplication().runReadAction(new Computable<PsiMethod>() {
|
||||
|
||||
+2
-2
@@ -191,8 +191,8 @@ class FunctionUsagesSearchHelper(
|
||||
override val selfUsages: Boolean = true,
|
||||
override val overrideUsages: Boolean = true,
|
||||
skipImports: Boolean = false
|
||||
) : DefaultSearchHelper<JetNamedFunction>(skipImports), OverrideSearchHelper {
|
||||
override fun makeFilter(target: UsagesSearchTarget<JetNamedFunction>): UsagesSearchFilter {
|
||||
) : DefaultSearchHelper<JetFunction>(skipImports), OverrideSearchHelper {
|
||||
override fun makeFilter(target: UsagesSearchTarget<JetFunction>): UsagesSearchFilter {
|
||||
return (isTargetOrOverrideUsage
|
||||
or isOverloadUsage.ifOrFalse(overloadUsages)
|
||||
or isExtensionUsage.ifOrFalse(extensionUsages)) and isFilteredImport
|
||||
|
||||
@@ -144,8 +144,8 @@ public class KotlinSmartStepIntoHandler : JvmSmartStepIntoHandler() {
|
||||
val delegatedPropertyGetterDescriptor = delegatedResolvedCall.getResultingDescriptor()
|
||||
if (delegatedPropertyGetterDescriptor is CallableMemberDescriptor) {
|
||||
val function = DescriptorToSourceUtilsIde.getAnyDeclaration(file.getProject(), delegatedPropertyGetterDescriptor)
|
||||
if (function is JetNamedFunction) {
|
||||
val psiMethod = LightClassUtil.getLightClassMethod(function)
|
||||
if (function is JetNamedFunction || function is JetSecondaryConstructor) {
|
||||
val psiMethod = LightClassUtil.getLightClassMethod(function as JetFunction)
|
||||
if (psiMethod != null) {
|
||||
result.add(KotlinMethodSmartStepTarget(function, psiMethod, "${propertyDescriptor.getName()}.", expression, false, lines))
|
||||
}
|
||||
@@ -166,8 +166,8 @@ public class KotlinSmartStepIntoHandler : JvmSmartStepIntoHandler() {
|
||||
if (descriptor is CallableMemberDescriptor) {
|
||||
// TODO doesn't work for libraries
|
||||
val function = DescriptorToSourceUtilsIde.getAnyDeclaration(file.getProject(), descriptor)
|
||||
if (function is JetNamedFunction) {
|
||||
val psiMethod = LightClassUtil.getLightClassMethod(function)
|
||||
if (function is JetNamedFunction || function is JetSecondaryConstructor) {
|
||||
val psiMethod = LightClassUtil.getLightClassMethod(function as JetFunction)
|
||||
if (psiMethod != null) {
|
||||
result.add(KotlinMethodSmartStepTarget(function, psiMethod, null, expression, false, lines))
|
||||
}
|
||||
|
||||
+9
-12
@@ -39,19 +39,16 @@ import org.jetbrains.kotlin.idea.search.declarationsSearch.HierarchySearchReques
|
||||
import org.jetbrains.kotlin.idea.search.usagesSearch.UsagesSearch;
|
||||
import org.jetbrains.kotlin.idea.search.usagesSearch.UsagesSearchHelper;
|
||||
import org.jetbrains.kotlin.idea.search.usagesSearch.UsagesSearchRequest;
|
||||
import org.jetbrains.kotlin.psi.JetNamedDeclaration;
|
||||
import org.jetbrains.kotlin.psi.JetNamedFunction;
|
||||
import org.jetbrains.kotlin.psi.JetParameter;
|
||||
import org.jetbrains.kotlin.psi.JetProperty;
|
||||
import org.jetbrains.kotlin.psi.*;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
|
||||
public abstract class KotlinFindMemberUsagesHandler<T extends JetNamedDeclaration> extends KotlinFindUsagesHandler<T> {
|
||||
private static class Function extends KotlinFindMemberUsagesHandler<JetNamedFunction> {
|
||||
private static class Function extends KotlinFindMemberUsagesHandler<JetFunction> {
|
||||
public Function(
|
||||
@NotNull JetNamedFunction declaration,
|
||||
@NotNull JetFunction declaration,
|
||||
@NotNull Collection<? extends PsiElement> elementsToSearch,
|
||||
@NotNull KotlinFindUsagesHandlerFactory factory
|
||||
) {
|
||||
@@ -59,7 +56,7 @@ public abstract class KotlinFindMemberUsagesHandler<T extends JetNamedDeclaratio
|
||||
}
|
||||
|
||||
@Override
|
||||
protected UsagesSearchHelper<JetNamedFunction> getSearchHelper(KotlinCallableFindUsagesOptions options) {
|
||||
protected UsagesSearchHelper<JetFunction> getSearchHelper(KotlinCallableFindUsagesOptions options) {
|
||||
return FindUsagesPackage.toHelper((KotlinFunctionFindUsagesOptions) options);
|
||||
}
|
||||
|
||||
@@ -124,8 +121,8 @@ public abstract class KotlinFindMemberUsagesHandler<T extends JetNamedDeclaratio
|
||||
protected abstract UsagesSearchHelper<T> getSearchHelper(KotlinCallableFindUsagesOptions options);
|
||||
|
||||
private static Iterable<PsiMethod> getLightMethods(JetNamedDeclaration element) {
|
||||
if (element instanceof JetNamedFunction) {
|
||||
PsiMethod method = LightClassUtil.getLightClassMethod((JetNamedFunction) element);
|
||||
if (element instanceof JetNamedFunction || element instanceof JetSecondaryConstructor) {
|
||||
PsiMethod method = LightClassUtil.getLightClassMethod((JetFunction) element);
|
||||
return method != null ? Collections.singletonList(method) : Collections.<PsiMethod>emptyList();
|
||||
}
|
||||
|
||||
@@ -152,7 +149,7 @@ public abstract class KotlinFindMemberUsagesHandler<T extends JetNamedDeclaratio
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
UsagesSearchRequest request =
|
||||
getSearchHelper(kotlinOptions).newRequest(FindUsagesPackage.toSearchTarget(options, (T) element, true));
|
||||
getSearchHelper(kotlinOptions).newRequest(FindUsagesPackage.<T>toSearchTarget(options, (T) element, true));
|
||||
|
||||
for (PsiReference ref : UsagesSearch.INSTANCE$.search(request)) {
|
||||
processUsage(processor, ref);
|
||||
@@ -189,8 +186,8 @@ public abstract class KotlinFindMemberUsagesHandler<T extends JetNamedDeclaratio
|
||||
@NotNull JetNamedDeclaration declaration,
|
||||
@NotNull Collection<? extends PsiElement> elementsToSearch,
|
||||
@NotNull KotlinFindUsagesHandlerFactory factory) {
|
||||
return declaration instanceof JetNamedFunction
|
||||
? new Function((JetNamedFunction) declaration, elementsToSearch, factory)
|
||||
return declaration instanceof JetFunction
|
||||
? new Function((JetFunction) declaration, elementsToSearch, factory)
|
||||
: new Property(declaration, elementsToSearch, factory);
|
||||
}
|
||||
|
||||
|
||||
@@ -91,8 +91,8 @@ public abstract class KotlinCallTreeStructure extends HierarchyTreeStructure {
|
||||
return (PsiMethod) element;
|
||||
}
|
||||
|
||||
if (element instanceof JetNamedFunction) {
|
||||
return LightClassUtil.getLightClassMethod((JetNamedFunction) element);
|
||||
if (element instanceof JetNamedFunction || element instanceof JetSecondaryConstructor) {
|
||||
return LightClassUtil.getLightClassMethod((JetFunction) element);
|
||||
}
|
||||
|
||||
if (element instanceof JetProperty) {
|
||||
|
||||
+2
-2
@@ -103,8 +103,8 @@ public class KotlinCallerMethodsTreeStructure extends KotlinCallTreeStructure {
|
||||
Collections.singleton((PsiMethod) element), descriptor, methodToDescriptorMap, searchScope, true
|
||||
);
|
||||
}
|
||||
if (element instanceof JetNamedFunction) {
|
||||
PsiMethod lightMethod = LightClassUtil.getLightClassMethod((JetNamedFunction) element);
|
||||
if (element instanceof JetNamedFunction || element instanceof JetSecondaryConstructor) {
|
||||
PsiMethod lightMethod = LightClassUtil.getLightClassMethod((JetFunction) element);
|
||||
processPsiMethodCallers(Collections.singleton(lightMethod), descriptor, methodToDescriptorMap, searchScope, false);
|
||||
}
|
||||
if (element instanceof JetProperty) {
|
||||
|
||||
@@ -16,15 +16,17 @@
|
||||
|
||||
package org.jetbrains.kotlin.idea.highlighter.markers
|
||||
|
||||
import java.util.HashSet
|
||||
import com.intellij.psi.PsiMethod
|
||||
import com.intellij.psi.PsiClass
|
||||
import com.intellij.psi.CommonClassNames
|
||||
import com.intellij.openapi.progress.ProgressManager
|
||||
import com.intellij.psi.CommonClassNames
|
||||
import com.intellij.psi.PsiClass
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.psi.JetClass
|
||||
import com.intellij.psi.PsiMethod
|
||||
import org.jetbrains.kotlin.asJava.LightClassUtil
|
||||
import org.jetbrains.kotlin.psi.JetClass
|
||||
import org.jetbrains.kotlin.psi.JetFunction
|
||||
import org.jetbrains.kotlin.psi.JetNamedFunction
|
||||
import org.jetbrains.kotlin.psi.JetSecondaryConstructor
|
||||
import java.util.HashSet
|
||||
|
||||
fun collectContainingClasses(methods: Collection<PsiMethod>): Set<PsiClass> {
|
||||
val classes = HashSet<PsiClass>()
|
||||
@@ -50,11 +52,11 @@ private fun getPsiClass(element: PsiElement?): PsiClass? {
|
||||
}
|
||||
|
||||
private fun getPsiMethod(element: PsiElement?): PsiMethod? {
|
||||
val parent = element?.getParent()
|
||||
return when {
|
||||
element == null -> null
|
||||
element is PsiMethod -> element
|
||||
element.getParent() is JetNamedFunction ->
|
||||
LightClassUtil.getLightClassMethod(element.getParent() as JetNamedFunction)
|
||||
parent is JetNamedFunction, parent is JetSecondaryConstructor -> LightClassUtil.getLightClassMethod(parent as JetFunction)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,10 +20,8 @@ import com.intellij.codeInsight.FileModificationService
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle
|
||||
import com.intellij.codeInspection.*
|
||||
import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection
|
||||
import com.intellij.codeInspection.ex.EntryPointsManager
|
||||
import com.intellij.codeInspection.ex.EntryPointsManagerImpl
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.project.ProjectUtil
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
@@ -64,7 +62,7 @@ public class UnusedSymbolInspection : AbstractKotlinInspection() {
|
||||
|
||||
val lightElement: PsiElement? = when (declaration) {
|
||||
is JetClassOrObject -> declaration.toLightClass()
|
||||
is JetNamedFunction -> LightClassUtil.getLightClassMethod(declaration)
|
||||
is JetNamedFunction, is JetSecondaryConstructor -> LightClassUtil.getLightClassMethod(declaration as JetFunction)
|
||||
else -> return false
|
||||
}
|
||||
return lightElement != null && javaInspection.isEntryPoint(lightElement)
|
||||
|
||||
+1
-1
@@ -97,7 +97,7 @@ public class JetChangeSignatureData(
|
||||
|
||||
override val affectedFunctions: Collection<UsageInfo> by Delegates.lazy {
|
||||
primaryFunctions + primaryFunctions.flatMapTo(HashSet<UsageInfo>()) { primaryFunction ->
|
||||
val primaryDeclaration = primaryFunction.getDeclaration() as? JetNamedFunction
|
||||
val primaryDeclaration = primaryFunction.getDeclaration() as? JetFunction
|
||||
val lightMethod = primaryDeclaration?.let { LightClassUtil.getLightClassMethod(it) }
|
||||
val overrides = lightMethod?.let { OverridingMethodsSearch.search(it).findAll() } ?: Collections.emptyList()
|
||||
overrides.map { method ->
|
||||
|
||||
+8
-4
@@ -20,12 +20,14 @@ import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiMethod
|
||||
import com.intellij.psi.search.SearchScope
|
||||
import org.jetbrains.kotlin.asJava.LightClassUtil
|
||||
import org.jetbrains.kotlin.psi.JetNamedFunction
|
||||
import com.intellij.refactoring.rename.RenameJavaMethodProcessor
|
||||
import org.jetbrains.kotlin.asJava.KotlinLightMethod
|
||||
import kotlin.properties.Delegates
|
||||
import org.jetbrains.kotlin.asJava.LightClassUtil
|
||||
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||
import org.jetbrains.kotlin.psi.JetFunction
|
||||
import org.jetbrains.kotlin.psi.JetNamedFunction
|
||||
import org.jetbrains.kotlin.psi.JetSecondaryConstructor
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
public class RenameKotlinFunctionProcessor : RenameKotlinPsiProcessor() {
|
||||
private val javaMethodProcessorInstance by Delegates.lazy {
|
||||
@@ -61,7 +63,9 @@ public class RenameKotlinFunctionProcessor : RenameKotlinPsiProcessor() {
|
||||
|
||||
private fun wrapPsiMethod(element: PsiElement?): PsiMethod? = when (element) {
|
||||
is KotlinLightMethod -> element
|
||||
is JetNamedFunction -> runReadAction { LightClassUtil.getLightClassMethod(element) }
|
||||
is JetNamedFunction, is JetSecondaryConstructor -> runReadAction {
|
||||
LightClassUtil.getLightClassMethod(element as JetFunction)
|
||||
}
|
||||
else -> throw IllegalStateException("Can't be for element $element there because of canProcessElement()")
|
||||
}
|
||||
}
|
||||
|
||||
+3
-6
@@ -38,10 +38,7 @@ import org.jetbrains.kotlin.descriptors.ClassifierDescriptor;
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor;
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.ResolvePackage;
|
||||
import org.jetbrains.kotlin.idea.stubindex.JetAnnotationsIndex;
|
||||
import org.jetbrains.kotlin.psi.JetAnnotationEntry;
|
||||
import org.jetbrains.kotlin.psi.JetClass;
|
||||
import org.jetbrains.kotlin.psi.JetDeclaration;
|
||||
import org.jetbrains.kotlin.psi.JetNamedFunction;
|
||||
import org.jetbrains.kotlin.psi.*;
|
||||
import org.jetbrains.kotlin.resolve.BindingContext;
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils;
|
||||
|
||||
@@ -84,8 +81,8 @@ public class KotlinAnnotatedElementsSearcher extends AnnotatedElementsSearcher {
|
||||
PsiClass lightClass = LightClassUtil.getPsiClass((JetClass) parentOfType);
|
||||
consumer.process(lightClass);
|
||||
}
|
||||
else if (parentOfType instanceof JetNamedFunction) {
|
||||
PsiMethod wrappedMethod = LightClassUtil.getLightClassMethod((JetNamedFunction) parentOfType);
|
||||
else if (parentOfType instanceof JetNamedFunction || parentOfType instanceof JetSecondaryConstructor) {
|
||||
PsiMethod wrappedMethod = LightClassUtil.getLightClassMethod((JetFunction) parentOfType);
|
||||
consumer.process(wrappedMethod);
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -43,8 +43,8 @@ public class KotlinDefinitionsSearcher implements QueryExecutor<PsiElement, Defi
|
||||
return processClassImplementations((JetClass) element, consumer);
|
||||
}
|
||||
|
||||
if (element instanceof JetNamedFunction) {
|
||||
return processFunctionImplementations((JetNamedFunction) element, scope, consumer);
|
||||
if (element instanceof JetNamedFunction || element instanceof JetSecondaryConstructor) {
|
||||
return processFunctionImplementations((JetFunction) element, scope, consumer);
|
||||
}
|
||||
|
||||
if (element instanceof JetProperty) {
|
||||
@@ -74,7 +74,7 @@ public class KotlinDefinitionsSearcher implements QueryExecutor<PsiElement, Defi
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean processFunctionImplementations(final JetNamedFunction function, SearchScope scope, Processor<PsiElement> consumer) {
|
||||
private static boolean processFunctionImplementations(final JetFunction function, SearchScope scope, Processor<PsiElement> consumer) {
|
||||
PsiMethod psiMethod = ApplicationManager.getApplication().runReadAction(new Computable<PsiMethod>() {
|
||||
@Override
|
||||
public PsiMethod compute() {
|
||||
|
||||
Reference in New Issue
Block a user