Search in sources :

Example 1 with DeclareStyleable

use of org.jetbrains.android.dom.resources.DeclareStyleable in project android by JetBrains.

the class LocalResourceManager method findStyleableAttributesByFieldName.

public List<Attr> findStyleableAttributesByFieldName(@NotNull String fieldName) {
    int index = fieldName.lastIndexOf('_');
    // is "miny_moe".
    while (index != -1) {
        int prev = fieldName.lastIndexOf('_', index - 1);
        if (prev == -1 || Character.isUpperCase(fieldName.charAt(prev + 1))) {
            break;
        }
        index = prev;
    }
    if (index == -1) {
        return Collections.emptyList();
    }
    String styleableName = fieldName.substring(0, index);
    String attrName = fieldName.substring(index + 1);
    List<Attr> list = new ArrayList<Attr>();
    for (Pair<Resources, VirtualFile> pair : getResourceElements()) {
        final Resources res = pair.getFirst();
        for (DeclareStyleable styleable : res.getDeclareStyleables()) {
            if (styleableName.equals(styleable.getName().getValue())) {
                for (Attr attr : styleable.getAttrs()) {
                    if (attrName.equals(attr.getName().getValue())) {
                        list.add(attr);
                    }
                }
            }
        }
    }
    return list;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Resources(org.jetbrains.android.dom.resources.Resources) Attr(org.jetbrains.android.dom.resources.Attr) DeclareStyleable(org.jetbrains.android.dom.resources.DeclareStyleable)

Example 2 with DeclareStyleable

use of org.jetbrains.android.dom.resources.DeclareStyleable in project android by JetBrains.

the class AndroidGotoDeclarationHandler method getGotoDeclarationTargets.

@Override
public PsiElement[] getGotoDeclarationTargets(@Nullable PsiElement sourceElement, int offset, Editor editor) {
    if (!(sourceElement instanceof PsiIdentifier)) {
        return null;
    }
    final PsiFile file = sourceElement.getContainingFile();
    if (file == null) {
        return null;
    }
    AndroidFacet facet = AndroidFacet.getInstance(file);
    if (facet == null) {
        return null;
    }
    final PsiReferenceExpression refExp = PsiTreeUtil.getParentOfType(sourceElement, PsiReferenceExpression.class);
    if (refExp == null) {
        return null;
    }
    AndroidResourceUtil.MyReferredResourceFieldInfo info = AndroidResourceUtil.getReferredResourceOrManifestField(facet, refExp, false);
    if (info == null) {
        PsiElement parent = refExp.getParent();
        if (parent instanceof PsiReferenceExpression) {
            info = AndroidResourceUtil.getReferredResourceOrManifestField(facet, (PsiReferenceExpression) parent, false);
        }
        if (info == null) {
            parent = parent.getParent();
            if (parent instanceof PsiReferenceExpression) {
                info = AndroidResourceUtil.getReferredResourceOrManifestField(facet, (PsiReferenceExpression) parent, false);
            }
        }
    }
    if (info == null) {
        return null;
    }
    final String nestedClassName = info.getClassName();
    final String fieldName = info.getFieldName();
    final List<PsiElement> resourceList = new ArrayList<PsiElement>();
    if (info.isFromManifest()) {
        collectManifestElements(nestedClassName, fieldName, facet, resourceList);
    } else {
        final ResourceManager manager = info.isSystem() ? facet.getSystemResourceManager(false) : facet.getLocalResourceManager();
        if (manager == null) {
            return null;
        }
        manager.collectLazyResourceElements(nestedClassName, fieldName, false, refExp, resourceList);
        if (manager instanceof LocalResourceManager) {
            final LocalResourceManager lrm = (LocalResourceManager) manager;
            if (nestedClassName.equals(ResourceType.ATTR.getName())) {
                for (Attr attr : lrm.findAttrs(fieldName)) {
                    resourceList.add(attr.getName().getXmlAttributeValue());
                }
            } else if (nestedClassName.equals(ResourceType.STYLEABLE.getName())) {
                for (DeclareStyleable styleable : lrm.findStyleables(fieldName)) {
                    resourceList.add(styleable.getName().getXmlAttributeValue());
                }
                for (Attr styleable : lrm.findStyleableAttributesByFieldName(fieldName)) {
                    resourceList.add(styleable.getName().getXmlAttributeValue());
                }
            }
        }
    }
    if (resourceList.size() > 1) {
        // Sort to ensure the output is stable, and to prefer the base folders
        Collections.sort(resourceList, AndroidResourceUtil.RESOURCE_ELEMENT_COMPARATOR);
    }
    return resourceList.toArray(new PsiElement[resourceList.size()]);
}
Also used : LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) ArrayList(java.util.ArrayList) ResourceManager(org.jetbrains.android.resourceManagers.ResourceManager) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Attr(org.jetbrains.android.dom.resources.Attr) DeclareStyleable(org.jetbrains.android.dom.resources.DeclareStyleable) AndroidResourceUtil(org.jetbrains.android.util.AndroidResourceUtil)

Example 3 with DeclareStyleable

use of org.jetbrains.android.dom.resources.DeclareStyleable in project kotlin by JetBrains.

the class KotlinAndroidGotoDeclarationHandler method getGotoDeclarationTargets.

@Override
public PsiElement[] getGotoDeclarationTargets(@Nullable PsiElement sourceElement, int offset, Editor editor) {
    KtSimpleNameExpression referenceExpression = GotoResourceHelperKt.getReferenceExpression(sourceElement);
    if (referenceExpression == null) {
        return null;
    }
    AndroidFacet facet = AndroidUtilKt.getAndroidFacetForFile(referenceExpression);
    if (facet == null) {
        return null;
    }
    AndroidResourceUtil.MyReferredResourceFieldInfo info = GotoResourceHelperKt.getInfo(referenceExpression, facet);
    if (info == null)
        return null;
    String nestedClassName = info.getClassName();
    String fieldName = info.getFieldName();
    List<PsiElement> resourceList = new ArrayList<PsiElement>();
    if (info.isFromManifest()) {
        collectManifestElements(nestedClassName, fieldName, facet, resourceList);
    } else {
        ResourceManager manager = info.isSystem() ? facet.getSystemResourceManager(false) : facet.getLocalResourceManager();
        if (manager == null) {
            return null;
        }
        manager.collectLazyResourceElements(nestedClassName, fieldName, false, referenceExpression, resourceList);
        if (manager instanceof LocalResourceManager) {
            LocalResourceManager lrm = (LocalResourceManager) manager;
            if (nestedClassName.equals(ResourceType.ATTR.getName())) {
                for (Attr attr : lrm.findAttrs(fieldName)) {
                    resourceList.add(attr.getName().getXmlAttributeValue());
                }
            } else if (nestedClassName.equals(ResourceType.STYLEABLE.getName())) {
                for (DeclareStyleable styleable : lrm.findStyleables(fieldName)) {
                    resourceList.add(styleable.getName().getXmlAttributeValue());
                }
                for (Attr styleable : lrm.findStyleableAttributesByFieldName(fieldName)) {
                    resourceList.add(styleable.getName().getXmlAttributeValue());
                }
            }
        }
    }
    if (resourceList.size() > 1) {
        // Sort to ensure the output is stable, and to prefer the base folders
        Collections.sort(resourceList, AndroidResourceUtil.RESOURCE_ELEMENT_COMPARATOR);
    }
    return resourceList.toArray(new PsiElement[resourceList.size()]);
}
Also used : AndroidResourceUtil(org.jetbrains.android.util.AndroidResourceUtil) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) ArrayList(java.util.ArrayList) KtSimpleNameExpression(org.jetbrains.kotlin.psi.KtSimpleNameExpression) ResourceManager(org.jetbrains.android.resourceManagers.ResourceManager) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) PsiElement(com.intellij.psi.PsiElement) Attr(org.jetbrains.android.dom.resources.Attr) DeclareStyleable(org.jetbrains.android.dom.resources.DeclareStyleable)

Aggregations

Attr (org.jetbrains.android.dom.resources.Attr)3 DeclareStyleable (org.jetbrains.android.dom.resources.DeclareStyleable)3 ArrayList (java.util.ArrayList)2 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)2 LocalResourceManager (org.jetbrains.android.resourceManagers.LocalResourceManager)2 ResourceManager (org.jetbrains.android.resourceManagers.ResourceManager)2 AndroidResourceUtil (org.jetbrains.android.util.AndroidResourceUtil)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 Resources (org.jetbrains.android.dom.resources.Resources)1 KtSimpleNameExpression (org.jetbrains.kotlin.psi.KtSimpleNameExpression)1