diff --git a/java/java-impl/src/com/intellij/refactoring/safeDelete/ImportSearcher.java b/java/java-impl/src/com/intellij/refactoring/safeDelete/ImportSearcher.java new file mode 100644 index 000000000000..9ebd0f296a34 --- /dev/null +++ b/java/java-impl/src/com/intellij/refactoring/safeDelete/ImportSearcher.java @@ -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 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; + } +} diff --git a/java/java-impl/src/com/intellij/refactoring/safeDelete/JavaImportSearcher.java b/java/java-impl/src/com/intellij/refactoring/safeDelete/JavaImportSearcher.java new file mode 100644 index 000000000000..a31285b6f49d --- /dev/null +++ b/java/java-impl/src/com/intellij/refactoring/safeDelete/JavaImportSearcher.java @@ -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; + } +} diff --git a/java/java-impl/src/com/intellij/refactoring/safeDelete/JavaSafeDeleteProcessor.java b/java/java-impl/src/com/intellij/refactoring/safeDelete/JavaSafeDeleteProcessor.java index b60edb36e71f..eefad2425412 100644 --- a/java/java-impl/src/com/intellij/refactoring/safeDelete/JavaSafeDeleteProcessor.java +++ b/java/java-impl/src/com/intellij/refactoring/safeDelete/JavaSafeDeleteProcessor.java @@ -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) { diff --git a/java/java-impl/src/com/intellij/refactoring/safeDelete/usageInfo/SafeDeleteReferenceJavaDeleteUsageInfo.java b/java/java-impl/src/com/intellij/refactoring/safeDelete/usageInfo/SafeDeleteReferenceJavaDeleteUsageInfo.java index 8d1b485bcb6f..9a9b8edea969 100644 --- a/java/java-impl/src/com/intellij/refactoring/safeDelete/usageInfo/SafeDeleteReferenceJavaDeleteUsageInfo.java +++ b/java/java-impl/src/com/intellij/refactoring/safeDelete/usageInfo/SafeDeleteReferenceJavaDeleteUsageInfo.java @@ -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(); + } } } } diff --git a/plugins/groovy/src/META-INF/plugin.xml b/plugins/groovy/src/META-INF/plugin.xml index d9734d6b6c8d..77a2141ed065 100644 --- a/plugins/groovy/src/META-INF/plugin.xml +++ b/plugins/groovy/src/META-INF/plugin.xml @@ -137,6 +137,7 @@ + diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/safeDelete/GroovyImportSearcher.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/safeDelete/GroovyImportSearcher.java new file mode 100644 index 000000000000..b82f1d5bb446 --- /dev/null +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/safeDelete/GroovyImportSearcher.java @@ -0,0 +1,37 @@ +/* + * 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 org.jetbrains.plugins.groovy.refactoring.safeDelete; + +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.refactoring.safeDelete.ImportSearcher; +import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; +import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement; + +/** + * @author Max Medvedev + */ +public class GroovyImportSearcher extends ImportSearcher { + @Override + public PsiElement findImport(PsiElement element) { + PsiFile file = element.getContainingFile(); + if (file instanceof GroovyFile) { + return PsiTreeUtil.getParentOfType(element, GrImportStatement.class); + } + return null; + } +} diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index d13db957a80c..6b707648b41d 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -78,6 +78,9 @@ + + @@ -1017,6 +1020,7 @@ +