Search in sources :

Example 1 with RenameJavaVariableProcessor

use of com.intellij.refactoring.rename.RenameJavaVariableProcessor in project android by JetBrains.

the class AndroidResourceRenameResourceProcessor method prepareResourceFieldRenaming.

private static void prepareResourceFieldRenaming(PsiField field, String newName, Map<PsiElement, String> allRenames) {
    new RenameJavaVariableProcessor().prepareRenaming(field, newName, allRenames);
    List<PsiElement> resources = AndroidResourceUtil.findResourcesByField(field);
    PsiElement res = resources.get(0);
    final String newResName;
    if (res instanceof PsiFile) {
        // Resource comes from XML file, don't need to suggest change underscores to dots
        newResName = newName;
    } else if (res instanceof XmlAttributeValue) {
        newResName = getResourceName(field.getProject(), newName, ((XmlAttributeValue) res).getValue());
    } else {
        // AndroidResourceUtil.findResourcesByField supposed to return a list of PsiElements that are
        // either PsiFile or XmlAttributeValue. Previous version of this code doesn't handle other
        // possibilities at all and would crash with ClassCastException, having an explicit error message
        // seems to be a slightly better option.
        Logger.getInstance(AndroidResourceRenameResourceProcessor.class).error(String.format("%s: res is neither PsiFile nor XmlAttributeValue", AndroidResourceRenameResourceProcessor.class.getSimpleName()));
        newResName = newName;
    }
    for (PsiElement resource : resources) {
        if (resource instanceof PsiFile) {
            PsiFile file = (PsiFile) resource;
            String extension = FileUtilRt.getExtension(file.getName());
            allRenames.put(resource, newResName + '.' + extension);
        } else if (resource instanceof XmlAttributeValue) {
            XmlAttributeValue value = (XmlAttributeValue) resource;
            final String s = AndroidResourceUtil.isIdDeclaration(value) ? NEW_ID_PREFIX + newResName : newResName;
            allRenames.put(new ValueResourceElementWrapper(value), s);
            // we have to rename not just R.styleable.Foo but the also R.styleable.Foo_* attributes
            if (value.getParent() instanceof XmlAttribute) {
                XmlAttribute parent = (XmlAttribute) value.getParent();
                XmlTag tag = parent.getParent();
                if (tag.getName().equals(TAG_DECLARE_STYLEABLE)) {
                    AndroidFacet facet = AndroidFacet.getInstance(tag);
                    String oldName = tag.getAttributeValue(ATTR_NAME);
                    if (facet != null && oldName != null) {
                        for (XmlTag attr : tag.getSubTags()) {
                            if (attr.getName().equals(TAG_ATTR)) {
                                String name = attr.getAttributeValue(ATTR_NAME);
                                if (name != null) {
                                    String oldAttributeName = oldName + '_' + name;
                                    PsiField[] fields = AndroidResourceUtil.findResourceFields(facet, STYLEABLE.getName(), oldAttributeName, true);
                                    if (fields.length > 0) {
                                        String newAttributeName = newName + '_' + name;
                                        for (PsiField f : fields) {
                                            allRenames.put(f, newAttributeName);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : RenameJavaVariableProcessor(com.intellij.refactoring.rename.RenameJavaVariableProcessor) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) LazyValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper) ValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper) XmlTag(com.intellij.psi.xml.XmlTag)

Example 2 with RenameJavaVariableProcessor

use of com.intellij.refactoring.rename.RenameJavaVariableProcessor in project android by JetBrains.

the class AndroidResourceRenameResourceProcessor method renameElement.

@Override
public void renameElement(PsiElement element, final String newName, UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
    if (element instanceof PsiField) {
        new RenameJavaVariableProcessor().renameElement(element, newName, usages, listener);
    } else {
        if (element instanceof PsiNamedElement) {
            super.renameElement(element, newName, usages, listener);
            if (element instanceof PsiFile) {
                VirtualFile virtualFile = ((PsiFile) element).getVirtualFile();
                if (virtualFile != null && !LocalHistory.getInstance().isUnderControl(virtualFile)) {
                    DocumentReference ref = DocumentReferenceManager.getInstance().create(virtualFile);
                    UndoManager.getInstance(element.getProject()).nonundoableActionPerformed(ref, false);
                }
            }
        } else if (element instanceof XmlAttributeValue) {
            new RenameXmlAttributeProcessor().renameElement(element, newName, usages, listener);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) RenameJavaVariableProcessor(com.intellij.refactoring.rename.RenameJavaVariableProcessor) RenameXmlAttributeProcessor(com.intellij.refactoring.rename.RenameXmlAttributeProcessor) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) DocumentReference(com.intellij.openapi.command.undo.DocumentReference)

Aggregations

XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)2 RenameJavaVariableProcessor (com.intellij.refactoring.rename.RenameJavaVariableProcessor)2 DocumentReference (com.intellij.openapi.command.undo.DocumentReference)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 XmlTag (com.intellij.psi.xml.XmlTag)1 RenameXmlAttributeProcessor (com.intellij.refactoring.rename.RenameXmlAttributeProcessor)1 LazyValueResourceElementWrapper (org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper)1 ValueResourceElementWrapper (org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1