Extract utility methods from KotlinSafeDeleteProcessor
This commit is contained in:
@@ -7,4 +7,16 @@
|
||||
name='com.intellij.psi.search.searches.DirectClassInheritorsSearch com.intellij.util.Query<com.intellij.psi.PsiClass> search(com.intellij.psi.PsiClass, com.intellij.psi.search.SearchScope, boolean, boolean)'>
|
||||
<annotation name='org.jetbrains.annotations.NotNull'/>
|
||||
</item>
|
||||
<item
|
||||
name='com.intellij.psi.search.searches.OverridingMethodsSearch com.intellij.util.Query<com.intellij.psi.PsiMethod> search(com.intellij.psi.PsiMethod)'>
|
||||
<annotation name='org.jetbrains.annotations.NotNull'/>
|
||||
</item>
|
||||
<item
|
||||
name='com.intellij.psi.search.searches.OverridingMethodsSearch com.intellij.util.Query<com.intellij.psi.PsiMethod> search(com.intellij.psi.PsiMethod, boolean)'>
|
||||
<annotation name='org.jetbrains.annotations.NotNull'/>
|
||||
</item>
|
||||
<item
|
||||
name='com.intellij.psi.search.searches.OverridingMethodsSearch com.intellij.util.Query<com.intellij.psi.PsiMethod> search(com.intellij.psi.PsiMethod, com.intellij.psi.search.SearchScope, boolean)'>
|
||||
<annotation name='org.jetbrains.annotations.NotNull'/>
|
||||
</item>
|
||||
</root>
|
||||
@@ -189,4 +189,12 @@ fun PsiElement.isExtensionDeclaration(): Boolean {
|
||||
return callable?.getReceiverTypeRef() != null
|
||||
}
|
||||
|
||||
fun PsiElement.isObjectLiteral(): Boolean = this is JetObjectDeclaration && isObjectLiteral()
|
||||
fun PsiElement.isObjectLiteral(): Boolean = this is JetObjectDeclaration && isObjectLiteral()
|
||||
|
||||
fun PsiElement.deleteElementAndCleanParent() {
|
||||
val parent = getParent()
|
||||
|
||||
JetPsiUtil.deleteElementWithDelimiters(this)
|
||||
[suppress("UNCHECKED_CAST")]
|
||||
JetPsiUtil.deleteChildlessElement(parent, this.getClass() as Class<PsiElement>)
|
||||
}
|
||||
@@ -18,6 +18,7 @@ package org.jetbrains.jet.plugin.refactoring;
|
||||
|
||||
import com.intellij.codeInsight.unwrap.ScopeHighlighter;
|
||||
import com.intellij.ide.IdeBundle;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
@@ -27,6 +28,7 @@ import com.intellij.openapi.ui.popup.LightweightWindowEvent;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.searches.OverridingMethodsSearch;
|
||||
import com.intellij.psi.util.PsiFormatUtil;
|
||||
import com.intellij.psi.util.PsiFormatUtilBase;
|
||||
import com.intellij.ui.components.JBList;
|
||||
@@ -37,6 +39,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
import org.jetbrains.jet.lang.descriptors.impl.LocalVariableDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
|
||||
import org.jetbrains.jet.lang.resolve.OverridingUtil;
|
||||
@@ -283,6 +286,77 @@ public class JetRefactoringUtil {
|
||||
return "class " + classOrObject.getName();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Collection<? extends PsiElement> checkParametersInMethodHierarchy(@NotNull PsiParameter parameter) {
|
||||
PsiMethod method = (PsiMethod)parameter.getDeclarationScope();
|
||||
int parameterIndex = method.getParameterList().getParameterIndex(parameter);
|
||||
|
||||
Set<PsiElement> parametersToDelete = collectParametersHierarchy(method, parameterIndex);
|
||||
if (parametersToDelete.size() > 1) {
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
return parametersToDelete;
|
||||
}
|
||||
|
||||
String message =
|
||||
JetBundle.message("delete.param.in.method.hierarchy", formatJavaOrLightMethod(method));
|
||||
int exitCode = Messages.showOkCancelDialog(
|
||||
parameter.getProject(), message, IdeBundle.message("title.warning"), Messages.getQuestionIcon()
|
||||
);
|
||||
if (exitCode == Messages.OK) {
|
||||
return parametersToDelete;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return parametersToDelete;
|
||||
}
|
||||
|
||||
// TODO: generalize breadth-first search
|
||||
@NotNull
|
||||
private static Set<PsiElement> collectParametersHierarchy(@NotNull PsiMethod method, int parameterIndex) {
|
||||
Deque<PsiMethod> queue = new ArrayDeque<PsiMethod>();
|
||||
Set<PsiMethod> visited = new HashSet<PsiMethod>();
|
||||
Set<PsiElement> parametersToDelete = new HashSet<PsiElement>();
|
||||
|
||||
queue.add(method);
|
||||
while (!queue.isEmpty()) {
|
||||
PsiMethod currentMethod = queue.poll();
|
||||
|
||||
visited.add(currentMethod);
|
||||
addParameter(currentMethod, parametersToDelete, parameterIndex);
|
||||
|
||||
for (PsiMethod superMethod : currentMethod.findSuperMethods(true)) {
|
||||
if (!visited.contains(superMethod)) {
|
||||
queue.offer(superMethod);
|
||||
}
|
||||
}
|
||||
for (PsiMethod overrider : OverridingMethodsSearch.search(currentMethod)) {
|
||||
if (!visited.contains(overrider)) {
|
||||
queue.offer(overrider);
|
||||
}
|
||||
}
|
||||
}
|
||||
return parametersToDelete;
|
||||
}
|
||||
|
||||
private static void addParameter(@NotNull PsiMethod method, @NotNull Set<PsiElement> result, int parameterIndex) {
|
||||
if (method instanceof KotlinLightMethod) {
|
||||
JetDeclaration declaration = ((KotlinLightMethod) method).getOrigin();
|
||||
int jetParameterIndex = PsiUtilPackage.isExtensionDeclaration(declaration) ? parameterIndex - 1 : parameterIndex;
|
||||
if (declaration instanceof JetNamedFunction) {
|
||||
result.add(((JetNamedFunction) declaration).getValueParameters().get(jetParameterIndex));
|
||||
}
|
||||
else if (declaration instanceof JetClass) {
|
||||
result.add(((JetClass) declaration).getPrimaryConstructorParameters().get(jetParameterIndex));
|
||||
}
|
||||
}
|
||||
else {
|
||||
result.add(method.getParameterList().getParameters()[parameterIndex]);
|
||||
}
|
||||
}
|
||||
|
||||
public interface SelectExpressionCallback {
|
||||
void run(@Nullable JetExpression expression);
|
||||
}
|
||||
|
||||
+1
-1
@@ -25,6 +25,6 @@ public class KotlinSafeDeleteOverrideAnnotation(
|
||||
element: PsiElement, referencedElement: PsiElement
|
||||
) : SafeDeleteUsageInfo(element, referencedElement), SafeDeleteCustomUsageInfo {
|
||||
public override fun performRefactoring() {
|
||||
getElement()?.let { element -> KotlinSafeDeleteProcessor.removeOverrideModifier(element) }
|
||||
getElement()?.removeOverrideModifier()
|
||||
}
|
||||
}
|
||||
|
||||
+12
-151
@@ -16,15 +16,15 @@
|
||||
|
||||
package org.jetbrains.jet.plugin.refactoring.safeDelete;
|
||||
|
||||
import com.intellij.ide.IdeBundle;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
import com.intellij.openapi.util.Condition;
|
||||
import com.intellij.openapi.util.Conditions;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.searches.OverridingMethodsSearch;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.PsiParameter;
|
||||
import com.intellij.psi.PsiReference;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.refactoring.safeDelete.JavaSafeDeleteProcessor;
|
||||
@@ -400,49 +400,10 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor {
|
||||
return result.toArray(new UsageInfo[result.size()]);
|
||||
}
|
||||
|
||||
public static void removeOverrideModifier(@NotNull PsiElement element) {
|
||||
if (element instanceof JetNamedFunction || element instanceof JetProperty) {
|
||||
JetModifierList modifierList = ((JetModifierListOwner) element).getModifierList();
|
||||
if (modifierList == null) return;
|
||||
|
||||
PsiElement overrideModifier = modifierList.getModifier(JetTokens.OVERRIDE_KEYWORD);
|
||||
if (overrideModifier != null) {
|
||||
overrideModifier.delete();
|
||||
}
|
||||
}
|
||||
else if (element instanceof PsiMethod) {
|
||||
PsiMethod method = (PsiMethod) element;
|
||||
|
||||
PsiAnnotation overrideAnnotation = null;
|
||||
for (PsiAnnotation annotation : method.getModifierList().getAnnotations()) {
|
||||
if ("java.lang.Override".equals(annotation.getQualifiedName())) {
|
||||
overrideAnnotation = annotation;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (overrideAnnotation != null) {
|
||||
overrideAnnotation.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiParameter getPsiParameter(@NotNull JetParameter parameter) {
|
||||
JetNamedFunction function = PsiTreeUtil.getParentOfType(parameter, JetNamedFunction.class);
|
||||
if (function == null || parameter.getParent() != function.getValueParameterList()) return null;
|
||||
|
||||
PsiMethod lightMethod = LightClassUtil.getLightClassMethod(function);
|
||||
if (lightMethod == null) return null;
|
||||
|
||||
int parameterIndex = function.getValueParameters().indexOf(parameter);
|
||||
return lightMethod.getParameterList().getParameters()[parameterIndex];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareForDeletion(@NotNull PsiElement element) throws IncorrectOperationException {
|
||||
if (element instanceof PsiMethod) {
|
||||
cleanUpOverrides((PsiMethod) element);
|
||||
SafeDeletePackage.cleanUpOverrides((PsiMethod) element);
|
||||
}
|
||||
else if (element instanceof JetNamedFunction) {
|
||||
PsiMethod lightMethod = LightClassUtil.getLightClassMethod((JetNamedFunction) element);
|
||||
@@ -450,7 +411,7 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor {
|
||||
return;
|
||||
}
|
||||
|
||||
cleanUpOverrides(lightMethod);
|
||||
SafeDeletePackage.cleanUpOverrides(lightMethod);
|
||||
}
|
||||
else if (element instanceof JetProperty) {
|
||||
LightClassUtil.PropertyAccessorsPsiMethods propertyMethods =
|
||||
@@ -459,65 +420,32 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor {
|
||||
PsiMethod setter = propertyMethods.getSetter();
|
||||
|
||||
if (getter != null) {
|
||||
cleanUpOverrides(getter);
|
||||
SafeDeletePackage.cleanUpOverrides(getter);
|
||||
}
|
||||
if (setter != null) {
|
||||
cleanUpOverrides(setter);
|
||||
SafeDeletePackage.cleanUpOverrides(setter);
|
||||
}
|
||||
}
|
||||
else if (element instanceof JetTypeParameter) {
|
||||
deleteElementAndCleanParent(element);
|
||||
PsiUtilPackage.deleteElementAndCleanParent(element);
|
||||
}
|
||||
else if (element instanceof JetParameter) {
|
||||
JetPsiUtil.deleteElementWithDelimiters(element);
|
||||
}
|
||||
}
|
||||
|
||||
public static void deleteElementAndCleanParent(@NotNull PsiElement element) {
|
||||
PsiElement parent = element.getParent();
|
||||
JetPsiUtil.deleteElementWithDelimiters(element);
|
||||
JetPsiUtil.deleteChildlessElement(parent, element.getClass());
|
||||
}
|
||||
|
||||
private static boolean checkPsiMethodEquality(@NotNull PsiMethod method1, @NotNull PsiMethod method2) {
|
||||
if (method1 instanceof KotlinLightMethod && method2 instanceof KotlinLightMethod) {
|
||||
return ((KotlinLightMethod) method1).getOrigin().equals(((KotlinLightMethod) method2).getOrigin());
|
||||
}
|
||||
return method1.equals(method2);
|
||||
}
|
||||
|
||||
public static void cleanUpOverrides(@NotNull PsiMethod method) {
|
||||
Collection<PsiMethod> superMethods = Arrays.asList(method.findSuperMethods(true));
|
||||
Collection<PsiMethod> overridingMethods = OverridingMethodsSearch.search(method, true).findAll();
|
||||
overrideLoop:
|
||||
for (PsiMethod overridingMethod : overridingMethods) {
|
||||
PsiElement overridingElement = overridingMethod instanceof KotlinLightMethod
|
||||
? ((KotlinLightMethod) overridingMethod).getOrigin()
|
||||
: overridingMethod;
|
||||
|
||||
Collection<PsiMethod> currentSuperMethods = new ArrayList<PsiMethod>();
|
||||
ContainerUtil.addAll(currentSuperMethods, overridingMethod.findSuperMethods(true));
|
||||
currentSuperMethods.addAll(superMethods);
|
||||
for (PsiMethod superMethod : currentSuperMethods) {
|
||||
if (!checkPsiMethodEquality(superMethod, method)) continue overrideLoop;
|
||||
}
|
||||
|
||||
removeOverrideModifier(overridingElement);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Collection<? extends PsiElement> getElementsToSearch(
|
||||
@NotNull PsiElement element, @Nullable Module module, @NotNull Collection<PsiElement> allElementsToDelete
|
||||
) {
|
||||
if (element instanceof JetParameter) {
|
||||
PsiParameter psiParameter = getPsiParameter((JetParameter) element);
|
||||
if (psiParameter != null) return checkParametersInMethodHierarchy(psiParameter);
|
||||
PsiParameter psiParameter = AsJavaPackage.toPsiParameter((JetParameter) element);
|
||||
if (psiParameter != null) return JetRefactoringUtil.checkParametersInMethodHierarchy(psiParameter);
|
||||
}
|
||||
|
||||
if (element instanceof PsiParameter) {
|
||||
return checkParametersInMethodHierarchy((PsiParameter) element);
|
||||
return JetRefactoringUtil.checkParametersInMethodHierarchy((PsiParameter) element);
|
||||
}
|
||||
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
@@ -532,71 +460,4 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor {
|
||||
|
||||
return super.getElementsToSearch(element, module, allElementsToDelete);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static Collection<? extends PsiElement> checkParametersInMethodHierarchy(@NotNull PsiParameter parameter) {
|
||||
PsiMethod method = (PsiMethod)parameter.getDeclarationScope();
|
||||
int parameterIndex = method.getParameterList().getParameterIndex(parameter);
|
||||
|
||||
Set<PsiElement> parametersToDelete = collectParametersToDelete(method, parameterIndex);
|
||||
if (parametersToDelete.size() > 1) {
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
return parametersToDelete;
|
||||
}
|
||||
|
||||
String message =
|
||||
JetBundle.message("delete.param.in.method.hierarchy", JetRefactoringUtil.formatJavaOrLightMethod(method));
|
||||
int exitCode = Messages.showOkCancelDialog(
|
||||
parameter.getProject(), message, IdeBundle.message("title.warning"), Messages.getQuestionIcon()
|
||||
);
|
||||
if (exitCode == Messages.OK) {
|
||||
return parametersToDelete;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return parametersToDelete;
|
||||
}
|
||||
|
||||
// TODO: generalize breadth-first search
|
||||
@NotNull
|
||||
private static Set<PsiElement> collectParametersToDelete(@NotNull PsiMethod method, int parameterIndex) {
|
||||
Deque<PsiMethod> queue = new ArrayDeque<PsiMethod>();
|
||||
Set<PsiMethod> visited = new HashSet<PsiMethod>();
|
||||
Set<PsiElement> parametersToDelete = new HashSet<PsiElement>();
|
||||
|
||||
queue.add(method);
|
||||
while (!queue.isEmpty()) {
|
||||
PsiMethod currentMethod = queue.poll();
|
||||
|
||||
visited.add(currentMethod);
|
||||
addParameter(currentMethod, parametersToDelete, parameterIndex);
|
||||
|
||||
for (PsiMethod superMethod : currentMethod.findSuperMethods(true)) {
|
||||
if (!visited.contains(superMethod)) {
|
||||
queue.offer(superMethod);
|
||||
}
|
||||
}
|
||||
for (PsiMethod overrider : OverridingMethodsSearch.search(currentMethod)) {
|
||||
if (!visited.contains(overrider)) {
|
||||
queue.offer(overrider);
|
||||
}
|
||||
}
|
||||
}
|
||||
return parametersToDelete;
|
||||
}
|
||||
|
||||
private static void addParameter(@NotNull PsiMethod method, @NotNull Set<PsiElement> result, int parameterIndex) {
|
||||
if (method instanceof KotlinLightMethod) {
|
||||
JetDeclaration declaration = ((KotlinLightMethod) method).getOrigin();
|
||||
if (declaration instanceof JetNamedFunction) {
|
||||
result.add(((JetNamedFunction) declaration).getValueParameters().get(parameterIndex));
|
||||
}
|
||||
}
|
||||
else {
|
||||
result.add(method.getParameterList().getParameters()[parameterIndex]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-3
@@ -16,15 +16,14 @@
|
||||
|
||||
package org.jetbrains.jet.plugin.refactoring.safeDelete
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteReferenceSimpleDeleteUsageInfo
|
||||
import com.intellij.util.IncorrectOperationException
|
||||
import org.jetbrains.jet.lang.psi.*
|
||||
import org.jetbrains.jet.lang.psi.psiUtil.deleteElementAndCleanParent
|
||||
|
||||
public class SafeDeleteTypeArgumentListUsageInfo(
|
||||
typeProjection: JetTypeProjection, parameter: JetTypeParameter
|
||||
) : SafeDeleteReferenceSimpleDeleteUsageInfo(typeProjection, parameter, true) {
|
||||
public override fun deleteElement() {
|
||||
getElement()?.let { element -> KotlinSafeDeleteProcessor.deleteElementAndCleanParent(element) }
|
||||
getElement()?.deleteElementAndCleanParent()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2010-2014 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.refactoring.safeDelete
|
||||
|
||||
import org.jetbrains.jet.lang.psi.JetNamedFunction
|
||||
import com.intellij.psi.PsiMethod
|
||||
import org.jetbrains.jet.lang.psi.JetModifierListOwner
|
||||
import org.jetbrains.jet.lang.psi.JetProperty
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.jet.lexer.JetTokens
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil
|
||||
import com.intellij.psi.search.searches.OverridingMethodsSearch
|
||||
import org.jetbrains.jet.asJava.unwrapped
|
||||
import java.util.ArrayList
|
||||
|
||||
fun PsiElement.removeOverrideModifier() {
|
||||
val modifier = when (this) {
|
||||
is JetNamedFunction, is JetProperty -> {
|
||||
(this as JetModifierListOwner).getModifierList()?.getModifier(JetTokens.OVERRIDE_KEYWORD)
|
||||
}
|
||||
is PsiMethod -> {
|
||||
getModifierList().getAnnotations().find {
|
||||
annotation -> annotation.getQualifiedName() == "java.lang.Override"
|
||||
}
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
modifier?.let { modifier -> modifier.delete() }
|
||||
}
|
||||
|
||||
fun PsiMethod.cleanUpOverrides() {
|
||||
val superMethods = findSuperMethods(true)
|
||||
for (overridingMethod in OverridingMethodsSearch.search(this, true).findAll()) {
|
||||
val currentSuperMethods = overridingMethod.findSuperMethods(true).iterator() + superMethods.iterator()
|
||||
if (currentSuperMethods.all { superMethod -> superMethod.unwrapped == unwrapped }) {
|
||||
overridingMethod.unwrapped?.removeOverrideModifier()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user