Search in sources :

Example 1 with XmlSchemaProvider

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

the class XmlLocationCompletionContributor method completeNamespace.

private static Object[] completeNamespace(PsiElement myElement) {
    final XmlFile file = (XmlFile) myElement.getContainingFile();
    PsiElement parent = myElement.getParent();
    final Set<Object> preferred = new HashSet<>();
    if (parent instanceof XmlAttribute && "xmlns".equals(((XmlAttribute) parent).getName())) {
        XmlNamespaceHelper helper = XmlNamespaceHelper.getHelper(file);
        preferred.addAll(helper.guessUnboundNamespaces(parent.getParent(), file));
    }
    Set<String> list = new HashSet<>();
    for (XmlSchemaProvider provider : Extensions.getExtensions(XmlSchemaProvider.EP_NAME)) {
        if (provider.isAvailable(file)) {
            list.addAll(provider.getAvailableNamespaces(file, null));
        }
    }
    if (!list.isEmpty()) {
        return ArrayUtil.toObjectArray(list);
    }
    Object[] resourceUrls = ExternalResourceManagerEx.getInstanceEx().getUrlsByNamespace(myElement.getProject()).keySet().toArray();
    final XmlDocument document = file.getDocument();
    assert document != null;
    XmlTag rootTag = document.getRootTag();
    final ArrayList<String> additionalNs = new ArrayList<>();
    if (rootTag != null)
        URLReference.processWsdlSchemas(rootTag, xmlTag -> {
            final String s = xmlTag.getAttributeValue(URLReference.TARGET_NAMESPACE_ATTR_NAME);
            if (s != null) {
                additionalNs.add(s);
            }
            return true;
        });
    resourceUrls = ArrayUtil.mergeArrays(resourceUrls, ArrayUtil.toStringArray(additionalNs));
    return ContainerUtil.map2Array(resourceUrls, o -> {
        LookupElementBuilder builder = LookupElementBuilder.create(o);
        return preferred.contains(o) ? PrioritizedLookupElement.withPriority(builder, 100) : builder;
    });
}
Also used : Extensions(com.intellij.openapi.extensions.Extensions) ArrayUtil(com.intellij.util.ArrayUtil) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference) LookupElement(com.intellij.codeInsight.lookup.LookupElement) Collection(java.util.Collection) PsiReference(com.intellij.psi.PsiReference) Set(java.util.Set) ContainerUtil(com.intellij.util.containers.ContainerUtil) DependentNSReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.DependentNSReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) com.intellij.psi.xml(com.intellij.psi.xml) XmlNamespaceHelper(com.intellij.xml.XmlNamespaceHelper) ExternalResourceManagerEx(com.intellij.javaee.ExternalResourceManagerEx) Function(com.intellij.util.Function) Processor(com.intellij.util.Processor) XmlSchemaProvider(com.intellij.xml.XmlSchemaProvider) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull) URLReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.URLReference) MultiMap(com.intellij.util.containers.MultiMap) Condition(com.intellij.openapi.util.Condition) ArrayList(java.util.ArrayList) XmlNamespaceHelper(com.intellij.xml.XmlNamespaceHelper) XmlSchemaProvider(com.intellij.xml.XmlSchemaProvider) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet)

Aggregations

LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)1 ExternalResourceManagerEx (com.intellij.javaee.ExternalResourceManagerEx)1 Extensions (com.intellij.openapi.extensions.Extensions)1 Condition (com.intellij.openapi.util.Condition)1 PsiElement (com.intellij.psi.PsiElement)1 PsiReference (com.intellij.psi.PsiReference)1 PsiMultiReference (com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference)1 DependentNSReference (com.intellij.psi.impl.source.resolve.reference.impl.providers.DependentNSReference)1 URLReference (com.intellij.psi.impl.source.resolve.reference.impl.providers.URLReference)1 com.intellij.psi.xml (com.intellij.psi.xml)1 ArrayUtil (com.intellij.util.ArrayUtil)1 Function (com.intellij.util.Function)1 Processor (com.intellij.util.Processor)1 ContainerUtil (com.intellij.util.containers.ContainerUtil)1 MultiMap (com.intellij.util.containers.MultiMap)1 XmlNamespaceHelper (com.intellij.xml.XmlNamespaceHelper)1 XmlSchemaProvider (com.intellij.xml.XmlSchemaProvider)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1