Search in sources :

Example 56 with ResourceItem

use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.

the class AndroidResourceReferenceBase method resolveInner.

@NotNull
private ResolveResult[] resolveInner() {
    final List<PsiElement> elements = new ArrayList<>();
    final boolean attrReference = myResourceValue.getPrefix() == '?';
    collectTargets(myFacet, myResourceValue, elements, attrReference);
    final List<ResolveResult> result = new ArrayList<>();
    if (elements.isEmpty() && myResourceValue.getResourceName() != null && !AndroidUtils.SYSTEM_RESOURCE_PACKAGE.equals(myResourceValue.getNamespace())) {
        // Dynamic items do not appear in the XML scanning file index; look for
        // these in the resource repositories.
        LocalResourceRepository resources = AppResourceRepository.getAppResources(myFacet.getModule(), true);
        ResourceType resourceType = myResourceValue.getType();
        if (resourceType != null && (resourceType != ResourceType.ATTR || attrReference)) {
            // If not, it could be some broken source, such as @android/test
            assert resources != null;
            List<ResourceItem> items = resources.getResourceItem(resourceType, myResourceValue.getResourceName());
            if (items != null && FolderTypeRelationship.getRelatedFolders(resourceType).contains(ResourceFolderType.VALUES)) {
                for (ResourceItem item : items) {
                    XmlTag tag = LocalResourceRepository.getItemTag(myFacet.getModule().getProject(), item);
                    if (tag != null) {
                        elements.add(tag);
                    } else if (item instanceof DynamicResourceValueItem) {
                        result.add(((DynamicResourceValueItem) item).createResolveResult());
                    }
                }
            }
        }
    }
    if (elements.size() > 1) {
        Collections.sort(elements, AndroidResourceUtil.RESOURCE_ELEMENT_COMPARATOR);
    }
    for (PsiElement target : elements) {
        result.add(new PsiElementResolveResult(target));
    }
    return result.toArray(new ResolveResult[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) DynamicResourceValueItem(com.android.tools.idea.res.DynamicResourceValueItem) ResourceType(com.android.resources.ResourceType) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ResourceItem(com.android.ide.common.res2.ResourceItem) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 57 with ResourceItem

use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.

the class StyleItemNameConverter method getVariants.

@NotNull
@Override
public Collection<String> getVariants(ConvertContext context) {
    List<String> result = Lists.newArrayList();
    if (context.getModule() != null && context.getTag() != null) {
        // Try to find the parents of the styles where this item is defined and add to the suggestion every non-framework attribute that has been used.
        // This is helpful in themes like AppCompat where there is not only a framework attribute defined but also a custom attribute. This
        // will show both in the completion list.
        AppResourceRepository appResourceRepository = AppResourceRepository.getAppResources(context.getModule(), true);
        XmlTag styleTag = context.getTag().getParentTag();
        String parent = getParentNameFromTag(styleTag);
        List<ResourceItem> parentDefinitions = parent != null && appResourceRepository != null ? appResourceRepository.getResourceItem(ResourceType.STYLE, parent) : null;
        if (parentDefinitions != null && !parentDefinitions.isEmpty()) {
            HashSet<String> attributeNames = Sets.newHashSet();
            LinkedList<ResourceItem> toExplore = Lists.newLinkedList(parentDefinitions);
            int i = 0;
            while (!toExplore.isEmpty() && i++ < ResourceResolver.MAX_RESOURCE_INDIRECTION) {
                ResourceItem parentItem = toExplore.pop();
                StyleResourceValue parentValue = (StyleResourceValue) parentItem.getResourceValue(false);
                if (parentValue == null || parentValue.isFramework()) {
                    // No parent or the parent is a framework style
                    continue;
                }
                for (ItemResourceValue value : parentValue.getValues()) {
                    if (!value.isFramework()) {
                        attributeNames.add(value.getName());
                    }
                }
                List<ResourceItem> parents = appResourceRepository.getResourceItem(ResourceType.STYLE, parentValue.getParentStyle());
                if (parents != null) {
                    toExplore.addAll(parents);
                }
            }
            result.addAll(attributeNames);
        }
    }
    ResourceManager manager = SystemResourceManager.getInstance(context);
    if (manager != null) {
        AttributeDefinitions attrDefs = manager.getAttributeDefinitions();
        if (attrDefs != null) {
            for (String name : attrDefs.getAttributeNames()) {
                result.add(SdkConstants.PREFIX_ANDROID + name);
            }
        }
    }
    return result;
}
Also used : AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) SystemResourceManager(org.jetbrains.android.resourceManagers.SystemResourceManager) ResourceManager(org.jetbrains.android.resourceManagers.ResourceManager) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) AttributeDefinitions(org.jetbrains.android.dom.attrs.AttributeDefinitions) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ResourceItem(com.android.ide.common.res2.ResourceItem) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 58 with ResourceItem

use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.

the class LocalResourceRepository method doMerge.

protected void doMerge(@NotNull Set<LocalResourceRepository> visited, @NotNull ResourceType type, @NotNull SetMultimap<String, String> seenQualifiers, @NotNull ListMultimap<String, ResourceItem> result) {
    ListMultimap<String, ResourceItem> items = getMap(type, false);
    if (items == null) {
        return;
    }
    for (ResourceItem item : items.values()) {
        String name = item.getName();
        String qualifiers = item.getQualifiers();
        if (!result.containsKey(name) || type == ResourceType.ID || !seenQualifiers.containsEntry(name, qualifiers)) {
            // We only add a duplicate item if there isn't an item with the same qualifiers (and it's
            // not an id; id's are allowed to be defined in multiple places even with the same
            // qualifiers)
            result.put(name, item);
            seenQualifiers.put(name, qualifiers);
        }
    }
}
Also used : ResourceItem(com.android.ide.common.res2.ResourceItem)

Example 59 with ResourceItem

use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.

the class PsiResourceFile method addItem.

@Override
public void addItem(@NonNull ResourceItem item) {
    item.setSource(this);
    String key = item.getKey();
    ResourceItem prev = mItems.get(key);
    if (prev != null) {
        // only contains a single key position.
        if (myDuplicates == null) {
            myDuplicates = ArrayListMultimap.create();
        }
        myDuplicates.put(key, prev);
    }
    mItems.put(key, item);
}
Also used : ResourceItem(com.android.ide.common.res2.ResourceItem)

Example 60 with ResourceItem

use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.

the class ResourceClassGenerator method generateStyleable.

private void generateStyleable(@NotNull ClassWriter cw, @NotNull TObjectIntHashMap<String> styleableIntCache, String className) {
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("generateStyleable(%s)", anonymizeClassName(className)));
    }
    boolean debug = LOG.isDebugEnabled() && isPublicClass(className);
    Collection<String> declaredStyleables = myAppResources.getItemsOfType(ResourceType.DECLARE_STYLEABLE);
    // Generate all declarations - both int[] and int for the indices into the array.
    for (String styleableName : declaredStyleables) {
        List<ResourceItem> items = myAppResources.getResourceItem(ResourceType.DECLARE_STYLEABLE, styleableName);
        if (items == null || items.isEmpty()) {
            if (debug) {
                LOG.debug("  No items for " + styleableName);
            }
            continue;
        }
        String fieldName = AndroidResourceUtil.getFieldNameByResourceName(styleableName);
        cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, fieldName, "[I", null, null);
        if (debug) {
            LOG.debug("  Defined styleable " + fieldName);
        }
        ResourceValue resourceValue = items.get(0).getResourceValue(false);
        assert resourceValue instanceof DeclareStyleableResourceValue;
        DeclareStyleableResourceValue dv = (DeclareStyleableResourceValue) resourceValue;
        List<AttrResourceValue> attributes = dv.getAllAttributes();
        int idx = 0;
        for (AttrResourceValue value : attributes) {
            Integer initialValue = idx++;
            String styleableEntryName = getResourceName(fieldName, value);
            cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, styleableEntryName, "I", null, initialValue);
            styleableIntCache.put(styleableEntryName, initialValue);
            if (debug) {
                LOG.debug("  Defined styleable " + styleableEntryName);
            }
        }
    }
    // Generate class initializer block to initialize the arrays declared above.
    MethodVisitor mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
    mv.visitCode();
    for (String styleableName : declaredStyleables) {
        List<ResourceItem> items = myAppResources.getResourceItem(ResourceType.DECLARE_STYLEABLE, styleableName);
        if (items == null || items.isEmpty()) {
            continue;
        }
        ResourceValue resourceValue = items.get(0).getResourceValue(false);
        assert resourceValue instanceof DeclareStyleableResourceValue;
        DeclareStyleableResourceValue dv = (DeclareStyleableResourceValue) resourceValue;
        List<AttrResourceValue> attributes = dv.getAllAttributes();
        if (attributes.isEmpty()) {
            continue;
        }
        Integer[] valuesArray = myAppResources.getDeclaredArrayValues(attributes, styleableName);
        if (valuesArray == null) {
            valuesArray = new Integer[attributes.size()];
        }
        List<Integer> values = Arrays.asList(valuesArray);
        String fieldName = AndroidResourceUtil.getFieldNameByResourceName(styleableName);
        myStyleableCache.put(fieldName, values);
        int idx = -1;
        for (AttrResourceValue value : attributes) {
            if (valuesArray[++idx] == null || !value.isFramework()) {
                valuesArray[idx] = myAppResources.getResourceId(ResourceType.ATTR, value.getName());
            }
        }
        generateArrayInitialization(mv, className, fieldName, values);
    }
    mv.visitInsn(RETURN);
    mv.visitMaxs(4, 0);
    mv.visitEnd();
}
Also used : DeclareStyleableResourceValue(com.android.ide.common.rendering.api.DeclareStyleableResourceValue) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor) AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue) DeclareStyleableResourceValue(com.android.ide.common.rendering.api.DeclareStyleableResourceValue) ResourceItem(com.android.ide.common.res2.ResourceItem)

Aggregations

ResourceItem (com.android.ide.common.res2.ResourceItem)83 VirtualFile (com.intellij.openapi.vfs.VirtualFile)44 PsiFile (com.intellij.psi.PsiFile)35 Document (com.intellij.openapi.editor.Document)26 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)26 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)11 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)11 XmlTag (com.intellij.psi.xml.XmlTag)11 NotNull (org.jetbrains.annotations.NotNull)11 IOException (java.io.IOException)10 AbstractResourceRepository (com.android.ide.common.res2.AbstractResourceRepository)8 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)8 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)7 ResourceFile (com.android.ide.common.res2.ResourceFile)7 ResourceType (com.android.resources.ResourceType)7 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)7 Nullable (org.jetbrains.annotations.Nullable)7 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)6 Project (com.intellij.openapi.project.Project)6 File (java.io.File)6