Implement parameterIndex() function
This commit is contained in:
@@ -46,6 +46,9 @@ import org.jetbrains.jet.lang.psi.JetNamedFunction
|
|||||||
import org.jetbrains.jet.lang.psi.JetProperty
|
import org.jetbrains.jet.lang.psi.JetProperty
|
||||||
import org.jetbrains.jet.lang.psi.JetCallableDeclaration
|
import org.jetbrains.jet.lang.psi.JetCallableDeclaration
|
||||||
import org.jetbrains.jet.lang.psi.JetPropertyAccessor
|
import org.jetbrains.jet.lang.psi.JetPropertyAccessor
|
||||||
|
import org.jetbrains.jet.lang.psi.JetParameter
|
||||||
|
import com.intellij.psi.PsiParameterList
|
||||||
|
import com.intellij.psi.PsiParameter
|
||||||
|
|
||||||
fun PsiElement.getParentByTypesAndPredicate<T: PsiElement>(
|
fun PsiElement.getParentByTypesAndPredicate<T: PsiElement>(
|
||||||
strict : Boolean = false, vararg parentClasses : Class<T>, predicate: (T) -> Boolean
|
strict : Boolean = false, vararg parentClasses : Class<T>, predicate: (T) -> Boolean
|
||||||
@@ -198,3 +201,12 @@ fun PsiElement.deleteElementAndCleanParent() {
|
|||||||
[suppress("UNCHECKED_CAST")]
|
[suppress("UNCHECKED_CAST")]
|
||||||
JetPsiUtil.deleteChildlessElement(parent, this.getClass() as Class<PsiElement>)
|
JetPsiUtil.deleteChildlessElement(parent, this.getClass() as Class<PsiElement>)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun PsiElement.parameterIndex(): Int {
|
||||||
|
val parent = getParent()
|
||||||
|
return when {
|
||||||
|
this is JetParameter && parent is JetParameterList -> parent.getParameters().indexOf(this)
|
||||||
|
this is PsiParameter && parent is PsiParameterList -> parent.getParameterIndex(this)
|
||||||
|
else -> -1
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -36,6 +36,7 @@ import com.intellij.util.Function;
|
|||||||
import com.intellij.util.containers.ContainerUtil;
|
import com.intellij.util.containers.ContainerUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.jet.asJava.AsJavaPackage;
|
||||||
import org.jetbrains.jet.lang.descriptors.*;
|
import org.jetbrains.jet.lang.descriptors.*;
|
||||||
import org.jetbrains.jet.lang.descriptors.impl.LocalVariableDescriptor;
|
import org.jetbrains.jet.lang.descriptors.impl.LocalVariableDescriptor;
|
||||||
import org.jetbrains.jet.lang.psi.*;
|
import org.jetbrains.jet.lang.psi.*;
|
||||||
@@ -289,9 +290,8 @@ public class JetRefactoringUtil {
|
|||||||
@Nullable
|
@Nullable
|
||||||
public static Collection<? extends PsiElement> checkParametersInMethodHierarchy(@NotNull PsiParameter parameter) {
|
public static Collection<? extends PsiElement> checkParametersInMethodHierarchy(@NotNull PsiParameter parameter) {
|
||||||
PsiMethod method = (PsiMethod)parameter.getDeclarationScope();
|
PsiMethod method = (PsiMethod)parameter.getDeclarationScope();
|
||||||
int parameterIndex = method.getParameterList().getParameterIndex(parameter);
|
|
||||||
|
|
||||||
Set<PsiElement> parametersToDelete = collectParametersHierarchy(method, parameterIndex);
|
Set<PsiElement> parametersToDelete = collectParametersHierarchy(method, parameter);
|
||||||
if (parametersToDelete.size() > 1) {
|
if (parametersToDelete.size() > 1) {
|
||||||
if (ApplicationManager.getApplication().isUnitTestMode()) {
|
if (ApplicationManager.getApplication().isUnitTestMode()) {
|
||||||
return parametersToDelete;
|
return parametersToDelete;
|
||||||
@@ -315,7 +315,7 @@ public class JetRefactoringUtil {
|
|||||||
|
|
||||||
// TODO: generalize breadth-first search
|
// TODO: generalize breadth-first search
|
||||||
@NotNull
|
@NotNull
|
||||||
private static Set<PsiElement> collectParametersHierarchy(@NotNull PsiMethod method, int parameterIndex) {
|
private static Set<PsiElement> collectParametersHierarchy(@NotNull PsiMethod method, @NotNull PsiParameter parameter) {
|
||||||
Deque<PsiMethod> queue = new ArrayDeque<PsiMethod>();
|
Deque<PsiMethod> queue = new ArrayDeque<PsiMethod>();
|
||||||
Set<PsiMethod> visited = new HashSet<PsiMethod>();
|
Set<PsiMethod> visited = new HashSet<PsiMethod>();
|
||||||
Set<PsiElement> parametersToDelete = new HashSet<PsiElement>();
|
Set<PsiElement> parametersToDelete = new HashSet<PsiElement>();
|
||||||
@@ -325,7 +325,7 @@ public class JetRefactoringUtil {
|
|||||||
PsiMethod currentMethod = queue.poll();
|
PsiMethod currentMethod = queue.poll();
|
||||||
|
|
||||||
visited.add(currentMethod);
|
visited.add(currentMethod);
|
||||||
addParameter(currentMethod, parametersToDelete, parameterIndex);
|
addParameter(currentMethod, parametersToDelete, parameter);
|
||||||
|
|
||||||
for (PsiMethod superMethod : currentMethod.findSuperMethods(true)) {
|
for (PsiMethod superMethod : currentMethod.findSuperMethods(true)) {
|
||||||
if (!visited.contains(superMethod)) {
|
if (!visited.contains(superMethod)) {
|
||||||
@@ -341,15 +341,16 @@ public class JetRefactoringUtil {
|
|||||||
return parametersToDelete;
|
return parametersToDelete;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void addParameter(@NotNull PsiMethod method, @NotNull Set<PsiElement> result, int parameterIndex) {
|
private static void addParameter(@NotNull PsiMethod method, @NotNull Set<PsiElement> result, @NotNull PsiParameter parameter) {
|
||||||
|
int parameterIndex = PsiUtilPackage.parameterIndex(AsJavaPackage.getUnwrapped(parameter));
|
||||||
|
|
||||||
if (method instanceof KotlinLightMethod) {
|
if (method instanceof KotlinLightMethod) {
|
||||||
JetDeclaration declaration = ((KotlinLightMethod) method).getOrigin();
|
JetDeclaration declaration = ((KotlinLightMethod) method).getOrigin();
|
||||||
int jetParameterIndex = PsiUtilPackage.isExtensionDeclaration(declaration) ? parameterIndex - 1 : parameterIndex;
|
|
||||||
if (declaration instanceof JetNamedFunction) {
|
if (declaration instanceof JetNamedFunction) {
|
||||||
result.add(((JetNamedFunction) declaration).getValueParameters().get(jetParameterIndex));
|
result.add(((JetNamedFunction) declaration).getValueParameters().get(parameterIndex));
|
||||||
}
|
}
|
||||||
else if (declaration instanceof JetClass) {
|
else if (declaration instanceof JetClass) {
|
||||||
result.add(((JetClass) declaration).getPrimaryConstructorParameters().get(jetParameterIndex));
|
result.add(((JetClass) declaration).getPrimaryConstructorParameters().get(parameterIndex));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
+1
-4
@@ -28,10 +28,7 @@ public class KotlinJavaSafeDeleteDelegate implements JavaSafeDeleteDelegate {
|
|||||||
PsiElement originalDeclaration = AsJavaPackage.getUnwrapped(method);
|
PsiElement originalDeclaration = AsJavaPackage.getUnwrapped(method);
|
||||||
if (!(originalDeclaration instanceof PsiMethod || originalDeclaration instanceof JetDeclaration)) return;
|
if (!(originalDeclaration instanceof PsiMethod || originalDeclaration instanceof JetDeclaration)) return;
|
||||||
|
|
||||||
int parameterIndex = method.getParameterList().getParameterIndex(parameter);
|
int parameterIndex = PsiUtilPackage.parameterIndex(AsJavaPackage.getUnwrapped(parameter));
|
||||||
if (PsiUtilPackage.isExtensionDeclaration(originalDeclaration)) {
|
|
||||||
parameterIndex--;
|
|
||||||
}
|
|
||||||
if (parameterIndex < 0) return;
|
if (parameterIndex < 0) return;
|
||||||
|
|
||||||
JetCallExpression callExpression = PsiTreeUtil.getParentOfType(reference.getElement(), JetCallExpression.class, false);
|
JetCallExpression callExpression = PsiTreeUtil.getParentOfType(reference.getElement(), JetCallExpression.class, false);
|
||||||
|
|||||||
Reference in New Issue
Block a user