IDEA-50625 Safe delete doesn't remove class when its only usage is in an unused import in a groovy file

This commit is contained in:
Maxim.Medvedev
2012-04-30 17:13:39 +04:00
parent ac2acaeaa7
commit 5f5a4e5428
7 changed files with 141 additions and 10 deletions
@@ -0,0 +1,43 @@
/*
* Copyright 2000-2012 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 com.intellij.refactoring.safeDelete;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.Nullable;
/**
* @author Max Medvedev
*/
public abstract class ImportSearcher {
private static final ExtensionPointName<ImportSearcher> EP_NAME = ExtensionPointName.create("com.intellij.safeDelete.importChecker");
/**
* @return found import or null
*/
@Nullable
public abstract PsiElement findImport(PsiElement element);
@Nullable
public static PsiElement getImport(PsiElement element) {
for (ImportSearcher searcher : EP_NAME.getExtensions()) {
PsiElement anImport = searcher.findImport(element);
if (anImport != null) return anImport;
}
return null;
}
}
@@ -0,0 +1,36 @@
/*
* Copyright 2000-2012 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 com.intellij.refactoring.safeDelete;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiImportList;
import com.intellij.psi.PsiJavaFile;
import com.intellij.psi.util.PsiTreeUtil;
/**
* @author Max Medvedev
*/
public class JavaImportSearcher extends ImportSearcher {
@Override
public PsiElement findImport(PsiElement element) {
final PsiFile containingFile = element.getContainingFile();
if (containingFile instanceof PsiJavaFile) {
return PsiTreeUtil.getParentOfType(element, PsiImportList.class, true);
}
return null;
}
}
@@ -329,13 +329,17 @@ public class JavaSafeDeleteProcessor extends SafeDeleteProcessorDelegateBase {
}
}
LOG.assertTrue(element.getTextRange() != null);
usages.add(new SafeDeleteReferenceJavaDeleteUsageInfo(element, psiClass, parent instanceof PsiImportStatement));
usages.add(new SafeDeleteReferenceJavaDeleteUsageInfo(element, psiClass, isInImport(element)));
}
return true;
}
});
}
private static boolean isInImport(PsiElement element) {
return ImportSearcher.getImport(element) != null;
}
private static boolean containsOnlyPrivates(final PsiClass aClass) {
final PsiField[] fields = aClass.getFields();
for (PsiField field : fields) {
@@ -15,11 +15,10 @@
*/
package com.intellij.refactoring.safeDelete.usageInfo;
import com.intellij.util.IncorrectOperationException;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiImportStatementBase;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiElement;
import com.intellij.refactoring.safeDelete.ImportSearcher;
import com.intellij.util.IncorrectOperationException;
/**
* @author yole
@@ -31,19 +30,26 @@ public class SafeDeleteReferenceJavaDeleteUsageInfo extends SafeDeleteReferenceS
super(element, referencedElement, isSafeDelete);
}
public SafeDeleteReferenceJavaDeleteUsageInfo(final PsiElement element, final PsiElement referencedElement, final int startOffset, final int endOffset,
public SafeDeleteReferenceJavaDeleteUsageInfo(final PsiElement element,
final PsiElement referencedElement,
final int startOffset,
final int endOffset,
final boolean isNonCodeUsage,
final boolean isSafeDelete) {
super(element, referencedElement, startOffset, endOffset, isNonCodeUsage, isSafeDelete);
}
public void deleteElement() throws IncorrectOperationException {
if(isSafeDelete()) {
if (isSafeDelete()) {
PsiElement element = getElement();
LOG.assertTrue(element != null);
PsiImportStatementBase importStatement = PsiTreeUtil.getParentOfType(element, PsiImportStatementBase.class);
if (importStatement != null) importStatement.delete();
else element.delete();
PsiElement importStatement = ImportSearcher.getImport(element);
if (importStatement != null) {
importStatement.delete();
}
else {
element.delete();
}
}
}
}