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;
}
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;
}
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;
}
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));
}
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();
}
}
}
Aggregations