Search in sources :

Example 6 with LocalResourceManager

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

the class AndroidReferenceSearchExecutor method processQuery.

@Override
public void processQuery(@NotNull ReferencesSearch.SearchParameters params, @NotNull Processor<PsiReference> consumer) {
    final PsiElement refElement = params.getElementToSearch();
    if (!(refElement instanceof PsiFile)) {
        return;
    }
    final VirtualFile vFile = ((PsiFile) refElement).getVirtualFile();
    if (vFile == null) {
        return;
    }
    LocalResourceManager manager = LocalResourceManager.getInstance(refElement);
    if (manager == null) {
        return;
    }
    String resType = manager.getFileResourceType((PsiFile) refElement);
    if (resType != null) {
        String resName = AndroidCommonUtils.getResourceName(resType, vFile.getName());
        // unless references can be found by a simple CachedBasedRefSearcher
        if (!resName.equals(vFile.getNameWithoutExtension()) && StringUtil.isNotEmpty(resName)) {
            params.getOptimizer().searchWord(resName, params.getEffectiveSearchScope(), true, refElement);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 7 with LocalResourceManager

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

the class AndroidResourceRenameResourceProcessor method prepareIdRenaming.

private static void prepareIdRenaming(XmlAttributeValue value, String newName, Map<PsiElement, String> allRenames, AndroidFacet facet) {
    LocalResourceManager manager = facet.getLocalResourceManager();
    allRenames.remove(value);
    String id = AndroidResourceUtil.getResourceNameByReferenceText(value.getValue());
    assert id != null;
    List<XmlAttributeValue> idDeclarations = manager.findIdDeclarations(id);
    for (XmlAttributeValue idDeclaration : idDeclarations) {
        // framework which looks related to elements getting modified multiple times.
        if (!ATTR_ID.equals(((XmlAttribute) idDeclaration.getParent()).getLocalName())) {
            continue;
        }
        allRenames.put(new ValueResourceElementWrapper(idDeclaration), newName);
    }
    String name = AndroidResourceUtil.getResourceNameByReferenceText(newName);
    if (name != null) {
        for (PsiField resField : AndroidResourceUtil.findIdFields(value)) {
            allRenames.put(resField, AndroidResourceUtil.getFieldNameByResourceName(name));
        }
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) LazyValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper) ValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper)

Example 8 with LocalResourceManager

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

the class Configuration method computeBestDevice.

@Nullable
private Device computeBestDevice() {
    for (Device device : myManager.getRecentDevices()) {
        String stateName = myStateName;
        if (stateName == null) {
            stateName = device.getDefaultState().getName();
        }
        State selectedState = ConfigurationFileState.getState(device, stateName);
        Module module = myManager.getModule();
        FolderConfiguration currentConfig = getFolderConfig(module, selectedState, getLocale(), getTarget());
        if (currentConfig != null) {
            if (myEditedConfig.isMatchFor(currentConfig)) {
                LocalResourceRepository resources = AppResourceRepository.getAppResources(module, true);
                if (resources != null && myFile != null) {
                    ResourceFolderType folderType = ResourceHelper.getFolderType(myFile);
                    if (folderType != null) {
                        if (ResourceFolderType.VALUES.equals(folderType)) {
                            // If it's a file in the values folder, ResourceRepository.getMatchingFiles won't work.
                            // We get instead all the available folders and check that there is one compatible.
                            LocalResourceManager resourceManager = LocalResourceManager.getInstance(module);
                            if (resourceManager != null) {
                                for (PsiFile resourceFile : resourceManager.findResourceFiles("values")) {
                                    if (myFile.equals(resourceFile.getVirtualFile()) && resourceFile.getParent() != null) {
                                        FolderConfiguration folderConfiguration = FolderConfiguration.getConfigForFolder(resourceFile.getParent().getName());
                                        if (currentConfig.isMatchFor(folderConfiguration)) {
                                            return device;
                                        }
                                    }
                                }
                            }
                        } else {
                            List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folderType);
                            if (!types.isEmpty()) {
                                ResourceType type = types.get(0);
                                List<VirtualFile> matches = resources.getMatchingFiles(myFile, type, currentConfig);
                                if (matches.contains(myFile)) {
                                    return device;
                                }
                            }
                        }
                    } else if ("Kotlin".equals(myFile.getFileType().getName())) {
                        return device;
                    } else if (myFile.equals(myManager.getProject().getProjectFile())) {
                        // takes care of correct device selection for Theme Editor
                        return device;
                    }
                }
            }
        }
    }
    return myManager.getDefaultDevice();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) Device(com.android.sdklib.devices.Device) State(com.android.sdklib.devices.State) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with LocalResourceManager

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

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