Search in sources :

Example 6 with NamespaceContext

use of org.intellij.lang.xpath.context.NamespaceContext in project intellij-community by JetBrains.

the class XPathAnnotator method checkPrefixReferences.

private static void checkPrefixReferences(AnnotationHolder holder, QNameElement element, ContextProvider myProvider) {
    final PsiReference[] references = element.getReferences();
    for (PsiReference reference : references) {
        if (reference instanceof PrefixReference) {
            final PrefixReference pr = ((PrefixReference) reference);
            if (!pr.isSoft() && pr.isUnresolved()) {
                final TextRange range = pr.getRangeInElement().shiftRight(pr.getElement().getTextRange().getStartOffset());
                final Annotation a = holder.createErrorAnnotation(range, "Unresolved namespace prefix '" + pr.getCanonicalText() + "'");
                a.setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
                final NamespaceContext namespaceContext = myProvider.getNamespaceContext();
                final PrefixedName qName = element.getQName();
                if (namespaceContext != null && qName != null) {
                    final IntentionAction[] fixes = namespaceContext.getUnresolvedNamespaceFixes(reference, qName.getLocalName());
                    for (IntentionAction fix : fixes) {
                        a.registerFix(fix);
                    }
                }
            }
        }
    }
}
Also used : NamespaceContext(org.intellij.lang.xpath.context.NamespaceContext) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) Annotation(com.intellij.lang.annotation.Annotation)

Example 7 with NamespaceContext

use of org.intellij.lang.xpath.context.NamespaceContext in project intellij-community by JetBrains.

the class CompletionLists method getFunctionCompletions.

public static Collection<Lookup> getFunctionCompletions(XPathElement element) {
    final XPathFile xpathFile = (XPathFile) element.getContainingFile();
    final ContextProvider contextProvider = ContextProvider.getContextProvider(xpathFile);
    final NamespaceContext nsContext = contextProvider.getNamespaceContext();
    String uri;
    PrefixedName qn = null;
    if (element instanceof QNameElement) {
        qn = ((QNameElement) element).getQName();
        if (qn != null) {
            QName qName = contextProvider.getQName(qn, element);
            if (qn.getPrefix() != null) {
                if (qName != null) {
                    uri = qName.getNamespaceURI();
                } else {
                    return Collections.emptySet();
                }
            } else {
                uri = null;
            }
        } else {
            uri = null;
        }
    } else {
        uri = null;
    }
    final Map<Pair<QName, Integer>, ? extends Function> functions = contextProvider.getFunctionContext().getFunctions();
    final List<Lookup> lookups = new ArrayList<>(functions.size());
    for (Map.Entry<Pair<QName, Integer>, ? extends Function> entry : functions.entrySet()) {
        final Function functionDecl = entry.getValue();
        final QName f = entry.getKey().first;
        final String p;
        if (nsContext != null) {
            String namespaceURI = f.getNamespaceURI();
            if (uri != null && !namespaceURI.equals(uri)) {
                continue;
            }
            final String prefixForURI = nsContext.getPrefixForURI(namespaceURI, PsiTreeUtil.getContextOfType(element, XmlElement.class, true));
            if (prefixForURI == null && namespaceURI.length() > 0) {
                continue;
            }
            p = qn == null || qn.getPrefix() == null ? makePrefix(prefixForURI) : "";
        } else {
            p = "";
        }
        lookups.add(FunctionLookup.newFunctionLookup(p + f.getLocalPart(), functionDecl));
    }
    return lookups;
}
Also used : QName(javax.xml.namespace.QName) ContextProvider(org.intellij.lang.xpath.context.ContextProvider) Function(org.intellij.lang.xpath.context.functions.Function) XPathFile(org.intellij.lang.xpath.XPathFile) NamespaceContext(org.intellij.lang.xpath.context.NamespaceContext) XmlElement(com.intellij.psi.xml.XmlElement) Pair(com.intellij.openapi.util.Pair)

Aggregations

NamespaceContext (org.intellij.lang.xpath.context.NamespaceContext)7 XmlElement (com.intellij.psi.xml.XmlElement)3 ContextProvider (org.intellij.lang.xpath.context.ContextProvider)3 QName (javax.xml.namespace.QName)2 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 Annotation (com.intellij.lang.annotation.Annotation)1 Disposable (com.intellij.openapi.Disposable)1 Pair (com.intellij.openapi.util.Pair)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiReference (com.intellij.psi.PsiReference)1 XPathFile (org.intellij.lang.xpath.XPathFile)1 Function (org.intellij.lang.xpath.context.functions.Function)1 Nullable (org.jetbrains.annotations.Nullable)1 TestOnly (org.jetbrains.annotations.TestOnly)1