Search in sources :

Example 1 with TypeOrElementOrAttributeReference

use of com.intellij.psi.impl.source.resolve.reference.impl.providers.TypeOrElementOrAttributeReference 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)

Aggregations

Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 InputValidator (com.intellij.openapi.ui.InputValidator)1 PsiElement (com.intellij.psi.PsiElement)1 PsiReference (com.intellij.psi.PsiReference)1 XmlRecursiveElementVisitor (com.intellij.psi.XmlRecursiveElementVisitor)1 TypeOrElementOrAttributeReference (com.intellij.psi.impl.source.resolve.reference.impl.providers.TypeOrElementOrAttributeReference)1 SchemaPrefixReference (com.intellij.psi.impl.source.xml.SchemaPrefixReference)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1 XmlElement (com.intellij.psi.xml.XmlElement)1 XmlTag (com.intellij.psi.xml.XmlTag)1 ArrayList (java.util.ArrayList)1