Search in sources :

Example 11 with ResourceManager

use of org.jetbrains.android.resourceManagers.ResourceManager 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 12 with ResourceManager

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

the class NlProperties method getPropertiesWithReadLock.

@NotNull
private Table<String, String, NlPropertyItem> getPropertiesWithReadLock(@NotNull AndroidFacet facet, @NotNull List<NlComponent> components, @NotNull GradleDependencyManager dependencyManager) {
    ResourceManager localResourceManager = facet.getLocalResourceManager();
    ResourceManager systemResourceManager = facet.getSystemResourceManager();
    if (systemResourceManager == null) {
        Logger.getInstance(NlProperties.class).error("No system resource manager for module: " + facet.getModule().getName());
        return ImmutableTable.of();
    }
    AttributeDefinitions localAttrDefs = localResourceManager.getAttributeDefinitions();
    AttributeDefinitions systemAttrDefs = systemResourceManager.getAttributeDefinitions();
    Table<String, String, NlPropertyItem> combinedProperties = null;
    for (NlComponent component : components) {
        XmlTag tag = component.getTag();
        if (!tag.isValid()) {
            return ImmutableTable.of();
        }
        XmlElementDescriptor elementDescriptor = myDescriptorProvider.getDescriptor(tag);
        if (elementDescriptor == null) {
            return ImmutableTable.of();
        }
        XmlAttributeDescriptor[] descriptors = elementDescriptor.getAttributesDescriptors(tag);
        Table<String, String, NlPropertyItem> properties = HashBasedTable.create(3, descriptors.length);
        for (XmlAttributeDescriptor desc : descriptors) {
            String namespace = getNamespace(desc, tag);
            AttributeDefinitions attrDefs = NS_RESOURCES.equals(namespace) ? systemAttrDefs : localAttrDefs;
            AttributeDefinition attrDef = attrDefs == null ? null : attrDefs.getAttrDefByName(desc.getName());
            NlPropertyItem property = NlPropertyItem.create(components, desc, namespace, attrDef);
            properties.put(StringUtil.notNullize(namespace), property.getName(), property);
        }
        // Exceptions:
        switch(tag.getName()) {
            case AUTO_COMPLETE_TEXT_VIEW:
                // An AutoCompleteTextView has a popup that is created at runtime.
                // Properties for this popup can be added to the AutoCompleteTextView tag.
                properties.put(ANDROID_URI, ATTR_POPUP_BACKGROUND, NlPropertyItem.create(components, new AndroidAnyAttributeDescriptor(ATTR_POPUP_BACKGROUND), ANDROID_URI, systemAttrDefs != null ? systemAttrDefs.getAttrDefByName(ATTR_POPUP_BACKGROUND) : null));
                break;
        }
        combinedProperties = combine(properties, combinedProperties);
    }
    // The following properties are deprecated in the support library and can be ignored by tools:
    assert combinedProperties != null;
    combinedProperties.remove(AUTO_URI, ATTR_PADDING_START);
    combinedProperties.remove(AUTO_URI, ATTR_PADDING_END);
    combinedProperties.remove(AUTO_URI, ATTR_THEME);
    setUpDesignProperties(combinedProperties);
    setUpSrcCompat(combinedProperties, facet, components, dependencyManager);
    initStarState(combinedProperties);
    //noinspection ConstantConditions
    return combinedProperties;
}
Also used : AndroidAnyAttributeDescriptor(org.jetbrains.android.dom.AndroidAnyAttributeDescriptor) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) ResourceManager(org.jetbrains.android.resourceManagers.ResourceManager) AttributeDefinitions(org.jetbrains.android.dom.attrs.AttributeDefinitions) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) NamespaceAwareXmlAttributeDescriptor(com.intellij.xml.NamespaceAwareXmlAttributeDescriptor) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with ResourceManager

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

the class PropertyTestCase method getDefinition.

@Nullable
protected static AttributeDefinition getDefinition(@NotNull NlComponent component, @NotNull XmlAttributeDescriptor descriptor) {
    AndroidFacet facet = component.getModel().getFacet();
    ResourceManager localResourceManager = facet.getLocalResourceManager();
    ResourceManager systemResourceManager = facet.getSystemResourceManager();
    assertThat(systemResourceManager).isNotNull();
    AttributeDefinitions localAttrDefs = localResourceManager.getAttributeDefinitions();
    AttributeDefinitions systemAttrDefs = systemResourceManager.getAttributeDefinitions();
    AttributeDefinitions attrDefs = NS_RESOURCES.equals(getNamespace(component, descriptor)) ? systemAttrDefs : localAttrDefs;
    return attrDefs == null ? null : attrDefs.getAttrDefByName(descriptor.getName());
}
Also used : AttributeDefinitions(org.jetbrains.android.dom.attrs.AttributeDefinitions) ResourceManager(org.jetbrains.android.resourceManagers.ResourceManager) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with ResourceManager

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

the class KotlinAndroidGotoDeclarationHandler method getGotoDeclarationTargets.

@Override
public PsiElement[] getGotoDeclarationTargets(@Nullable PsiElement sourceElement, int offset, Editor editor) {
    KtSimpleNameExpression referenceExpression = GotoResourceHelperKt.getReferenceExpression(sourceElement);
    if (referenceExpression == null) {
        return null;
    }
    AndroidFacet facet = AndroidUtilKt.getAndroidFacetForFile(referenceExpression);
    if (facet == null) {
        return null;
    }
    AndroidResourceUtil.MyReferredResourceFieldInfo info = GotoResourceHelperKt.getInfo(referenceExpression, facet);
    if (info == null)
        return null;
    String nestedClassName = info.getClassName();
    String fieldName = info.getFieldName();
    List<PsiElement> resourceList = new ArrayList<PsiElement>();
    if (info.isFromManifest()) {
        collectManifestElements(nestedClassName, fieldName, facet, resourceList);
    } else {
        ResourceManager manager = info.isSystem() ? facet.getSystemResourceManager(false) : facet.getLocalResourceManager();
        if (manager == null) {
            return null;
        }
        manager.collectLazyResourceElements(nestedClassName, fieldName, false, referenceExpression, resourceList);
        if (manager instanceof LocalResourceManager) {
            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 : AndroidResourceUtil(org.jetbrains.android.util.AndroidResourceUtil) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) ArrayList(java.util.ArrayList) KtSimpleNameExpression(org.jetbrains.kotlin.psi.KtSimpleNameExpression) ResourceManager(org.jetbrains.android.resourceManagers.ResourceManager) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) PsiElement(com.intellij.psi.PsiElement) Attr(org.jetbrains.android.dom.resources.Attr) DeclareStyleable(org.jetbrains.android.dom.resources.DeclareStyleable)

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