Search in sources :

Example 1 with SchemaPrefix

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

the class DefaultXmlExtension method getPrefixDeclaration.

@Override
public SchemaPrefix getPrefixDeclaration(final XmlTag context, String namespacePrefix) {
    @NonNls String nsDeclarationAttrName = null;
    for (XmlTag t = context; t != null; t = t.getParentTag()) {
        if (t.hasNamespaceDeclarations()) {
            if (nsDeclarationAttrName == null)
                nsDeclarationAttrName = namespacePrefix.length() > 0 ? "xmlns:" + namespacePrefix : "xmlns";
            XmlAttribute attribute = t.getAttribute(nsDeclarationAttrName);
            if (attribute != null) {
                final String attrPrefix = attribute.getNamespacePrefix();
                final TextRange textRange = TextRange.from(attrPrefix.length() + 1, namespacePrefix.length());
                return new SchemaPrefix(attribute, textRange, namespacePrefix);
            }
        }
    }
    return null;
}
Also used : NonNls(org.jetbrains.annotations.NonNls) XmlAttribute(com.intellij.psi.xml.XmlAttribute) SchemaPrefix(com.intellij.psi.impl.source.xml.SchemaPrefix) TextRange(com.intellij.openapi.util.TextRange) XmlTag(com.intellij.psi.xml.XmlTag)

Example 2 with SchemaPrefix

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

the class XsltImplicitUsagesProvider method isImplicitUsage.

public boolean isImplicitUsage(PsiElement element) {
    if (!(element instanceof XmlAttribute)) {
        return false;
    }
    final XmlAttribute attr = (XmlAttribute) element;
    if (!attr.isNamespaceDeclaration()) {
        return false;
    }
    final PsiFile file = attr.getContainingFile();
    if (!(file instanceof XmlFile)) {
        return false;
    }
    // ContextProvider.hasXPathInjections() is an optimization that avoids to run the references search on totally XPath-free XML files
    if (!ContextProvider.hasXPathInjections((XmlFile) file) && !XsltSupport.isXsltFile(file)) {
        return false;
    }
    // This need to catch both prefix references from injected XPathFiles and prefixes from mode declarations/references:
    // <xsl:template match="*" mode="prefix:name" />
    // BTW: Almost the same logic applies to other XML dialects (RELAX-NG).
    // Pull this class into the platform?
    final String prefix = attr.getLocalName();
    final SchemaPrefix target = new SchemaPrefix(attr, TextRange.from("xmlns:".length(), prefix.length()), prefix);
    final Query<PsiReference> q = ReferencesSearch.search(target, new LocalSearchScope(attr.getParent()));
    return !q.forEach(psiReference -> {
        if (psiReference.getElement() == attr) {
            return true;
        }
        return false;
    });
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) SchemaPrefix(com.intellij.psi.impl.source.xml.SchemaPrefix) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XsltSupport(org.intellij.lang.xpath.xslt.XsltSupport) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Query(com.intellij.util.Query) Processor(com.intellij.util.Processor) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) ImplicitUsageProvider(com.intellij.codeInsight.daemon.ImplicitUsageProvider) ContextProvider(org.intellij.lang.xpath.context.ContextProvider) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) SchemaPrefix(com.intellij.psi.impl.source.xml.SchemaPrefix) PsiReference(com.intellij.psi.PsiReference) PsiFile(com.intellij.psi.PsiFile)

Example 3 with SchemaPrefix

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

Example 4 with SchemaPrefix

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

the class XsltNamespaceContext method resolvePrefix.

@Nullable
public static PsiElement resolvePrefix(final String prefix, XmlElement context) {
    final String name = "xmlns:" + prefix;
    XmlTag parent = PsiTreeUtil.getParentOfType(context, XmlTag.class);
    while (parent != null) {
        final XmlAttribute attribute = parent.getAttribute(name, null);
        if (attribute != null) {
            final TextRange textRange = TextRange.from("xmlns:".length(), prefix.length());
            return new SchemaPrefix(attribute, textRange, prefix) {

                @Override
                public boolean equals(Object obj) {
                    if (obj instanceof SchemaPrefix) {
                        final SchemaPrefix p = (SchemaPrefix) obj;
                        return prefix.equals(p.getName()) && p.getParent() == attribute;
                    }
                    return super.equals(obj);
                }
            };
        }
        parent = PsiTreeUtil.getParentOfType(parent, XmlTag.class);
    }
    return null;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) SchemaPrefix(com.intellij.psi.impl.source.xml.SchemaPrefix) TextRange(com.intellij.openapi.util.TextRange) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with SchemaPrefix

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

SchemaPrefix (com.intellij.psi.impl.source.xml.SchemaPrefix)5 XmlAttribute (com.intellij.psi.xml.XmlAttribute)4 XmlTag (com.intellij.psi.xml.XmlTag)4 TextRange (com.intellij.openapi.util.TextRange)3 PsiReference (com.intellij.psi.PsiReference)2 SchemaPrefixReference (com.intellij.psi.impl.source.xml.SchemaPrefixReference)2 XmlFile (com.intellij.psi.xml.XmlFile)2 ImplicitUsageProvider (com.intellij.codeInsight.daemon.ImplicitUsageProvider)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 XmlRecursiveElementVisitor (com.intellij.psi.XmlRecursiveElementVisitor)1 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)1 ReferencesSearch (com.intellij.psi.search.searches.ReferencesSearch)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1 Processor (com.intellij.util.Processor)1 Query (com.intellij.util.Query)1 ArrayList (java.util.ArrayList)1 ContextProvider (org.intellij.lang.xpath.context.ContextProvider)1