IDEA-90358 ask for delete alternative file resources

This commit is contained in:
Eugene Kudelevsky
2012-10-08 22:38:11 +04:00
parent 4f3e082663
commit eb03dd7ef2
4 changed files with 160 additions and 6 deletions
+5 -2
View File
@@ -306,8 +306,9 @@
<sdkType implementation="org.jetbrains.android.sdk.AndroidSdkType"/>
<gotoDeclarationHandler implementation="org.jetbrains.android.AndroidGotoDeclarationHandler"/>
<refactoring.safeDeleteProcessor id="android" order="before javaProcessor"
implementation="org.jetbrains.android.AndroidSafeDeleteProcessorDelegate"/>
<refactoring.safeDeleteProcessor id="android_component" order="before javaProcessor"
implementation="org.jetbrains.android.AndroidComponentSafeDeleteProcessor"/>
<refactoring.safeDeleteProcessor id="android_resource_file" implementation="org.jetbrains.android.AndroidResourceFileSafeDeleteProcessor"/>
<externalAnnotator language="JAVA" implementationClass="org.jetbrains.android.inspections.lint.AndroidLintExternalAnnotator"/>
<externalAnnotator language="XML" implementationClass="org.jetbrains.android.inspections.lint.AndroidLintExternalAnnotator"/>
@@ -331,6 +332,8 @@
<inlineActionHandler implementation="org.jetbrains.android.refactoring.AndroidInlineStyleHandler"/>
<inlineActionHandler implementation="org.jetbrains.android.refactoring.AndroidInlineLayoutHandler" order="first"/>
<!--<treeStructureProvider implementation="org.jetbrains.android.AndroidTreeStructureProvider"/>-->
</extensions>
<extensionPoints>
@@ -29,7 +29,7 @@ import java.util.List;
/**
* @author Eugene.Kudelevsky
*/
public class AndroidSafeDeleteProcessorDelegate extends SafeDeleteProcessorDelegateBase {
public class AndroidComponentSafeDeleteProcessor extends SafeDeleteProcessorDelegateBase {
private final JavaSafeDeleteProcessor myBaseHandler = new JavaSafeDeleteProcessor();
@@ -0,0 +1,134 @@
package org.jetbrains.android;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.refactoring.RefactoringSettings;
import com.intellij.refactoring.safeDelete.NonCodeUsageSearchInfo;
import com.intellij.refactoring.safeDelete.SafeDeleteProcessor;
import com.intellij.refactoring.safeDelete.SafeDeleteProcessorDelegateBase;
import com.intellij.usageView.UsageInfo;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.android.facet.AndroidFacet;
import org.jetbrains.android.util.AndroidCommonUtils;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* @author Eugene.Kudelevsky
*/
public class AndroidResourceFileSafeDeleteProcessor extends SafeDeleteProcessorDelegateBase {
@Nullable
@Override
public Collection<? extends PsiElement> getElementsToSearch(PsiElement element,
@Nullable Module module,
Collection<PsiElement> allElementsToDelete) {
return Collections.emptyList();
}
@Override
public boolean handlesElement(PsiElement element) {
if (!(element instanceof PsiFile)) {
return false;
}
final AndroidFacet facet = AndroidFacet.getInstance(element);
if (facet == null) {
return false;
}
final VirtualFile vFile = ((PsiFile)element).getVirtualFile();
final VirtualFile parent = vFile != null ? vFile.getParent() : null;
final VirtualFile resDir = parent != null ? parent.getParent() : null;
return resDir != null && facet.getLocalResourceManager().isResourceDir(resDir);
}
@Nullable
@Override
public NonCodeUsageSearchInfo findUsages(PsiElement element, PsiElement[] allElementsToDelete, List<UsageInfo> result) {
SafeDeleteProcessor.findGenericElementUsages(element, result, allElementsToDelete);
return new NonCodeUsageSearchInfo(SafeDeleteProcessor.getDefaultInsideDeletedCondition(allElementsToDelete), element);
}
@Nullable
@Override
public Collection<PsiElement> getAdditionalElementsToDelete(PsiElement element,
Collection<PsiElement> allElementsToDelete,
boolean askUser) {
if (askUser) {
final int r = Messages
.showYesNoDialog(element.getProject(), "Delete alternative resource files for other configurations?",
"Delete", Messages.getQuestionIcon());
if (r != Messages.YES) {
return Collections.emptyList();
}
}
final AndroidFacet facet = AndroidFacet.getInstance(element);
assert facet != null;
final PsiFile file = (PsiFile)element;
final VirtualFile vFile = file.getVirtualFile();
assert vFile != null;
final VirtualFile dir = vFile.getParent();
assert dir != null;
final String type = AndroidCommonUtils.getResourceTypeByDirName(dir.getName());
if (type == null) {
return Collections.emptyList();
}
final String name = vFile.getName();
final List<PsiFile> resourceFiles = facet.getLocalResourceManager().findResourceFiles(
type, AndroidCommonUtils.getResourceName(type, name), true, false);
final List<PsiElement> result = new ArrayList<PsiElement>();
for (PsiFile resourceFile : resourceFiles) {
if (!resourceFile.getManager().areElementsEquivalent(file, resourceFile) &&
resourceFile.getName().equals(name)) {
result.add(resourceFile);
}
}
return result;
}
@Nullable
@Override
public Collection<String> findConflicts(PsiElement element, PsiElement[] allElementsToDelete) {
return null;
}
@Nullable
@Override
public UsageInfo[] preprocessUsages(Project project, UsageInfo[] usages) {
return usages;
}
@Override
public void prepareForDeletion(PsiElement element) throws IncorrectOperationException {
}
@Override
public boolean isToSearchInComments(PsiElement element) {
return RefactoringSettings.getInstance().SAFE_DELETE_SEARCH_IN_COMMENTS;
}
@Override
public boolean isToSearchForTextOccurrences(PsiElement element) {
return RefactoringSettings.getInstance().SAFE_DELETE_SEARCH_IN_NON_JAVA;
}
@Override
public void setToSearchInComments(PsiElement element, boolean enabled) {
RefactoringSettings.getInstance().SAFE_DELETE_SEARCH_IN_COMMENTS = enabled;
}
@Override
public void setToSearchForTextOccurrences(PsiElement element, boolean enabled) {
RefactoringSettings.getInstance().SAFE_DELETE_SEARCH_IN_NON_JAVA = enabled;
}
}
@@ -79,8 +79,16 @@ public abstract class ResourceManager {
return processFileResources(resourceType, processor, true);
}
public boolean processFileResources(@Nullable String resourceType, @NotNull FileResourceProcessor processor, boolean publicOnly) {
for (VirtualFile resSubdir : getResourceSubdirs(resourceType)) {
public boolean processFileResources(@Nullable String resourceType, @NotNull FileResourceProcessor processor,
boolean withDependencies) {
return processFileResources(resourceType, processor, withDependencies, true);
}
public boolean processFileResources(@Nullable String resourceType, @NotNull FileResourceProcessor processor,
boolean withDependencies, boolean publicOnly) {
final VirtualFile[] resDirs = withDependencies ? getAllResourceDirs() : new VirtualFile[]{getResourceDir()};
for (VirtualFile resSubdir : AndroidResourceUtil.getResourceSubdirs(resourceType, resDirs)) {
final String resType = AndroidCommonUtils.getResourceTypeByDirName(resSubdir.getName());
if (resType != null) {
@@ -117,10 +125,19 @@ public abstract class ResourceManager {
return AndroidResourceUtil.getResourceSubdirs(resourceType, getAllResourceDirs());
}
@NotNull
public List<PsiFile> findResourceFiles(@NotNull final String resType,
@Nullable final String resName,
final boolean distinguishDelimetersInName,
@NotNull String... extensions) {
return findResourceFiles(resType, resName, distinguishDelimetersInName, true, extensions);
}
@NotNull
public List<PsiFile> findResourceFiles(@NotNull final String resType1,
@Nullable final String resName1,
final boolean distinguishDelimetersInName,
final boolean withDependencies,
@NotNull final String... extensions) {
final List<PsiFile> result = new ArrayList<PsiFile>();
final Set<String> extensionSet = new HashSet<String>();
@@ -145,7 +162,7 @@ public abstract class ResourceManager {
}
return true;
}
});
}, withDependencies);
return result;
}