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