Search in sources :

Example 1 with ContextProvider

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

the class XPathAnnotator method visitXPathVariableReference.

@Override
public void visitXPathVariableReference(XPathVariableReference o) {
    final ContextProvider contextProvider = o.getXPathContext();
    checkVariableReference(myHolder, o, contextProvider);
    super.visitXPathVariableReference(o);
}
Also used : ContextProvider(org.intellij.lang.xpath.context.ContextProvider)

Example 2 with ContextProvider

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

the class CompletionLists method getVariableCompletions.

public static Collection<Lookup> getVariableCompletions(XPathElement reference) {
    final ContextProvider contextProvider = ContextProvider.getContextProvider(reference);
    final VariableContext resolver = contextProvider.getVariableContext();
    if (resolver != null) {
        final Object[] variablesInScope = resolver.getVariablesInScope(reference);
        final List<Lookup> lookups = new ArrayList<>(variablesInScope.length);
        for (final Object o : variablesInScope) {
            if (o instanceof PsiNamedElement) {
                final String type;
                if (o instanceof XPathVariable) {
                    final XPathType t = ((XPathVariable) o).getType();
                    if (t != XPathType.UNKNOWN) {
                        type = t.getName();
                    } else {
                        type = "";
                    }
                } else {
                    type = "";
                }
                final String name = ((PsiNamedElement) o).getName();
                lookups.add(new VariableLookup("$" + name, type, ((PsiNamedElement) o).getIcon(0), (PsiElement) o));
            } else {
                lookups.add(new VariableLookup("$" + String.valueOf(o), PlatformIcons.VARIABLE_ICON));
            }
        }
        return lookups;
    } else {
        return Collections.emptySet();
    }
}
Also used : ContextProvider(org.intellij.lang.xpath.context.ContextProvider) VariableContext(org.intellij.lang.xpath.context.VariableContext)

Example 3 with ContextProvider

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

the class CompletionLists method getNodeTestCompletions.

public static Collection<Lookup> getNodeTestCompletions(final XPathNodeTest element) {
    if (!element.isNameTest()) {
        return Collections.emptyList();
    }
    final PrefixedName prefixedName = element.getQName();
    assert prefixedName != null;
    final String canonicalText = prefixedName.toString();
    final String suffix = canonicalText.substring(canonicalText.indexOf(INTELLIJ_IDEA_RULEZ));
    final XPathAxisSpecifier axisSpecifier = element.getStep().getAxisSpecifier();
    final ContextProvider contextProvider = ContextProvider.getContextProvider(element);
    final XmlElement context = contextProvider.getContextElement();
    final boolean insidePrefix = suffix.contains(INTELLIJ_IDEA_RULEZ + ":");
    final Set<Lookup> list = new HashSet<>();
    addNameCompletions(contextProvider, element, list);
    final String namespacePrefix = prefixedName.getPrefix();
    if (namespacePrefix == null || insidePrefix) {
        addNamespaceCompletions(contextProvider.getNamespaceContext(), list, context);
    }
    final XPathNodeTest.PrincipalType principalType = addContextNames(element, contextProvider, prefixedName, list);
    if (namespacePrefix == null && !insidePrefix) {
        if (axisSpecifier == null || axisSpecifier.isDefaultAxis()) {
            list.addAll(getAxisCompletions());
            // wow, this code sux. find a better implementation
            PsiElement sibling = element.getParent().getPrevSibling();
            while (sibling instanceof PsiWhiteSpace) {
                sibling = sibling.getPrevSibling();
            }
            boolean check = sibling != null;
            if (!check) {
                XPathLocationPath lp = null;
                do {
                    lp = PsiTreeUtil.getParentOfType(lp == null ? element : lp, XPathLocationPath.class, true);
                } while (lp != null && lp.getPrevSibling() == null);
                check = lp == null || (sibling = lp.getPrevSibling()) != null;
            }
            if (check) {
                if (sibling instanceof XPathToken && XPathTokenTypes.PATH_OPS.contains(((XPathToken) sibling).getTokenType())) {
                // xx/yy<caret> : prevSibl = /
                } else {
                    list.addAll(getFunctionCompletions(element));
                    list.addAll(getVariableCompletions(element));
                }
            }
        }
        if (principalType == XPathNodeTest.PrincipalType.ELEMENT && prefixedName.getPrefix() == null) {
            list.addAll(getNodeTypeCompletions(element));
        }
    }
    return list;
}
Also used : ContextProvider(org.intellij.lang.xpath.context.ContextProvider) XmlElement(com.intellij.psi.xml.XmlElement)

Example 4 with ContextProvider

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

the class PrefixReferenceImpl method resolve.

@Nullable
public PsiElement resolve() {
    final ContextProvider provider = getElement().getXPathContext();
    final NamespaceContext namespaceContext = provider.getNamespaceContext();
    if (namespaceContext != null) {
        return namespaceContext.resolve(getCanonicalText(), provider.getContextElement());
    } else {
        return null;
    }
}
Also used : NamespaceContext(org.intellij.lang.xpath.context.NamespaceContext) ContextProvider(org.intellij.lang.xpath.context.ContextProvider) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with ContextProvider

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

the class PrefixReferenceImpl method isUnresolved.

public boolean isUnresolved() {
    if (!canResolve())
        return true;
    if (resolve() != null) {
        return false;
    }
    final ContextProvider provider = getElement().getXPathContext();
    final NamespaceContext namespaceContext = provider.getNamespaceContext();
    assert namespaceContext != null;
    final String prefix = getCanonicalText();
    return !namespaceContext.getKnownPrefixes(provider.getContextElement()).contains(prefix);
}
Also used : NamespaceContext(org.intellij.lang.xpath.context.NamespaceContext) ContextProvider(org.intellij.lang.xpath.context.ContextProvider)

Aggregations

ContextProvider (org.intellij.lang.xpath.context.ContextProvider)9 NamespaceContext (org.intellij.lang.xpath.context.NamespaceContext)3 XmlElement (com.intellij.psi.xml.XmlElement)2 EmptyResolveMessageProvider (com.intellij.codeInsight.daemon.EmptyResolveMessageProvider)1 Annotation (com.intellij.lang.annotation.Annotation)1 Pair (com.intellij.openapi.util.Pair)1 PsiReference (com.intellij.psi.PsiReference)1 QName (javax.xml.namespace.QName)1 XPathFile (org.intellij.lang.xpath.XPathFile)1 VariableContext (org.intellij.lang.xpath.context.VariableContext)1 Function (org.intellij.lang.xpath.context.functions.Function)1 Nullable (org.jetbrains.annotations.Nullable)1