XPathVariable () { final PsiElement elementAtCaret = myFixture.getElementAtCaret(); XPathVariableReference ref = PsiTreeUtil.getParentOfType(elementAtCaret, XPathVariableReference.class, false); if (ref != null) { return ref.resolve(); } XPathVariable variable = PsiTreeUtil.getParentOfType(elementAtCaret, XPathVariable.class, false); if (variable != null) { return variable; } final PsiReference[] references = elementAtCaret.getReferences(); for (PsiReference reference : references) { final PsiElement var = reference.resolve(); if (var instanceof XPathVariable) { return (XPathVariable)var; } } return null; }