Search in sources :

Example 6 with ResourceManager

use of org.jetbrains.android.resourceManagers.ResourceManager in project android by JetBrains.

the class AndroidGotoDeclarationHandler method getGotoDeclarationTargets.

@Override
public PsiElement[] getGotoDeclarationTargets(@Nullable PsiElement sourceElement, int offset, Editor editor) {
    if (!(sourceElement instanceof PsiIdentifier)) {
        return null;
    }
    final PsiFile file = sourceElement.getContainingFile();
    if (file == null) {
        return null;
    }
    AndroidFacet facet = AndroidFacet.getInstance(file);
    if (facet == null) {
        return null;
    }
    final PsiReferenceExpression refExp = PsiTreeUtil.getParentOfType(sourceElement, PsiReferenceExpression.class);
    if (refExp == null) {
        return null;
    }
    AndroidResourceUtil.MyReferredResourceFieldInfo info = AndroidResourceUtil.getReferredResourceOrManifestField(facet, refExp, false);
    if (info == null) {
        PsiElement parent = refExp.getParent();
        if (parent instanceof PsiReferenceExpression) {
            info = AndroidResourceUtil.getReferredResourceOrManifestField(facet, (PsiReferenceExpression) parent, false);
        }
        if (info == null) {
            parent = parent.getParent();
            if (parent instanceof PsiReferenceExpression) {
                info = AndroidResourceUtil.getReferredResourceOrManifestField(facet, (PsiReferenceExpression) parent, false);
            }
        }
    }
    if (info == null) {
        return null;
    }
    final String nestedClassName = info.getClassName();
    final String fieldName = info.getFieldName();
    final List<PsiElement> resourceList = new ArrayList<PsiElement>();
    if (info.isFromManifest()) {
        collectManifestElements(nestedClassName, fieldName, facet, resourceList);
    } else {
        final ResourceManager manager = info.isSystem() ? facet.getSystemResourceManager(false) : facet.getLocalResourceManager();
        if (manager == null) {
            return null;
        }
        manager.collectLazyResourceElements(nestedClassName, fieldName, false, refExp, resourceList);
        if (manager instanceof LocalResourceManager) {
            final LocalResourceManager lrm = (LocalResourceManager) manager;
            if (nestedClassName.equals(ResourceType.ATTR.getName())) {
                for (Attr attr : lrm.findAttrs(fieldName)) {
                    resourceList.add(attr.getName().getXmlAttributeValue());
                }
            } else if (nestedClassName.equals(ResourceType.STYLEABLE.getName())) {
                for (DeclareStyleable styleable : lrm.findStyleables(fieldName)) {
                    resourceList.add(styleable.getName().getXmlAttributeValue());
                }
                for (Attr styleable : lrm.findStyleableAttributesByFieldName(fieldName)) {
                    resourceList.add(styleable.getName().getXmlAttributeValue());
                }
            }
        }
    }
    if (resourceList.size() > 1) {
        // Sort to ensure the output is stable, and to prefer the base folders
        Collections.sort(resourceList, AndroidResourceUtil.RESOURCE_ELEMENT_COMPARATOR);
    }
    return resourceList.toArray(new PsiElement[resourceList.size()]);
}
Also used : LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) ArrayList(java.util.ArrayList) ResourceManager(org.jetbrains.android.resourceManagers.ResourceManager) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Attr(org.jetbrains.android.dom.resources.Attr) DeclareStyleable(org.jetbrains.android.dom.resources.DeclareStyleable) AndroidResourceUtil(org.jetbrains.android.util.AndroidResourceUtil)

Example 7 with ResourceManager

use of org.jetbrains.android.resourceManagers.ResourceManager in project android by JetBrains.

the class AndroidCompletionContributor method isFrameworkAttributeDeprecated.

private static boolean isFrameworkAttributeDeprecated(AndroidFacet facet, XmlAttribute attribute, String attributeName) {
    final ResourceManager manager = facet.getResourceManager(AndroidUtils.SYSTEM_RESOURCE_PACKAGE, attribute.getParent());
    if (manager == null) {
        return false;
    }
    final AttributeDefinitions attributes = manager.getAttributeDefinitions();
    if (attributes == null) {
        return false;
    }
    final AttributeDefinition attributeDefinition = attributes.getAttrDefByName(attributeName);
    return attributeDefinition != null && attributeDefinition.isAttributeDeprecated();
}
Also used : AttributeDefinitions(org.jetbrains.android.dom.attrs.AttributeDefinitions) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) ResourceManager(org.jetbrains.android.resourceManagers.ResourceManager)

Example 8 with ResourceManager

use of org.jetbrains.android.resourceManagers.ResourceManager in project android by JetBrains.

the class AndroidResourceRenameResourceProcessor method prepareResourceFileRenaming.

private static void prepareResourceFileRenaming(PsiFile file, String newName, Map<PsiElement, String> allRenames, AndroidFacet facet) {
    Project project = file.getProject();
    ResourceManager manager = facet.getLocalResourceManager();
    String type = manager.getFileResourceType(file);
    if (type == null)
        return;
    String name = file.getName();
    if (AndroidCommonUtils.getResourceName(type, name).equals(AndroidCommonUtils.getResourceName(type, newName))) {
        return;
    }
    List<PsiFile> resourceFiles = manager.findResourceFiles(type, AndroidCommonUtils.getResourceName(type, name), true, false);
    List<PsiFile> alternativeResources = new ArrayList<PsiFile>();
    for (PsiFile resourceFile : resourceFiles) {
        if (!resourceFile.getManager().areElementsEquivalent(file, resourceFile) && resourceFile.getName().equals(name)) {
            alternativeResources.add(resourceFile);
        }
    }
    if (alternativeResources.size() > 0) {
        int r = 0;
        if (ASK) {
            r = Messages.showDialog(project, message("rename.alternate.resources.question"), message("rename.dialog.title"), new String[] { Messages.YES_BUTTON, Messages.NO_BUTTON }, 1, Messages.getQuestionIcon());
        }
        if (r == 0) {
            for (PsiFile candidate : alternativeResources) {
                allRenames.put(candidate, newName);
            }
        } else {
            return;
        }
    }
    PsiField[] resFields = AndroidResourceUtil.findResourceFieldsForFileResource(file, false);
    for (PsiField resField : resFields) {
        String newFieldName = AndroidCommonUtils.getResourceName(type, newName);
        allRenames.put(resField, AndroidResourceUtil.getFieldNameByResourceName(newFieldName));
    }
}
Also used : Project(com.intellij.openapi.project.Project) ResourceManager(org.jetbrains.android.resourceManagers.ResourceManager) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager)

Example 9 with ResourceManager

use of org.jetbrains.android.resourceManagers.ResourceManager in project android by JetBrains.

the class AndroidResourceRenameResourceProcessor method prepareValueResourceRenaming.

private static void prepareValueResourceRenaming(PsiElement element, String newName, Map<PsiElement, String> allRenames, final AndroidFacet facet) {
    ResourceManager manager = facet.getLocalResourceManager();
    XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
    assert tag != null;
    String type = manager.getValueResourceType(tag);
    assert type != null;
    Project project = tag.getProject();
    DomElement domElement = DomManager.getDomManager(project).getDomElement(tag);
    assert domElement instanceof ResourceElement;
    String name = ((ResourceElement) domElement).getName().getValue();
    assert name != null;
    List<ResourceElement> resources = manager.findValueResources(type, name);
    for (ResourceElement resource : resources) {
        XmlElement xmlElement = resource.getName().getXmlAttributeValue();
        if (!element.getManager().areElementsEquivalent(element, xmlElement)) {
            allRenames.put(xmlElement, newName);
        }
    }
    if (getResourceType(element) == ResourceType.STYLE) {
        // For styles, try also to find child styles defined by name (i.e. "ParentName.StyleName") and add them
        // to the rename list. This will allow the rename processor to also handle the references to those. For example,
        // If you rename "MyTheme" and your manifest theme is "MyTheme.NoActionBar", this will make sure that
        // the reference from the manifest is also updated by adding "MyTheme.NoActionBar" to the rename list.
        // We iterate the styles in order to cascade any changes to children down the hierarchy.
        // List of styles that will be renamed.
        HashSet<String> renamedStyles = Sets.newHashSet();
        renamedStyles.add(name);
        final String stylePrefix = name + ".";
        Collection<String> renameCandidates;
        ResourceType resourceType = ResourceType.getEnum(type);
        if (resourceType == null) {
            renameCandidates = Collections.emptyList();
        } else {
            renameCandidates = Collections2.filter(manager.getResourceNames(resourceType), new Predicate<String>() {

                @Override
                public boolean apply(String input) {
                    return input.startsWith(stylePrefix);
                }
            });
        }
        for (String resourceName : ORDER_BY_LENGTH.sortedCopy(renameCandidates)) {
            // resourceName.lastIndexOf will never return -1 because we've filtered all names that
            // do not contain stylePrefix
            String parentName = resourceName.substring(0, resourceName.lastIndexOf('.'));
            if (!renamedStyles.contains(parentName)) {
                // This resource's parent wasn't affected by the rename
                continue;
            }
            for (ResourceElement resource : manager.findValueResources(type, resourceName)) {
                if (!(resource instanceof Style) || ((Style) resource).getParentStyle().getXmlAttributeValue() != null) {
                    // This element is not a style or does have an explicit parent so we do not rename it.
                    continue;
                }
                XmlAttributeValue xmlElement = resource.getName().getXmlAttributeValue();
                if (xmlElement != null) {
                    String newStyleName = newName + StringUtil.trimStart(resourceName, name);
                    allRenames.put(new ValueResourceElementWrapper(xmlElement), newStyleName);
                    renamedStyles.add(resourceName);
                }
            }
        }
    }
    PsiField[] resFields = AndroidResourceUtil.findResourceFieldsForValueResource(tag, false);
    for (PsiField resField : resFields) {
        String escaped = AndroidResourceUtil.getFieldNameByResourceName(newName);
        allRenames.put(resField, escaped);
    }
    // Also rename the dependent fields, e.g. if you rename <declare-styleable name="Foo">,
    // we have to rename not just R.styleable.Foo but the also R.styleable.Foo_* attributes
    PsiField[] styleableFields = AndroidResourceUtil.findStyleableAttributeFields(tag, false);
    if (styleableFields.length > 0) {
        String tagName = tag.getName();
        boolean isDeclareStyleable = tagName.equals(TAG_DECLARE_STYLEABLE);
        boolean isAttr = !isDeclareStyleable && tagName.equals(TAG_ATTR) && tag.getParentTag() != null;
        assert isDeclareStyleable || isAttr;
        String style = isAttr ? tag.getParentTag().getAttributeValue(ATTR_NAME) : null;
        for (PsiField resField : styleableFields) {
            String fieldName = resField.getName();
            String newAttributeName;
            if (isDeclareStyleable && fieldName.startsWith(name)) {
                newAttributeName = newName + fieldName.substring(name.length());
            } else if (isAttr && style != null) {
                newAttributeName = style + '_' + newName;
            } else {
                newAttributeName = name;
            }
            String escaped = AndroidResourceUtil.getFieldNameByResourceName(newAttributeName);
            allRenames.put(resField, escaped);
        }
    }
}
Also used : ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) ResourceType(com.android.resources.ResourceType) ResourceManager(org.jetbrains.android.resourceManagers.ResourceManager) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Predicate(com.google.common.base.Predicate) Project(com.intellij.openapi.project.Project) DomElement(com.intellij.util.xml.DomElement) XmlElement(com.intellij.psi.xml.XmlElement) Style(org.jetbrains.android.dom.resources.Style) XmlTag(com.intellij.psi.xml.XmlTag) LazyValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper) ValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper)

Example 10 with ResourceManager

use of org.jetbrains.android.resourceManagers.ResourceManager in project android by JetBrains.

the class AndroidResourceReferenceBase method collectTargets.

private void collectTargets(AndroidFacet facet, ResourceValue resValue, List<PsiElement> elements, boolean attrReference) {
    ResourceType resType = resValue.getType();
    if (resType == null) {
        return;
    }
    ResourceManager manager = facet.getResourceManager(resValue.getNamespace(), myElement);
    if (manager != null) {
        String resName = resValue.getResourceName();
        if (resName != null) {
            manager.collectLazyResourceElements(resType.getName(), resName, attrReference, myElement, elements);
        }
    }
}
Also used : ResourceType(com.android.resources.ResourceType) ResourceManager(org.jetbrains.android.resourceManagers.ResourceManager)

Aggregations

ResourceManager (org.jetbrains.android.resourceManagers.ResourceManager)14 AttributeDefinitions (org.jetbrains.android.dom.attrs.AttributeDefinitions)5 LocalResourceManager (org.jetbrains.android.resourceManagers.LocalResourceManager)4 XmlTag (com.intellij.psi.xml.XmlTag)3 AttributeDefinition (org.jetbrains.android.dom.attrs.AttributeDefinition)3 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)3 SystemResourceManager (org.jetbrains.android.resourceManagers.SystemResourceManager)3 ResourceType (com.android.resources.ResourceType)2 Project (com.intellij.openapi.project.Project)2 ArrayList (java.util.ArrayList)2 Attr (org.jetbrains.android.dom.resources.Attr)2 DeclareStyleable (org.jetbrains.android.dom.resources.DeclareStyleable)2 AndroidResourceUtil (org.jetbrains.android.util.AndroidResourceUtil)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)1 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)1 ResourceItem (com.android.ide.common.res2.ResourceItem)1 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)1 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)1