Search in sources :

Example 66 with XmlAttributeValue

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

the class DataBindingUtil method getBindingExprDefault.

@Nullable
public static String getBindingExprDefault(@NotNull XmlAttribute psiAttribute) {
    XmlAttributeValue attrValue = psiAttribute.getValueElement();
    if (attrValue instanceof PsiLanguageInjectionHost) {
        final Ref<PsiElement> injections = Ref.create();
        InjectedLanguageUtil.enumerate(attrValue, (injectedPsi, places) -> {
            if (injectedPsi instanceof DbFile) {
                injections.set(injectedPsi);
            }
        });
        if (injections.get() != null) {
            PsiDbDefaults defaults = PsiTreeUtil.getChildOfType(injections.get(), PsiDbDefaults.class);
            if (defaults != null) {
                PsiDbConstantValue constantValue = defaults.getConstantValue();
                ASTNode stringLiteral = constantValue.getNode().findChildByType(DbTokenTypes.STRING_LITERAL);
                if (stringLiteral == null) {
                    return constantValue.getText();
                } else {
                    String text = stringLiteral.getText();
                    if (text.length() > 1) {
                        // return unquoted string literal.
                        return text.substring(1, text.length() - 1);
                    } else {
                        return text;
                    }
                }
            }
        }
    }
    return null;
}
Also used : PsiDbConstantValue(com.android.tools.idea.lang.databinding.psi.PsiDbConstantValue) PsiDbDefaults(com.android.tools.idea.lang.databinding.psi.PsiDbDefaults) DbFile(com.android.tools.idea.lang.databinding.DbFile) ASTNode(com.intellij.lang.ASTNode) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Nullable(org.jetbrains.annotations.Nullable)

Example 67 with XmlAttributeValue

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

the class ThemeEditorStyle method getNamePsiElement.

/**
   * Returns a PsiElement of the name attribute for this theme
   * made from a RANDOM sourceXml
   */
@Nullable
public PsiElement getNamePsiElement() {
    Collection<ResourceItem> resources = getStyleResourceItems();
    if (resources.isEmpty()) {
        return null;
    }
    // Any sourceXml will do to get the name attribute from
    final XmlTag sourceXml = LocalResourceRepository.getItemTag(myManager.getProject(), resources.iterator().next());
    assert sourceXml != null;
    final XmlAttribute nameAttribute = sourceXml.getAttribute("name");
    if (nameAttribute == null) {
        return null;
    }
    XmlAttributeValue attributeValue = nameAttribute.getValueElement();
    if (attributeValue == null) {
        return null;
    }
    return new ValueResourceElementWrapper(attributeValue);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) ResourceItem(com.android.ide.common.res2.ResourceItem) XmlTag(com.intellij.psi.xml.XmlTag) ValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper) Nullable(org.jetbrains.annotations.Nullable)

Example 68 with XmlAttributeValue

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

the class AndroidResourceRenameResourceProcessor method prepareValueResourceRenaming.

private static void prepareValueResourceRenaming(PsiElement element, String newName, Map<PsiElement, String> allRenames, final AndroidFacet facet) {
    ResourceManager manager = facet.getLocalResourceManager();
    XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
    assert tag != null;
    String type = manager.getValueResourceType(tag);
    assert type != null;
    Project project = tag.getProject();
    DomElement domElement = DomManager.getDomManager(project).getDomElement(tag);
    assert domElement instanceof ResourceElement;
    String name = ((ResourceElement) domElement).getName().getValue();
    assert name != null;
    List<ResourceElement> resources = manager.findValueResources(type, name);
    for (ResourceElement resource : resources) {
        XmlElement xmlElement = resource.getName().getXmlAttributeValue();
        if (!element.getManager().areElementsEquivalent(element, xmlElement)) {
            allRenames.put(xmlElement, newName);
        }
    }
    if (getResourceType(element) == ResourceType.STYLE) {
        // For styles, try also to find child styles defined by name (i.e. "ParentName.StyleName") and add them
        // to the rename list. This will allow the rename processor to also handle the references to those. For example,
        // If you rename "MyTheme" and your manifest theme is "MyTheme.NoActionBar", this will make sure that
        // the reference from the manifest is also updated by adding "MyTheme.NoActionBar" to the rename list.
        // We iterate the styles in order to cascade any changes to children down the hierarchy.
        // List of styles that will be renamed.
        HashSet<String> renamedStyles = Sets.newHashSet();
        renamedStyles.add(name);
        final String stylePrefix = name + ".";
        Collection<String> renameCandidates;
        ResourceType resourceType = ResourceType.getEnum(type);
        if (resourceType == null) {
            renameCandidates = Collections.emptyList();
        } else {
            renameCandidates = Collections2.filter(manager.getResourceNames(resourceType), new Predicate<String>() {

                @Override
                public boolean apply(String input) {
                    return input.startsWith(stylePrefix);
                }
            });
        }
        for (String resourceName : ORDER_BY_LENGTH.sortedCopy(renameCandidates)) {
            // resourceName.lastIndexOf will never return -1 because we've filtered all names that
            // do not contain stylePrefix
            String parentName = resourceName.substring(0, resourceName.lastIndexOf('.'));
            if (!renamedStyles.contains(parentName)) {
                // This resource's parent wasn't affected by the rename
                continue;
            }
            for (ResourceElement resource : manager.findValueResources(type, resourceName)) {
                if (!(resource instanceof Style) || ((Style) resource).getParentStyle().getXmlAttributeValue() != null) {
                    // This element is not a style or does have an explicit parent so we do not rename it.
                    continue;
                }
                XmlAttributeValue xmlElement = resource.getName().getXmlAttributeValue();
                if (xmlElement != null) {
                    String newStyleName = newName + StringUtil.trimStart(resourceName, name);
                    allRenames.put(new ValueResourceElementWrapper(xmlElement), newStyleName);
                    renamedStyles.add(resourceName);
                }
            }
        }
    }
    PsiField[] resFields = AndroidResourceUtil.findResourceFieldsForValueResource(tag, false);
    for (PsiField resField : resFields) {
        String escaped = AndroidResourceUtil.getFieldNameByResourceName(newName);
        allRenames.put(resField, escaped);
    }
    // Also rename the dependent fields, e.g. if you rename <declare-styleable name="Foo">,
    // we have to rename not just R.styleable.Foo but the also R.styleable.Foo_* attributes
    PsiField[] styleableFields = AndroidResourceUtil.findStyleableAttributeFields(tag, false);
    if (styleableFields.length > 0) {
        String tagName = tag.getName();
        boolean isDeclareStyleable = tagName.equals(TAG_DECLARE_STYLEABLE);
        boolean isAttr = !isDeclareStyleable && tagName.equals(TAG_ATTR) && tag.getParentTag() != null;
        assert isDeclareStyleable || isAttr;
        String style = isAttr ? tag.getParentTag().getAttributeValue(ATTR_NAME) : null;
        for (PsiField resField : styleableFields) {
            String fieldName = resField.getName();
            String newAttributeName;
            if (isDeclareStyleable && fieldName.startsWith(name)) {
                newAttributeName = newName + fieldName.substring(name.length());
            } else if (isAttr && style != null) {
                newAttributeName = style + '_' + newName;
            } else {
                newAttributeName = name;
            }
            String escaped = AndroidResourceUtil.getFieldNameByResourceName(newAttributeName);
            allRenames.put(resField, escaped);
        }
    }
}
Also used : ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) ResourceType(com.android.resources.ResourceType) ResourceManager(org.jetbrains.android.resourceManagers.ResourceManager) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Predicate(com.google.common.base.Predicate) Project(com.intellij.openapi.project.Project) DomElement(com.intellij.util.xml.DomElement) XmlElement(com.intellij.psi.xml.XmlElement) Style(org.jetbrains.android.dom.resources.Style) XmlTag(com.intellij.psi.xml.XmlTag) LazyValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper) ValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper)

Example 69 with XmlAttributeValue

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

the class AndroidResourceRenameResourceProcessor method renameElement.

@Override
public void renameElement(PsiElement element, final String newName, UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
    if (element instanceof PsiField) {
        new RenameJavaVariableProcessor().renameElement(element, newName, usages, listener);
    } else {
        if (element instanceof PsiNamedElement) {
            super.renameElement(element, newName, usages, listener);
            if (element instanceof PsiFile) {
                VirtualFile virtualFile = ((PsiFile) element).getVirtualFile();
                if (virtualFile != null && !LocalHistory.getInstance().isUnderControl(virtualFile)) {
                    DocumentReference ref = DocumentReferenceManager.getInstance().create(virtualFile);
                    UndoManager.getInstance(element.getProject()).nonundoableActionPerformed(ref, false);
                }
            }
        } else if (element instanceof XmlAttributeValue) {
            new RenameXmlAttributeProcessor().renameElement(element, newName, usages, listener);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) RenameJavaVariableProcessor(com.intellij.refactoring.rename.RenameJavaVariableProcessor) RenameXmlAttributeProcessor(com.intellij.refactoring.rename.RenameXmlAttributeProcessor) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) DocumentReference(com.intellij.openapi.command.undo.DocumentReference)

Example 70 with XmlAttributeValue

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

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