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