Search in sources :

Example 1 with AttributeDefinition

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

the class AndroidXmlDocumentationProvider method getResourceDocumentation.

@Nullable
private static String getResourceDocumentation(PsiElement element, String value) {
    ResourceUrl url = ResourceUrl.parse(value);
    if (url != null) {
        return generateDoc(element, url);
    } else {
        XmlAttribute attribute = PsiTreeUtil.getParentOfType(element, XmlAttribute.class, false);
        XmlTag tag = null;
        // True if getting the documentation of the XML value (not the value of the name attribute)
        boolean isXmlValue = false;
        // get the XmlAttribute using the containing tag
        if (element instanceof XmlToken && XML_DATA_CHARACTERS.equals(((XmlToken) element).getTokenType())) {
            tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
            attribute = tag == null ? null : tag.getAttribute(ATTR_NAME);
            isXmlValue = true;
        }
        if (attribute == null) {
            return null;
        }
        if (ATTR_NAME.equals(attribute.getName())) {
            tag = tag != null ? tag : attribute.getParent();
            XmlTag parentTag = tag.getParentTag();
            if (parentTag == null) {
                return null;
            }
            if (TAG_RESOURCES.equals(parentTag.getName())) {
                // Handle ID definitions, http://developer.android.com/guide/topics/resources/more-resources.html#Id
                String typeName = tag.getName();
                if (TAG_ITEM.equals(typeName)) {
                    typeName = tag.getAttributeValue(ATTR_TYPE);
                }
                ResourceType type = ResourceType.getEnum(typeName);
                if (type != null) {
                    return generateDoc(element, type, value, false);
                }
            } else if (TAG_STYLE.equals(parentTag.getName())) {
                // String used to get attribute definitions
                String targetValue = value;
                if (isXmlValue && attribute.getValue() != null) {
                    // In this case, the target is the name attribute of the <item> tag, which contains the key of the attr enum
                    targetValue = attribute.getValue();
                }
                if (targetValue.startsWith(ANDROID_NS_NAME_PREFIX)) {
                    targetValue = targetValue.substring(ANDROID_NS_NAME_PREFIX.length());
                }
                // Handle style item definitions, http://developer.android.com/guide/topics/resources/style-resource.html
                AttributeDefinition attributeDefinition = getAttributeDefinitionForElement(element, targetValue);
                if (attributeDefinition == null) {
                    return null;
                }
                // Return the doc of the value if searching for an enum value, otherwise return the doc of the enum itself
                return StringUtil.trim(isXmlValue ? attributeDefinition.getValueDoc(value) : attributeDefinition.getDocValue(null));
            }
        }
        // Display documentation for enum values defined in attrs.xml file, if it's present
        if (ANDROID_URI.equals(attribute.getNamespace())) {
            AttributeDefinition attributeDefinition = getAttributeDefinitionForElement(element, attribute.getLocalName());
            if (attributeDefinition == null) {
                return null;
            }
            return StringUtil.trim(attributeDefinition.getValueDoc(value));
        }
    }
    return null;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) ResourceType(com.android.resources.ResourceType) ResourceUrl(com.android.ide.common.resources.ResourceUrl) XmlTag(com.intellij.psi.xml.XmlTag) XmlToken(com.intellij.psi.xml.XmlToken) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with AttributeDefinition

use of org.jetbrains.android.dom.attrs.AttributeDefinition 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 3 with AttributeDefinition

use of org.jetbrains.android.dom.attrs.AttributeDefinition 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 4 with AttributeDefinition

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

the class EditedStyleItem method isDeprecated.

public boolean isDeprecated() {
    AttributeDefinition def = ResolutionUtils.getAttributeDefinition(mySourceTheme.getConfiguration(), getSelectedValue());
    String doc = (def == null) ? null : def.getDocValue(null);
    return (doc != null && StringUtil.containsIgnoreCase(doc, DEPRECATED));
}
Also used : AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition)

Example 5 with AttributeDefinition

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

Aggregations

AttributeDefinition (org.jetbrains.android.dom.attrs.AttributeDefinition)35 NotNull (org.jetbrains.annotations.NotNull)10 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)8 NlProperty (com.android.tools.idea.uibuilder.property.NlProperty)7 Nullable (org.jetbrains.annotations.Nullable)7 NlPropertyItem (com.android.tools.idea.uibuilder.property.NlPropertyItem)6 AttributeDefinitions (org.jetbrains.android.dom.attrs.AttributeDefinitions)6 ModelBuilder (com.android.tools.idea.uibuilder.fixtures.ModelBuilder)5 NlModel (com.android.tools.idea.uibuilder.model.NlModel)5 AttributeFormat (org.jetbrains.android.dom.attrs.AttributeFormat)5 ResourceType (com.android.resources.ResourceType)4 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)3 XmlTag (com.intellij.psi.xml.XmlTag)3 ResourceManager (org.jetbrains.android.resourceManagers.ResourceManager)3 HtmlBuilder (com.android.utils.HtmlBuilder)2 NamespaceAwareXmlAttributeDescriptor (com.intellij.xml.NamespaceAwareXmlAttributeDescriptor)2 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)2 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)2 ViewGroup (android.view.ViewGroup)1 LinearLayout (android.widget.LinearLayout)1