use of org.jetbrains.android.dom.resources.ResourceValue in project android by JetBrains.
the class AndroidDomUtil method getAndroidResourceReference.
@Nullable
public static AndroidResourceReferenceBase getAndroidResourceReference(@Nullable GenericAttributeValue<ResourceValue> attribute, boolean localOnly) {
if (attribute == null) {
return null;
}
final ResourceValue resValue = attribute.getValue();
if (resValue == null || (localOnly && resValue.getNamespace() != null)) {
return null;
}
final XmlAttributeValue value = attribute.getXmlAttributeValue();
if (value == null) {
return null;
}
for (PsiReference reference : value.getReferences()) {
if (reference instanceof AndroidResourceReferenceBase) {
return (AndroidResourceReferenceBase) reference;
}
}
return null;
}
use of org.jetbrains.android.dom.resources.ResourceValue in project android by JetBrains.
the class ParentStyleConverter method createReferences.
@NotNull
@Override
public PsiReference[] createReferences(GenericDomValue<ResourceValue> value, PsiElement element, ConvertContext context) {
if (element instanceof XmlAttributeValue) {
// parent="" is allowed; it's used to deliberately allow style A.B.C to not be a child of A.B
XmlAttributeValue attributeValue = (XmlAttributeValue) element;
if (attributeValue.isValid() && attributeValue.getValue().isEmpty()) {
return PsiReference.EMPTY_ARRAY;
}
}
final PsiReference[] refsFromSuper = super.createReferences(value, element, context);
final ResourceValue resValue = value.getValue();
if (resValue == null || resValue.getNamespace() != null) {
return refsFromSuper;
}
final AndroidFacet facet = AndroidFacet.getInstance(context);
if (facet != null) {
final PsiReference[] refs = getReferencesInStyleName(value, facet);
if (refs.length > 0) {
return ArrayUtil.mergeArrays(refsFromSuper, refs);
}
}
return refsFromSuper;
}
use of org.jetbrains.android.dom.resources.ResourceValue in project android by JetBrains.
the class ResourceReferenceConverter method getQuickFixes.
@Override
public LocalQuickFix[] getQuickFixes(ConvertContext context) {
AndroidFacet facet = AndroidFacet.getInstance(context);
if (facet != null) {
final DomElement domElement = context.getInvocationElement();
if (domElement instanceof GenericDomValue) {
final String value = ((GenericDomValue) domElement).getStringValue();
if (value != null) {
ResourceValue resourceValue = ResourceValue.parse(value, false, myWithPrefix, true);
if (resourceValue != null) {
String aPackage = resourceValue.getNamespace();
ResourceType resType = resourceValue.getType();
if (resType == null && myResourceTypes.size() == 1) {
resType = myResourceTypes.iterator().next();
}
final String resourceName = resourceValue.getResourceName();
if (aPackage == null && resType != null && resourceName != null && AndroidResourceUtil.isCorrectAndroidResourceName(resourceName)) {
final List<LocalQuickFix> fixes = new ArrayList<>();
ResourceFolderType folderType = AndroidResourceUtil.XML_FILE_RESOURCE_TYPES.get(resType);
if (folderType != null) {
fixes.add(new CreateFileResourceQuickFix(facet, folderType, resourceName, context.getFile(), false));
}
if (VALUE_RESOURCE_TYPES.contains(resType) && resType != ResourceType.LAYOUT) {
// layouts: aliases only
fixes.add(new CreateValueResourceQuickFix(facet, resType, resourceName, context.getFile(), false));
}
return fixes.toArray(new LocalQuickFix[fixes.size()]);
}
}
}
}
}
return LocalQuickFix.EMPTY_ARRAY;
}
use of org.jetbrains.android.dom.resources.ResourceValue 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 org.jetbrains.android.dom.resources.ResourceValue in project android by JetBrains.
the class AndroidInlineUtil method getLayoutUsageData.
@Nullable
static LayoutUsageData getLayoutUsageData(@NotNull XmlTag tag) {
final Project project = tag.getProject();
final DomElement domElement = DomManager.getDomManager(project).getDomElement(tag);
if (domElement instanceof Include) {
final GenericAttributeValue<ResourceValue> layoutAttribute = ((Include) domElement).getLayout();
final AndroidResourceReferenceBase reference = AndroidDomUtil.getAndroidResourceReference(layoutAttribute, true);
if (reference != null) {
return new LayoutUsageData(project, tag, reference);
}
}
return null;
}
Aggregations