Search in sources :

Example 1 with VariableContext

use of org.intellij.lang.xpath.context.VariableContext 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 2 with VariableContext

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

the class XPathAnnotator method markUnresolvedVariable.

private static void markUnresolvedVariable(XPathVariableReference reference, AnnotationHolder holder) {
    final String referencedName = reference.getReferencedName();
    // missing name is already flagged by parser
    if (referencedName.length() > 0) {
        final TextRange range = reference.getTextRange().shiftRight(1).grown(-1);
        final Annotation ann = holder.createErrorAnnotation(range, "Unresolved variable '" + referencedName + "'");
        ann.setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
        final VariableContext variableContext = ContextProvider.getContextProvider(reference).getVariableContext();
        if (variableContext != null) {
            final IntentionAction[] fixes = variableContext.getUnresolvedVariableFixes(reference);
            for (IntentionAction fix : fixes) {
                ann.registerFix(fix);
            }
        }
    }
}
Also used : IntentionAction(com.intellij.codeInsight.intention.IntentionAction) TextRange(com.intellij.openapi.util.TextRange) VariableContext(org.intellij.lang.xpath.context.VariableContext) Annotation(com.intellij.lang.annotation.Annotation)

Example 3 with VariableContext

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

the class XPathVariableReferenceImpl method resolve.

@Nullable
public XPathVariable resolve() {
    if (getContainingFile().getLanguage() == XPathFileType.XPATH2.getLanguage()) {
        XPathVariableHolder f = PsiTreeUtil.getParentOfType(this, XPathVariableHolder.class, true);
        while (f != null) {
            final XPathVariable variable = findVariable(f.getVariables(), getReferencedName());
            if (variable != null) {
                return variable;
            }
            f = PsiTreeUtil.getParentOfType(f, XPathVariableHolder.class, true);
        }
    }
    final VariableContext context = getXPathContext().getVariableContext();
    if (context == null) {
        return null;
    }
    return context.resolve(this);
}
Also used : VariableContext(org.intellij.lang.xpath.context.VariableContext) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

VariableContext (org.intellij.lang.xpath.context.VariableContext)3 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 Annotation (com.intellij.lang.annotation.Annotation)1 TextRange (com.intellij.openapi.util.TextRange)1 ContextProvider (org.intellij.lang.xpath.context.ContextProvider)1 Nullable (org.jetbrains.annotations.Nullable)1