Search in sources :

Example 36 with ResourceFolderType

use of com.android.resources.ResourceFolderType in project android by JetBrains.

the class CreateResourceFileAction method getActionByDir.

private CreateTypedResourceFileAction getActionByDir(PsiDirectory directory) {
    String baseDirName = directory.getName();
    ResourceFolderType folderType = ResourceFolderType.getFolderType(baseDirName);
    if (folderType == null) {
        return null;
    }
    return mySubactions.get(folderType);
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType)

Example 37 with ResourceFolderType

use of com.android.resources.ResourceFolderType in project android by JetBrains.

the class CreateResourceFileDialogBase method setupSubActions.

protected String setupSubActions(@NotNull Collection<CreateTypedResourceFileAction> actions, @NotNull TemplateKindCombo resourceTypeCombo, @Nullable ResourceFolderType folderType) {
    CreateTypedResourceFileAction[] actionArray = actions.toArray(new CreateTypedResourceFileAction[actions.size()]);
    Arrays.sort(actionArray, (a1, a2) -> a1.toString().compareTo(a2.toString()));
    String selectedTemplate = null;
    for (CreateTypedResourceFileAction action : actionArray) {
        ResourceFolderType resType = action.getResourceFolderType();
        assert !myResType2ActionMap.containsKey(resType);
        myResType2ActionMap.put(resType, action);
        resourceTypeCombo.addItem(action.toString(), null, resType.getName());
        if (folderType != null && folderType == resType) {
            selectedTemplate = resType.getName();
        }
    }
    return selectedTemplate;
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType)

Example 38 with ResourceFolderType

use of com.android.resources.ResourceFolderType in project android by JetBrains.

the class UnusedResourcesProcessor method computeUnusedDeclarationElements.

@NotNull
private List<PsiElement> computeUnusedDeclarationElements(Map<Issue, Map<File, List<ProblemData>>> map) {
    final List<PsiElement> elements = Lists.newArrayList();
    // Make sure lint didn't put extra issues into the map
    for (Issue issue : Lists.newArrayList(map.keySet())) {
        if (issue != UnusedResourceDetector.ISSUE && issue != UnusedResourceDetector.ISSUE_IDS) {
            map.remove(issue);
        }
    }
    ApplicationManager.getApplication().assertReadAccessAllowed();
    PsiManager manager = PsiManager.getInstance(myProject);
    for (Issue issue : new Issue[] { UnusedResourceDetector.ISSUE, UnusedResourceDetector.ISSUE_IDS }) {
        Map<File, List<ProblemData>> fileListMap = map.get(issue);
        if (fileListMap != null && !fileListMap.isEmpty()) {
            Map<File, PsiFile> files = Maps.newHashMap();
            for (File file : fileListMap.keySet()) {
                VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByIoFile(file);
                if (virtualFile != null) {
                    if (!virtualFile.isDirectory()) {
                        // Gradle model errors currently don't have source positions
                        PsiFile psiFile = manager.findFile(virtualFile);
                        if (psiFile != null) {
                            files.put(file, psiFile);
                        }
                    }
                }
            }
            if (!files.isEmpty()) {
                for (File file : files.keySet()) {
                    PsiFile psiFile = files.get(file);
                    if (psiFile == null) {
                        // where we only had the project directory as the location from the Gradle model
                        continue;
                    }
                    if (!CommonRefactoringUtil.checkReadOnlyStatus(myProject, psiFile)) {
                        continue;
                    }
                    List<ProblemData> problems = fileListMap.get(file);
                    if (psiFile.getFileType().isBinary()) {
                        // Delete the whole file
                        if (matchesFilter(fileListMap, file)) {
                            elements.add(psiFile);
                        }
                    } else {
                        ResourceFolderType folderType = ResourceHelper.getFolderType(psiFile);
                        if (folderType == null) {
                            // file; see for example http://b.android.com/220069.)
                            if (psiFile.getFileType() == GroovyFileType.GROOVY_FILE_TYPE && psiFile instanceof GroovyFile) {
                                ((GroovyFile) psiFile).accept(new GroovyRecursiveElementVisitor() {

                                    @Override
                                    public void visitApplicationStatement(GrApplicationStatement applicationStatement) {
                                        super.visitApplicationStatement(applicationStatement);
                                        PsiMethod method = applicationStatement.resolveMethod();
                                        if (method != null && method.getName().equals("resValue")) {
                                            GrExpression[] args = applicationStatement.getArgumentList().getExpressionArguments();
                                            if (args.length >= 3) {
                                                Object typeString = GroovyConstantExpressionEvaluator.evaluate(args[0]);
                                                Object nameString = GroovyConstantExpressionEvaluator.evaluate(args[1]);
                                                // See if this is one of the unused resources
                                                if (typeString != null && nameString != null) {
                                                    List<ProblemData> problems = fileListMap.get(VfsUtilCore.virtualToIoFile(psiFile.getVirtualFile()));
                                                    if (problems != null) {
                                                        for (ProblemData problem : problems) {
                                                            String unusedResource = UnusedResourceDetector.getUnusedResource(problem.getMessage(), TextFormat.RAW);
                                                            if (unusedResource != null && unusedResource.equals(SdkConstants.R_PREFIX + typeString + '.' + nameString)) {
                                                                elements.add(applicationStatement);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                });
                            }
                            continue;
                        }
                        if (folderType != ResourceFolderType.VALUES) {
                            // also being deleted as unused
                            if (issue == UnusedResourceDetector.ISSUE_IDS) {
                                Map<File, List<ProblemData>> m = map.get(UnusedResourceDetector.ISSUE);
                                if (m != null && m.containsKey(file)) {
                                    // Yes - skip
                                    continue;
                                }
                                // Delete ranges within the file
                                addElementsInFile(elements, psiFile, problems);
                            } else {
                                // Unused non-value resource file: Delete the whole file
                                if (matchesFilter(fileListMap, file)) {
                                    elements.add(psiFile);
                                }
                            }
                        } else {
                            addElementsInFile(elements, psiFile, problems);
                        }
                    }
                }
            }
        }
    }
    return elements;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Issue(com.android.tools.lint.detector.api.Issue) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) ProblemData(org.jetbrains.android.inspections.lint.ProblemData) ResourceFolderType(com.android.resources.ResourceFolderType) List(java.util.List) XmlFile(com.intellij.psi.xml.XmlFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) File(java.io.File) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) NotNull(org.jetbrains.annotations.NotNull)

Example 39 with ResourceFolderType

use of com.android.resources.ResourceFolderType in project android by JetBrains.

the class ResourceMergerTreeStructureProvider method mergeResourceDirectories.

private static Collection<AbstractTreeNode> mergeResourceDirectories(Collection<AbstractTreeNode> children, ViewSettings settings) {
    List<AbstractTreeNode> result = new ArrayList<AbstractTreeNode>();
    Map<ResourceFolderType, ResourceDirectoryNode> resourceDirectories = new HashMap<ResourceFolderType, ResourceDirectoryNode>();
    for (AbstractTreeNode child : children) {
        if (!(child instanceof PsiDirectoryNode)) {
            result.add(child);
            continue;
        }
        PsiDirectoryNode directoryNode = (PsiDirectoryNode) child;
        PsiDirectory directory = directoryNode.getValue();
        ResourceFolderType type = ResourceFolderType.getFolderType(directory.getName());
        if (type == null) {
            result.add(child);
            continue;
        }
        ResourceDirectoryNode node = resourceDirectories.get(type);
        if (node == null || !directory.getName().contains("-")) {
            node = new ResourceDirectoryNode(directoryNode.getProject(), directoryNode, settings);
            resourceDirectories.put(type, node);
        }
    }
    for (ResourceDirectoryNode node : resourceDirectories.values()) {
        node.collectChildren();
        result.add(node);
    }
    return result;
}
Also used : HashMap(com.intellij.util.containers.HashMap) ResourceFolderType(com.android.resources.ResourceFolderType) PsiDirectory(com.intellij.psi.PsiDirectory) ArrayList(java.util.ArrayList) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) PsiDirectoryNode(com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode)

Example 40 with ResourceFolderType

use of com.android.resources.ResourceFolderType in project android by JetBrains.

the class AndroidEditorTitleProvider method getEditorTabTitle.

@Nullable
@Override
public String getEditorTabTitle(Project project, VirtualFile file) {
    if (DumbService.isDumb(project)) {
        return null;
    }
    if (file.getFileType() != StdFileTypes.XML) {
        return null;
    }
    // Resource file?
    if (file.getName().equals(FN_ANDROID_MANIFEST_XML)) {
        return null;
    }
    VirtualFile parent = file.getParent();
    if (parent == null) {
        return null;
    }
    String parentName = parent.getName();
    int index = parentName.indexOf('-');
    if (index == -1 || index == parentName.length() - 1) {
        return null;
    }
    ResourceFolderType folderType = ResourceFolderType.getFolderType(parentName);
    if (folderType == null) {
        return null;
    }
    return parentName.substring(index + 1) + File.separator + file.getPresentableName();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceFolderType(com.android.resources.ResourceFolderType) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ResourceFolderType (com.android.resources.ResourceFolderType)46 VirtualFile (com.intellij.openapi.vfs.VirtualFile)12 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)10 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)10 NotNull (org.jetbrains.annotations.NotNull)10 ResourceType (com.android.resources.ResourceType)8 XmlFile (com.intellij.psi.xml.XmlFile)8 File (java.io.File)8 PsiFile (com.intellij.psi.PsiFile)7 Nullable (org.jetbrains.annotations.Nullable)7 PsiDirectory (com.intellij.psi.PsiDirectory)6 Module (com.intellij.openapi.module.Module)5 XmlTag (com.intellij.psi.xml.XmlTag)5 Configuration (com.android.tools.idea.configurations.Configuration)3 PsiElement (com.intellij.psi.PsiElement)3 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)2 VersionQualifier (com.android.ide.common.resources.configuration.VersionQualifier)2 HtmlBuilder (com.android.utils.HtmlBuilder)2 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)2 Project (com.intellij.openapi.project.Project)2