Search implementations for properties from constructor parameters
This commit is contained in:
committed by
Nikolay Krasko
parent
5cc49f55f3
commit
0a2ca39f88
@@ -29,10 +29,7 @@ import com.intellij.util.Processor;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.asJava.LightClassUtil;
|
||||
import org.jetbrains.jet.lang.psi.JetClass;
|
||||
import org.jetbrains.jet.lang.psi.JetNamedFunction;
|
||||
import org.jetbrains.jet.lang.psi.JetProperty;
|
||||
import org.jetbrains.jet.lang.psi.JetPropertyAccessor;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
|
||||
public class KotlinDefinitionsSearcher extends QueryExecutorBase<PsiElement, PsiElement> {
|
||||
@Override
|
||||
@@ -48,6 +45,13 @@ public class KotlinDefinitionsSearcher extends QueryExecutorBase<PsiElement, Psi
|
||||
if (queryParameters instanceof JetProperty) {
|
||||
processPropertyImplementations((JetProperty) queryParameters, consumer);
|
||||
}
|
||||
|
||||
if (queryParameters instanceof JetParameter) {
|
||||
JetParameter parameter = (JetParameter) queryParameters;
|
||||
if (parameter.getValOrVarNode() != null && queryParameters.getParent().getParent() instanceof JetClass) {
|
||||
processPropertyImplementations(parameter, consumer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void processClassImplementations(final JetClass queryParameters, Processor<PsiElement> consumer) {
|
||||
@@ -75,6 +79,18 @@ public class KotlinDefinitionsSearcher extends QueryExecutorBase<PsiElement, Psi
|
||||
}
|
||||
}
|
||||
|
||||
private static void processPropertyImplementations(@NotNull final JetParameter parameter, @NotNull Processor<PsiElement> consumer) {
|
||||
LightClassUtil.PropertyAccessorsPsiMethods accessorsPsiMethods = ApplicationManager.getApplication().runReadAction(
|
||||
new Computable<LightClassUtil.PropertyAccessorsPsiMethods>() {
|
||||
@Override
|
||||
public LightClassUtil.PropertyAccessorsPsiMethods compute() {
|
||||
return LightClassUtil.getLightClassPropertyMethods(parameter);
|
||||
}
|
||||
});
|
||||
|
||||
processPropertyImplementationsMethods(accessorsPsiMethods, consumer);
|
||||
}
|
||||
|
||||
private static void processPropertyImplementations(@NotNull final JetProperty property, @NotNull Processor<PsiElement> consumer) {
|
||||
LightClassUtil.PropertyAccessorsPsiMethods accessorsPsiMethods = ApplicationManager.getApplication().runReadAction(
|
||||
new Computable<LightClassUtil.PropertyAccessorsPsiMethods>() {
|
||||
@@ -84,11 +100,15 @@ public class KotlinDefinitionsSearcher extends QueryExecutorBase<PsiElement, Psi
|
||||
}
|
||||
});
|
||||
|
||||
for (PsiMethod method : accessorsPsiMethods) {
|
||||
processPropertyImplementationsMethods(accessorsPsiMethods, consumer);
|
||||
}
|
||||
|
||||
private static void processPropertyImplementationsMethods(LightClassUtil.PropertyAccessorsPsiMethods accessors, @NotNull Processor<PsiElement> consumer) {
|
||||
for (PsiMethod method : accessors) {
|
||||
PsiMethod[] implementations = MethodImplementationsSearch.getMethodImplementations(method);
|
||||
for (PsiMethod implementation : implementations) {
|
||||
PsiElement mirrorElement = implementation instanceof PsiCompiledElement ? ((PsiCompiledElement) implementation).getMirror() : null;
|
||||
if (mirrorElement instanceof JetProperty) {
|
||||
if (mirrorElement instanceof JetProperty || mirrorElement instanceof JetParameter) {
|
||||
consumer.process(mirrorElement);
|
||||
}
|
||||
else if (mirrorElement instanceof JetPropertyAccessor && mirrorElement.getParent() instanceof JetProperty) {
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
class Test(var <caret>field: Int)
|
||||
@@ -0,0 +1 @@
|
||||
class Test(var field: Int, <caret>parameter: Int)
|
||||
@@ -0,0 +1 @@
|
||||
fun testFun(<caret>parameter: Int) = 0
|
||||
@@ -122,6 +122,18 @@ public class JetJavaFacadeTest extends LightCodeInsightFixtureTestCase {
|
||||
doTestWrapPropertyAccessor(true);
|
||||
}
|
||||
|
||||
public void testWrapConstructorField() {
|
||||
doTestWrapParameter(true, true);
|
||||
}
|
||||
|
||||
public void testWrapConstructorParameter() {
|
||||
doTestWrapParameter(false, false);
|
||||
}
|
||||
|
||||
public void testWrapFunctionParameter() {
|
||||
doTestWrapParameter(false, false);
|
||||
}
|
||||
|
||||
public void testEa38770() {
|
||||
myFixture.configureByFile(getTestName(true) + ".kt");
|
||||
|
||||
@@ -197,6 +209,16 @@ public class JetJavaFacadeTest extends LightCodeInsightFixtureTestCase {
|
||||
checkDeclarationMethodWrapped(shouldBeWrapped, jetFunction, psiMethod);
|
||||
}
|
||||
|
||||
private void doTestWrapParameter(boolean shouldWrapGetter, boolean shouldWrapSetter) {
|
||||
JetParameter jetParameter = getPreparedElement(JetParameter.class);
|
||||
|
||||
// Should not fail!
|
||||
LightClassUtil.PropertyAccessorsPsiMethods propertyAccessors = LightClassUtil.getLightClassPropertyMethods(jetParameter);
|
||||
|
||||
checkDeclarationMethodWrapped(shouldWrapGetter, jetParameter, propertyAccessors.getGetter());
|
||||
checkDeclarationMethodWrapped(shouldWrapSetter, jetParameter, propertyAccessors.getSetter());
|
||||
}
|
||||
|
||||
private void doTestWrapProperty(boolean shouldWrapGetter, boolean shouldWrapSetter) {
|
||||
JetProperty jetProperty = getPreparedElement(JetProperty.class);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user