Search in sources :

Example 26 with AndroidFacet

use of org.jetbrains.android.facet.AndroidFacet in project android by JetBrains.

the class AndroidDomElementDescriptorProvider method getDescriptor.

@Nullable
private static XmlElementDescriptor getDescriptor(DomElement domElement, XmlTag tag, @Nullable String baseClassName) {
    AndroidFacet facet = AndroidFacet.getInstance(domElement);
    if (facet == null)
        return null;
    final String name = domElement.getXmlTag().getName();
    final PsiClass aClass = baseClassName != null ? LayoutViewClassUtils.findClassByTagName(facet, name, baseClassName) : null;
    final Icon icon = getIconForTag(name, domElement);
    final DefinesXml definesXml = domElement.getAnnotation(DefinesXml.class);
    if (definesXml != null) {
        return new AndroidXmlTagDescriptor(aClass, new DomElementXmlDescriptor(domElement), baseClassName, icon);
    }
    final PsiElement parent = tag.getParent();
    if (parent instanceof XmlTag) {
        final XmlElementDescriptor parentDescriptor = ((XmlTag) parent).getDescriptor();
        if (parentDescriptor != null && parentDescriptor instanceof AndroidXmlTagDescriptor) {
            XmlElementDescriptor domDescriptor = parentDescriptor.getElementDescriptor(tag, (XmlTag) parent);
            if (domDescriptor != null) {
                return new AndroidXmlTagDescriptor(aClass, domDescriptor, baseClassName, icon);
            }
        }
    }
    return null;
}
Also used : DomElementXmlDescriptor(com.intellij.xml.impl.dom.DomElementXmlDescriptor) PsiClass(com.intellij.psi.PsiClass) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) DefinesXml(com.intellij.util.xml.DefinesXml) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 27 with AndroidFacet

use of org.jetbrains.android.facet.AndroidFacet in project android by JetBrains.

the class AndroidDomExtender method registerExtensions.

@Override
public void registerExtensions(@NotNull AndroidDomElement element, @NotNull final DomExtensionsRegistrar registrar) {
    final AndroidFacet facet = AndroidFacet.getInstance(element);
    if (facet == null) {
        return;
    }
    AttributeProcessingUtil.processAttributes(element, facet, true, (xmlName, attrDef, parentStyleableName) -> {
        Set<AttributeFormat> formats = attrDef.getFormats();
        Class valueClass = formats.size() == 1 ? getValueClass(formats.iterator().next()) : String.class;
        registrar.registerAttributeChildExtension(xmlName, GenericAttributeValue.class);
        return registrar.registerGenericAttributeValueChildExtension(xmlName, valueClass);
    });
    SubtagsProcessingUtil.processSubtags(facet, element, registrar::registerCollectionChildrenExtension);
}
Also used : AttributeFormat(org.jetbrains.android.dom.attrs.AttributeFormat) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 28 with AndroidFacet

use of org.jetbrains.android.facet.AndroidFacet in project android by JetBrains.

the class AndroidDomUtil method findComponentDeclarationInManifest.

@Nullable
public static AndroidAttributeValue<PsiClass> findComponentDeclarationInManifest(@NotNull PsiClass aClass) {
    final AndroidFacet facet = AndroidFacet.getInstance(aClass);
    if (facet == null) {
        return null;
    }
    final boolean isActivity = isInheritor(aClass, AndroidUtils.ACTIVITY_BASE_CLASS_NAME);
    final boolean isService = isInheritor(aClass, AndroidUtils.SERVICE_CLASS_NAME);
    final boolean isReceiver = isInheritor(aClass, AndroidUtils.RECEIVER_CLASS_NAME);
    final boolean isProvider = isInheritor(aClass, AndroidUtils.PROVIDER_CLASS_NAME);
    if (!isActivity && !isService && !isReceiver && !isProvider) {
        return null;
    }
    final Manifest manifest = facet.getManifest();
    if (manifest == null) {
        return null;
    }
    final Application application = manifest.getApplication();
    if (application == null) {
        return null;
    }
    if (isActivity) {
        for (Activity activity : application.getActivities()) {
            final AndroidAttributeValue<PsiClass> activityClass = activity.getActivityClass();
            if (activityClass.getValue() == aClass) {
                return activityClass;
            }
        }
    } else if (isService) {
        for (Service service : application.getServices()) {
            final AndroidAttributeValue<PsiClass> serviceClass = service.getServiceClass();
            if (serviceClass.getValue() == aClass) {
                return serviceClass;
            }
        }
    } else if (isReceiver) {
        for (Receiver receiver : application.getReceivers()) {
            final AndroidAttributeValue<PsiClass> receiverClass = receiver.getReceiverClass();
            if (receiverClass.getValue() == aClass) {
                return receiverClass;
            }
        }
    } else {
        for (Provider provider : application.getProviders()) {
            final AndroidAttributeValue<PsiClass> providerClass = provider.getProviderClass();
            if (providerClass.getValue() == aClass) {
                return providerClass;
            }
        }
    }
    return null;
}
Also used : PsiClass(com.intellij.psi.PsiClass) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Nullable(org.jetbrains.annotations.Nullable)

Example 29 with AndroidFacet

use of org.jetbrains.android.facet.AndroidFacet 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;
}
Also used : AttributeDefinitions(org.jetbrains.android.dom.attrs.AttributeDefinitions) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Nullable(org.jetbrains.annotations.Nullable)

Example 30 with AndroidFacet

use of org.jetbrains.android.facet.AndroidFacet in project android by JetBrains.

the class PackageClassConverter method createReferences.

@NotNull
@Override
public PsiReference[] createReferences(GenericDomValue<PsiClass> value, final PsiElement element, ConvertContext context) {
    assert element instanceof XmlAttributeValue;
    final XmlAttributeValue attrValue = (XmlAttributeValue) element;
    final String strValue = attrValue.getValue();
    final boolean startsWithPoint = strValue.startsWith(".");
    final int start = attrValue.getValueTextRange().getStartOffset() - attrValue.getTextRange().getStartOffset();
    final DomElement domElement = context.getInvocationElement();
    final String manifestPackage = getManifestPackage(context);
    final ExtendClass extendClassAnnotation = domElement.getAnnotation(ExtendClass.class);
    final String[] extendClassesNames = extendClassAnnotation != null ? new String[] { extendClassAnnotation.value() } : myExtendClassesNames;
    final boolean inModuleOnly = domElement.getAnnotation(CompleteNonModuleClass.class) == null;
    AndroidFacet facet = AndroidFacet.getInstance(context);
    // If the source XML file is contained within the test folders, we'll also allow to resolve test classes
    final boolean isTestFile = facet != null && isTestFile(facet, element.getContainingFile().getVirtualFile());
    if (strValue.isEmpty()) {
        return PsiReference.EMPTY_ARRAY;
    }
    final List<PsiReference> result = new ArrayList<>();
    final Module module = context.getModule();
    /**
     * Using inner class here as opposed to anonymous one as with anonymous class it wouldn't be possible to access {@code myPartStart} later
     */
    class CustomConsumer implements Consumer<Integer> {

        int myPartStart = 0;

        private boolean myIsPackage = true;

        @Override
        public void consume(Integer index) {
            if (index > myPartStart) {
                final TextRange range = new TextRange(start + myPartStart, start + index);
                final MyReference reference = new MyReference(element, range, manifestPackage, startsWithPoint, start, myIsPackage, module, extendClassesNames, inModuleOnly, isTestFile);
                result.add(reference);
            }
            myPartStart = index + 1;
        }
    }
    final CustomConsumer consumer = new CustomConsumer();
    AndroidTextUtils.forEachOccurrence(strValue, '.', consumer);
    consumer.myIsPackage = false;
    AndroidTextUtils.forEachOccurrence(strValue, '$', consumer.myPartStart, consumer);
    consumer.consume(strValue.length());
    return result.toArray(new PsiReference[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Consumer(com.intellij.util.Consumer) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

AndroidFacet (org.jetbrains.android.facet.AndroidFacet)299 Module (com.intellij.openapi.module.Module)122 VirtualFile (com.intellij.openapi.vfs.VirtualFile)73 NotNull (org.jetbrains.annotations.NotNull)61 Nullable (org.jetbrains.annotations.Nullable)51 Project (com.intellij.openapi.project.Project)39 File (java.io.File)29 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)28 PsiFile (com.intellij.psi.PsiFile)24 XmlFile (com.intellij.psi.xml.XmlFile)20 PsiElement (com.intellij.psi.PsiElement)17 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)16 XmlTag (com.intellij.psi.xml.XmlTag)16 ArrayList (java.util.ArrayList)16 Manifest (org.jetbrains.android.dom.manifest.Manifest)14 IAndroidTarget (com.android.sdklib.IAndroidTarget)13 ResourceFolderType (com.android.resources.ResourceFolderType)11 Configuration (com.android.tools.idea.configurations.Configuration)10 PsiClass (com.intellij.psi.PsiClass)10 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)10