Search in sources :

Example 1 with AttributeDefinitions

use of org.jetbrains.android.dom.attrs.AttributeDefinitions in project android by JetBrains.

the class AndroidDomUtil method getAttributeDefinition.

@Nullable
public static AttributeDefinition getAttributeDefinition(@NotNull AndroidFacet facet, @NotNull XmlAttribute attribute) {
    String localName = attribute.getLocalName();
    String namespace = attribute.getNamespace();
    boolean isFramework = namespace.equals(ANDROID_URI);
    if (!isFramework && TOOLS_URI.equals(namespace)) {
        // Treat tools namespace attributes as aliases for Android namespaces: see https://developer.android.com/studio/write/tool-attributes.html#design-time_view_attributes
        isFramework = true;
        // However, there are some attributes with other meanings: https://developer.android.com/studio/write/tool-attributes.html
        // Filter some of these out such that they are not treated as the (unrelated but identically named) platform attributes
        AttributeDefinition toolsAttr = TOOLS_ATTRIBUTE_DEFINITIONS.getAttrDefByName(localName);
        if (toolsAttr != null) {
            return toolsAttr;
        }
    }
    ResourceManager manager = facet.getResourceManager(isFramework ? SYSTEM_RESOURCE_PACKAGE : null);
    if (manager != null) {
        AttributeDefinitions attrDefs = manager.getAttributeDefinitions();
        if (attrDefs != null) {
            return attrDefs.getAttrDefByName(localName);
        }
    }
    return null;
}
Also used : AttributeDefinitions(org.jetbrains.android.dom.attrs.AttributeDefinitions) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) ResourceManager(org.jetbrains.android.resourceManagers.ResourceManager) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with AttributeDefinitions

use of org.jetbrains.android.dom.attrs.AttributeDefinitions in project android by JetBrains.

the class AndroidXmlDocumentationProvider method getAttributeDefinitionForElement.

/**
   * Try to get the attribute definition for an element using first the system resource manager and then the local one.
   * Return null if can't find definition in any resource manager.
   */
@Nullable
private static AttributeDefinition getAttributeDefinitionForElement(@NotNull PsiElement element, @NotNull String name) {
    AndroidFacet facet = AndroidFacet.getInstance(element);
    if (facet == null) {
        return null;
    }
    AttributeDefinitions definitions = getAttributeDefinitions(facet.getSystemResourceManager());
    if (definitions == null) {
        return null;
    }
    AttributeDefinition attributeDefinition = definitions.getAttrDefByName(name);
    if (attributeDefinition == null) {
        // Try to get the attribute definition using the local resource manager instead
        definitions = getAttributeDefinitions(facet.getLocalResourceManager());
        if (definitions == null) {
            return null;
        }
        attributeDefinition = definitions.getAttrDefByName(name);
    }
    return attributeDefinition;
}
Also used : AttributeDefinitions(org.jetbrains.android.dom.attrs.AttributeDefinitions) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with AttributeDefinitions

use of org.jetbrains.android.dom.attrs.AttributeDefinitions in project android by JetBrains.

the class RenderErrorContributor method reportTagResourceFormat.

private void reportTagResourceFormat(@NotNull RenderResult result, @NotNull RenderProblem message) {
    Object clientData = message.getClientData();
    if (!(clientData instanceof String[])) {
        return;
    }
    String[] strings = (String[]) clientData;
    if (strings.length != 2) {
        return;
    }
    RenderTask renderTask = result.getRenderTask();
    if (renderTask == null) {
        return;
    }
    IAndroidTarget target = renderTask.getConfiguration().getRealTarget();
    if (target == null) {
        return;
    }
    AndroidPlatform platform = renderTask.getPlatform();
    if (platform == null) {
        return;
    }
    AndroidTargetData targetData = platform.getSdkData().getTargetData(target);
    AttributeDefinitions definitionLookup = targetData.getPublicAttrDefs(result.getFile().getProject());
    final String attributeName = strings[0];
    final String currentValue = strings[1];
    if (definitionLookup == null) {
        return;
    }
    AttributeDefinition definition = definitionLookup.getAttrDefByName(attributeName);
    if (definition == null) {
        return;
    }
    Set<AttributeFormat> formats = definition.getFormats();
    if (formats.contains(AttributeFormat.Flag) || formats.contains(AttributeFormat.Enum)) {
        String[] values = definition.getValues();
        if (values.length > 0) {
            HtmlBuilder builder = new HtmlBuilder();
            builder.add("Change ").add(currentValue).add(" to: ");
            boolean first = true;
            for (String value : values) {
                if (first) {
                    first = false;
                } else {
                    builder.add(", ");
                }
                builder.addLink(value, myLinkManager.createReplaceAttributeValueUrl(attributeName, currentValue, value));
            }
            addRefreshAction(builder);
            addIssue().setSummary("Incorrect resource value format").setHtmlContent(builder).build();
        }
    }
}
Also used : HtmlBuilder(com.android.utils.HtmlBuilder) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) IAndroidTarget(com.android.sdklib.IAndroidTarget) AttributeFormat(org.jetbrains.android.dom.attrs.AttributeFormat) AttributeDefinitions(org.jetbrains.android.dom.attrs.AttributeDefinitions) AndroidTargetData(org.jetbrains.android.sdk.AndroidTargetData)

Example 4 with AttributeDefinitions

use of org.jetbrains.android.dom.attrs.AttributeDefinitions in project android by JetBrains.

the class SetAttributeQuickFix method askForAttributeValue.

@Nullable
private String askForAttributeValue(@NotNull PsiElement context) {
    final AndroidFacet facet = AndroidFacet.getInstance(context);
    final String message = "Specify value of attribute '" + myAttributeName + "'";
    final String title = "Set Attribute Value";
    if (facet != null) {
        final SystemResourceManager srm = facet.getSystemResourceManager();
        if (srm != null) {
            final AttributeDefinitions attrDefs = srm.getAttributeDefinitions();
            if (attrDefs != null) {
                final AttributeDefinition def = attrDefs.getAttrDefByName(myAttributeName);
                if (def != null) {
                    final String[] variants = def.getValues();
                    if (variants.length > 0) {
                        return Messages.showEditableChooseDialog(message, title, Messages.getQuestionIcon(), variants, variants[0], null);
                    }
                }
            }
        }
    }
    return Messages.showInputDialog(context.getProject(), message, title, Messages.getQuestionIcon());
}
Also used : AttributeDefinitions(org.jetbrains.android.dom.attrs.AttributeDefinitions) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) SystemResourceManager(org.jetbrains.android.resourceManagers.SystemResourceManager) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with AttributeDefinitions

use of org.jetbrains.android.dom.attrs.AttributeDefinitions 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)

Aggregations

AttributeDefinitions (org.jetbrains.android.dom.attrs.AttributeDefinitions)9 AttributeDefinition (org.jetbrains.android.dom.attrs.AttributeDefinition)6 ResourceManager (org.jetbrains.android.resourceManagers.ResourceManager)5 Nullable (org.jetbrains.annotations.Nullable)5 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)4 IAndroidTarget (com.android.sdklib.IAndroidTarget)2 XmlTag (com.intellij.psi.xml.XmlTag)2 SystemResourceManager (org.jetbrains.android.resourceManagers.SystemResourceManager)2 AndroidTargetData (org.jetbrains.android.sdk.AndroidTargetData)2 NotNull (org.jetbrains.annotations.NotNull)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 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)1 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)1 HtmlBuilder (com.android.utils.HtmlBuilder)1 NamespaceAwareXmlAttributeDescriptor (com.intellij.xml.NamespaceAwareXmlAttributeDescriptor)1 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)1 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)1 AndroidAnyAttributeDescriptor (org.jetbrains.android.dom.AndroidAnyAttributeDescriptor)1