use of com.android.resources.ResourceType in project android by JetBrains.
the class ResourceReferenceConverter method getResourceTypes.
@NotNull
public Set<ResourceType> getResourceTypes(@NotNull DomElement element) {
AndroidResourceType resourceType = element.getAnnotation(AndroidResourceType.class);
Set<ResourceType> types = EnumSet.copyOf(myResourceTypes);
if (resourceType != null) {
String s = resourceType.value();
if (s != null) {
ResourceType t = ResourceType.getEnum(s);
if (t != null) {
types.add(t);
}
}
}
if (types.size() == 0) {
return VALUE_RESOURCE_TYPES;
} else if (types.contains(ResourceType.DRAWABLE)) {
types.add(ResourceType.COLOR);
}
return types;
}
use of com.android.resources.ResourceType in project android by JetBrains.
the class ResourceReferenceConverter method getVariants.
@Override
@NotNull
public Collection<? extends ResourceValue> getVariants(ConvertContext context) {
Set<ResourceValue> result = new HashSet<>();
Module module = context.getModule();
if (module == null)
return result;
AndroidFacet facet = AndroidFacet.getInstance(module);
if (facet == null)
return result;
final Set<ResourceType> recommendedTypes = getResourceTypes(context);
if (recommendedTypes.contains(ResourceType.BOOL) && recommendedTypes.size() < VALUE_RESOURCE_TYPES.size()) {
// Is this resource reference expected to be a @bool reference? Specifically
// check that it's not allowed to be *all* resource types since that's a fallback
// for when we don't have metadata, and we don't want to show true and false
// as possible completions for things like com.google.android.gms.version
result.add(ResourceValue.literal(VALUE_TRUE));
result.add(ResourceValue.literal(VALUE_FALSE));
}
// hack to check if it is a real id attribute
if (recommendedTypes.contains(ResourceType.ID) && recommendedTypes.size() == 1) {
result.add(ResourceValue.reference(NEW_ID_PREFIX));
}
XmlElement element = context.getXmlElement();
if (element == null)
return result;
String value = getValue(element);
assert value != null;
boolean startsWithRefChar = StringUtil.startsWithChar(value, '@');
if (!myQuiet || startsWithRefChar) {
String resourcePackage = null;
// Retrieve the system prefix depending on the prefix settings ("@android:" or "android:")
String systemPrefix = getPackagePrefix(SYSTEM_RESOURCE_PACKAGE, myWithPrefix || startsWithRefChar);
if (value.startsWith(systemPrefix)) {
resourcePackage = SYSTEM_RESOURCE_PACKAGE;
} else {
result.add(ResourceValue.literal(systemPrefix));
}
final char prefix = myWithPrefix || startsWithRefChar ? '@' : 0;
if (value.startsWith(NEW_ID_PREFIX)) {
addVariantsForIdDeclaration(result, facet, prefix, value);
}
if (recommendedTypes.size() >= 1 && myExpandedCompletionSuggestion) {
// We will add the resource type (e.g. @style/) if the current value starts like a reference using @
final boolean explicitResourceType = startsWithRefChar || myWithExplicitResourceType;
for (final ResourceType type : recommendedTypes) {
addResourceReferenceValues(facet, prefix, type, resourcePackage, result, explicitResourceType);
}
} else {
final Set<ResourceType> filteringSet = SYSTEM_RESOURCE_PACKAGE.equals(resourcePackage) ? null : getResourceTypesInCurrentModule(facet);
for (ResourceType resourceType : ResourceType.values()) {
final String type = resourceType.getName();
String typePrefix = getTypePrefix(resourcePackage, type);
if (value.startsWith(typePrefix)) {
addResourceReferenceValues(facet, prefix, resourceType, resourcePackage, result, true);
} else if (recommendedTypes.contains(resourceType) && (filteringSet == null || filteringSet.contains(resourceType))) {
result.add(ResourceValue.literal(typePrefix));
}
}
}
}
if (myAllowAttributeReferences) {
completeAttributeReferences(value, facet, result);
}
final ResolvingConverter<String> additionalConverter = getAdditionalConverter(context);
if (additionalConverter != null) {
for (String variant : additionalConverter.getVariants(context)) {
result.add(ResourceValue.literal(variant));
}
}
return result;
}
use of com.android.resources.ResourceType in project android by JetBrains.
the class AndroidResourceReferenceBase method resolveInner.
@NotNull
private ResolveResult[] resolveInner() {
final List<PsiElement> elements = new ArrayList<>();
final boolean attrReference = myResourceValue.getPrefix() == '?';
collectTargets(myFacet, myResourceValue, elements, attrReference);
final List<ResolveResult> result = new ArrayList<>();
if (elements.isEmpty() && myResourceValue.getResourceName() != null && !AndroidUtils.SYSTEM_RESOURCE_PACKAGE.equals(myResourceValue.getNamespace())) {
// Dynamic items do not appear in the XML scanning file index; look for
// these in the resource repositories.
LocalResourceRepository resources = AppResourceRepository.getAppResources(myFacet.getModule(), true);
ResourceType resourceType = myResourceValue.getType();
if (resourceType != null && (resourceType != ResourceType.ATTR || attrReference)) {
// If not, it could be some broken source, such as @android/test
assert resources != null;
List<ResourceItem> items = resources.getResourceItem(resourceType, myResourceValue.getResourceName());
if (items != null && FolderTypeRelationship.getRelatedFolders(resourceType).contains(ResourceFolderType.VALUES)) {
for (ResourceItem item : items) {
XmlTag tag = LocalResourceRepository.getItemTag(myFacet.getModule().getProject(), item);
if (tag != null) {
elements.add(tag);
} else if (item instanceof DynamicResourceValueItem) {
result.add(((DynamicResourceValueItem) item).createResolveResult());
}
}
}
}
}
if (elements.size() > 1) {
Collections.sort(elements, AndroidResourceUtil.RESOURCE_ELEMENT_COMPARATOR);
}
for (PsiElement target : elements) {
result.add(new PsiElementResolveResult(target));
}
return result.toArray(new ResolveResult[result.size()]);
}
use of com.android.resources.ResourceType in project android by JetBrains.
the class AndroidResourceReferenceBase method collectTargets.
private void collectTargets(AndroidFacet facet, ResourceValue resValue, List<PsiElement> elements, boolean attrReference) {
ResourceType resType = resValue.getType();
if (resType == null) {
return;
}
ResourceManager manager = facet.getResourceManager(resValue.getNamespace(), myElement);
if (manager != null) {
String resName = resValue.getResourceName();
if (resName != null) {
manager.collectLazyResourceElements(resType.getName(), resName, attrReference, myElement, elements);
}
}
}
use of com.android.resources.ResourceType in project android by JetBrains.
the class ResourceNameConverter method getErrorMessage.
@Override
public String getErrorMessage(@Nullable String s, ConvertContext context) {
ResourceType type = null;
XmlTag tag = context.getTag();
if (tag != null && SdkConstants.TAG_ATTR.equals(tag.getName())) {
// The resource name validator needs to know if we're dealing with
// an <attr>
type = ResourceType.ATTR;
}
return s == null ? null : ValueResourceNameValidator.getErrorText(s, type);
}
Aggregations