Search in sources :

Example 6 with SchemaPrefixReference

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

the class XmlUnboundNsPrefixInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    return new XmlElementVisitor() {

        private Boolean isXml;

        private boolean isXmlFile(XmlElement element) {
            if (isXml == null) {
                final PsiFile file = element.getContainingFile();
                isXml = file instanceof XmlFile && !InjectedLanguageManager.getInstance(element.getProject()).isInjectedFragment(file);
            }
            return isXml.booleanValue();
        }

        @Override
        public void visitXmlToken(final XmlToken token) {
            if (isXmlFile(token) && token.getTokenType() == XmlTokenType.XML_NAME) {
                PsiElement element = token.getPrevSibling();
                while (element instanceof PsiWhiteSpace) element = element.getPrevSibling();
                if (element instanceof XmlToken && ((XmlToken) element).getTokenType() == XmlTokenType.XML_START_TAG_START) {
                    PsiElement parent = element.getParent();
                    if (parent instanceof XmlTag && !(token.getNextSibling() instanceof OuterLanguageElement)) {
                        XmlTag tag = (XmlTag) parent;
                        checkUnboundNamespacePrefix(tag, tag, tag.getNamespacePrefix(), token, holder, isOnTheFly);
                    }
                }
            }
        }

        @Override
        public void visitXmlAttribute(final XmlAttribute attribute) {
            if (!isXmlFile(attribute)) {
                return;
            }
            final String namespace = attribute.getNamespace();
            if (attribute.isNamespaceDeclaration() || XmlUtil.XML_SCHEMA_INSTANCE_URI.equals(namespace)) {
                return;
            }
            XmlTag tag = attribute.getParent();
            if (tag == null)
                return;
            XmlElementDescriptor elementDescriptor = tag.getDescriptor();
            if (elementDescriptor == null || elementDescriptor instanceof AnyXmlElementDescriptor) {
                return;
            }
            final String name = attribute.getName();
            checkUnboundNamespacePrefix(attribute, tag, XmlUtil.findPrefixByQualifiedName(name), null, holder, isOnTheFly);
        }

        @Override
        public void visitXmlAttributeValue(XmlAttributeValue value) {
            PsiReference[] references = value.getReferences();
            for (PsiReference reference : references) {
                if (reference instanceof SchemaPrefixReference) {
                    if (!XML.equals(((SchemaPrefixReference) reference).getNamespacePrefix()) && reference.resolve() == null) {
                        holder.registerProblem(reference, XmlErrorMessages.message("unbound.namespace", ((SchemaPrefixReference) reference).getNamespacePrefix()), ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
                    }
                }
            }
        }
    };
}
Also used : OuterLanguageElement(com.intellij.psi.templateLanguages.OuterLanguageElement) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) SchemaPrefixReference(com.intellij.psi.impl.source.xml.SchemaPrefixReference) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with SchemaPrefixReference

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

the class MxmlTagNameReference method bindToElement.

public PsiElement bindToElement(@NotNull final PsiElement element) throws IncorrectOperationException {
    final String newPackage = getNewPackage(element);
    if (newPackage == null) {
        return super.bindToElement(element);
    } else {
        final XmlTag tag = getTagElement();
        if (tag == null || !myStartTagFlag)
            return tag;
        final String newNamespace = newPackage.isEmpty() ? "*" : newPackage + ".*";
        String newPrefix = tag.getPrefixByNamespace(newNamespace);
        if (newPrefix == null) {
            final XmlFile xmlFile = (XmlFile) tag.getContainingFile();
            newPrefix = FlexSchemaHandler.getUniquePrefix(newNamespace, xmlFile);
            final XmlTag rootTag = xmlFile.getRootTag();
            assert rootTag != null;
            insertNamespaceDeclaration(rootTag, newNamespace, newPrefix);
        }
        final SchemaPrefixReference schemaPrefixReference = getSchemaPrefixReference(tag);
        final SchemaPrefix schemaPrefix = schemaPrefixReference == null ? null : schemaPrefixReference.resolve();
        final String oldPrefix = tag.getNamespacePrefix();
        final String newLocalName = FileUtil.getNameWithoutExtension(((PsiFile) element).getName());
        tag.setName(StringUtil.isEmpty(newPrefix) ? newLocalName : (newPrefix + ":" + newLocalName));
        fixSubTagsPrefixes(tag, oldPrefix, newPrefix);
        removeNamespaceDeclarationIfNotUsed(schemaPrefix);
        return tag;
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) SchemaPrefix(com.intellij.psi.impl.source.xml.SchemaPrefix) 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