Search in sources :

Example 6 with ResourceValue

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;
}
Also used : ResourceValue(org.jetbrains.android.dom.resources.ResourceValue) PsiReference(com.intellij.psi.PsiReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with ResourceValue

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;
}
Also used : ResourceValue(org.jetbrains.android.dom.resources.ResourceValue) PsiReference(com.intellij.psi.PsiReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with ResourceValue

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;
}
Also used : CreateValueResourceQuickFix(org.jetbrains.android.inspections.CreateValueResourceQuickFix) ResourceFolderType(com.android.resources.ResourceFolderType) ResourceValue(org.jetbrains.android.dom.resources.ResourceValue) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) ResourceType(com.android.resources.ResourceType) AndroidResourceType(org.jetbrains.android.dom.AndroidResourceType) CreateFileResourceQuickFix(org.jetbrains.android.inspections.CreateFileResourceQuickFix) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 9 with ResourceValue

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;
}
Also used : ResourceValue(org.jetbrains.android.dom.resources.ResourceValue) XmlElement(com.intellij.psi.xml.XmlElement) ResourceType(com.android.resources.ResourceType) AndroidResourceType(org.jetbrains.android.dom.AndroidResourceType) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with ResourceValue

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;
}
Also used : Project(com.intellij.openapi.project.Project) DomElement(com.intellij.util.xml.DomElement) AndroidResourceReferenceBase(org.jetbrains.android.dom.converters.AndroidResourceReferenceBase) ResourceValue(org.jetbrains.android.dom.resources.ResourceValue) Include(org.jetbrains.android.dom.layout.Include) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ResourceValue (org.jetbrains.android.dom.resources.ResourceValue)10 Nullable (org.jetbrains.annotations.Nullable)4 ResourceType (com.android.resources.ResourceType)3 PsiReference (com.intellij.psi.PsiReference)3 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)3 NotNull (org.jetbrains.annotations.NotNull)3 ResourceFolderType (com.android.resources.ResourceFolderType)2 Project (com.intellij.openapi.project.Project)2 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)2 DomElement (com.intellij.util.xml.DomElement)2 AndroidResourceType (org.jetbrains.android.dom.AndroidResourceType)2 AndroidResourceReferenceBase (org.jetbrains.android.dom.converters.AndroidResourceReferenceBase)2 LayoutViewElement (org.jetbrains.android.dom.layout.LayoutViewElement)2 Style (org.jetbrains.android.dom.resources.Style)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 Result (com.intellij.openapi.application.Result)1 UndoConfirmationPolicy (com.intellij.openapi.command.UndoConfirmationPolicy)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Module (com.intellij.openapi.module.Module)1