Search in sources :

Example 56 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue 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 57 with XmlAttributeValue

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

the class AndroidResourceRenameResourceProcessor method prepareResourceFieldRenaming.

private static void prepareResourceFieldRenaming(PsiField field, String newName, Map<PsiElement, String> allRenames) {
    new RenameJavaVariableProcessor().prepareRenaming(field, newName, allRenames);
    List<PsiElement> resources = AndroidResourceUtil.findResourcesByField(field);
    PsiElement res = resources.get(0);
    final String newResName;
    if (res instanceof PsiFile) {
        // Resource comes from XML file, don't need to suggest change underscores to dots
        newResName = newName;
    } else if (res instanceof XmlAttributeValue) {
        newResName = getResourceName(field.getProject(), newName, ((XmlAttributeValue) res).getValue());
    } else {
        // AndroidResourceUtil.findResourcesByField supposed to return a list of PsiElements that are
        // either PsiFile or XmlAttributeValue. Previous version of this code doesn't handle other
        // possibilities at all and would crash with ClassCastException, having an explicit error message
        // seems to be a slightly better option.
        Logger.getInstance(AndroidResourceRenameResourceProcessor.class).error(String.format("%s: res is neither PsiFile nor XmlAttributeValue", AndroidResourceRenameResourceProcessor.class.getSimpleName()));
        newResName = newName;
    }
    for (PsiElement resource : resources) {
        if (resource instanceof PsiFile) {
            PsiFile file = (PsiFile) resource;
            String extension = FileUtilRt.getExtension(file.getName());
            allRenames.put(resource, newResName + '.' + extension);
        } else if (resource instanceof XmlAttributeValue) {
            XmlAttributeValue value = (XmlAttributeValue) resource;
            final String s = AndroidResourceUtil.isIdDeclaration(value) ? NEW_ID_PREFIX + newResName : newResName;
            allRenames.put(new ValueResourceElementWrapper(value), s);
            // we have to rename not just R.styleable.Foo but the also R.styleable.Foo_* attributes
            if (value.getParent() instanceof XmlAttribute) {
                XmlAttribute parent = (XmlAttribute) value.getParent();
                XmlTag tag = parent.getParent();
                if (tag.getName().equals(TAG_DECLARE_STYLEABLE)) {
                    AndroidFacet facet = AndroidFacet.getInstance(tag);
                    String oldName = tag.getAttributeValue(ATTR_NAME);
                    if (facet != null && oldName != null) {
                        for (XmlTag attr : tag.getSubTags()) {
                            if (attr.getName().equals(TAG_ATTR)) {
                                String name = attr.getAttributeValue(ATTR_NAME);
                                if (name != null) {
                                    String oldAttributeName = oldName + '_' + name;
                                    PsiField[] fields = AndroidResourceUtil.findResourceFields(facet, STYLEABLE.getName(), oldAttributeName, true);
                                    if (fields.length > 0) {
                                        String newAttributeName = newName + '_' + name;
                                        for (PsiField f : fields) {
                                            allRenames.put(f, newAttributeName);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : RenameJavaVariableProcessor(com.intellij.refactoring.rename.RenameJavaVariableProcessor) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) LazyValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper) ValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper) XmlTag(com.intellij.psi.xml.XmlTag)

Example 58 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project idea-php-typo3-plugin by cedricziel.

the class TranslationFoldingBuilder method buildFoldRegions.

@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) {
    FoldingGroup group = FoldingGroup.newGroup("TYPO3Translation");
    List<FoldingDescriptor> descriptors = new ArrayList<>();
    Collection<StringLiteralExpression> literalExpressions = PsiTreeUtil.findChildrenOfType(root, StringLiteralExpression.class);
    for (final StringLiteralExpression literalExpression : literalExpressions) {
        String value = literalExpression.getContents();
        if (value.startsWith("LLL:")) {
            Project project = literalExpression.getProject();
            final List<StubTranslation> properties = TranslationIndex.findById(project, value);
            StubTranslation defaultTranslation = findDefaultTranslationFromVariants(properties);
            if (defaultTranslation != null) {
                TextRange foldingRange = new TextRange(literalExpression.getTextRange().getStartOffset() + 1, literalExpression.getTextRange().getEndOffset() - 1);
                descriptors.add(new FoldingDescriptor(literalExpression.getNode(), foldingRange, group) {

                    @Nullable
                    @Override
                    public String getPlaceholderText() {
                        PsiElement[] definitionElements = TranslationUtil.findDefinitionElements(project, value);
                        for (PsiElement definitionElement : definitionElements) {
                            if (definitionElement instanceof XmlTag) {
                                if (((XmlTag) definitionElement).getName().equals("label")) {
                                    return ((XmlTag) definitionElement).getValue().getTrimmedText();
                                }
                                for (XmlTag xmlTag : ((XmlTag) definitionElement).getSubTags()) {
                                    if (xmlTag.getName().equals("source")) {
                                        return xmlTag.getValue().getTrimmedText();
                                    }
                                }
                            }
                            if (definitionElement instanceof XmlAttributeValue) {
                                if (((XmlTag) definitionElement.getParent().getParent()).getName().equals("label")) {
                                    return ((XmlTag) definitionElement).getValue().getTrimmedText();
                                }
                                for (XmlTag xmlTag : ((XmlTag) definitionElement.getParent().getParent()).getSubTags()) {
                                    if (xmlTag.getName().equals("source")) {
                                        return xmlTag.getValue().getTrimmedText();
                                    }
                                }
                            }
                        }
                        return null;
                    }
                });
            }
        }
    }
    return descriptors.toArray(new FoldingDescriptor[descriptors.size()]);
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) FoldingGroup(com.intellij.openapi.editor.FoldingGroup) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Project(com.intellij.openapi.project.Project) Nullable(org.jetbrains.annotations.Nullable) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 59 with XmlAttributeValue

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

the class AndroidColorAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (element instanceof XmlTag) {
        XmlTag tag = (XmlTag) element;
        String tagName = tag.getName();
        if ((ResourceType.COLOR.getName().equals(tagName) || ResourceType.DRAWABLE.getName().equals(tagName) || ResourceType.MIPMAP.getName().equals(tagName))) {
            DomElement domElement = DomManager.getDomManager(element.getProject()).getDomElement(tag);
            if (domElement instanceof ResourceElement || ApplicationManager.getApplication().isUnitTestMode()) {
                String value = tag.getValue().getText().trim();
                annotateXml(element, holder, value);
            }
        } else if (TAG_ITEM.equals(tagName)) {
            XmlTagValue value = tag.getValue();
            String text = value.getText();
            annotateXml(element, holder, text);
        }
    } else if (element instanceof XmlAttributeValue) {
        XmlAttributeValue v = (XmlAttributeValue) element;
        String value = v.getValue();
        if (value == null || value.isEmpty()) {
            return;
        }
        annotateXml(element, holder, value);
    } else if (element instanceof PsiReferenceExpression) {
        ResourceReferenceType referenceType = AndroidPsiUtils.getResourceReferenceType(element);
        if (referenceType != ResourceReferenceType.NONE) {
            // (isResourceReference will return true for both "R.drawable.foo" and the foo literal leaf in the
            // same expression, which would result in both elements getting annotated and the icon showing up
            // in the gutter twice. Instead we only count the outer one.
            ResourceType type = AndroidPsiUtils.getResourceType(element);
            if (type == ResourceType.COLOR || type == ResourceType.DRAWABLE || type == ResourceType.MIPMAP) {
                String name = AndroidPsiUtils.getResourceName(element);
                annotateResourceReference(type, holder, element, name, referenceType == ResourceReferenceType.FRAMEWORK);
            }
        }
    }
}
Also used : ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) DomElement(com.intellij.util.xml.DomElement) XmlTagValue(com.intellij.psi.xml.XmlTagValue) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) ResourceReferenceType(com.android.tools.idea.AndroidPsiUtils.ResourceReferenceType) ResourceType(com.android.resources.ResourceType) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlTag(com.intellij.psi.xml.XmlTag)

Example 60 with XmlAttributeValue

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

the class AndroidGotoRelatedProvider method findDeclarationInManifest.

@Nullable
private static GotoRelatedItem findDeclarationInManifest(@NotNull PsiClass psiClass) {
    final AndroidAttributeValue<PsiClass> domAttrValue = AndroidDomUtil.findComponentDeclarationInManifest(psiClass);
    if (domAttrValue == null) {
        return null;
    }
    final XmlAttributeValue attrValue = domAttrValue.getXmlAttributeValue();
    return attrValue != null ? new MyGotoManifestItem(attrValue) : null;
}
Also used : XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)127 XmlAttribute (com.intellij.psi.xml.XmlAttribute)57 XmlTag (com.intellij.psi.xml.XmlTag)50 NotNull (org.jetbrains.annotations.NotNull)38 PsiElement (com.intellij.psi.PsiElement)31 Nullable (org.jetbrains.annotations.Nullable)24 PsiReference (com.intellij.psi.PsiReference)20 XmlFile (com.intellij.psi.xml.XmlFile)19 TextRange (com.intellij.openapi.util.TextRange)18 VirtualFile (com.intellij.openapi.vfs.VirtualFile)15 ArrayList (java.util.ArrayList)13 Project (com.intellij.openapi.project.Project)12 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)10 DomElement (com.intellij.util.xml.DomElement)9 PsiFile (com.intellij.psi.PsiFile)8 LazyValueResourceElementWrapper (org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper)7 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)5 Module (com.intellij.openapi.module.Module)5 ValueResourceElementWrapper (org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper)5