Search in sources :

Example 1 with LocalResourceManager

use of org.jetbrains.android.resourceManagers.LocalResourceManager in project android by JetBrains.

the class ResourceNameConverter method getStyleNameVariants.

private static Collection<String> getStyleNameVariants(ConvertContext context, GenericAttributeValue element) {
    final Module module = context.getModule();
    if (module == null) {
        return Collections.emptyList();
    }
    final LocalResourceManager manager = LocalResourceManager.getInstance(module);
    if (manager == null) {
        return Collections.emptyList();
    }
    final Collection<String> styleNames = manager.getResourceNames(ResourceType.STYLE);
    final List<String> result = new ArrayList<>();
    final String currentValue = element.getStringValue();
    for (String name : styleNames) {
        if (currentValue == null || !currentValue.startsWith(name)) {
            result.add(name + '.');
        }
    }
    return result;
}
Also used : LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) ArrayList(java.util.ArrayList) Module(com.intellij.openapi.module.Module)

Example 2 with LocalResourceManager

use of org.jetbrains.android.resourceManagers.LocalResourceManager in project android by JetBrains.

the class AndroidResourceRenameResourceProcessor method getResourceName.

/** Looks up the resource name for the given refactored element. Uses the same
   * instanceof chain checkups as is done in {@link #canProcessElement} */
@Nullable
private static String getResourceName(PsiElement originalElement) {
    PsiElement element = LazyValueResourceElementWrapper.computeLazyElement(originalElement);
    if (element == null) {
        return null;
    }
    if (element instanceof PsiFile) {
        PsiFile file = (PsiFile) element;
        LocalResourceManager manager = LocalResourceManager.getInstance(element);
        if (manager != null) {
            String type = manager.getFileResourceType(file);
            if (type != null) {
                String name = file.getName();
                return AndroidCommonUtils.getResourceName(type, name);
            }
        }
        return LintUtils.getBaseName(file.getName());
    } else if (element instanceof PsiField) {
        PsiField field = (PsiField) element;
        return field.getName();
    } else if (element instanceof XmlAttributeValue) {
        if (AndroidResourceUtil.isIdDeclaration((XmlAttributeValue) element)) {
            return AndroidResourceUtil.getResourceNameByReferenceText(((XmlAttributeValue) element).getValue());
        }
        XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
        if (tag != null) {
            DomElement domElement = DomManager.getDomManager(tag.getProject()).getDomElement(tag);
            if (domElement instanceof ResourceElement) {
                return ((ResourceElement) domElement).getName().getValue();
            }
        }
    }
    return null;
}
Also used : ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) DomElement(com.intellij.util.xml.DomElement) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with LocalResourceManager

use of org.jetbrains.android.resourceManagers.LocalResourceManager in project android by JetBrains.

the class AndroidResourceRenameResourceProcessor method prepareCustomViewRenaming.

private static void prepareCustomViewRenaming(PsiClass cls, String newName, Map<PsiElement, String> allRenames, AndroidFacet facet) {
    AppResourceRepository appResources = AppResourceRepository.getAppResources(facet, true);
    String oldName = cls.getName();
    if (appResources.hasResourceItem(DECLARE_STYLEABLE, oldName)) {
        LocalResourceManager manager = facet.getLocalResourceManager();
        for (PsiElement element : manager.findResourcesByFieldName(STYLEABLE.getName(), oldName)) {
            if (element instanceof XmlAttributeValue) {
                if (element.getParent() instanceof XmlAttribute) {
                    XmlTag tag = ((XmlAttribute) element.getParent()).getParent();
                    String tagName = tag.getName();
                    if (tagName.equals(TAG_DECLARE_STYLEABLE)) {
                        // Rename main styleable field
                        for (PsiField field : AndroidResourceUtil.findResourceFields(facet, STYLEABLE.getName(), oldName, false)) {
                            String escaped = AndroidResourceUtil.getFieldNameByResourceName(newName);
                            allRenames.put(field, escaped);
                        }
                        // Rename dependent attribute fields
                        PsiField[] styleableFields = AndroidResourceUtil.findStyleableAttributeFields(tag, false);
                        if (styleableFields.length > 0) {
                            for (PsiField resField : styleableFields) {
                                String fieldName = resField.getName();
                                String newAttributeName;
                                if (fieldName.startsWith(oldName)) {
                                    newAttributeName = newName + fieldName.substring(oldName.length());
                                } else {
                                    newAttributeName = oldName;
                                }
                                String escaped = AndroidResourceUtil.getFieldNameByResourceName(newAttributeName);
                                allRenames.put(resField, escaped);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlTag(com.intellij.psi.xml.XmlTag)

Example 4 with LocalResourceManager

use of org.jetbrains.android.resourceManagers.LocalResourceManager in project android by JetBrains.

the class CreateResourceDialogUtils method findResourceDirectory.

@Nullable
public static PsiDirectory findResourceDirectory(@NotNull DataContext dataContext) {
    // Look at the set of selected files and see if one *specific* resource directory is implied (selected, or a parent
    // of all selected nodes); if so, use it; otherwise return null.
    //
    // In the Android Project View we don't want to do this, since there is only ever a single "res" node,
    // even when you have other overlays.
    // If you're in the Android View, we want to ask you not just the filename but also let you
    // create other resource folder configurations
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project != null) {
        AbstractProjectViewPane pane = ProjectView.getInstance(project).getCurrentProjectViewPane();
        if (pane instanceof AndroidProjectViewPane) {
            return null;
        }
    }
    VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(dataContext);
    if (file != null) {
        // See if it's inside a res folder (or is equal to a resource folder)
        Module module = LangDataKeys.MODULE.getData(dataContext);
        if (module != null) {
            LocalResourceManager manager = LocalResourceManager.getInstance(module);
            if (manager != null) {
                VirtualFile resFolder = getResFolderParent(manager, file);
                if (resFolder != null) {
                    return AndroidPsiUtils.getPsiDirectorySafely(module.getProject(), resFolder);
                }
            }
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) AndroidProjectViewPane(com.android.tools.idea.navigator.AndroidProjectViewPane) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) Module(com.intellij.openapi.module.Module) AbstractProjectViewPane(com.intellij.ide.projectView.impl.AbstractProjectViewPane) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with LocalResourceManager

use of org.jetbrains.android.resourceManagers.LocalResourceManager 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

LocalResourceManager (org.jetbrains.android.resourceManagers.LocalResourceManager)9 Module (com.intellij.openapi.module.Module)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)3 ArrayList (java.util.ArrayList)3 Nullable (org.jetbrains.annotations.Nullable)3 PsiElement (com.intellij.psi.PsiElement)2 PsiFile (com.intellij.psi.PsiFile)2 XmlAttribute (com.intellij.psi.xml.XmlAttribute)2 XmlTag (com.intellij.psi.xml.XmlTag)2 Attr (org.jetbrains.android.dom.resources.Attr)2 DeclareStyleable (org.jetbrains.android.dom.resources.DeclareStyleable)2 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)2 ResourceManager (org.jetbrains.android.resourceManagers.ResourceManager)2 AndroidResourceUtil (org.jetbrains.android.util.AndroidResourceUtil)2 Device (com.android.sdklib.devices.Device)1 State (com.android.sdklib.devices.State)1 AndroidProjectViewPane (com.android.tools.idea.navigator.AndroidProjectViewPane)1 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)1 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)1