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