Search in sources :

Example 56 with XmlElement

use of com.intellij.psi.xml.XmlElement in project intellij-community by JetBrains.

the class XmlElementImpl method getNavigationElement.

@Override
@NotNull
public PsiElement getNavigationElement() {
    if (!isPhysical()) {
        final XmlElement including = getUserData(INCLUDING_ELEMENT);
        if (including != null) {
            return including;
        }
        PsiElement astParent = getAstParent();
        PsiElement parentNavigation = astParent.getNavigationElement();
        if (parentNavigation.getTextOffset() == getTextOffset())
            return parentNavigation;
        return this;
    }
    return super.getNavigationElement();
}
Also used : XmlElement(com.intellij.psi.xml.XmlElement) PsiElement(com.intellij.psi.PsiElement) CompositePsiElement(com.intellij.psi.impl.source.tree.CompositePsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 57 with XmlElement

use of com.intellij.psi.xml.XmlElement in project intellij-community by JetBrains.

the class XmlEnumeratedTypeImpl method getEnumeratedValues.

@Override
public XmlElement[] getEnumeratedValues() {
    final List<XmlElement> result = new ArrayList<>();
    processElements(new FilterElementProcessor(new XmlTokenTypeFilter(XmlTokenType.XML_NAME), result), this);
    return result.toArray(new XmlElement[result.size()]);
}
Also used : FilterElementProcessor(com.intellij.psi.scope.processor.FilterElementProcessor) XmlTokenTypeFilter(com.intellij.psi.filters.position.XmlTokenTypeFilter) ArrayList(java.util.ArrayList) XmlElement(com.intellij.psi.xml.XmlElement)

Example 58 with XmlElement

use of com.intellij.psi.xml.XmlElement in project intellij-community by JetBrains.

the class AddSchemaPrefixIntention method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
    final XmlAttribute xmlns = getXmlnsDeclaration(element);
    if (xmlns == null)
        return;
    final String namespace = xmlns.getValue();
    final XmlTag tag = xmlns.getParent();
    if (tag != null) {
        final Set<String> ns = tag.getLocalNamespaceDeclarations().keySet();
        final String nsPrefix = Messages.showInputDialog(project, "Namespace Prefix:", StringUtil.capitalize(NAME), Messages.getInformationIcon(), "", new InputValidator() {

            @Override
            public boolean checkInput(String inputString) {
                return !ns.contains(inputString);
            }

            @Override
            public boolean canClose(String inputString) {
                return checkInput(inputString);
            }
        });
        if (nsPrefix == null)
            return;
        final List<XmlTag> tags = new ArrayList<>();
        final List<XmlAttributeValue> values = new ArrayList<>();
        new WriteCommandAction(project, NAME, tag.getContainingFile()) {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                tag.accept(new XmlRecursiveElementVisitor() {

                    @Override
                    public void visitXmlTag(XmlTag tag) {
                        if (tag.getNamespace().equals(namespace) && tag.getNamespacePrefix().isEmpty()) {
                            tags.add(tag);
                        }
                        super.visitXmlTag(tag);
                    }

                    @Override
                    public void visitXmlAttributeValue(XmlAttributeValue value) {
                        PsiReference ref = null;
                        boolean skip = false;
                        for (PsiReference reference : value.getReferences()) {
                            if (reference instanceof TypeOrElementOrAttributeReference) {
                                ref = reference;
                            } else if (reference instanceof SchemaPrefixReference) {
                                skip = true;
                                break;
                            }
                        }
                        if (!skip && ref != null) {
                            final PsiElement xmlElement = ref.resolve();
                            if (xmlElement instanceof XmlElement) {
                                final XmlTag tag = PsiTreeUtil.getParentOfType(xmlElement, XmlTag.class, false);
                                if (tag != null) {
                                    if (tag.getNamespace().equals(namespace)) {
                                        if (ref.getRangeInElement().getLength() == value.getValue().length()) {
                                            //no ns prefix
                                            values.add(value);
                                        }
                                    }
                                }
                            }
                        }
                    }
                });
                for (XmlAttributeValue value : values) {
                    ((XmlAttribute) value.getParent()).setValue(nsPrefix + ":" + value.getValue());
                }
                for (XmlTag xmlTag : tags) {
                    xmlTag.setName(nsPrefix + ":" + xmlTag.getLocalName());
                }
                xmlns.setName("xmlns:" + nsPrefix);
            }
        }.execute();
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlRecursiveElementVisitor(com.intellij.psi.XmlRecursiveElementVisitor) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) TypeOrElementOrAttributeReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.TypeOrElementOrAttributeReference) Result(com.intellij.openapi.application.Result) InputValidator(com.intellij.openapi.ui.InputValidator) XmlElement(com.intellij.psi.xml.XmlElement) SchemaPrefixReference(com.intellij.psi.impl.source.xml.SchemaPrefixReference) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 59 with XmlElement

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

the class AttributeProcessingUtil method isLayoutAttributeRequired.

/**
   * Check whether layout tag attribute with given name should be marked as required.
   * Currently, tests for layout_width and layout_height attribute and marks them as required in appropriate context.
   */
public static boolean isLayoutAttributeRequired(@NotNull XmlName attributeName, @NotNull DomElement element) {
    // Mark layout_width and layout_height required - if the context calls for it
    String localName = attributeName.getLocalName();
    if (!(ATTR_LAYOUT_WIDTH.equals(localName) || ATTR_LAYOUT_HEIGHT.equals(localName))) {
        return false;
    }
    if ((element instanceof LayoutViewElement || element instanceof Fragment) && NS_RESOURCES.equals(attributeName.getNamespaceKey())) {
        XmlElement xmlElement = element.getXmlElement();
        XmlTag tag = xmlElement instanceof XmlTag ? (XmlTag) xmlElement : null;
        String tagName = tag != null ? tag.getName() : null;
        if (!SIZE_NOT_REQUIRED_TAG_NAMES.contains(tagName) && (tag == null || tag.getAttribute(ATTR_STYLE) == null)) {
            XmlTag parentTag = tag != null ? tag.getParentTag() : null;
            String parentTagName = parentTag != null ? parentTag.getName() : null;
            if (!SIZE_NOT_REQUIRED_PARENT_TAG_NAMES.contains(parentTagName)) {
                return true;
            }
        }
    }
    return false;
}
Also used : XmlElement(com.intellij.psi.xml.XmlElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 60 with XmlElement

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

Aggregations

XmlElement (com.intellij.psi.xml.XmlElement)69 PsiElement (com.intellij.psi.PsiElement)16 NotNull (org.jetbrains.annotations.NotNull)16 Nullable (org.jetbrains.annotations.Nullable)11 XmlTag (com.intellij.psi.xml.XmlTag)10 PsiFile (com.intellij.psi.PsiFile)7 PsiReference (com.intellij.psi.PsiReference)6 XmlAttribute (com.intellij.psi.xml.XmlAttribute)6 XmlFile (com.intellij.psi.xml.XmlFile)6 Manifest (org.jetbrains.android.dom.manifest.Manifest)5 Module (com.intellij.openapi.module.Module)4 Project (com.intellij.openapi.project.Project)4 DomElement (com.intellij.util.xml.DomElement)4 ArrayList (java.util.ArrayList)4 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)3 CompositePsiElement (com.intellij.psi.impl.source.tree.CompositePsiElement)3 SmartList (com.intellij.util.SmartList)3 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)3 ResourceType (com.android.resources.ResourceType)2 Result (com.intellij.openapi.application.Result)2