Search in sources :

Example 66 with XmlAttribute

use of com.intellij.psi.xml.XmlAttribute in project android by JetBrains.

the class AttributeProcessingUtil method registerExistingAttributes.

@NotNull
private static Set<XmlName> registerExistingAttributes(@NotNull AndroidFacet facet, @NotNull XmlTag tag, @NotNull AndroidDomElement element, @NotNull AttributeProcessor callback) {
    final Set<XmlName> result = new HashSet<>();
    XmlAttribute[] attrs = tag.getAttributes();
    for (XmlAttribute attr : attrs) {
        String localName = attr.getLocalName();
        if (!localName.endsWith(CompletionUtil.DUMMY_IDENTIFIER_TRIMMED)) {
            if (!"xmlns".equals(attr.getNamespacePrefix())) {
                AttributeDefinition attrDef = AndroidDomUtil.getAttributeDefinition(facet, attr);
                if (attrDef != null) {
                    String namespace = attr.getNamespace();
                    result.add(new XmlName(attr.getLocalName(), attr.getNamespace()));
                    registerAttribute(attrDef, null, namespace.length() > 0 ? namespace : null, element, callback);
                }
            }
        }
    }
    return result;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlName(com.intellij.util.xml.XmlName) NotNull(org.jetbrains.annotations.NotNull)

Example 67 with XmlAttribute

use of com.intellij.psi.xml.XmlAttribute in project android by JetBrains.

the class MigrateDrawableToMipmapFix method apply.

@Override
public void apply(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.Context context) {
    Project project = startElement.getProject();
    AndroidFacet facet = AndroidFacet.getInstance(startElement);
    if (facet == null) {
        return;
    }
    final List<PsiFile> bitmaps = Lists.newArrayList();
    final Set<PsiElement> references = Sets.newHashSet();
    GlobalSearchScope useScope = GlobalSearchScope.projectScope(project);
    ProjectResourceRepository projectResources = facet.getProjectResources(true);
    List<ResourceItem> resourceItems = projectResources.getResourceItem(myUrl.type, myUrl.name);
    if (resourceItems != null) {
        for (ResourceItem item : resourceItems) {
            PsiFile file = LocalResourceRepository.getItemPsiFile(project, item);
            if (file == null) {
                continue;
            }
            bitmaps.add(file);
            Iterable<PsiReference> allReferences = SearchUtils.findAllReferences(file, useScope);
            for (PsiReference next : allReferences) {
                PsiElement element = next.getElement();
                if (element != null) {
                    references.add(element);
                }
            }
        }
    }
    PsiField[] resourceFields = AndroidResourceUtil.findResourceFields(facet, ResourceType.DRAWABLE.getName(), myUrl.name, true);
    if (resourceFields.length == 1) {
        Iterable<PsiReference> allReferences = SearchUtils.findAllReferences(resourceFields[0], useScope);
        for (PsiReference next : allReferences) {
            PsiElement element = next.getElement();
            if (element != null) {
                references.add(element);
            }
        }
    }
    Set<PsiFile> applicableFiles = Sets.newHashSet();
    applicableFiles.addAll(bitmaps);
    for (PsiElement element : references) {
        PsiFile containingFile = element.getContainingFile();
        if (containingFile != null) {
            applicableFiles.add(containingFile);
        }
    }
    WriteCommandAction<Void> action = new WriteCommandAction<Void>(project, "Migrate Drawable to Bitmap", applicableFiles.toArray(new PsiFile[applicableFiles.size()])) {

        @Override
        protected void run(@NotNull Result<Void> result) throws Throwable {
            // Move each drawable bitmap from drawable-my-qualifiers to bitmap-my-qualifiers
            for (PsiFile bitmap : bitmaps) {
                VirtualFile file = bitmap.getVirtualFile();
                if (file == null) {
                    continue;
                }
                VirtualFile parent = file.getParent();
                if (parent == null) {
                    // shouldn't happen for bitmaps found in the resource repository
                    continue;
                }
                if (file.getFileType() == StdFileTypes.XML && parent.getName().startsWith(FD_RES_VALUES)) {
                    // Resource alias rather than an actual drawable XML file: update the type reference instead
                    XmlFile xmlFile = (XmlFile) bitmap;
                    XmlTag root = xmlFile.getRootTag();
                    if (root != null) {
                        for (XmlTag item : root.getSubTags()) {
                            String name = item.getAttributeValue(ATTR_NAME);
                            if (myUrl.name.equals(name)) {
                                if (ResourceType.DRAWABLE.getName().equals(item.getName())) {
                                    item.setName(ResourceType.MIPMAP.getName());
                                } else if (ResourceType.DRAWABLE.getName().equals(item.getAttributeValue(ATTR_TYPE))) {
                                    item.setAttribute(ATTR_TYPE, ResourceType.MIPMAP.getName());
                                }
                            }
                        }
                    }
                    // Don't move the file
                    continue;
                }
                VirtualFile res = parent.getParent();
                if (res == null) {
                    // shouldn't happen for bitmaps found in the resource repository
                    continue;
                }
                FolderConfiguration configuration = FolderConfiguration.getConfigForFolder(parent.getName());
                if (configuration == null) {
                    continue;
                }
                String targetFolderName = configuration.getFolderName(ResourceFolderType.MIPMAP);
                VirtualFile targetFolder = res.findChild(targetFolderName);
                if (targetFolder == null) {
                    targetFolder = res.createChildDirectory(this, targetFolderName);
                }
                file.move(this, targetFolder);
            }
            // Update references
            for (PsiElement reference : references) {
                if (reference instanceof XmlAttributeValue) {
                    // Convert @drawable/foo references to @mipmap/foo
                    XmlAttributeValue value = (XmlAttributeValue) reference;
                    XmlAttribute attribute = (XmlAttribute) value.getParent();
                    attribute.setValue(ResourceUrl.create(ResourceType.MIPMAP, myUrl.name, false, false).toString());
                } else if (reference instanceof PsiReferenceExpression) {
                    // Convert R.drawable.foo references to R.mipmap.foo
                    PsiReferenceExpression inner = (PsiReferenceExpression) reference;
                    PsiExpression qualifier = inner.getQualifierExpression();
                    if (qualifier instanceof PsiReferenceExpression) {
                        PsiReferenceExpression outer = (PsiReferenceExpression) qualifier;
                        if (outer.getReferenceNameElement() instanceof PsiIdentifier) {
                            PsiIdentifier identifier = (PsiIdentifier) outer.getReferenceNameElement();
                            if (ResourceType.DRAWABLE.getName().equals(identifier.getText())) {
                                Project project = reference.getProject();
                                final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
                                PsiIdentifier newIdentifier = elementFactory.createIdentifier(ResourceType.MIPMAP.getName());
                                identifier.replace(newIdentifier);
                            }
                        }
                    }
                }
            }
        }
    };
    action.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlAttribute(com.intellij.psi.xml.XmlAttribute) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ProjectResourceRepository(com.android.tools.idea.res.ProjectResourceRepository) XmlFile(com.intellij.psi.xml.XmlFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Project(com.intellij.openapi.project.Project) ResourceItem(com.android.ide.common.res2.ResourceItem) XmlTag(com.intellij.psi.xml.XmlTag)

Example 68 with XmlAttribute

use of com.intellij.psi.xml.XmlAttribute in project android by JetBrains.

the class SuppressLintIntentionAction method addSuppressAttribute.

private static void addSuppressAttribute(final Project project, final XmlFile file, final XmlTag element, final String id) throws IncorrectOperationException {
    XmlAttribute attribute = element.getAttribute(ATTR_IGNORE, TOOLS_URI);
    String value;
    if (attribute == null) {
        value = id;
    } else {
        List<String> ids = new ArrayList<String>();
        for (String existing : Splitter.on(',').trimResults().split(attribute.getValue())) {
            if (!existing.equals(id)) {
                ids.add(existing);
            }
        }
        ids.add(id);
        Collections.sort(ids);
        value = Joiner.on(',').join(ids);
    }
    AndroidResourceUtil.ensureNamespaceImported(file, TOOLS_URI, null);
    element.setAttribute(ATTR_IGNORE, TOOLS_URI, value);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) ArrayList(java.util.ArrayList)

Example 69 with XmlAttribute

use of com.intellij.psi.xml.XmlAttribute in project android by JetBrains.

the class RenameXmlTagQuickFix method apply.

@Override
public void apply(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.Context context) {
    XmlTag currentTag = PsiTreeUtil.getParentOfType(startElement, XmlTag.class, false);
    if (currentTag == null) {
        return;
    }
    XmlTag parentTag = currentTag.getParentTag();
    if (parentTag == null) {
        return;
    }
    XmlTag newTag = parentTag.createChildTag(myNewName, null, currentTag.isEmpty() ? null : currentTag.getValue().getText(), true);
    // copy attributes.
    for (XmlAttribute attr : currentTag.getAttributes()) {
        newTag.setAttribute(attr.getLocalName(), attr.getNamespace(), attr.getValue());
    }
    currentTag.replace(newTag);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlTag(com.intellij.psi.xml.XmlTag)

Example 70 with XmlAttribute

use of com.intellij.psi.xml.XmlAttribute in project android by JetBrains.

the class ValueResourceElementWrapper method setName.

@Override
@Nullable
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
    if (AndroidResourceUtil.isIdDeclaration(myWrappee)) {
        XmlAttribute attribute = (XmlAttribute) myWrappee.getParent();
        attribute.setValue(name);
    } else {
        // then it is a value resource
        XmlTag tag = PsiTreeUtil.getParentOfType(myWrappee, XmlTag.class);
        DomElement domElement = DomManager.getDomManager(getProject()).getDomElement(tag);
        assert domElement instanceof ResourceElement;
        ResourceElement resElement = (ResourceElement) domElement;
        resElement.getName().setValue(name);
    }
    return null;
}
Also used : ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) XmlAttribute(com.intellij.psi.xml.XmlAttribute) DomElement(com.intellij.util.xml.DomElement) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

XmlAttribute (com.intellij.psi.xml.XmlAttribute)220 XmlTag (com.intellij.psi.xml.XmlTag)116 PsiElement (com.intellij.psi.PsiElement)60 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)57 NotNull (org.jetbrains.annotations.NotNull)44 XmlFile (com.intellij.psi.xml.XmlFile)37 Nullable (org.jetbrains.annotations.Nullable)27 Project (com.intellij.openapi.project.Project)25 VirtualFile (com.intellij.openapi.vfs.VirtualFile)20 TextRange (com.intellij.openapi.util.TextRange)18 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)18 ArrayList (java.util.ArrayList)18 PsiFile (com.intellij.psi.PsiFile)17 PsiReference (com.intellij.psi.PsiReference)17 IncorrectOperationException (com.intellij.util.IncorrectOperationException)10 DomElement (com.intellij.util.xml.DomElement)10 Result (com.intellij.openapi.application.Result)9 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)9 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)8 XmlElement (com.intellij.psi.xml.XmlElement)6