Search in sources :

Example 1 with SchemaPrefixReference

use of com.intellij.psi.impl.source.xml.SchemaPrefixReference in project intellij-community by JetBrains.

the class XmlNsPrefixAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (PsiUtilCore.getElementType(element) != XmlTokenType.XML_NAME)
        return;
    PsiElement parent = element.getParent();
    if (!(parent instanceof XmlTag) && !(parent instanceof XmlAttribute))
        return;
    TextRange elementRange = element.getTextRange();
    List<SchemaPrefixReference> references = ContainerUtil.findAll(parent.getReferences(), SchemaPrefixReference.class);
    for (SchemaPrefixReference ref : references) {
        TextRange rangeInElement = ref.getRangeInElement();
        if (rangeInElement.isEmpty())
            continue;
        TextRange range = rangeInElement.shiftRight(ref.getElement().getTextRange().getStartOffset());
        if (!range.intersects(elementRange))
            continue;
        holder.createInfoAnnotation(range, null).setTextAttributes(XmlHighlighterColors.XML_NS_PREFIX);
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) TextRange(com.intellij.openapi.util.TextRange) SchemaPrefixReference(com.intellij.psi.impl.source.xml.SchemaPrefixReference) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 2 with SchemaPrefixReference

use of com.intellij.psi.impl.source.xml.SchemaPrefixReference in project intellij-community by JetBrains.

the class XmlPrefixReferenceProvider method getReferencesByElement.

@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
    XmlAttributeValue attributeValue = (XmlAttributeValue) element;
    String value = attributeValue.getValue();
    if (value == null)
        return PsiReference.EMPTY_ARRAY;
    int i = value.indexOf(':');
    if (i <= 0)
        return PsiReference.EMPTY_ARRAY;
    PsiElement parent = attributeValue.getParent();
    if (parent instanceof XmlAttribute) {
        XmlTag tag = ((XmlAttribute) parent).getParent();
        if (tag != null && !XmlNSDescriptorImpl.checkSchemaNamespace(tag)) {
            XmlAttributeDescriptor descriptor = ((XmlAttribute) parent).getDescriptor();
            if (descriptor instanceof XmlAttributeDescriptorImpl) {
                String type = ((XmlAttributeDescriptorImpl) descriptor).getType();
                if (type != null && type.endsWith(":QName")) {
                    String prefix = XmlUtil.findPrefixByQualifiedName(type);
                    String ns = ((XmlTag) descriptor.getDeclaration()).getNamespaceByPrefix(prefix);
                    if (XmlNSDescriptorImpl.checkSchemaNamespace(ns)) {
                        return new PsiReference[] { new SchemaPrefixReference(attributeValue, TextRange.from(1, i), value.substring(0, i), null) };
                    }
                }
            }
        }
    }
    return PsiReference.EMPTY_ARRAY;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) PsiReference(com.intellij.psi.PsiReference) XmlAttributeDescriptorImpl(com.intellij.xml.impl.schema.XmlAttributeDescriptorImpl) SchemaPrefixReference(com.intellij.psi.impl.source.xml.SchemaPrefixReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with SchemaPrefixReference

use of com.intellij.psi.impl.source.xml.SchemaPrefixReference in project intellij-community by JetBrains.

the class SchemaReferencesProvider method createSchemaPrefixReference.

@Nullable
private static PsiReference createSchemaPrefixReference(final PsiElement element) {
    if (element instanceof XmlAttributeValue) {
        final XmlAttributeValue attributeValue = (XmlAttributeValue) element;
        final String prefix = XmlUtil.findPrefixByQualifiedName(attributeValue.getValue());
        if (!prefix.isEmpty()) {
            return new SchemaPrefixReference(attributeValue, TextRange.from(1, prefix.length()), prefix, null);
        }
    }
    return null;
}
Also used : SchemaPrefixReference(com.intellij.psi.impl.source.xml.SchemaPrefixReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with SchemaPrefixReference

use of com.intellij.psi.impl.source.xml.SchemaPrefixReference 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 5 with SchemaPrefixReference

use of com.intellij.psi.impl.source.xml.SchemaPrefixReference in project intellij-community by JetBrains.

the class ConvertSchemaPrefixToDefaultIntention method invoke.

@Override
public void invoke(@NotNull final Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
    final XmlAttribute xmlns = getXmlnsDeclaration(element);
    if (xmlns == null)
        return;
    SchemaPrefixReference prefixRef = null;
    for (PsiReference ref : xmlns.getReferences()) {
        if (ref instanceof SchemaPrefixReference) {
            prefixRef = (SchemaPrefixReference) ref;
            break;
        }
    }
    if (prefixRef == null)
        return;
    final SchemaPrefix prefix = prefixRef.resolve();
    final String ns = prefixRef.getNamespacePrefix();
    final ArrayList<XmlTag> tags = new ArrayList<>();
    final ArrayList<XmlAttribute> attrs = new ArrayList<>();
    xmlns.getParent().accept(new XmlRecursiveElementVisitor() {

        @Override
        public void visitXmlTag(XmlTag tag) {
            if (ns.equals(tag.getNamespacePrefix())) {
                tags.add(tag);
            }
            super.visitXmlTag(tag);
        }

        @Override
        public void visitXmlAttributeValue(XmlAttributeValue value) {
            if (value.getValue().startsWith(ns + ":")) {
                for (PsiReference ref : value.getReferences()) {
                    if (ref instanceof SchemaPrefixReference && ref.isReferenceTo(prefix)) {
                        attrs.add((XmlAttribute) value.getParent());
                    }
                }
            }
        }
    });
    if (!FileModificationService.getInstance().preparePsiElementsForWrite(xmlns.getContainingFile()))
        return;
    CommandProcessor.getInstance().executeCommand(project, () -> {
        convertTagsAndAttributes(ns, tags, attrs, project);
        ApplicationManager.getApplication().runWriteAction(() -> {
            xmlns.setName("xmlns");
        });
    }, NAME, null);
    new WriteCommandAction(project, NAME, xmlns.getContainingFile()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            xmlns.setName("xmlns");
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XmlAttribute(com.intellij.psi.xml.XmlAttribute) SchemaPrefix(com.intellij.psi.impl.source.xml.SchemaPrefix) XmlRecursiveElementVisitor(com.intellij.psi.XmlRecursiveElementVisitor) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Result(com.intellij.openapi.application.Result) SchemaPrefixReference(com.intellij.psi.impl.source.xml.SchemaPrefixReference) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

SchemaPrefixReference (com.intellij.psi.impl.source.xml.SchemaPrefixReference)7 XmlTag (com.intellij.psi.xml.XmlTag)5 XmlAttribute (com.intellij.psi.xml.XmlAttribute)4 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)4 PsiElement (com.intellij.psi.PsiElement)3 PsiReference (com.intellij.psi.PsiReference)3 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 XmlRecursiveElementVisitor (com.intellij.psi.XmlRecursiveElementVisitor)2 SchemaPrefix (com.intellij.psi.impl.source.xml.SchemaPrefix)2 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 InputValidator (com.intellij.openapi.ui.InputValidator)1 TextRange (com.intellij.openapi.util.TextRange)1 TypeOrElementOrAttributeReference (com.intellij.psi.impl.source.resolve.reference.impl.providers.TypeOrElementOrAttributeReference)1 OuterLanguageElement (com.intellij.psi.templateLanguages.OuterLanguageElement)1 XmlElement (com.intellij.psi.xml.XmlElement)1 XmlFile (com.intellij.psi.xml.XmlFile)1 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)1 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)1